How to omit fields in Objection.js dynamically - sql

Is there a way to omit or change the json of a model dynamically for different cases,
Though by overriding the $formatJson method on the Model does give the ability to do that,
but it affect every model instance.
I want to be able to omit some fields for some request and get the full data for other request.
Example
// GET /cars
[
{
"id": 1,
"name": "honda",
"year": "1999",
"image": "full-view.jpg"
},
...
]
// GET /car
{
"id": 1,
"name": "honda",
"year": "1999",
"image": "full-view.jpg",
"manufacturer": "Honda Motor Company, Ltd.",
"country": "Japanese",
"createdAt": "2022-08-15 10:42:36",
"updatedAt": "2022-08-15 10:42:36",
...
}
Please is there any way to do this in Objection.js?
Any solution will appreciated, Thanks

Related

Strapi filtering localization with images

I think problem may be in how I compose the request for API?
According to strapi documentation strapi docs there is possible to filter data by using following syntax. GET /api/:pluralApiId?filters[field][operator]=value
example:
Example request: Find users having 'John' as first name
So my goal is get images and text for specific localization for my collection AboutBox which contains 3 text fields and 1 media type. My request look like this: api/about-boxes?populate=*&?filters[locale][$eq]=en
What return request look like:
{
"data": [
{
"id": 1,
"attributes": {
"title": "About us",
"createdAt": "2023-02-08T07:47:38.543Z",
"updatedAt": "2023-02-09T08:49:40.543Z",
"publishedAt": "2023-02-08T07:48:48.681Z",
"locale": "en",
"text": "At CGI we provide a wide range of services and solutions that significantly help our clients grow financially and technologically. Our CGI CZ Space team focuses on the use of satellite data for remote scanning of the Earth tailored to all our customers across sectors. We deliver projects to the European Space Agency (ESA), participate in the development and support of the Galileo navigation system and more.",
"buttonText": "Check it out",
"logo": {
"data": {
"id": 3,
"attributes": {
"name": "cgi_logo.svg",
"alternativeText": "cgi_logo",
"caption": null,
"width": 59,
"height": 27,
"formats": null,
"hash": "cgi_logo_934146d58f",
"ext": ".svg",
"mime": "image/svg+xml",
"size": 0.98,
"url": "/uploads/cgi_logo_934146d58f.svg",
"previewUrl": null,
"provider": "local",
"provider_metadata": null,
"createdAt": "2023-02-08T07:46:17.180Z",
"updatedAt": "2023-02-08T07:48:31.102Z"
}
}
},
"localizations": {
"data": [
{
"id": 2,
"attributes": {
"title": "O nás",
"createdAt": "2023-02-08T07:47:50.849Z",
"updatedAt": "2023-02-10T14:04:21.010Z",
"publishedAt": "2023-02-10T14:04:20.963Z",
"locale": "cs",
"text": "Ve společnosti CGI poskytujeme širokou škálu služeb a řešení, která našim klientům významně pomáhají finančně a technologicky růst. Náš tým CGI CZ Space se zaměřuje na využití družicových dat pro dálkové snímkování Země na míru všem našim zákazníkům napříč odvětvími. Dodáváme projekty pro Evropskou kosmickou agenturu (ESA), podílíme se na vývoji a podpoře navigačního systému Galileo a dalších.",
"buttonText": "Podívejte se na to"
}
}
]
}
}
}
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 25,
"pageCount": 1,
"total": 1
}
}
}
What I am expecting is get only data where is: "locale": "en",
not the one where is "locale": "cs",
basically without this block
"localizations": {
"data": [
{
"id": 2,
"attributes": {
"title": "O nás",
"createdAt": "2023-02-08T07:47:50.849Z",
"updatedAt": "2023-02-10T14:04:21.010Z",
"publishedAt": "2023-02-10T14:04:20.963Z",
"locale": "cs",
"text": "Ve společnosti CGI poskytujeme širokou škálu služeb a řešení, která našim klientům významně pomáhají finančně a technologicky růst. Náš tým CGI CZ Space se zaměřuje na využití družicových dat pro dálkové snímkování Země na míru všem našim zákazníkům napříč odvětvími. Dodáváme projekty pro Evropskou kosmickou agenturu (ESA), podílíme se na vývoji a podpoře navigačního systému Galileo a dalších.",
"buttonText": "Podívejte se na to"
}
}
]
}
Also for unknow reason when I get request for czech localization I did not get data in return:
czech request return
Any help would be appreciated
According to Strapi docs you can receive localized entries via GET /api/{content-type}?locale={locale-code}

