How can I do complex database queries such as joins using Sails.js?
I tried the generate method using the command line, it's great and I connected it to my MySQL database and it works great, but now what I need to understand is how I can do complex SELECT statements. I can't find it in the documentation.
For complicated queries refer to this answer by particlebanana, one of Sails.js core contributors.
As an alternative to perform joins, you can use the associations branch.
Related
I´m starting with nestjs and I want to create an application with a kind of 20 tables in the database, what's the better way to create these resources in my code? should I create a new resource for each table in the database? I can't find information about this.
Is there an easy way to create migrations among the models created in the resources?
I suggest to you Typeorm
With typeorm you can connect with any database and migration is so easy
And also typeorm use code first you your tables and fields all in the backend code which is nestjs then you can sync or migrate it to get real tables
You can read more about from here
https://typeorm.io/
I'm currently writing a program that is a To-Do list that just runs locally on my machine in the terminal screen(written in node.js, no extra dependencies). I've set up everything and it works totally fine, but I have never actually used any databases other than just session cookies on the browser. What would you all suggest I use when all I want to do is learn how to store some simple data in a database and then be able to manipulate it in the same fashion.
Any help would be greatly appreciated on this one! I'm really just looking for suggestions on a good database, as well as a bit of guidance on the best way to use that particular technology. I'm familiar with SQL and NoSQL databases, but like I said, I have never even used one on an actual application.
Thanks in advance.
In my view, You can use mongodb which is easy to configure and manipulate data. It helps you to work on json objects directly from database. Use express to create your RESTful endpoints and exposed data. Sometime back i use it for angular application, repo link is below for your reference :
AngularJs, ExpressJs, MongoDB application
Hope this helps.
Cheers
You can use sqlite which provides a good SQL API but keeps things very simple by simply using a file on your disk and accessing it through code directly in your application. You don't need to start another server and take care of configuring it.
For a simple To-Do list that should be more than adequate for storing your data whilst keeping your configuration simple and easy.
Here's a tutorial on how to set it up:
http://www.sqlitetutorial.net/sqlite-nodejs/
You'll essentially be using the sqlite3 npm package: https://www.npmjs.com/package/sqlite3
That page also comes with brief usage instructions.
Nodejs has client libraries for database interaction
Ex.
For SQL database:
postgresql : https://github.com/brianc/node-postgres
For NoSQL db:
mongodb : https://github.com/Automattic/mongoose
You can also create pool of db connections
I'm working on an Open Source microservice based project in Node.js.
Description of the problem
Some microservices are so thin and only interact with MongoDB, so I haven't used Sails there and preferred using Mongoose directly. Thus, I've implemented mongoose schemas for each of the objects I need to persist in mongo.
I'm also going to have Sails.js in some other microservices, Therefore I'll have to implement Waterline-type schemas for the objects I want to persist & query there.
The problem is the duplication of schemas, which I find useless.
Solutions I thought of
I wish to use one type of schemas across all the services.
There are 3 options now:
A magic method to connect between the schemas (which I'm not aware of), and have both worlds work simultaneously (least probable option).
To use Waterline across all the microservices, whether I use Sails.js or not, therefore have 1 schema type in the system.
To force Sails.js to use mongoose adapter instead of Waterline, and then implement some CRUD Data Access Layer (DAL) which uses Mongoose internally, and use it everywhere to access the DB.
Therefore the Mongoose schemas will be across all the system. But then I give up on the Waterline encapsulation capabilities, and if in the future I'd like to change DB, I'll be in trouble.
I could also compromise and duplicate schemas in my app, but that's the last resort as I believe there has to be some solution to this problem.
Whoever consider Waterline vs Mongoose... right to this date, choose Mongoose.
Waterline made us so much head-ache, while mongoose is so smooth and straightforward.
I could think of the following to go fully Sails:
put the common parts, like the Sails models, in a Sails project and push it to a (private) npm package
use https://github.com/tjwebb/marlinspike or other sails hooks to import them in every instance you need it.
I have not tried this, but preparing for similar steps in the future.
Also, I saw this issue: https://github.com/tjwebb/marlinspike/issues/4
I want to preface this with saying that I really like sails.js for a simple MVC framework, but feel that it lacks in its documentation and api reference.
I searched all over to try and find any information on a good mssql (SQL server) adapter for Sails.js, but cannot find one. I am hoping someone has come across one, and could recommend it. I also looked into building a custom adapter, but found the documentation to not be helpful. Any help on this topic would be greatly appreciated. Thank you.
We don't currently have a MS SQL Server adapter but it's something I'd love to add. There is a basic definition of the various interfaces and how adapters are created at: API Adapter Interface.
If anyone would like to tackle this you can use the Sails MySQL Adapter or the Sails PostgreSQL Adapter as examples.
I don't have access to a MS SQL Server to build one out but there is an Integration Test library I use to build out adapters that support the CRUD interface: Waterline Adapter Tests.
I have recently started building a sails adapter for MSSQL based on the node-sqlserver module.
It isn't finished yet but I have finished the main parts of the adapter and it still needs testing, but there is enough there to get going.
It can be found at https://github.com/swelham/sails-mssql
There is now: https://github.com/cnect/sails-sqlserver
Complete with sails 0.10 associations support, and fully unit tested against the Waterline spec:
Technically since Sails is base on express.js you could use custom express middleware to specify the use of your an adapter that has already been created.
The first one is the top rated adapter
checkout https://nodejsmodules.org/tags/mysql
Otherwise you can just use the waterline ORM and the MySQL adapter that is provided for sails.js
https://github.com/balderdashy/sails-mysql
For documentation on how to use waterline models checkout the 0.9x docuemntation
http://sailsjs.org/#!documentation/models
sails-mssql is working fine for me on 0.10.x, here's a gist of how to configure it, using sails-mssql 0.10.x
https://gist.github.com/hybrisCole/868f979d8d129247a2da
my package.json has this:
"sails-mssql": "git://github.com/jaredfromsubway/sails-mssql.git#master",
Sails-sql is currently the latest adapter for MySQL/MSSQL (eventually PostgreSQL as well). It's currently safe, fine-tuned, and upgraded for use in production with MySQL and MSSQL. Most developments are primarily pending for PostgreSQL at the moment.
Another current option is https://www.npmjs.com/package/sails-mssql (https://github.com/intel/sails-mssql).
I've been trying to do some research on node.js and Mongodb and have a few things that need clarifying.
I'm trying to query Mongodb from the web and want to use JavaScript because I'm familiar with the language, plus it makes sense because that's what Mongodb uses in the command line interface.
It seems that I'll have to use node.js to query mongo from the web, but what I can't find info on is a way to tell my node.js server what I want to query, from the web.
If someone can point me in the proper direction ( reading material, how to, examples) that would be awesome.
Here's some articles I used...
http://css.dzone.com/articles/nodejs-and-mongodb-beginner%E2%80%99s-0
http://srirangan.net/2012-02-node-js-and-mongodb-getting-started-with-mongojs
http://mattkopala.com/blog/2012/02/12/getting-started-with-nodejs/
I found Mongoose to be the best Node.js MongoDB library (the creator of Mongoose actually now works for 10gen, the company behind MongoDB).
Besides a lot of good examples on the official Mongoose website, you can find lots of other resources:
http://pixelhandler.com/blog/2012/02/09/develop-a-restful-api-using-node-js-with-express-and-mongoose/
http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/
http://dailyjs.com/web-app.html (how to build an app from the start, multiple lessons)
One of the most important things I like about Node.js and MongoDB (Mongoose in my case) is that you are writing the query commands sometimes exactly like you write them in the MongoDB shell.
P.S. Last but not least, checkout the videos from 10gen related to Node.js and MongoDB, there are lots of them (and people using Node with Mongo in production): http://www.10gen.com/presentations#programming_lang__javascript_nodejs