NodeJS sequelize model issues - node.js

Since yet I have always worked in C# with ORM's like Entity Framework, but now I want to start with NodeJS, since I need it for school.
In NodeJS I started using Sequelize since it seems to be the most famous ORM for NodeJS, but I have quite a few problems with it:
Why do I have to write, when chaning or expanding a model, the changes on my own in a migration. Is there a way to let a package automated write my migrations depending on my model changes like it is in entity framework by default. (I have already tried sequelize-mig and sequelize-auto-migration npm packages, but they didn't work)
Is it possible to let a sequelize model inheritate from an other sequelize model. For instance I have a model User which has the properties name and password (only an example). Now I want to expand the model by creating a new model that should have the name UserWithToken and should extend User and has an additional attribute token. Is this possible?

Related

Sequelize: preview sync query without executing it

I'm trying to make an application with node.js and sequelize ORM. I learnt about the function Sequelize.sync() to update database schema based on the app model.
Is there a way to log the SQL statements that Sequelize.sync() would run without executing them?
I'm used to Doctrine ORM where such a thing is possible and quite convenient (it allows to double-check your model before actually persisting it to the DB).
Is there a way to do it with sequelize too?
Thanks

What is the best practise of using same Model on several projects?

I create a ReactJS app using Sequelize and PostgreSQL database. I defined my models on my ReactJS API, but I need to use the same database (so the same models) on an other API.
How should I do so without writing again the definition of each model, because if I made a change, I would have to do it everywhere. Is there a better way to handle this king of problem?
Why I don't use the ReactJS API? Because when I built my React app, the API disappears: I'm using proxy.
var myModel = require('./models/myModel');
you can add by require here my model is in root directory inside model folder.
You could place the models in their own separate Node.js package, so they could be reused by both.
Please, refer to this: https://docs.npmjs.com/getting-started/creating-node-modules

Mongoose Sharing Validation/Pre-Save Methods Across NodeJS Apps

If one Nodejs app connects to a Mongo instance, and that app has defined a User schema with pre-save hooks, validation, etc.
And then another Nodejs app connects to the same database, and tries to register a User schema with different properties.
And then the second app saves a User
What happens?
I'm confused with how two Nodejs apps may communicate to the same database.
For example, it's very easy to see how one might want to have V2 of an api on a separate nodejs app developed by a separate team. But they will plug it into the same database and use the same Schema (or will they?), and I'm confused with how things are shared between the two apps.
Any help clarifying this in best-practices would be appreciated
I believe I've found the answer in the Documentation.
This connection object is then used to create and retrieve models. Models are always scoped to a single connection. docs
And
Models are fancy constructors compiled from our Schema definitions. docs
Which explains that a DB Connection 1's Schema Definitions (pre-save, etc), do not affect DB Connection 2's writes/etc.
Essentially, they are completely independent of validation and everything else. They only need to be OK in their own context.

bookshelf without knex - execute query

I have a functioning node.js application with logs data to a mysql database. (without using knex.js)
Now, I want to add functionality to query into my database tables. My question is do I now need knex.js? Is it possible to execute queries without knex?
I could not clearly find examples of this.
From the main Bookshelf page:
Bookshelf is a JavaScript ORM for Node.js, built on the Knex SQL query builder. Featuring both promise based and traditional callback interfaces, it follows the Model & Collection patterns seen in Backbone.js, providing transaction support, eager/nested-eager relation loading, polymorphic associations, and support for one-to-one, one-to-many, and many-to-many relations.
Bookshelf requires a knex connection to operate. You cannot (nor should you) use Bookshelf without Knex.

How do we create a Schema at runtime in Mongoose ODM?

It's my understanding that Mongoose Schema's are defined at some point in the initiating stages of a Node app. These will obviously be hard coded, like so:
var SomeSchema = new Schema({
some_string:String,
some_number:Number
});
If I'm creating a CMS whereby the administrator (via a pretty web GUI) could add their own models, with their own pre-defined types (sometimes custom types like e-mail) the app would need to create these Schema's on the fly. Sometimes adding to existing Schemas (presumably using Mongoose's add() method, and sometimes creating new Schemas all-together.
I've done some initial searching but it seems not many people have this requirement. Is this functionality, therefore, a gross misunderstanding of what Mongoose was built for? Was Mongoose built as a closed ODM without any real ability to leverage runtime Schema creation?
Additionally, if I was to go ahead and force this type of functionality, would the prospect of dynamic Schema creation be a potential bottleneck for the speed of my application?
To better summarise my question so as not to attract accusations of debate:
1) Is there a way to create Mongoose Schema's on the fly?
2) Is this programmatically expensive, and is there a better way of achieving my goals of delegating Model creation to a GUI within the administration panel of my CMS?

Resources