Get all API fields definitions from Podio application

Responses from Podio API returns an JSON array of items with a fields property. Each field carries its values and its config.
For example a category field for the Gender:
{
"type": "category",
"field_id": 219922852,
"label": "Gender",
"values": [
{
"value": {
"status": "active",
"text": "Prefer not to say",
"id": 3,
"color": "F7F0C5"
}
}
],
"config": {
"settings": {
"multiple": true,
"options": [
{
"status": "active",
"text": "Male",
"id": 1,
"color": "DCEBD8"
},
{
"status": "active",
"text": "Female",
"id": 2,
"color": "F7F0C5"
},
{
"status": "active",
"text": "Prefer not to say",
"id": 3,
"color": "F7F0C5"
}
],
"display": "inline"
},
"mapping": null,
"label": "Gender"
},
"external_id": "gender"
},
How can I fetch the config without having to query a specific item?
Is there a way to get every field in the response? Because if the queried item does not have a field value set, Podio doesn't return it in the response.
I would like to get the field config for ALL the fields. If possible, with a single API request. In particular I am interested in all the possible values (in case of Category or Relationship fields) so that I could match them with local values I have.
This way I can use the field structure to programmatically map some local values to the format required by the Podio API; and then generate a fields payload that to update/create Podio items via an API calls.
You can request the Podio Get App method to get the app configuration.
Podio Doc Ref: https://developers.podio.com/doc/applications/get-app-22349

BigCommerce customer attributes from client/store

Can I retrieve the customer's
There is a current-customer API - https://developer.bigcommerce.com/api-docs/customers/current-customer-api, but it only returns the following bits about a customer.
"customer": {
"id": 4927,
"email": "john.doe#gmail.com",
"group_id": "6"
}
I'd really to have the ability to get all the data from
https://developer.bigcommerce.com/api-reference/customer-subscribers/v3-customers-api/customer-attributes/customersattributesget as a property of the payload for current-customer-api
/customer/current.jwt?app_client_id={appClientId}&attribute-ids:in=1,2,3
"customer": {
"id": 4927,
"email": "john.doe#gmail.com",
"group_id": "6"
"attributes": [
{
"id": 1,
"name": "Age",
"type": "string",
"date_created": "2018-11-13T21:42:06Z",
"date_modified": "2018-11-14T16:46:23Z"
},
{
"id": 2,
"name": "Shoe Size",
"type": "number",
"date_created": "2018-11-14T16:34:57Z",
"date_modified": "2018-11-14T16:34:57Z"
},
{
"id": 3,
"name": "Date Joined",
"type": "date",
"date_created": "2019-02-19T19:13:21Z",
"date_modified": "2019-02-19T19:13:21Z"
}
]
}
This is an interesting feature request. The purpose of the current customer API is to verify the identity of a logged in customer in a secure way, which is why the payload is so minimal (with the understanding that you would likely be making a request for the full customer record based on the id returned).
We are planning to surface customer attribute data on the storefront though, which sounds like it would solve for your use case. We'll be adding Handlebars support soon, and we've got a storefront GraphQL API in the works as well. You can read more in our blog post here:
https://medium.com/bigcommerce-developer-blog/customize-and-extend-your-customer-data-with-the-new-bigcommerce-v3-customers-api-8609903e102a

