Models
Overview
code.store platform by default use Postgres RDBMS with TypeORM and provides opportunity to generate models and migrations based on your schema.graphql
.
PostgreSQL is a powerful, open source object-relational database based on SQL and supports many of the features of the SQL standard.
TypeORM 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 TypeORM 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!
Generate models and migrations
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...
Simple entity
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:
Generated data
generatedData/
includes two directories:
entities/
dir with your TypeORM entitiesmigrations/
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:
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 TypeORM migrations page.
Also, you can find a TypeORM entity inside generatedData/entities/Post.ts
file.
Apply generated changes
If everything is OK and generated files meet your needs - just copy them to the appropriate directories: src/data/entities
and src/data/migrations
.
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 local development section.
We recommend use entities and migrations generation as bootstrap feature. Always check the files that are generated to avoid surprises.
Last updated