How to use Sequelize ORM with multiple Schemas - mysql

So I have a multi-tenant application. So each tenant has their own schema and there is a common schema between all of them. The common database is created and maintained programmatically by sequelize using the folders and files created by sequelize init. This allows me to use ORM functions such as findAll or findByPK for the common schema but to do any CRUD on one of the tenants schemas I use a mysql connection with raw queries. My question is: Is it possible interface with the tenants schemas using ORM? Like can I change the current default schema being used by sequelize so that I can use findAll for a tenants schema?
Please let me know if this question is confusing and I will try ask it in a different way.

Related

NodeJS application database versioning and data migration

I am planning to use Postgres as my database and sequelize as my ORM framework one of my NodeJS project. I need your suggestion on how to accomplish database table changes in production enviroment.
For example , we deployed our version-1 application with some database schema.
Later , there is a change in my schema in Version-2. Say one of the table is altered and few columns are dropped and few columns are added.
Also one of the table itself is not required in Version-2.
In my production , I have some database with data. When I install Version-2 of my NodeJS application , I need to keep the old data as it is and apply the new database schema of Version-2.
How to achieve this , Let me point out some reference to this.
Thanks
Sequelize (or rather, sequelize-cli) has built in support for migrations.
You can write migrations using the Sequelize js api (e.g. queryInterface.dropColumn('users', 'address')) or write raw SQL and pass it to sequelize.query()

I don't want to define models twice in any of Node ORM lib's

I have an MySQL database with couple tables and I started to use Node ORM lib's like Sequelize.
Every ORM tutorial have an article about model definition like:
http://docs.sequelizejs.com/en/latest/docs/models-definition/
https://github.com/dresende/node-orm2/wiki/Defining-Models
So... Really? Can ORM generate model automatically, please? Kohana ORM and Idiorm doesn't want me to define model by myself.
Thanks a lot!

MySQL Views Support in Sequelize ORM

I woululd like to use sequelize as my ORM in a web app I am building. The app will be based on NodeJS - Express for constructing and MySQL as its relational database. What I haven't found anywhere on documentaino or on the net, is how to declare in sequelize models the "Views" that exist in my MySQL Database....
Is the only way to manually construct the query SQL? Thanks
You can use Node ORM2, which has support for MySQL views as well.
Try at
https://github.com/dresende/node-orm2

Are Grails domain classes necessary when interacting with a database?

I'm still kind of new to Grails (and Groovy), so apologies if this question seems dumb.
I'm trying to access a SQL database, and it seems that I could use SQL commands in the Controller (taken from this StackOverflow question):
import groovy.sql.Sql
class MyFancySqlController {
def dataSource // the Spring-Bean "dataSource" is auto-injected
def list = {
def db = new Sql(dataSource) // Create a new instance of groovy.sql.Sql with the DB of the Grails app
def result = db.rows("SELECT foo, bar FROM my_view") // Perform the query
[ result: result ] // return the results as model
}
}
I know that if I were to create a domain class with some variables, it would create a database table in SQL:
package projecttracker2
class ListProject {
String name
String description
Date dueDate
static constraints = {
}
}
but this would create the table named "list_projects". If I didn't do that and just created the SQL table outside of Grails, and if this follow-up question says that you can disconnect the Domain class from your database, what purpose do Domain classes serve? I'm looking to do some sql queries to insert, update, delete, etc. data, so I'm wondering what's the best way to do this.
Domain classes are used to model your domain of knowledge within your application. This is not only the structure of the data but also the basis of interaction of those models within your domain of knowledge.
That said, there is no reason why you can't create a Grails project without any domain classes and use your own SQL statements to create, read, update, and delete data in your database. I have worked on projects where there was no domain classes and everything was modeled using DTO (data transfer objects) and services for accessing an already existing database and tables.
Of course by not using Domain classes you lose the integration with GORM, but that doesn't seem like an issue for your case (nor was it in the case I outlined above).
That's part of the beauty of Grails. You don't have to use all of it, you can use only the parts that make sense for your project.
In one of my projects I needed to dump the contents of a MySQL into a Lucene index. Creating the the whole domain class structure for such an one-off operation would be an overkill, so the groovy SQL API did just fine.
So, my answer is no, you DON'T have to use the domain classes if you don't want to.
I agree with what #joshua-moore have said, plus domain classes can drastically simplify you project if you use them properly
I agree to both answers but for your particular case, I would suggest having a domain model for the underlying table.
Reasons:
You mentioned about all CRUD operations in your requirement. With a domain class it will be convenient to let GORM handle the boiler plate code for any of the CRUD operation.
When using SQL, you have to handle transactions manually for update operation, if transaction is a requirement. With GORM and Hibernate, you get that handled automatically.
Code will be DRY. You do not have to create a SQL instance every time you need a operation to be done.
You can conveniently create domain classes for existing tables using db-reverse-engineer plugin
You get one level of abstraction using domain classes. In future, if there is a plan to replace a MySQL db with Oracle or a no-sql db then all that will be needed is to change the driver (in most cases, with Mongodb there will be a bit of churn involved but very less as compared to replacing SQL queries)
Auditing can be easily achieved if domain class is used.
This feature (add/update/delete) can be easily exposed as a service, if required.
Data Validation is easier in domain classes
Better support to avoid SQL Injection as compared to plain vanilla queries.

Using existing mysql schema in nodejs

I tried using sequelize and node-orm as well - but is there a way to reuse existing mysql schema? I Have a db schema defined with tables in mysql and wanted to use this with my node.js app (using express). So, I don't have to write all the define methods of defining tables again.
Any help appreciated...
I went through bunch of stackoverflow questions already such as: Which ORM should I use for Node.js and MySQL?
Thanks,
JP
Checkout bookshelf.js, it doesn't require that you define the schema in the model layer.