Some Sequelize users of SQL databases often use sync({ force: true }) in the early days, and then switch over to migrations. I'm confused because the migrations and models seem to have a similar syntax, but also a number of differences that don't seem to be strongly highlighted.
Some observations (which may be wrong):
the options objects in the two are different: options.indexes object in the model doesn't work in migrations (but works in models)
Custom index names using unique: 'somename' doesn't work in the migration field attributes (but works in models); but a unique: true works in both
foreign keys need to be explicitly created - and a references needs to be set in migrations (but in models, the hasMany/belongsTo automatically generates the foreign key field and foreign key reference)
id, createdAt and updatedAt must be manually created in a migration, but not a model
The last two make sense as it's delineating the migration responsibilities (which sets up the db schema) versus the model - but what the migration supports/how it works seems different for similar items, and the sync option exacerbates my confusion.
Some questions:
What are the main differences to keep in mind between migrations and models in Sequelize (not the general philosophy between the two)? What options in models (when using the sync option) do work in migrations?
Do others simply copy model files into migrations when switching from sync to discrete migrations? Do you cut out parts of the extraneous info that only works in models? Only works in migrations?
Related
I am pretty new to NoSQL and would like to fully understand the concept of namespace and how it compares to SQL schema.
I have seen plenty of useful analogies between tables, row, ... and their NoSQL counterparts.
Could you please help me understand the namespaces ?
In particular, I would like to know how I could leverage them to segregate the data of my dozen of customers ? I want to prevent accidental information leak between two of then, while still using a single database.
It really depends of the database engine you are using, it is hard to give a generic answer.
Ideally, if you really want to segregate the data, you can use multiple databases (in Redis, Redis Enterprise, MongoDB). In this case you are sure that data are separated. But you say you want to use a single DB. (why?)
If you want to stick with a single database you have various options, once again depending of the database engine you are using.
If you are using Redis:
you can use specific namespace based on key pattern, for example app:cust-001:orders, and you control the access to the data based on the key name/pattern. In Redis 6.0, the notion of ACL (Access Control List) has been added allowing you to limit the operations/access to the data based on a key pattern, for the connected user. This will allow you to have a good control of the data and who can see/manipulate them
If you are using MongoDB:
you can use multiple collections (tables), for example, prefixing the collection name with a context.
or you can use a composite key, where one of the fields will be your context
In both cases, for Redis and MongoDB, you are kind of creating using business logic the concept of "database".
If you provide more details/examples, the community can probably give you a more detailed answer.
I have to work on one Node Js project where I have to use two database in single application, One database is MongoDb and the other one is postgreSql. Till now I have only used only one dababase in one project. I just want to know that "Is it possible to use two different database as mentioned above in one Node Js project". If yes can you please provide me essential configs and plugin required to setup the project ?
yeah... NoSQL & another RDBMS together...because
NoSQL: fast and simple, but has little to none structure to enforce constraints on data.
RDBMS: satisfies all ACID properties, keeping your data safe and clean. But performance goes down rapidly as traffic and data set size grow.
for doing so ..
you can use an ODM (Object Document Mapper) like mongoose to deal with mongoDB
& an ORM (Object Relational Mapper) like sequalize to deal with mysql , postgre
As provided in the docs.. you got to install both
npm i -s mongoose sequelize
My service is built using Node + Loopback + Mongo.
I would like to drop the db at the startup if and only if at least one of the following two asserts is true:
any model schema has changed (note that I cannot use isActual() because that's usable only with relational DBs)
new data has been added/removed from the default data-set definition files (data folder)
I'm currently versioning the db using a custom logic comparing version table and a version unique var but I wonder if there is an easy way to check the two asserts above.
Any idea? Ty
i would like to ask for help with my backend flux.
I'm starting to use SQL now, and i have some background in noSQL databases, but i don't know SQL much, so i'm having some trouble finding out how to register my schemas.
I'm using node-mysql, and the way that i can create schemas is calling the method query, like:
myInstance.query( 'CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NULL NULL,
email VARCHAR(100) NULL NULL,
password VARCHAR(100) NULL NULL
)');
The problem with this solution is: this code will run in every server initialization.
So, i would like to know how to check if the schema already exists, and.. is this a good solution?? I was thinking in a bash script that creates all schemas, them i don't need this if statements.
Thanks.
What you are calling a "schema" is really a "table". Hence, create table statement, rather than create schema. This is very important. Perhaps this part of the documentation will help you understand the difference.
There are four very different constructs:
Database Server -- how you connect to one or more databases
Database Instance -- a grouping of objects, typically a unit of backup and storage
Schemas -- a grouping of objects (which may be within a database), typically a unit of permissions
Tables -- where data is stored
Note that different database systems have slightly different variations on these.
Of course, "tables" have schemas, which is why it is easy to get confused.
Generally, the management of the database is handled separately from user applications. That is, the DBA (which might also be the developer) would create the database, manage access, handle backup/recovery, and other things. The application would simply connect to the database and assume that the correct tables and data are there.
That is, under most circumstances, you wouldn't be creating tables in application code. Just use the tables that should already have been created for your database.
You can modify you sql statement to
CREATE TABLE IF NOT EXISTS users (…
That way the code will run on server init, but not do anything and also not fail when the tables are already there. See the corresponding mysql documentation.
Contrary to the answer you got, to have the SQL statements in application code is not that uncommon for backends.
I have my server in nodejs and client in angularjs, mongodb(mongoose) as database. I want my client to be able to make a search query with all the normal consitions put in like fixing few fields to certain values, search string contained in few of the fields etc
query can be
(value of field A can be 'x' or 'y') and
(value of field B can range between dates P and Q) and
(string s contained in fields C or D or E) few more
is there any npm plugin or standardisation I can follow to enrich my server side API and also directly put the expression while querying with out doing a map reduce with multiple queries(with lots of code).
Yes, there are a few. I only know of those that work with Mongoose so if you're using another driver you may need to modify them a bit.
With each of these you'll need to determine which to filter locally and which to let the server handle it. Where you filter should depend largely on how much data you have. Personally, it made more sense to abandon local filtering for my largest sets and simply rely on MongoDB to handle the workload.
angoose
Connecting Mongoose and Angular and More
The original motive for Angoose project is to do away with the dual
model declarations(server and client side) if we are building a rich
model based SPA application using modern Javascript framework such as
Angular and node.js. With both front end and backend using Javascript,
Angoose allows the server side models and services to be used in the
client side as if they reside in the client side.
Angoose depends on following frameworks and assumes you have basic
familarities with them:
mongoose
express
angular (optional, for non-angular app, jQuery is required)
Mongoose Api Query
Mongoose plugin that takes a set of URL params and constructs a query for use in a search API. Also, worst project name ever.
If you use Mongoose to help serve results for API calls, you might be
used to handling calls like:
/monsters?color=purple&eats_humans=true
mongoose-api-query handles
some of that busywork for you. Pass in a vanilla object (e.g.
req.query) and query conditions will be cast to their appropriate
types according to your Mongoose schema. For example, if you have a
boolean defined in your schema, we'll convert the eats_humans=true to
a boolean for searching.
It also adds a ton of additional search operators, like less than,
greater than, not equal, near (for geosearch), in, and all. You can
find a full list below.
When searching strings, by default it does a partial, case-insensitive
match. (Which is not the default in MongoDB.)
Angular Bridge
Link models easily via a REST interface between Mongoose/Node-Express/Angular.js
Create, read, update, delete MongoDB collections via AngularJS.
It uses :
Mongoose for accessing to Mongodb
Express for the routing
AngularJS (with the $resource method)