Rares Capilnar 5ad56a1fcd Add onboarding experience 2 jaren geleden
..
README.md ad046b87fe chore: update READMEs 2 jaren geleden
onboarding.ts 5ad56a1fcd Add onboarding experience 2 jaren geleden

README.md

Custom models

You may define custom models (entities) that will be registered on the global container by creating files in the src/models directory that export an instance of BaseEntity.

Example

1. Create the Entity

// src/models/post.ts

import { BeforeInsert, Column, Entity, PrimaryColumn } from "typeorm";
import { generateEntityId } from "@medusajs/utils";
import { BaseEntity } from "@medusajs/medusa";

@Entity()
export class Post extends BaseEntity {
  @Column({type: 'varchar'})
  title: string | null;

  @BeforeInsert()
  private beforeInsert(): void {
    this.id = generateEntityId(this.id, "post")
  }
}

2. Create the Migration

You also need to create a Migration to create the new table in the database. See How to Create Migrations in the documentation.

3. Create a Repository

Entities data can be easily accessed and modified using TypeORM Repositories. To create a repository, create a file in src/repositories. For example, here’s a repository PostRepository for the Post entity:

// src/repositories/post.ts

import { EntityRepository, Repository } from "typeorm"

import { Post } from "../models/post"

@EntityRepository(Post)
export class PostRepository extends Repository<Post> { }

See more about defining and accesing your custom Entities in the documentation.