LogoLogo
CS CONSOLECOMMUNITYDOCS
  • What is code.store?
  • Our vision
  • How it works?
  • Getting started
    • Quick Start
      • Core concepts
      • Quick Start with CLI
      • Quick Start with Web Interface
    • FUNDAMENTALS
      • Services
        • GraphQL
        • REST
        • Configuration
        • Environment Variables
      • Environments
      • Projects
      • Versioning
      • Access and Authorization
      • Secret management
      • Data management
        • Import and Export
      • Generation
        • Models
        • Handlers
        • Resolvers
        • REST
        • Middlewares
      • Logging
      • Microservices
        • Communication
    • RECIPES
      • TypeScript
        • Custom .tslint
      • Basic authentication
      • Setup a local database with your service
      • GraphQL
    • Tutorials
      • Auth0 authentication
      • Metabase integration
      • Database manipulations
        • External database: MongoDB
        • External database: DynamoDB
      • Wrapping an NPM package with GraphQL: a chess server
    • FAQ
  • Command Line Interface
    • Getting started
    • Commands
Powered by GitBook
On this page
  • Overview
  • Generate models and migrations
  • Simple entity
  • Generated data
  • Apply generated changes

Was this helpful?

Export as PDF
  1. Getting started
  2. FUNDAMENTALS
  3. Generation

Models

PreviousGenerationNextHandlers

Last updated 4 years ago

Was this helpful?

Overview

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!

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:

type Post {
    id: ID!
    createdAt: String!
    title: String!
    body: String!
    authorName: String!
}

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:

> cs generate:models

✔ Reading the service schema
✔ Uploading the service schema to the generator
✔ Generated code has been saved to ...demo_app/generatedData 

Generated data

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:

import {MigrationInterface, QueryRunner} from "typeorm";

export class Migration1606769241856 implements MigrationInterface {
    name = 'Migration1606769241856'

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query(`CREATE TABLE "Post" ("id" BIGSERIAL NOT NULL, "createdAt" varchar NOT NULL, "title" varchar NOT NULL, "body" varchar NOT NULL, "authorName" varchar NOT NULL, CONSTRAINT "PK_c4d3b3dcd73db0b0129ea829f9f" PRIMARY KEY ("id"))`, undefined);
    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query(`DROP TABLE "Post"`, undefined);
    }
}
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity('Post')
export default class Post {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  createdAt: string;
  
  @Column()
  title: string;
  
  @Column()
  body: string;
  
  @Column()
  authorName: string;
}

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.

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.

Postgres
TypeORM
PostgreSQL
TypeORM
TypeORM
TypeORM migrations
TypeORM entity
local development