-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use path params and body in a single query? #8
Comments
Option 1: improve the current logic Instead of always binding to body parameters, we can first search what path parameters are defined. In the example above, categoryId is defined in a path, so this can take precedence. Everything else will be bind to default source. Pros:
Cons:
|
Option 2: enhance SQL queries with variables Example: - path: /v1/categories/:categoryId
put: >-
UPDATE categories
SET name = #{body.name}
, name_ru = #{body.nameRu} <-- NEW
, slug = #{body.slug}
, updated_at = NOW()
, updated_by = #{header.userId} <-- NEW
WHERE id = #{path.categoryId} <-- NEW The syntax can be different:
Pros:
Cons:
|
Option 3: mix the previous approaches Example: - path: /v1/categories/:categoryId
put: >-
UPDATE categories
SET name = :name
, name_ru = :nameRu
, slug = :slug
, updated_at = NOW()
, updated_by = #{header.userId} <-- NEW
WHERE id = #{path.categoryId} <-- NEW Pros:
Cons:
|
Hey @php-coder, to be honest the all cons about all options are make sense. So, if I were you, I'd look at another way: - path: /v1/categories/:categoryId
params:
- header: userId <--list of params that we take from header>
- body: name, name_ru, slug <-- <list of params that we take from body>
.....
put: >-
UPDATE categories
SET name = :name
, name_ru = :nameRu
, slug = :slug
, updated_at = NOW()
, updated_by = :userId
WHERE id = :categoryId Pros:
Cons:
|
@theshamuel In other words, we can move binding logic into YAML configuration. Another con of that approach is that it makes configuration more verbose. Thank you for the input! I'll think about it. |
By the way, this way we will make At the same time, if we try to move this into a method, we will have to introduce a property for a query: - path: /v1/categories/:categoryId
put:
query: >-
UPDATE categories
SET name = :name
, name_ru = :nameRu
, slug = :slug
, updated_at = NOW()
, updated_by = :userId
WHERE id = :categoryId
params:
- header: [ 'userId' ]
- body: [ 'name', 'name_ru', 'slug' ] |
…citly bind to a desired origin This feature allows us to use parameters from different sources. See example for testing PUT methods in the tests. BREAKING CHANGE: every :parameter should be revised and modified to be :b.parameter to bind to request body or :p.parameter to bind to path parameter. See also #8
In order to push this forward, in the ddb2e16 commit the 2nd approach was partially implemented. All parameters now must have a prefix This might be not a best API but at least it allow us to proceed. |
In the current approach, we pass parameters to SQL query only from a single source (path params or body params):
But even for a simple
PUT
endpoint it isn't enough: we want to extract object ID from a path and other parameters from a body. Here is an example of generated code that we should improve:We can see that
categoryId
is bound toreq.body.categoryId
while it should use a value fromreq.params.categoryId
.Also there will be more cases where we need to combine different sources: userId might be extracted from a header. We might want to access cookies or query parameters.
The text was updated successfully, but these errors were encountered: