How to update data fields on the server-side? - node.js

How I write a script and update a collection field from my server?
I need to update a field of 100 users.
I was wondering if I can change them from the server, instead of sending update mutations over and over?
The Queries part of Strapi Documents says:
You can just call strapi.query('modelName', 'pluginName') to access
the query API for any model.
But it's not clear where to run it?
Do I need to create a plugin? or Can I create an updateUserData.js file and run node updateUserData.js?
strapi.query('user').update( { id: 1 },...

Related

How to load a child component of a relation in Strapi

In my Strapi application, I have the following structure:
Footer(single type) > Section(component) > Menu(collection type) > link(component)
When performing a query Strapi only returns the id of the link and I needed its LABEL and URL, does anyone know how I can bring this in a query only?
Strapi section
Link Component
Footer API return
I used this topic on the Strapi forum, but without success yet
https://forum.strapi.io/t/how-do-i-return-only-selected-fields-from-the-model-and-its-relation/1115/2
Disclaimer; im new to Strapi.
The Strapi get requests only populates with relational data in a single layer of nesting. Your links looks to be on the second layer.
You have two options:
Query for the links by ID from your client after receiving the IDs. This can be done efficiently using Promise.all.
Modify the default GET handler for the ressource in the Strapi code. Extend the query to join on your links, and return the response in its entirity.
Update on option 2;
You can customize the logic behind any single endpoint handler in the API in your Strapi backend. The process is best descriped in their documentation on Custom Data Response's. Note that the syntax on how to query the database is based on a popular ORM depending on your database of choice. The ORMs are:
MongoDB (through Mongoose)
Postgres, MySQL, SQLite3 and more (through Bookshelf)
Redis (through ioredis).

CouchDB longpoll without _changes

I want to use the longpoll option in couchDB, but without the _changes view.
The database has this view:
function (doc) {
if(doc.job)
emit(doc._id, 1);
}
So it emits all documents that have a tag “job” in them. Note that I have a bunch of other stuff in this DB. Now I want to use a longpoll where the connection stays open as long as there are no documents in the DB with the job tag. As soon as there is a document with the job tag, the db send it to the client and the connection should be closed. The client can now do the job, send a delete command to the db to delete the job, and start listening again.
So my idea is to call the view somehow like this:
http://mycouch/mydb/_design/visualize/_view/get_jobs?feed=longpoll&include_docs=true
However, it seems it is only possible to use longpoll with the _changes view. Any workaround?
Thanks for any help
_changes is the feed you need to use in your case. Yo can use it filtering the feed by receiving changes only for "job" tagged documents. You can do this using your view map function.
http://mycouch/_changes?filter=_view&view=visualize/get_jobs&feed=logpoll&include_docs=true
You'll receive through this feed any update (create/update/delete) over "job" tagged docs in your database.

Data not returned from custom collection

I have a hand-made collection (mymodel) in MongoDB. I have created a sails model+controller using 'sails generate api mymodel'
I am now trying to use the sails find() method using the REST api: http://localhost:1377/mymodel, but I do not get any data returned. I have even tried to write my own end-point method and used the Waterline ORM find() method, but still don't get anything returned.
Am I missing something? Is there something more to be done?
Its a simple model, like {A:123,b:"wf"}
No specific data pieces
From mongo shell, I get the complete information
Found the problem.
If a create a collection using the name "MyModel" in MongoDB, and then create a controller+model in sails.js (sails generate api MyModel), sailsjs does not seem to use the "MyModel" collection, but rather tries to create a new collection completely in lower case ("mymodel") where all the transactions are committed.
So the trick is to create a collection in lowercase only so that sails starts using it
Not sure if this is a bug in sails.js

User specific database in MongoDB

I am currently working on an inventory management software in Node js and MongoDB. I am pretty new to MongoDB, having worked in Oracle and MySQL for most of my projects.
Is it possible to create a separate database schema for every client who uses my software, with each client having access only to his copy of the database schema and collections?
The equivalent of selecting data in Oracle database would be
Select * from User1.table,
Select * from User2.table etc
Also, if it were possible, how would it be implemented using a node js mongo db client like mongoose?
I looked at MongoDB documentation, but it talks mainly about adding users to a database for authorization.
I apologize if it seems like a silly question, but id appreciate it if someone could point me in the right direction for this.
Before starting to invest a lot of time in the development of your project, check out other possible approaches to the scenario that you are trying to build.
I did a quick search on SO and found some additional threads with similar scenarios:
MongoDB Database vs. Collection
MongoDB Web App - Database per User
Additional info about mongoose database creation
Whenever you call the connect method on the mongoose object, you are either connecting to an existing database or you are creating it in case it doesn't already exist.
You could have a function that allows you to pass in a name argument with the name and create databases programmatically:
function createDatabase(name) {
var conn_string = 'mongodb://localhost/';
if (typeof name == 'string') {
conn_string += name;
}else{
return false;
}
mongoose.connect(conn_string);
}
Also, be aware that a database will be created when you first insert a record in a collection of that particular database.
It is not sufficient to only connect to the database, you also have to insert a record.
As per my previous example, you could also pass a schema parameter to the function, tailored to each user's profile and fire an insert statement after you connect to that database.

How to preload MongoDB document?

I'm writing a web application with Node and MongoDB (with mongoose) - I have a collection that has a pre-defined data (images links that the users will choose one of them - they set of the images links is closed and won't be changed during the run of the application) and it need to be joined with other collection. I don't know if it is better to create a collection for this or to save the data in a single JSON file. But because i need to join this collection with other collection I decided to save the data in the DB.
Now the question is how I can create the data in the DB once and each time I raise the Node server the data won't be created again? (something like upsert)? and where is the best place to add this method? in the Schema module or where?
Thanks.
I found a way to do it by running an import command before raising the server (or even after the server is up) - I run the following line which will create the new documents or update the exist documents (according to the _id in the JSON file):
mongoimport --db test --collection supported_images --type json --file SupportedImages.json --upsert --jsonArray
Thanks everyone.

Resources