How to get DB object in SailsJS / Waterline adapter methods? - node.js

I am trying to write a SailsJS / Waterline adapter for OrientDB using Oriento module for NodeJS.
I am able to understand the basics of Waterline adapter interface using boilerplate adapter code for SailsJS but not sure how to get hold of DB object in adapter methods. I am sure DB object is created and maintained by SailsJS or Waterline but how to get an access to it in adapter's methods as without DB object, I will not be able to make call to OrientDB. Look for code samples in Oriento module to know how to call OrientDB from NodeJS.
I will appreciate if some one can show some example code of getting DB object in adapter's method - define(), describe(), drop(), find() etc.

We're unfamiliar with Sails, but looks like the _dbPools private variable is what you're looking for - https://github.com/balderdashy/sails-adapter-boilerplate/blob/master/index.js#L59

Related

Best practice to use django db connection in network call

I have two Django projects with DRF and have some Rest API's
I want to use db connection of one Django project to another so i need to pass db connection/credentials over api network. I know we can get connection object using following
from django.db import connections
db_conn = connections['default']
and what am thinking is
1- to pass above db_conn in an api call as parameter to second project api but when am getting on other side its showing as string not as db connection object.
2- Another option is to pass credentials and create connection using sql alchemy in second project.
So what is the best approach in above two scenarios?. Also it would be great if you can answer how to pass db connection object in api params as object not string
I dont think you can put the object as parameter in the api request. A string is the best result you will get. Besides i would not be very happy with that info in the api!
Option 2 is the better aproach i would think.

How can i configure Mongoose to automatically create objects in my mongoDB on app start if none is found?

I am coming from a Java Hibernate frameworks, where an sql script can be written and added to a project, with the intentions of creating objects in my Mongo Database when the app runs and no data was found in a specific database table. I am using Mongoose presently with mongoDB, and i want to initialize my mongoDB table with objects if none is found. Initially i achieved this my creating an object and calling the new mongoose.save(obj) inside my app.js which is my start up file. But is there a cleaner way one already supported by Mongoose, or is this my best bet ?

use functions inside of defaultscope in sequelize

I'm using Sequelize for a project with a single db for multiple applications. I need to query MSSQL database for each request based on the application_id I receive in the request. (on Nodejs)
Calling a function inside of scope in Sequelize model definition requires specifying the name of the scope for each query which is not desired.
I think if I could use functions inside of defaultscope to make it work (without manually overriding it again and again) . Or if there's any other way to accomplish this (better without rewriting Sequelize functions). Thank you!

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

use Waterline as Standalone (no express)

Good Afternoon,
I am new with node.js and I try to develope an only command app.
For this app I need an ORM and I wish to use WATERLINE as standalone but not in express framework.
I looked at the example and I succeed to see my different collections.
// Our collections (i.e. models):
ontology.collections;
console.log(ontology.collections);
// Our connections (i.e. databases):
ontology.connections;
I am stucked after this. I can't find a way to return my models and make queries.
If someone could help me taht would be great.
Thanks
If you initialized Waterline in the ontology variable and got the collections successfully loaded as you say, now you can access each collection loaded (with loadCollection()) like this:
ontology.collection.mycollection
Where mycollection is the identity defined in your model.
Then you can make queries:
ontology.collection.mycollection.find(...)

Resources