I am using sequelize ORM in nodejs. I have two apps. One app is only for database related task like creating tables and migrations (app1). Other app is rest api (app2). I am using sequelize in app2.I dont want to create table using app2 and i want to throw error here if table doesnt exist or schema is not the same. Is this possible using sequelize?
If you dont want to create the table in your app2, then just dont put the sequelize.sync() code. (if you're using migration and the sequelize-cli, then dont do sequelize db:update before launching your app2)
Concerning the error, if a table is not created, you will have errors when trying to use the schema !
I don't think there is a "clean" way of checking if the schema is inline with yours.
Related
In Sequelize, when we create a model using the following command,
sequelize model:generate --name Company --attributes name:string, desc:text
A migration file is also getting created. And we can make the models sync with the DB by adding the following piece of code.
models.sequelize.sync().then(() => {
console.log("DB Synced");
}).catch((error) => {
console.log(error);
});
Therefore, when there's a change in the column names or something, they get synced with the DB.
So, do we really need to run migration ? At any point in the development or production ?
Please correct me if I am mistaken.
I strongly recommend to use explicit migrations and to point tables for storing applied migrations and seeds in a sequelize config in case you have different versions of a database in different production environments (even if you are just planning different production deployments).
If you run a newer version of your app with some model changes on an older production DB then by executing sync method you change this production DB accidentally. Moreover you cannot reverse these changes easily because you don't know anything about differences between models in your current app and tables in the production DB.
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 ?
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!
So, I want to write an application which use some data migration. Where should I store a current state of db?
For example: I have a production server and my development machine. I wrote an application and 3 migrations for it. When I deploy the application server also runs 3 migrations.
Now I'm going to write the 4th migration. How does server recognize that it need to run only 4th migration and it already run previous 3 migrations?
Ref https://github.com/sequelize/cli
sequelize init:migrations
will generate migration folder where you need to write migrations and
There are three types of storage that you can use: sequelize, json, and none.
sequelize : stores migrations and seeds in a table on the sequelize
database
json : stores migrations and seeds on a json file
none :
does not store any migration/seed
I am currently working on an inventory management software in Node js and MongoDB. I am pretty new to MongoDB, having worked in Oracle and MySQL for most of my projects.
Is it possible to create a separate database schema for every client who uses my software, with each client having access only to his copy of the database schema and collections?
The equivalent of selecting data in Oracle database would be
Select * from User1.table,
Select * from User2.table etc
Also, if it were possible, how would it be implemented using a node js mongo db client like mongoose?
I looked at MongoDB documentation, but it talks mainly about adding users to a database for authorization.
I apologize if it seems like a silly question, but id appreciate it if someone could point me in the right direction for this.
Before starting to invest a lot of time in the development of your project, check out other possible approaches to the scenario that you are trying to build.
I did a quick search on SO and found some additional threads with similar scenarios:
MongoDB Database vs. Collection
MongoDB Web App - Database per User
Additional info about mongoose database creation
Whenever you call the connect method on the mongoose object, you are either connecting to an existing database or you are creating it in case it doesn't already exist.
You could have a function that allows you to pass in a name argument with the name and create databases programmatically:
function createDatabase(name) {
var conn_string = 'mongodb://localhost/';
if (typeof name == 'string') {
conn_string += name;
}else{
return false;
}
mongoose.connect(conn_string);
}
Also, be aware that a database will be created when you first insert a record in a collection of that particular database.
It is not sufficient to only connect to the database, you also have to insert a record.
As per my previous example, you could also pass a schema parameter to the function, tailored to each user's profile and fire an insert statement after you connect to that database.