Sequelize model to SQL - sql

I have a project which uses sequelize to communicate with part of a database. The project should not have the responsibility to update the database structure so no sequelize syncing or migrations are being used.
I got asked to find a way to still be able to generate the database from the sequelize models. The ideia is to be able to update models and run a command that generates an SQL with the necessary queries to create the tables, based on the sequelize models. Not even asking for migrations, only the table creation.
Any solution?

Related

Flyway vs Prisma Migrations

I'm migrating an existing TypeORM + PostgresQL project from TypeORM to Prisma. This existing project was handling migrations with Flyway: I wrote the SQL scripts to change the DB and those script were executed against de DB via Flyway. Now that I'm using prisma, I would like to take advantage of prisma migration. However, I would still like to have full control of the migrations, and to that extent, I would like to keep using Flyway. My idea was to generate the SQL files with prisma and then use Flyway to run them against de DB. What I've read so far, is that prisma cannot be used to just generate migrations (it will run them eventually, even if I use the --create-only flag, as discussed in here). I found in the help of prisma cli the prisma migrate diff command and I saw that it receives two urls to compare the schemas of two databases and generate de diff as an SQL file. I was wondering if there is something like prisma migrate diff that receives a url and a schema.prisma file so I can generate the diff using the url to my DB and my current schema file. Or is there any other way to just generate prisma migrations without executing them?
Now, that is what I would prefer. In case there is no way to just generate prisma migrations, I think it's fine to use it to run the migrations. However, I'm a little concerned on possible conflicts between the Flyway migrations that have been executed in the past and the new prisma migrations. I know both of them create tables in the DB to keep track of the migrations and Flyway's and Prisma's table should be different, so there should be no problems. But I don't feel confident about this. Has anyone migrate from Flyway to Prisma than can give me some advise on this? Or can I just ignore Flyway migrations.
 I was wondering if there is something like prisma migrate diff that receives a url and a schema.prisma file so I can generate the diff using the url to my DB and my current schema file
Yes. You can point prisma migrate diff to your Prisma schema as the "to" state and your DATABASE_URL as the "from" state as follows:
npx prisma migrate diff \
-—from-schema-datamodel ./prisma/schema.prisma # path to your Prisma schema
-—to-url $DATABASE_URL # enviroment variable
-—script diff-migration.sql
You can then create a flyway migration using the generated SQL from prisma migrate diff.
You can learn more about the different arguments prisma migrate diff accepts in our CLI reference. Don't hesitate to let me know if you hit a snag. :)

Is it possible to access an SQL database migrated using Django with a JavaScript ORM instead of raw SQL for a microservice architecture?

Suppose that I have an app where the models have been created using Django ORM and Django acts like an API for authentication and gives control to the relation model User using its ORM. At the same time, we want to use Express JS to continuously update a field in one the User model for some feature that requires performance. Is it possible to use a JavaScript ORM with Express JS to update such field? If yes, then how?
In the following tutorial tutorial for Golang, the database is created using SQL and a table called go_test_model is created. Then he uses the struct called GoTestModel to create a row in the table go_test_model.
This basically means that if we create an app called api in Django and add in it a model called Example, then to handle that model in Golang we just create a struct called ApiExample and from there we can have CRUD access to the same table, there might be some conflicts in the fields datatypes between GORM and Django ORM but integrity is still applied in the database itself.
So this particular example solves my problem with Golang and can be replicated using Node JS.
You can use Sequelize for using native functions for performing queries instead of writing raw queries.
Also, please refer to Models section to define models.

how to find a way for mongodb schema?

I'm new to MongoDB. before I start MongoDB i was working with SQL databases. I know that MongoDB is different from SQL and in Mongodb you don't have to define a schema. but if you give your database to other teammates and ask him to work with your database, how he can figure out how to work with a Collection? in this situation in SQL, your teammate will open the database and look at the Table and he will understand how to work with it. Suppose according to your analyze, the User Collection should hold the following data model:
But when you have not defined it anywhere, How can you explain it to others?
I hope that I have been able to express my meaning correctly.

Creating a model in nodejs without an ORM or ODM

