feat(utils,currency): Migrate currency to use DML (#7807)
This commit is contained in:
@@ -1166,6 +1166,109 @@ describe("Entity builder", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("Entity builder | primaryKey", () => {
|
||||
test("should create both id fields and primaryKey fields", () => {
|
||||
const model = new EntityBuilder()
|
||||
const user = model.define("user", {
|
||||
id: model.id({ primaryKey: false }),
|
||||
email: model.text().primaryKey(),
|
||||
account_id: model.number().primaryKey(),
|
||||
})
|
||||
|
||||
const entityBuilder = createMikrORMEntity()
|
||||
const User = entityBuilder(user)
|
||||
|
||||
expectTypeOf(new User()).toMatchTypeOf<{
|
||||
id: string
|
||||
email: string
|
||||
account_id: number
|
||||
}>()
|
||||
|
||||
const metaData = MetadataStorage.getMetadataFromDecorator(User)
|
||||
const userInstance = new User()
|
||||
userInstance["generateId"]()
|
||||
|
||||
expect(metaData.className).toEqual("User")
|
||||
expect(metaData.path).toEqual("User")
|
||||
|
||||
expect(metaData.hooks).toEqual({
|
||||
beforeCreate: ["generateId"],
|
||||
onInit: ["generateId"],
|
||||
})
|
||||
|
||||
expect(metaData.filters).toEqual({
|
||||
softDeletable: {
|
||||
name: "softDeletable",
|
||||
cond: expect.any(Function),
|
||||
default: true,
|
||||
args: false,
|
||||
},
|
||||
})
|
||||
|
||||
expect(metaData.properties).toEqual({
|
||||
id: {
|
||||
reference: "scalar",
|
||||
type: "string",
|
||||
columnType: "text",
|
||||
name: "id",
|
||||
nullable: false,
|
||||
getter: false,
|
||||
setter: false,
|
||||
},
|
||||
email: {
|
||||
columnType: "text",
|
||||
name: "email",
|
||||
nullable: false,
|
||||
primary: true,
|
||||
reference: "scalar",
|
||||
type: "string",
|
||||
},
|
||||
account_id: {
|
||||
columnType: "integer",
|
||||
name: "account_id",
|
||||
nullable: false,
|
||||
primary: true,
|
||||
reference: "scalar",
|
||||
type: "number",
|
||||
},
|
||||
created_at: {
|
||||
reference: "scalar",
|
||||
type: "date",
|
||||
columnType: "timestamptz",
|
||||
name: "created_at",
|
||||
defaultRaw: "now()",
|
||||
onCreate: expect.any(Function),
|
||||
nullable: false,
|
||||
getter: false,
|
||||
setter: false,
|
||||
},
|
||||
updated_at: {
|
||||
reference: "scalar",
|
||||
type: "date",
|
||||
columnType: "timestamptz",
|
||||
name: "updated_at",
|
||||
defaultRaw: "now()",
|
||||
onCreate: expect.any(Function),
|
||||
onUpdate: expect.any(Function),
|
||||
nullable: false,
|
||||
getter: false,
|
||||
setter: false,
|
||||
},
|
||||
deleted_at: {
|
||||
reference: "scalar",
|
||||
type: "date",
|
||||
columnType: "timestamptz",
|
||||
name: "deleted_at",
|
||||
nullable: true,
|
||||
getter: false,
|
||||
setter: false,
|
||||
},
|
||||
})
|
||||
|
||||
expect(userInstance.id).toBeDefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe("Entity builder | indexes", () => {
|
||||
test("define index on a field", () => {
|
||||
const model = new EntityBuilder()
|
||||
|
||||
@@ -10,6 +10,9 @@ describe("Number property", () => {
|
||||
fieldName: "age",
|
||||
dataType: {
|
||||
name: "number",
|
||||
options: {
|
||||
primaryKey: false,
|
||||
},
|
||||
},
|
||||
nullable: false,
|
||||
indexes: [],
|
||||
|
||||
@@ -10,6 +10,7 @@ describe("Text property", () => {
|
||||
fieldName: "username",
|
||||
dataType: {
|
||||
name: "text",
|
||||
options: { primaryKey: false },
|
||||
},
|
||||
nullable: false,
|
||||
indexes: [],
|
||||
|
||||
@@ -240,7 +240,10 @@ export function createMikrORMEntity() {
|
||||
* Hook to generate entity within the code
|
||||
*/
|
||||
MikroORMEntity.prototype.generateId = function () {
|
||||
this.id = generateEntityId(this.id, field.dataType.options?.prefix)
|
||||
this[field.fieldName] = generateEntityId(
|
||||
this[field.fieldName],
|
||||
field.dataType.options?.prefix
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -248,6 +251,7 @@ export function createMikrORMEntity() {
|
||||
*/
|
||||
BeforeCreate()(MikroORMEntity.prototype, "generateId")
|
||||
OnInit()(MikroORMEntity.prototype, "generateId")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -257,6 +261,19 @@ export function createMikrORMEntity() {
|
||||
const columnType = COLUMN_TYPES[field.dataType.name]
|
||||
const propertyType = PROPERTY_TYPES[field.dataType.name]
|
||||
|
||||
/**
|
||||
* Defining a primary key property
|
||||
*/
|
||||
if (field.dataType.options?.primaryKey) {
|
||||
PrimaryKey({
|
||||
columnType,
|
||||
type: propertyType,
|
||||
nullable: false,
|
||||
})(MikroORMEntity.prototype, field.fieldName)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Property({
|
||||
columnType,
|
||||
type: propertyType,
|
||||
|
||||
@@ -15,6 +15,9 @@ export class IdProperty extends BaseProperty<string> {
|
||||
|
||||
constructor(options?: { primaryKey?: boolean; prefix?: string }) {
|
||||
super()
|
||||
this.dataType = { name: "id", options: { primaryKey: true, ...options } }
|
||||
this.dataType = {
|
||||
name: "id",
|
||||
options: { primaryKey: true, ...options },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,25 @@ import { BaseProperty } from "./base"
|
||||
* property
|
||||
*/
|
||||
export class NumberProperty extends BaseProperty<number> {
|
||||
protected dataType = {
|
||||
name: "number",
|
||||
} as const
|
||||
protected dataType: {
|
||||
name: "number"
|
||||
options: {
|
||||
primaryKey: boolean
|
||||
}
|
||||
}
|
||||
|
||||
primaryKey() {
|
||||
this.dataType.options.primaryKey = true
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
constructor(options?: { primaryKey?: boolean }) {
|
||||
super()
|
||||
|
||||
this.dataType = {
|
||||
name: "number",
|
||||
options: { primaryKey: false, ...options },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,25 @@ import { BaseProperty } from "./base"
|
||||
* The TextProperty is used to define a textual property
|
||||
*/
|
||||
export class TextProperty extends BaseProperty<string> {
|
||||
protected dataType = {
|
||||
name: "text",
|
||||
} as const
|
||||
protected dataType: {
|
||||
name: "text"
|
||||
options: {
|
||||
primaryKey: boolean
|
||||
}
|
||||
}
|
||||
|
||||
primaryKey() {
|
||||
this.dataType.options.primaryKey = true
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
constructor(options?: { primaryKey?: boolean }) {
|
||||
super()
|
||||
|
||||
this.dataType = {
|
||||
name: "text",
|
||||
options: { primaryKey: false, ...options },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user