chore: Update defineMikroOrmCliConfig api (#7940)
* chore: Update defineMikroOrmCliConfig api * update usage
This commit is contained in:
committed by
GitHub
parent
56394fe0d4
commit
cb36655b63
@@ -1,28 +1,18 @@
|
||||
import { defineMikroOrmCliConfig } from "../mikro-orm-cli-config-builder"
|
||||
|
||||
const moduleName = "myTestService"
|
||||
|
||||
describe("defineMikroOrmCliConfig", () => {
|
||||
test(`should throw an error if entities is not provided`, () => {
|
||||
const options = {
|
||||
databaseName: "medusa-fulfillment",
|
||||
}
|
||||
const options = {}
|
||||
|
||||
expect(() => defineMikroOrmCliConfig(options as any)).toThrow(
|
||||
expect(() => defineMikroOrmCliConfig(moduleName, options as any)).toThrow(
|
||||
"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", () => {
|
||||
const config = defineMikroOrmCliConfig({
|
||||
const config = defineMikroOrmCliConfig(moduleName, {
|
||||
entities: [{} as any],
|
||||
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 { DmlEntity, toMikroORMEntity } from "../dml"
|
||||
import { DmlEntity, toMikroOrmEntities } from "../dml"
|
||||
import { TSMigrationGenerator } from "../dal"
|
||||
import type {
|
||||
AnyEntity,
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
EntityClassGroup,
|
||||
} from "@mikro-orm/core/typings"
|
||||
import type { EntitySchema } from "@mikro-orm/core/metadata/EntitySchema"
|
||||
import { kebabCase } from "../common"
|
||||
|
||||
type Options = Partial<Omit<MikroORMOptions, "entities" | "entitiesTs">> & {
|
||||
entities: (
|
||||
@@ -16,7 +17,7 @@ type Options = Partial<Omit<MikroORMOptions, "entities" | "entitiesTs">> & {
|
||||
| EntitySchema
|
||||
| DmlEntity<any, any>
|
||||
)[]
|
||||
databaseName: string
|
||||
databaseName?: string
|
||||
}
|
||||
|
||||
type ReturnedOptions = Partial<MikroORMOptions> & {
|
||||
@@ -30,28 +31,30 @@ type ReturnedOptions = Partial<MikroORMOptions> & {
|
||||
* Defines a MikroORM CLI config based on the provided options.
|
||||
* Convert any DML entities to MikroORM entities to be consumed
|
||||
* by mikro orm cli.
|
||||
*
|
||||
* @param moduleName
|
||||
* @param options
|
||||
*/
|
||||
export function defineMikroOrmCliConfig(options: Options): ReturnedOptions {
|
||||
export function defineMikroOrmCliConfig(
|
||||
moduleName: string,
|
||||
options: Options
|
||||
): ReturnedOptions {
|
||||
if (!options.entities?.length) {
|
||||
throw new Error("defineMikroOrmCliConfig failed with: entities is required")
|
||||
}
|
||||
|
||||
const entities = options.entities.map((entity) => {
|
||||
if (DmlEntity.isDmlEntity(entity)) {
|
||||
return toMikroORMEntity(entity)
|
||||
}
|
||||
const dmlEntities = options.entities.filter(DmlEntity.isDmlEntity)
|
||||
const nonDmlEntities = options.entities.filter(
|
||||
(entity) => !DmlEntity.isDmlEntity(entity)
|
||||
)
|
||||
|
||||
return entity
|
||||
})
|
||||
const entities = nonDmlEntities.concat(
|
||||
toMikroOrmEntities(dmlEntities)
|
||||
) as MikroORMOptions["entities"]
|
||||
|
||||
if (!options.databaseName) {
|
||||
throw new Error(
|
||||
"defineMikroOrmCliConfig failed with: databaseName is required"
|
||||
)
|
||||
}
|
||||
const normalizedModuleName = kebabCase(moduleName.replace("Service", ""))
|
||||
let databaseName = `medusa-${normalizedModuleName}`
|
||||
|
||||
let databaseName
|
||||
if (options.databaseName) {
|
||||
databaseName = options.databaseName
|
||||
// @ts-ignore
|
||||
|
||||
Reference in New Issue
Block a user