[EntitySchema] Extend BaseSchema
See original GitHub issueIssue 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:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Ok, just an information for anyone, who may be stumbling upon the same issue here: In order to “include” some kind of
BaseColumns
to anEntitySchema
, you may need to define them as follows:Then, in your “regular” schema file, include it like this:
we don’t need extends in there. Just use object spread/rest syntax and you’ll get more than just extension, e.g.