How to use ORM while table already define in database? - node.js

I'm using "sequelize" and "sqlite3"
But I don't know how to do even if the table s already exist in database.
i have seen the document , but it seems that must define the schema everytime even if the table already exist ?
If the table already in database , how can I use it without define schema again?

You can use sequelize-auto npm module for the same.
https://github.com/sequelize/sequelize-auto
I had the same situation before and used this npm module to generate the models, later can tweak or do some minor change if needed.

Schema (model) definition only represents table in database and does not create table itself.
If table already exist you need to define schema for this table only when app starts.

FWIW, if the table already exists, you need not use sync(); you can begin with User.create().

Related

Change in mongoose Schema does not reflect

In my NodeJS project, I have created a mongoose schema which I changed midway through the project. More specifically, I added the unique parameter to a field.
However, this change does not seem to reflect as I am still able to create multiple documents with the same value for the parameter which I set as unique
How to fix this issue
Mongoose enforces the unique constraint by creating an index in MongoDB with the unique option.
If you already have duplicates in the collection, that index creation will fail.
Note that you may have to call syncIndexes to update the database with the new index.

sequelize force mark migration done

I re-created my database on one of my dev environment and now when I run the migration via sequelize db:migrate, it tries to run the migrations from the first.
I don't want to re-sync/re-create the database since running migrations on the dev environment ensures that the migrations are correctly written.
Is there a way to force-mark some migrations as 'done'?
Sequelize-cli stores the migration data on a table called SequelizeMeta.
You can copy the migration filename from your existing DB and insert into the above mentioned table in new environment's DB.
All the migrations recorded would be considered as they have already ran.
Though this would stop selected migrations from running, it's not the best approach to be taken.
This metadata could also be stored in a json, though I am not very aware of the structure for it.
You can dig through the docs here
The actual way of doing is as follow,
There are two tables sequelizemeta and SequelizeMeta(Hidden)
And for skip or say migration already ran is enter value in both table like
INSERT INTO sequelizemeta (name) VALUES
('20181019072815-roles.js'),
('20181019093229-users.js')
INSERT INTO SequelizeMeta (name) VALUES
('20181019072815-roles.js'),
('20181019093229-users.js')
Note- SequelizeMeta is hidden table but we can query it.
SELECT * from seerportal.SequelizeMeta

Can orm2 create a table automatically?

I use Hibernate in J2ee, when the datebase has't table the framework can create table and when fields changed it can update datebase, Can orm2 be done?
As this wiki pages explains, orm2 can create the database tables if they do not exist. Update of an database table is possible by another library, however i haven't tried this: https://github.com/dresende/node-orm2/issues/103

How to use migrations in Sequelize?

I have used ORMS such as EntityFramework, Waterline and Mongoose. I just started using Sequelize. I have created a sample model. Later I wanted to add another column to the table corresponding to the same model. When I edit the model, the new field doesn't seem to be reflecting in the table unless I drop the table. Am I supposed to use Migrations? I got confused after reading the documentation. Could someone help me out?
Yes, you should use migrations as if you edit a model in sequelize when you application starts it will only create the table if it doesn't exist. It does not check if the schema is different thus it doesn't update the schema of your table.
If I were you, I would install sequelize-cli and then run the sequelize init command. This sets up your project ready to use migrations.
Please note that we using sequelize migrations you have to explicitly define the primary key, updatedAt and createdAt columns otherwise they will not great created!
The docs are not too bad about migrations: http://sequelize.readthedocs.org/en/latest/docs/migrations/

Adding default values to mongoose models at runtime

I want to have default values for certain fields in my mongoose models. The trick is I don't want to store these values in the database, but what to add them when the model is initialized.
Please help.
You can define an 'init' middleware function for the schema which runs when a model instance is loaded from the database. That should let you manipulate the instance to add your defaults as needed.
Also see this related question for more details as the docs are pretty spare on this.
Maybe a json file?
Using a require('path/to/json' ) you can acces to it and get the values.
Im actually doing something like that, but using the same JSON SCHEMA wich is build for validation purpose.

Resources