I know that model.save({ silent: true }, callback); is not supported. But anyone know how to update Sails model without trigger lifecycle event??
I have ask on github issue, but still no respond so far. Link is here
You can do this by using .native()/.query() to execute your query, thus avoiding the call back.
Use .native() if your using MongoDB and use .query() if your running MySQL/Postgres
http://sailsjs.org/#!/documentation/reference/waterline/models/native.html
http://sailsjs.org/#!/documentation/reference/waterline/models/query.html
Related
When I deploy Strapi to a new server, I want to create and populate the database tables (PostgreSQL), particularly categories. How do I access production config, and create tables and category entries?
A hint on how-to approach this, would be much appreciated!
I know this is an old question, but i recently came upon the same issue.
Basically you should create the collections first, which result in the creation of models. Of course you also could create the models manually.
In the recent documentation you find a section about a bootstrap function.
docs bootstrap
The function is called at the start of the server.
The docs list the following use cases:
Here are some use cases:
Create an admin user if there isn't one.
Fill the database with some necessary data.
Load some environment variables.
The bootstrap function can be synchronous or asynchronous.
A great example can be found in the Plugin strapi-plugin-users-permissions
You can implement a new service or overwrite a function of an existing plugin.
the function initialize is implemented here async initialize
and called in the bootstrap function here
await ...initialize()
The initialize function is used to populate the database with the two roles
Authenticated and Public.
Hope that helps whoever stumbles upon this question.
I have a very annoying problem with a Mongoose plugin in combination with Feathers.
It is a straight forward plugin that is taken from the Mongoose documentation here that updates the document version (__v) on every type of update. It works fine as standalone plugin, but when combined with Feathersjs it fails.
Somehow Mongoose is not converting the object id strings to ObjectIDs correctly when running updates and patches with the plugin in combination with Feathersjs. If I disable the plugin updates and patches do work.
As far as I have been able to determine debugging, the data sent in the call from Feathersjs to Mongoose is the same both with the plugin enabled or disabled. So I'm inclinded to say that Feathersjs is not the culprit, but then again why does the plugin work without Feathersjs?
I have setup an example repo here. Unfortunately this is the minimum setup required to recreate the problem. The stup is as follows:
src/models/schema/categories.schema.js - The example Mongoose schema
definition
src/mongoose/always-update-version-key.js - The plugin
test/mongoose/always-update-version-key.test.js - Test the schema and
plugin without Feathers
test/services/category.test.js - Test the schema and plugin with Feathers
I have added the test to test the plugin with and without Feathersjs. To run the tests execute npm run test:unit. Mongo must be running on the default port (27017).
I hope someone can help me solve this very frustrating problem or point me towards the code in Mongoose where the id strings are converted to ObjectIDs.
The default setting of feathers-mongoose is the lean option set to true for faster queries. When model plugins should be used (which I think should be the case here), set lean: false in the service options:
const options = {
Model,
paginate,
lean: false
};
// Initialize our service with any options it requires
app.use('/category', createService(options));
I am looking for a way to perform MongoDB's upsert operation in waterline. One way is to use the native method.
Model.native(function (err, Collection){
Collection.update({"key": "val"}, {"$set": {"x": "val"}}, {"upsert": true}, function (err, updated){
// do something
})
})
But if I don't want to use native, I could see on similar function findOrCreate, which creates a new entry if the item does not exist. However, I could not find a way to update the item if it exists. Is that possible or one has to use the native method only for that ?
Note: I am using Sails 0.11
As of now (v0.10.26) there is no way of doing a Mongo-style upsert besides the ones you've mentioned. This keeps coming up every once in a while but I couldn't find an issue addressing it directly, so maybe you could raise one.
Update
This issue talks about updateOrCreate and the linked threads suggest this might be added soon. I was looking into the Sails issues initially so no wonder I found nothing.
In sails.js, Models support lifecycle callbacks for validate, create, update and destroy.
Is there support for callbacks for find() or query as well? Like beforeFind() and afterFind()?
The idea is same. I would want to validate / modify parameters before the query is run or after the query is run.
Any thoughts?
As of writing this it does NOT support these requests, however their is a pull request https://github.com/balderdashy/waterline/pull/525
You can use policies to do this in the mean time.
i don't get why this was left out in the first place. It's actually logical to want to add some data to the fetched model data after each model find.
The closest thing to afterFind in the documentation as of writting is customToJson model setting.
customToJSON: function() {
// Return a shallow copy of this record with the password and ssn removed.
return _.omit(this, ['password', 'ssn'])
}
you basically do your stuff before the return omit part. I still don't get why these lifecycles were omitted.
I think I am going to write a hook to provide these for now. I will post it here.
I was trying dapper orm and recently they added asyncquery support. I googled it about that. It is wonderful if you have heavy traffic on your site. I was trying that with postgressql and dapper. Now, in connection if I am passing simple connection string it works fine. But as per couple of articles it is not true async if I want to use it, I need async connection string.
Now, I don't know how to use with Postgresql and npgsql. Here is complete article for reference where author explains how to do it with Sql Server.
What I need to do if I want same with Postgresql?
Please let me know if any further requirement needed.
The author of this article is somewhat wrong - in .NET 4.5 the AsynchronousProcessing property is ignored because it is no longer required. You can just start calling the Async methods of SqlClient without any special connection strings.
Whether the operations will execute asynchronously, depends on the database provider. For example, the default implementation of DbCommand.ExecuteDbDataReaderAsync actually executes synchronously and blocks the calling thread. SqlCommand overrides this method and executes asynchronously.
Unfortunately, NpgsqlCommand doesn't override this method so you are left with synchronous execution only.