Karate-DSL doesn't return the response in an order

Suppose that I have a GET endpoint to and the result looks like below
[
{
"id": 42,
"studentName": "John",
"alternateName1": "",
"alternateName2": "",
"code": "NS2",
"year": 2019
},
{
"id": 41,
"studentName": "John",
"alternateName1": "",
"alternateName2": "",
"code": "NS1",
"year": 2019
},
{
"id": 1,
"studentName": "Tom",
"alternateName1": "",
"alternateName2": "",
"code": "NS2",
"year": 2019
}
]
The response doesn't return in an order based on ID or name.... so, when performing the match to compare it to result of corresponding query in database, it turns "FAILED". Try some tools like Postman to check the order, it follows "id" and same as the natural SQL query.
Also, follow and customize the js function 'sort-array.feature' to sort the response, but the response type is an array; thus, it doesn't work.
I would like to know how we handle the order of the response based on configured values (like: id) or a default value (like, "name") as in my case, or any approach to compare the result of an API endpoint(actual result) to result of a query (expected result)
Thank you so much.
Please read the docs and examples and especially look out for contains. I'm providing one example below, the rest is homework for you (especially since you like Postman so much :P). Paste this into a fresh scenario and see it work.
* def response =
"""
[
{
"id": 42,
"studentName": "John",
"alternateName1": "",
"alternateName2": "",
"code": "NS2",
"year": 2019
},
{
"id": 41,
"studentName": "John",
"alternateName1": "",
"alternateName2": "",
"code": "NS1",
"year": 2019
},
{
"id": 1,
"studentName": "Tom",
"alternateName1": "",
"alternateName2": "",
"code": "NS2",
"year": 2019
}
]
"""
* match response[*].id contains only [1, 41, 42]
Karate allows you to create and even transform JSON in multiple ways, so you can pretty much do any comparison you want.

Composite or Sideloading?

I am relatively new to building APIs and have come across the following two terms a few times now and am a bit confused. I'm hoping someone can put me straight with some definitions that I cannot even find on wikipedia. Im not sure if they can be reliably used interchangeably without upsetting people or whether it depends on who you talk to.
Sideloading
the ability to optionally include other related resource(s) in a response
eg. /accounts?include=transactions will include transactions resources in my /accounts resources response ?
this really means that we avoid duplication of resources in a response by moving them out into their own section of the message, eg.instead of
{ "orders": [ {
"id": "101",
"product": { "id": "2000", "desc": "blah", "price": "100.0" }
"id": "102",
"product": { "id": "2000", "desc": "blah", "price": "100.0" }
we would have:
{ "orders": [ {
"id": "101",
"product": { "id": "2000" }
"id": "102",
"product": { "id": "2000" }
],
"products": [
{ "id": "2000", "desc": "blah", "price": "100.0" }
Composite Document
Simply means you have a mix of related resources (that can also stand alone) in a resources response eg. accounts and transactions resources can be requested on their own or also combined in the same composite document ??
I've never come across what I would consider a formal definition of either sideloading or composite, I reckon you can define it how you want, provided its consistent with how your resources are modelled.
I cant imagine people getting too upset about the terminology. The basic premise is sound, everything else is just semantics and implementation :). That said, it's a good idea to keep your API object model relatively pure and functional for the client.
So if it is most likely to be used in the nested product fashion, and thats what the API naturally looks like, then the duplication of
{ "orders": [ {
"id": "101",
"product": { "id": "2000", "desc": "blah", "price": "100.0" }
"id": "102",
"product": { "id": "2000", "desc": "blah", "price": "100.0" }
might be less of a problem than the added indirection of
{ "orders": [ {
"id": "101",
"product": { "id": "2000" }
"id": "102",
"product": { "id": "2000" }
],
"products": [
{ "id": "2000", "desc": "blah", "price": "100.0" }