Modify Response Object in strapi - javascript

I would like to get a modified response object. For example I dont know how to get the user object without the roles.
The default response is:
{
"id": 6,
"username": "username",
"email": "user#email.com",
"provider": "local",
"confirmed": true,
"blocked": false,
"role": {
"id": 2,
"name": "Authenticated",
"description": "Default role given to authenticated user.",
"type": "authenticated"
}
}
Now I want to get the same response without the role attribute.
{
"id": 6,
"username": "username",
"email": "user#email.com",
"provider": "local",
"confirmed": true,
"blocked": false
}

Currently you cannot do this in the Rest API unless you change the UserController provided by permissions plugin, which is not recommended.
What you can do then is to use the GraphQL plugin provided by Strapi, so you can query only the fields you need on client side.
The docs about how to use GraphQL plugin are here.

For anyone still struggling with this problem:
The latest versions of strapi do support custom queries, you can pass an array containing all the names of relations you wish to populate (only relations!).
If you don't want to populate any relationships, you can keep it empty, your controller would then look something like this:
module.exports = {
UserWithoutRoles: ctx => {
return strapi.query('user').findOne({ id: ctx.params.id }, ['']);
}
}
If you do wish to populate it, it would be like this:
module.exports = {
UserWithoutRoles: ctx => {
return strapi.query('user').findOne({ id: ctx.params.id }, ['role']);
}
}
Also see:
[https://strapi.io/documentation/3.0.0-beta.x/concepts/queries.html#api-reference][1]

Related

How to create a query using typeorm query builder to filder data from the database?

I am just new to typeorm , I am using nodejs/nestjs , I am trying to create a query where in data from the database which is the course could be filter by instructor which is the firstname and lastname , filter by course_name and filter courses by subject. I tried using 'where' and 'orWhere' but cant seem to wrap things up, Anyone can give an advice of a better implementation ? Thank you. Much appreciated.
The query below is working , I am having trouble with the "where" condition
Code
async findAll(options: IPaginationOptions, query): Promise<Pagination<CourseEntity>> {
console.log('query :', query);
const courses = await getRepository(CourseEntity)
.createQueryBuilder('course')
.leftJoinAndSelect('course.user', 'user')
.leftJoinAndSelect('course.subject', 'subject')
// eslint-disable-next-line #typescript-eslint/camelcase
.where('course.course_name = :course_name', { course_name: query.course_name });
return paginate<CourseEntity>(courses, options);
}
here is the query param
query : { limit: '10', firstname: 'mark', lastname: 'gunn' , course_name: 'Comscie'}
Sample data from the database
{
"id": 4,
"course_name": "BS-IT",
"description": "BS-IT DBMS",
"created": "2020-03-19T16:40:46.000Z",
"updated": "2020-03-19T16:40:46.000Z",
"user": {
"id": 20,
"firstname": "Mark",
"lastname": "Gunn",
"role": "Instructor",
"email": "mark#gmail.com",
"isActive": false,
"created": "2020-03-19T16:06:21.000Z",
"updated": "2020-03-19T16:06:34.000Z"
},
"subject": {
"id": 2,
"subject_name": "IT 100",
"description": "Fundamandetals",
"created": "2020-03-18T03:58:34.000Z",
"updated": "2020-03-18T03:58:34.000Z"
}
}
It looks good. Just add other conditions with andWhere
For my project with Typeorm, I prefer use the Find Option Object (https://typeorm.io/#/find-options), because it's easier to generate request from an object but you can't do some advanced join (but in your case it's totally ok)
They are not better implementation just multiple solutions.

Is it appropriate to use Apollo local state to store a different representation of returned data

I have an Apollo Client that I'm using to request data from a service. I want to use the data I get in response to create a network of nodes and links e.g.
// Response data:
{
"Team": [
{
"name": "Example Team",
"members": [
{ "name": "Bob" },
{ "name": "Alice" }
]
}
]
}
// Network data:
{
"nodes": [
{ "name": "Example Team" }
{ "name": "Bob" },
{ "name": "Alice" }
],
"links": [
{ "source": "Example Team", "target": "Bob" },
{ "source": "Example Team", "target": "Alice" }
]
}
Historically, before using GraphQL, I would have used Redux to store the munged API response in state and read from there.
Is it appropriate to take a GraphQL result from Apollo and immediately save it back to Apollo local state in a different form so it can be queried by components in that format?
The main problem I foresee is that I think I'd have to query to check if the data I want exists in local state, then make another query if it didn't. In a Redux-world this would be wrapped up inside my store which would then only make the request off to the API if it didn't have the data it needed which 'feels' much cleaner.
In my case this could be solved using Afterware in Apollo Client, see this answer for more information.
It would allow me to munge the returned data into the form I need, and return it in the response alongside the original data.

sails.js update or add new row

I'm starting a project using sails.js and I'ma bit confused about something.
I have a structure like this
[
{
"logins": {
"1": {
"login time": "2016-04-02T11:40:06.731Z"
}
}
"username": "test",
"password": "test",
"createdAt": "2016-04-02T11:40:06.731Z",
"updatedAt": "2016-04-02T11:40:06.731Z",
"id": "56ffaf9692bcf108169ef7c8"
}
]
now when a user logs in again, ideally I want the data to look like this
[
{
"logins": {
"1": {
"login time": "2016-04-01T11:40:06.731Z"
},
"2": {
"login time": "2016-04-02T11:40:06.731Z"
}
}
"username": "test",
"password": "test",
"createdAt": "2016-04-01T11:40:06.731Z",
"updatedAt": "2016-04-01T11:40:06.731Z",
"id": "56ffaf9692bcf108169ef7c8"
}
]
However, I cannot for the live of me figure out how to "update" or "insert" a new row under "logins" for my user.
Creating the first record is fine and so far the best I could come up with is reading the current value for logins into a buffer and then re-inserting all the values.
That seems clumsy. Does anyone know if sails has some built in method to support what I'm trying to achieve?
I would change your 'Logins' argument to array. Than you can use $push while updating. Waterline support only $set operation, so if you want to do that you need to use .native() like that:
Model.native(function(error,collection){
collection.update(
{_id:YOURID}, // where condition
{$push:{logins:new Date()}}, // push new value to array
{}, // options
function(error,result){} // callback
)
})
To see what you can do with native, see Node.js MongoDB Driver API - Collections

Ember.js REST adapter handling different JSON structure

I'm using REST adapter, when I call App.Message.find() Ember.js makes call to the /messages to retrieve all messages and expect to see JSON structure like this:
{
"messages": [] // array contains objects
}
However API I have to work with response always with:
{
"data": [] // array contains objects
}
I only found the way1 to change namespace or URL for the API. How to tell REST adapter to look for data instead of messages property?
If this is not possible how to solve this problem? CTO said we can adapt API to use with REST adapter as we want, but from some reason we can't change this data property which will be on each response.
Assuming you are ok with writing your own adapter to deal with the difference, in the success callback you can simply modify the incoming name from "data" to your specific entity -in the case above "messages"
I do something like this to give you and idea of what if possible in a custom adapter
In the link below I highlighted the return line from my findMany
The json coming back from my REST api looks like
[
{
"id": 1,
"score": 2,
"feedback": "abc",
"session": 1
},
{
"id": 2,
"score": 4,
"feedback": "def",
"session": 1
}
]
I need to transform this before ember-data gets it to look like this
{
"sessions": [
{
"id": 1,
"score": 2,
"feedback": "abc",
"session": 1
},
{
"id": 2,
"score": 4,
"feedback": "def",
"session": 1
}
]
}
https://github.com/toranb/ember-data-django-rest-adapter/blob/master/packages/ember-data-django-rest-adapter/lib/adapter.js#L56-57
findMany: function(store, type, ids, parent) {
var json = {}
, adapter = this
, root = this.rootForType(type)
, plural = this.pluralize(root)
, ids = this.serializeIds(ids)
, url = this.buildFindManyUrlWithParent(store, type, ids, parent);
return this.ajax(url, "GET", {
data: {ids: ids}
}).then(function(pre_json) {
json[plural] = pre_json; //change the JSON before ember-data gets it
adapter.didFindMany(store, type, json);
}).then(null, rejectionHandler);
},

How to get a list of document's fields from couchDB using node/cradle?

First of all, i want to let you know that i am a novice with node and couchDB and i have this project where i need to add some functionality to the existing application.
So, i have javascript/node/express web application and i want to get a specific document from a remote couchDB, using cradle, and then get a list of its fields and their values. Later on i would need to display those fields/values in some html.
I don't know which fields the document has because they are dynamically added/removed by a third party.
I was able to get the document i wanted, but i don't know how to iterate through its fields. What would be the best way to do that?
Here is a simpificated sample of the document:
{
"_id": "1.1.5",
"_rev": "5-56ebac233e7f56a14a4534c6902727f7",
"1.1.5.39": {
"Project": {
"Project1": {
"files": "...",
"status": "NEW",
"id": 2
},
"Project2": {
"files": "...",
"status": "ASSIGNED",
"id": 3
}
}
}
"1.1.5.23": {
"Project": {
"Project3": {
"files": "...",
"status": "NEW",
"id": 4
},
"Project4": {
"files": "...",
"status": "NEW",
"id": 5
}
}
}
}
I would need to get the fields '1.1.5.39' and '1.1.5.23', and also the 'status' values. These fields represent some versions of a software. The problem is also fields' format: numbers and dots, so i can't just use 'Object.attribute' notation...
First part is getting the document via cradle:
// get the Connection
var connection = new(cradle.Connection)('http://living-room.couch', 5984, {
cache: true,
raw: false
});
// get the DB
var db = connection.db('yourDB');
// get the document
var docID = "1.1.5";
db.get('docID', function (err, doc) {
displayDoc(doc);
});
function displayDoc(doc) {
// Do your display handling here
}
The second part what you need is displaying arbitrary objects. Here I refer you to my answer to this question.
Good Luck.

Categories

Resources