Models
Last updated
Was this helpful?
Last updated
Was this helpful?
code.store platform by default use RDBMS with and provides opportunity to generate models and migrations based on your schema.graphql
.
is a powerful, open source object-relational database based on SQL and supports many of the features of the SQL standard.
is amazing ORM for TypeScript and JavaScript (ES7, ES6, ES5). Supports many SQL databases, including MySQL, PostgreSQL,
Each time, when you develop your application you should carry about your data layer
- the place, where database entities, migrations, seeds... are defined. code.store shared developers pain and provide amazing ability automatically generate entities and SQL migrations for your database.
When you create a new type or input for your query or mutation inside schema.graphql
, which located in your src
service directory, or make changes to already existed types you should make changes inside your data models and think about migrations... You don't have to do it each time, code.store platform provides cs generate:models
CLI command, which allows to do whole magic for you!
Models and migrations are powerful tool, which allows you to generate complex things. Generator supports different types and relations such as one-to-one, one-to-many, many-to-many...
First of all, you should define your types in schema.graphql
, which located in src
directory of your service. For example, let's define a simple type Post
with some fields:
After schema.graphql
modification execute cs generate:models
CLI command, which will trigger generation process. During command execution schema.graphql
file will upload to code.store's generator. code.store's generator will validate this schema, generate TypeORM entities and migrations and save it on your local machine on src/generatedData
folder:
generatedData/
includes two directories:
entities/
dir with your TypeORM entities
migrations/
dir with SQL migrations
code.store's generator allows you to generate models and migrations both for service which already has models and migrations and for an empty project, where you just define your GraphQL schema.
For Post
type generated next migration in src/generatedData/migrations/Migration1606769241856.ts
file:
If everything is OK and generated files meet your needs - just copy them to the appropriate directories: src/data/entities
and src/data/migrations
.
We recommend use entities and migrations generation as bootstrap feature. Always check the files that are generated to avoid surprises.
There are two methods you must fill with your migration code: up
and down
. up
has to contain the code you need to perform the migration. down
has to revert whatever up
changed. down
method is used to revert the last migration. To learn more about TypeORM migrations visit page.
Also, you can find a inside generatedData/entities/Post.ts
file.
To make sure that generated entity is correct, just execute cs dev
CLI command. As a result you can find the Post
table inside your database. More information about local launch of your code you can find in section.