I am surprised why this is so hard to find.
I am trying to convert my monolith to a 3 layer architecture for my node express app with a propriety sql.
Repository
BEFORE architecture change
1 file entry.js
1 endpoint with business logic
Functions with raw sql that are called after validation of res.body objects
AFTER
📁 backend
📁 src
📁 services
📄 service1
📁 routes
📄 service1Route [ Route handling /service1 from entry.js ]
📁 models
-service1Model [ Contains sql functions, not schema ]
📄 entry.js [ Main express app]
Tech used
- Using .js not .ts
- nodejs
- express
-
Constraints
- I cannot use postgres or sequlize as they don't support the propriety db that I am using.
Assumptions
postgres or mongoose have popular ORMs and ODMs without which you cannot use them hence developers are ( as a good practice ) forced to create models.
Now I want to create my own models / schema with such validations.
Ask
How do I create models without ORM or ODM.
Is there a difference between schema and model ?
Writing sql functions in model folder : is that the right way to use this architectural pattern.
If schema/model is created in 📁 models folder then where do the sql queries reside ?
What I have tried?
For validating objects with required keys from res.body object
I'll have a go at answering. I'm a database engineer and have written node.js apps that connect directly to the database without using an ORM.
1: So long as you know the structure of the data that you wish to store as it resides in the database you can write a javascript class that has methods to do all of your updating and class instantiation etc. For instance, you can have a user class with a constructor and a number of methods for creating a new user in the database, retrieving a user from the database, updating a user etc. You can read more about javascript classes here and here.
So here your Model is just a class that knows how to interact with the database.
You could store all of your SQL here, but I would advise against that in favour of using Stored Procedures in the database. That way if you ever need to tune your query, or make changes, you can change just the stored procedure without having to create a whole release of your application. Your DBAs can also tinker round with your SPs for you as well.
2: Depends what you're referring to here. A schema in the database is a container for functionality, business areas, or whole applications. Like within a business you could have a Sales schema and a Marketing schema, or you could store all of your application logic in the MySalesApp schema.
A javascript model is one particular piece of functionality and its interactions with the database. Like a user, or a person, or an animal etc. In the database all of these objects (there would probably be a person/user/animal table, with a number of stored procedures for updating, creating, etc) would be stored inside a schema. In MySQL a schema can also be a whole database.
3: You could store your SQL there. But for the reasons mentioned before I'd do this with Stored Procedures. Basically because you can tune them for efficiency without having to redeploy your whole application.
4: This also answered by using Stored Procedures. So instead of having a bunch of code that creates a user, you have an SP that lives in the database and handles everything for you. Then you just end up with a call to a stored procedure that looks like this:
var query = "BB.sp_CreateUser";
var params = [
parseInt(user.userId),
user.firstName,
user.surname,
user.userInitials,
user.telephone,
user.username,
user.password
];
let result = await database.asyncQueryDynamic(query, params);
I've got a bit of abstraction going on here because I'm using connection pools etc. But you can read more about using Stored Procedures here.

Expected database model is inconsistent in real-time

In this question, I was facing an issue where I was writing an update for a deployed application to bring the database up to date with the newer version we are deploying. Basic outline as follows:
Began with currently deployed version of application
Added new functionality that used existing database
Added new database tables and relationships
Added new functionality that depended on the new databse structure
Testing complete, ready for deployment
The issue here is that the currently deployed application has been in use for a few months and has a lot of data that would need to be preserved, so simply replacing the old with the new was not viable (at least not for the database, but of course it works for the code). So I used the following steps to write a script in SQL for the updated version of the application to run the first time it starts up to make the necessary changes to the database without touching existing data (aside from populating the new tables):
Use VS2010's "Generate database from model" functionality to create a .sql (the model was originally created using the "Generate model from database" functionality)
Remove all parts of the .sql that act on the existing tables, except for those that add FKs between new and old tables
Use the resulting script to build the new database
Sounds pretty clean and done, right? Wrong. The mapping from the model to the database was all wrong for the new tables. Long story short, the database that generated the model had tables named in the plural (and the mapping was correct and the application worked), and the database generated by the model created tables in the plural (identical names to what the tables where the DB generated the model, but the model did not map to them). The solution ended up being to change the script to name the tables in the singular, and then everything worked flawlessly.
What happened here? The code remained untouched, no changes were made to the model, and the old tables continued to work fine the entire time, yet somewhere in the process of
Generate script
Delete "new" tables and constraints (those that don't yet exist in the deployed version)
Run script to re-add the tables
the mapping decided to be to singularly named tables (User instead of Users, Address instead of Addresses, etc).
Can anyone explain to me how/why this would happen this way?
You might want to look at some of the tools that redgate supply - good tools for comparing two DB structures and generating a script to update.
http://www.red-gate.com/?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=redgate&gclid=CIamkumgw6sCFcYPfAodnGVjsQ