question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[EntitySchema] Extend BaseSchema

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [x] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Dear TypeORM maintainers and community,

first of all, i would like to thank you for this awesome project - i really like using TypeORM… Recently, I stumbled upon EntitySchema for decoupling the “database structure” from the actual models - and i like this approach very much!

Before, when using the normal Entity Decorators, i did something like this in order to DRY up my boilerplate code for my entities:

export abstract class AbstractEntity {
  @PrimaryGeneratedColumn('uuid') id: string;
  @CreateDateColumn() createdAt: Date;
  @UpdateDateColumn() updatedAt: Date;
}

export class Category extends AbstractEntity {
   @Column() slug: string;
   // ...
}

When switching to EntitySchema, i found, that there is some kind of extends param, which can be used to extend an existing Schema (see code here) https://github.com/typeorm/typeorm/blob/b9d2756c1726728da85c36aeedd924230dc601e6/src/entity-schema/EntitySchemaOptions.ts#L13-L18

However, I did not find any real documentation about this feature so far - not even in the sample.

Basically, what i would like to do, is something like this:

export const BaseSchema = new EntitySchema({
  name: 'BaseSchema',
  columns: {
    id: {
      type: 'uuid',
      generated: 'uuid',
      primary: 'true',
    },
    // ... more default column descriptions here
  },
  // maybe some other "default" descriptions here
});

and then “use” this schema as blueprint for other schemas, like so:

export const CategorySchema = new EntitySchema<Category>({
  name: 'Category',
  tablename: 'categories',
  extends: 'BaseSchema',
  columns: {
    slug: {
      type: 'varchar',
      length: 50,
      unique: true,
    }
  }
});

so that the CategorySchema inherits all columns that are described in the BaseSchema. However, this does not work - if i run the dev server (synchronize: true to automatically migrate everything), this does not work…

Maybe you can point me to the right direction? That would be awesome 👍

In order to give something back, i would then update the documentation for the EntitySchema, because this lacks documentation…

All the best and thanks a lot for your time and effort!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
johannesschobelcommented, Mar 1, 2019

Ok, just an information for anyone, who may be stumbling upon the same issue here: In order to “include” some kind of BaseColumns to an EntitySchema, you may need to define them as follows:

import { EntitySchemaColumnOptions } from 'typeorm';

export const BaseColumnsSchemaPart = {
  id: {
    type: 'uuid',
    primary: true,
    generated: 'uuid',
  } as EntitySchemaColumnOptions,

  createdAt: {
    name: 'created_at',
    type: 'timestamp with time zone',
    createDate: true,
  } as EntitySchemaColumnOptions,
  
  updatedAt: {
    name: 'updated_at',
    type: 'timestamp with time zone',
    updateDate: true,
  } as EntitySchemaColumnOptions,
};

Then, in your “regular” schema file, include it like this:

export const UserSchema = new EntitySchema<User>({
  name: 'UserSchema',
  tableName: 'users',

  columns: {
    ...BaseColumnsSchemaPart,

    email: {
      type: 'varchar',
      length: 150,
      unique: true,
    },
    // ... more columns may be defined here...
  },
  // other options like relations, ... 
});
2reactions
pleerockcommented, Mar 1, 2019

we don’t need extends in there. Just use object spread/rest syntax and you’ll get more than just extension, e.g.

 columns: {
    id: {
      type: 'uuid',
      generated: 'uuid',
      primary: 'true',
    },
   ...commonColumns,
   ...etc
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Schema Inheritance : entity.entitySchema.extend is ...
I have defined a base Schema as follows, and using mongoose-schema-extend. testEntitySchema.js var entitySchema = { entityId: { type: String } ...
Read more >
Defining Entities via EntitySchema - MikroORM
With EntitySchema helper we define the schema programmatically. ... extends: 'CustomBaseEntity', // only if we extend custom base entity properties: {
Read more >
Create the entity schema - Creatio Academy
Creatio configuration is based around schemas. Every type of the configuration item is represented by a schema of the appropriate type. From the...
Read more >
entitySchemaValidator() · Backstage Software Catalog and ...
Creates a validation function that takes some arbitrary data, and either returns that data cast to an Entity (or the given subtype) if...
Read more >
Separating Entity Definition - typeorm - GitBook
​Extending Schemas​ ... export const CategoryEntity = new EntitySchema({ ... When using the Decorator approach it is easy to extend basic columns to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found