Automatically create and update tables in Node.JS? - node.js

I have a prototype server connected to a PostgreSQL database. Because I'm iterating rapidly, I'm creating tables and changing the schemas all the time. I've looked for an npm package which allows me to create such tables and update existing schemas once the server starts, but the options I've found are tied to an ORM implementation, which I do not need.
TL;DR: Are there any npm packages to automatically create tables and update schemas according to a single configuration object provided to it?

A little late, but node-pg-migrate should do what you need.

Related

A Good Point to Create Design Documents in CouchDb

I have a CouchDb instance running a peruser database configuration.
Each user database generated (when a user is added to the _users database) needs to have the same design documents with view/list logic etc.
What is the defacto way to add the design documents to the database upon database creation? Is it simply to add them after a successful user creation request? Or is there a more elegant way of doing this in CouchDb?
There is not a mechanism for initialize newly created user databases, you should include it in your user creation logic.
If you want to decouple user creation and db initialization, I suggest you to explore the following strategy
1 - Create a template database and place on it your design documents that should be applied to every user db
2 - Listen continuously _db_updates endpoint where db level events will be notified. This library can help you.
3 - When a db that matches the user db name pattern is created, you can trigger a replication from the template database to the newly created database using the _replicate endpoint.
If you plan on using the Follow npm module as #Juanjo Rodriguez suggested, please consider using the Cloudant's version. The Iriscouch's version (the one pointed by #Juanjo Rodriguez) is way out of date. For example it doesn't support CouchDB v2.x among other issues. I worked with the Cloudant's team to improve all this these last days and they just released the updated npm package yesterday here: https://www.npmjs.com/package/cloudant-follow?activeTab=versions
The 0.17.0-SNAPSHOT.47 version embarks the patches we worked on so don't use the 0.16.1 (which is officially the latest).
You can read more about the issues we fixed here:
https://github.com/cloudant-labs/cloudant-follow/issues/54
https://github.com/cloudant-labs/cloudant-follow/issues/50
https://github.com/cloudant-labs/cloudant-follow/issues/47

How to run custom query using Waterline in Sails.js?

I'm looking for a way to run custom query using Waterline in Sails.js.
For example I want to create a view, e.g.: CREATE VIEW ... it doesn't make sense to run it via some model, like User.query().
Is there a way to run native query without referencing some specific model?
Waterline doesn't provide direct access to the underlying adapters; you have to go through a model. If it really bothers you philosophically to run a generic query through a specific model, you can always install and use the database driver directly (e.g. npm install pg or npm install node-mysql). But there are advantages to going through a model. For one thing, Waterline handles all the connection overhead for you. And if you change the connection for the model, then the generic query will automatically be using the new connection without you having to hunt it down and switch it yourself.

is there any way to convert mongodb database to waterline db?

I have a one file which has details of the database named xyz.js and another file which created the database using that file in mongodb and also has various functions like adding to database etc.The name of this file is create.js. Now I want to convert create.js code using mongodb to waterline.
me_aj, there is no possible short and simple answer for what you want. Nor there is any tool that will do what you need. Here are some pointers for what you'll need to do.
1. Learn about using waterline
It may sound obvious but you should start by getting acquainted with waterline and the best places are the waterline project and the Waterline Docs.
By then you'll know waterline is an adapter based ORM and that you'll have to decide which adapter to use. If you are using mongodb, you are probably looking at sails-mongo.
2. Config waterline to connect to the desired data store
Waterline, contrary to some other ORMs, has to be initialised before you can start using it. If you are using it outside sails you should look at these examples on how to set up a simple app. You'll also need to configure it by following the specific adapter settings. If you are connecting to mongodb check the instructions on the sails-mongo project.
3. CRUD operations
By now you've setup waterline to initialise and connect to your chosen data store, now it's time to do things with it. Similar to many other ORMs. waterline uses methods such as find, update, create and destroy to perform CRUD operations, to learn more about using these check the query methods documentation page.
This should be enough to point you in the right direction.

Node js ORM supporting Schema changes

I was considering Sequelize as nodejs orm, but ran into issue while modifying the schema it does not support new columns directly. This SO thread states "migrations" framework as workaround. This seems to be a development overhead especially after first cycle of changes you will end up either deleting the test data or will have to create migration for each new column change. does node-orm or bookshelf have the same problem? I am using postgres database.
Using sequelize's migrations, you do not need to create a new migration for each column change as you can include many changes within a migration.
I use fixtures to load my test data so that migrations do not affect my data.
I haven't tried node-orm nor bookshelf, however I have no reason yet to stop using sequelize!

Two nodejs applications, one mongodb database

Good day! I have created an application using nodejs + mongoose and now I want to make something like a superuser application. I need my admin panel application to connect to the same database. So, i have a question.
Should i store the same Schema file in both applications to have an ability to use my Schema methods? In other words, what is the best way to create one more API using the same db?
Thank you!
If I'm not mistaken, why not create another service that only interacts with the database? That way, the systems will refer to the same schema/DB regardless of which application you want to connect to it. So the superuser application and the normal application will just query the DB microservice that interacts the database.
Pro: source of truth for the schema for all applications and the DB queries will just be API calls
Con: additional overhead in creating your ecosystem
If you are using the same DB from two different applications, you will want to make sure those schemas are the same between the two. If one changes its inputs, the other might need to change its display (or risk not expecting all that information). Keep all this in mind during your release process.
I would suggest making the schemas an external library to both, or have the admin panel require the current app. You'll avoid getting two sets out of sync and know to look at one place for the schema definitions.

Resources