feat: initialize nullable properties with null value (#7795)
This commit is contained in:
1 parent
90e6ca0e9e
commit
d122b678a8
2 files changed
+27
-2
No files matched your search
@@ -236,6 +236,12 @@ describe("Entity builder", () => {
|
|||||||
expect(metaData.className).toEqual("User")
|
expect(metaData.className).toEqual("User")
|
||||||
expect(metaData.path).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({
|
expect(metaData.filters).toEqual({
|
||||||
softDeletable: {
|
softDeletable: {
|
||||||
name: "softDeletable",
|
name: "softDeletable",
|
||||||
@@ -547,6 +553,12 @@ describe("Entity builder", () => {
|
|||||||
|
|
||||||
const metaData = MetadataStorage.getMetadataFromDecorator(User)
|
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.className).toEqual("User")
|
||||||
expect(metaData.path).toEqual("User")
|
expect(metaData.path).toEqual("User")
|
||||||
|
|
||||||
|
|||||||
@@ -158,6 +158,18 @@ export function createMikrORMEntity() {
|
|||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
field: PropertyMetadata
|
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]) {
|
if (SPECIAL_PROPERTIES[field.fieldName]) {
|
||||||
SPECIAL_PROPERTIES[field.fieldName](MikroORMEntity, field)
|
SPECIAL_PROPERTIES[field.fieldName](MikroORMEntity, field)
|
||||||
return
|
return
|
||||||
@@ -188,12 +200,12 @@ export function createMikrORMEntity() {
|
|||||||
? PrimaryKey({
|
? PrimaryKey({
|
||||||
columnType: "text",
|
columnType: "text",
|
||||||
type: "string",
|
type: "string",
|
||||||
nullable: field.nullable,
|
nullable: false,
|
||||||
})
|
})
|
||||||
: Property({
|
: Property({
|
||||||
columnType: "text",
|
columnType: "text",
|
||||||
type: "string",
|
type: "string",
|
||||||
nullable: field.nullable,
|
nullable: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
IdDecorator(MikroORMEntity.prototype, field.fieldName)
|
IdDecorator(MikroORMEntity.prototype, field.fieldName)
|
||||||
@@ -584,6 +596,7 @@ export function createMikrORMEntity() {
|
|||||||
*/
|
*/
|
||||||
return function createEntity<T extends DmlEntity<any>>(entity: T): Infer<T> {
|
return function createEntity<T extends DmlEntity<any>>(entity: T): Infer<T> {
|
||||||
class MikroORMEntity {}
|
class MikroORMEntity {}
|
||||||
|
|
||||||
const { name, schema, cascades } = entity.parse()
|
const { name, schema, cascades } = entity.parse()
|
||||||
const { modelName, tableName } = parseEntityName(name)
|
const { modelName, tableName } = parseEntityName(name)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user