New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(mapping): allow defining schema via EntitySchema helper #335
Conversation
WDYT @matiux @whocaresk @lookfirst? It's still a WIP and docs are missing, but all tests are passing so you can take a look how the API looks like: entity definition: https://github.com/mikro-orm/mikro-orm/blob/2e13548890a5cad9c6571cde28fa94fa6244ce59/tests/entities-schema/Author4.ts |
Lgfm, but i'll prefer pojo definition based on interface, as it could help with right prop definition via type inferring. |
Agreed, inference ftw. =) |
An example from my current project, that uses typeorm: |
There is
Not sure if that is possible, you mean something like
It has
I guess I understand now what you mean, it will force you to define all interface properties. Does that really work like this? Looking at your example, you have Btw the main reason for having dedicated methods to add properties is that there is some additional work done for default values (mainly when adding relations). It should also have the same type safety as using decorators - e.g. when adding a relation, name of the |
First of all, i'm from phone now, i'll get closer look tomorrow :)
Implementation of typeorm are far (really far) from ideal, but it will force to define all the keys, required by interface, but only in column object. Usage of not defined columns in interface are provided by helper interface called
It more usable for more rapid development, and basic types are ok. I've seen something similar in redux-toolkit and custom Joi types, they are both pretty well (but not ideally) infer types from definitions. For relations, i think, we should pass extra arg, that would let infer type (already created schema, or class) |
So after some time playing with type definitions, I managed to implement type safe way for this: export const schema = new EntitySchema<Author4, BaseEntity5>({
name: 'Author4',
extends: 'BaseEntity5',
properties: {
name: { type: 'string' },
email: { type: 'string', unique: true },
age: { type: 'number', nullable: true },
termsAccepted: { type: 'boolean', default: 0, onCreate: () => false },
identities: { type: 'string[]', nullable: true },
born: { type: DateType, nullable: true, length: 3 },
bornTime: { type: TimeType, nullable: true, length: 3 },
books: { reference: '1:m', type: 'Book4', mappedBy: 'author' },
favouriteBook: { reference: 'm:1', type: 'Book4' },
version: { type: 'number', persist: false },
},
}); Pretty much everything is type checked in this approach:
The OOP API is still there, used inside the constructor, so both approaches are valid, this being more strict. Going forward (v4), we might use Here is an example with custom entity class (for mongo): export class BookTag implements MongoEntity<BookTag> {
_id!: ObjectId;
id!: string;
name: string;
books = new Collection<Book>(this);
constructor(name: string) {
this.name = name;
}
}
export const schema = new EntitySchema<BookTag>({
class: BookTag,
properties: {
_id: { type: 'ObjectId', primary: true },
id: { type: 'string', serializedPrimaryKey: true },
name: { type: 'string' },
books: { reference: 'm:n', entity: () => Book, mappedBy: 'tags' },
},
}); |
8aa069a
to
d34d30f
Compare
One can now define entity schema via EntitySchema helper instead of using decorators or JavaScriptMetadataProvider. Defining the EntitySchema is enough, blank entity class implementation will be created automatically. In that case, user has to create new entity instances via `em.create()`. Closes #283
let's see how it works, available in 3.2 |
One can now define entity schema via EntitySchema helper instead of using decorators or JavaScriptMetadataProvider.
Defining the EntitySchema is enough, blank entity class implementation will be created automatically. In that case,
user has to create new entity instances via
em.create()
.Closes #283