feat: initialize nullable properties with null value (#7795)

This commit is contained in:
Harminder Virk
2024-06-21 16:41:26 +05:30
committed by GitHub
parent 90e6ca0e9e
commit d122b678a8
2 changed files with 27 additions and 2 deletions

View File

@@ -236,6 +236,12 @@ describe("Entity builder", () => {
expect(metaData.className).toEqual("User")
expect(metaData.path).toEqual("User")
const userInstance = new User()
expect(userInstance.username).toEqual(null)
userInstance.username = "john"
expect(userInstance.username).toEqual("john")
expect(metaData.filters).toEqual({
softDeletable: {
name: "softDeletable",
@@ -547,6 +553,12 @@ describe("Entity builder", () => {
const metaData = MetadataStorage.getMetadataFromDecorator(User)
const userInstance = new User()
expect(userInstance.role).toEqual(null)
userInstance.role = "admin"
expect(userInstance.role).toEqual("admin")
expect(metaData.className).toEqual("User")
expect(metaData.path).toEqual("User")

View File

@@ -158,6 +158,18 @@ export function createMikrORMEntity() {
MikroORMEntity: EntityConstructor<any>,
field: PropertyMetadata
) {
/**
* Here we initialize nullable properties with a null value
*/
if (field.nullable) {
Object.defineProperty(MikroORMEntity.prototype, field.fieldName, {
value: null,
configurable: true,
enumerable: true,
writable: true,
})
}
if (SPECIAL_PROPERTIES[field.fieldName]) {
SPECIAL_PROPERTIES[field.fieldName](MikroORMEntity, field)
return
@@ -188,12 +200,12 @@ export function createMikrORMEntity() {
? PrimaryKey({
columnType: "text",
type: "string",
nullable: field.nullable,
nullable: false,
})
: Property({
columnType: "text",
type: "string",
nullable: field.nullable,
nullable: false,
})
IdDecorator(MikroORMEntity.prototype, field.fieldName)
@@ -584,6 +596,7 @@ export function createMikrORMEntity() {
*/
return function createEntity<T extends DmlEntity<any>>(entity: T): Infer<T> {
class MikroORMEntity {}
const { name, schema, cascades } = entity.parse()
const { modelName, tableName } = parseEntityName(name)