chore: Update defineMikroOrmCliConfig api (#7940)
* chore: Update defineMikroOrmCliConfig api * update usage
This commit is contained in:
1 parent
56394fe0d4
commit
cb36655b63
4 files changed
+42
-37
No files matched your search
+20
-15
@@ -1,28 +1,18 @@
|
|||||||
import { defineMikroOrmCliConfig } from "../mikro-orm-cli-config-builder"
|
import { defineMikroOrmCliConfig } from "../mikro-orm-cli-config-builder"
|
||||||
|
|
||||||
|
const moduleName = "myTestService"
|
||||||
|
|
||||||
describe("defineMikroOrmCliConfig", () => {
|
describe("defineMikroOrmCliConfig", () => {
|
||||||
test(`should throw an error if entities is not provided`, () => {
|
test(`should throw an error if entities is not provided`, () => {
|
||||||
const options = {
|
const options = {}
|
||||||
databaseName: "medusa-fulfillment",
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(() => defineMikroOrmCliConfig(options as any)).toThrow(
|
expect(() => defineMikroOrmCliConfig(moduleName, options as any)).toThrow(
|
||||||
"defineMikroOrmCliConfig failed with: entities is required"
|
"defineMikroOrmCliConfig failed with: entities is required"
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("should throw an error if databaseName is not provided", () => {
|
|
||||||
const options = {
|
|
||||||
entities: [{}],
|
|
||||||
}
|
|
||||||
|
|
||||||
expect(() => defineMikroOrmCliConfig(options as any)).toThrow(
|
|
||||||
"defineMikroOrmCliConfig failed with: databaseName is required"
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test("should return the correct config", () => {
|
test("should return the correct config", () => {
|
||||||
const config = defineMikroOrmCliConfig({
|
const config = defineMikroOrmCliConfig(moduleName, {
|
||||||
entities: [{} as any],
|
entities: [{} as any],
|
||||||
databaseName: "medusa-fulfillment",
|
databaseName: "medusa-fulfillment",
|
||||||
})
|
})
|
||||||
@@ -36,4 +26,19 @@ describe("defineMikroOrmCliConfig", () => {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test("should return the correct config inferring the databaseName", () => {
|
||||||
|
const config = defineMikroOrmCliConfig(moduleName, {
|
||||||
|
entities: [{} as any],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(config).toEqual({
|
||||||
|
entities: [{}],
|
||||||
|
clientUrl: "postgres://postgres@localhost/medusa-my-test",
|
||||||
|
type: "postgresql",
|
||||||
|
migrations: {
|
||||||
|
generator: expect.any(Function),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { MikroORMOptions } from "@mikro-orm/core/utils/Configuration"
|
import { MikroORMOptions } from "@mikro-orm/core/utils/Configuration"
|
||||||
import { DmlEntity, toMikroORMEntity } from "../dml"
|
import { DmlEntity, toMikroOrmEntities } from "../dml"
|
||||||
import { TSMigrationGenerator } from "../dal"
|
import { TSMigrationGenerator } from "../dal"
|
||||||
import type {
|
import type {
|
||||||
AnyEntity,
|
AnyEntity,
|
||||||
@@ -7,6 +7,7 @@ import type {
|
|||||||
EntityClassGroup,
|
EntityClassGroup,
|
||||||
} from "@mikro-orm/core/typings"
|
} from "@mikro-orm/core/typings"
|
||||||
import type { EntitySchema } from "@mikro-orm/core/metadata/EntitySchema"
|
import type { EntitySchema } from "@mikro-orm/core/metadata/EntitySchema"
|
||||||
|
import { kebabCase } from "../common"
|
||||||
|
|
||||||
type Options = Partial<Omit<MikroORMOptions, "entities" | "entitiesTs">> & {
|
type Options = Partial<Omit<MikroORMOptions, "entities" | "entitiesTs">> & {
|
||||||
entities: (
|
entities: (
|
||||||
@@ -16,7 +17,7 @@ type Options = Partial<Omit<MikroORMOptions, "entities" | "entitiesTs">> & {
|
|||||||
| EntitySchema
|
| EntitySchema
|
||||||
| DmlEntity<any, any>
|
| DmlEntity<any, any>
|
||||||
)[]
|
)[]
|
||||||
databaseName: string
|
databaseName?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReturnedOptions = Partial<MikroORMOptions> & {
|
type ReturnedOptions = Partial<MikroORMOptions> & {
|
||||||
@@ -30,28 +31,30 @@ type ReturnedOptions = Partial<MikroORMOptions> & {
|
|||||||
* Defines a MikroORM CLI config based on the provided options.
|
* Defines a MikroORM CLI config based on the provided options.
|
||||||
* Convert any DML entities to MikroORM entities to be consumed
|
* Convert any DML entities to MikroORM entities to be consumed
|
||||||
* by mikro orm cli.
|
* by mikro orm cli.
|
||||||
|
*
|
||||||
|
* @param moduleName
|
||||||
* @param options
|
* @param options
|
||||||
*/
|
*/
|
||||||
export function defineMikroOrmCliConfig(options: Options): ReturnedOptions {
|
export function defineMikroOrmCliConfig(
|
||||||
|
moduleName: string,
|
||||||
|
options: Options
|
||||||
|
): ReturnedOptions {
|
||||||
if (!options.entities?.length) {
|
if (!options.entities?.length) {
|
||||||
throw new Error("defineMikroOrmCliConfig failed with: entities is required")
|
throw new Error("defineMikroOrmCliConfig failed with: entities is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
const entities = options.entities.map((entity) => {
|
const dmlEntities = options.entities.filter(DmlEntity.isDmlEntity)
|
||||||
if (DmlEntity.isDmlEntity(entity)) {
|
const nonDmlEntities = options.entities.filter(
|
||||||
return toMikroORMEntity(entity)
|
(entity) => !DmlEntity.isDmlEntity(entity)
|
||||||
}
|
)
|
||||||
|
|
||||||
return entity
|
const entities = nonDmlEntities.concat(
|
||||||
})
|
toMikroOrmEntities(dmlEntities)
|
||||||
|
) as MikroORMOptions["entities"]
|
||||||
|
|
||||||
if (!options.databaseName) {
|
const normalizedModuleName = kebabCase(moduleName.replace("Service", ""))
|
||||||
throw new Error(
|
let databaseName = `medusa-${normalizedModuleName}`
|
||||||
"defineMikroOrmCliConfig failed with: databaseName is required"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let databaseName
|
|
||||||
if (options.databaseName) {
|
if (options.databaseName) {
|
||||||
databaseName = options.databaseName
|
databaseName = options.databaseName
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import * as entities from "./src/models"
|
import * as entities from "./src/models"
|
||||||
import { defineMikroOrmCliConfig } from "@medusajs/utils"
|
import { defineMikroOrmCliConfig, Modules } from "@medusajs/utils"
|
||||||
|
|
||||||
module.exports = defineMikroOrmCliConfig({
|
module.exports = defineMikroOrmCliConfig(Modules.FULFILLMENT, {
|
||||||
entities: Object.values(entities),
|
entities: Object.values(entities),
|
||||||
databaseName: "medusa-fulfillment",
|
|
||||||
})
|
})
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
import * as entities from "./src/models"
|
import * as entities from "./src/models"
|
||||||
import { defineMikroOrmCliConfig } from "@medusajs/utils"
|
import { defineMikroOrmCliConfig, Modules } from "@medusajs/utils"
|
||||||
|
|
||||||
module.exports = defineMikroOrmCliConfig({
|
module.exports = defineMikroOrmCliConfig(Modules.NOTIFICATION, {
|
||||||
entities: Object.values(entities),
|
entities: Object.values(entities),
|
||||||
schema: "public",
|
|
||||||
databaseName: "medusa-notification",
|
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user