Feat/define mikro orm configuration dev (#7798)
* chore: define mikro orm config for CLI * add tests * fix types * fix import --------- Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
committed by
GitHub
parent
8ac74c1357
commit
8fa43a6db3
@@ -0,0 +1,39 @@
|
||||
import { defineMikroOrmCliConfig } from "../mikro-orm-cli-config-builder"
|
||||
|
||||
describe("defineMikroOrmCliConfig", () => {
|
||||
test(`should throw an error if entities is not provided`, () => {
|
||||
const options = {
|
||||
databaseName: "medusa-fulfillment",
|
||||
}
|
||||
|
||||
expect(() => defineMikroOrmCliConfig(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({
|
||||
entities: [{} as any],
|
||||
databaseName: "medusa-fulfillment",
|
||||
})
|
||||
|
||||
expect(config).toEqual({
|
||||
entities: [{}],
|
||||
clientUrl: "postgres://postgres@localhost/medusa-fulfillment",
|
||||
type: "postgresql",
|
||||
migrations: {
|
||||
generator: expect.any(Function),
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -4,6 +4,7 @@ import { EntitySchema } from "@mikro-orm/core"
|
||||
import { upperCaseFirst } from "../../common"
|
||||
import { loadDatabaseConfig } from "../load-module-database-config"
|
||||
import { mikroOrmCreateConnection } from "../../dal"
|
||||
import { DmlEntity, toMikroORMEntity } from "../../dml"
|
||||
|
||||
/**
|
||||
* Utility function to build a migration script that will revert the migrations.
|
||||
@@ -34,7 +35,13 @@ export function buildRevertMigrationScript({
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = loadDatabaseConfig(moduleName, options)!
|
||||
const entities = Object.values(models) as unknown as EntitySchema[]
|
||||
const entities = Object.values(models).map((model) => {
|
||||
if (DmlEntity.isDmlEntity(model)) {
|
||||
return toMikroORMEntity(model)
|
||||
}
|
||||
|
||||
return model
|
||||
}) as unknown as EntitySchema[]
|
||||
|
||||
const orm = await mikroOrmCreateConnection(
|
||||
dbData,
|
||||
|
||||
@@ -3,6 +3,7 @@ import { EntitySchema } from "@mikro-orm/core"
|
||||
import { upperCaseFirst } from "../../common"
|
||||
import { loadDatabaseConfig } from "../load-module-database-config"
|
||||
import { mikroOrmCreateConnection } from "../../dal"
|
||||
import { DmlEntity, toMikroORMEntity } from "../../dml"
|
||||
|
||||
/**
|
||||
* Utility function to build a migration script that will run the migrations.
|
||||
@@ -29,7 +30,13 @@ export function buildMigrationScript({ moduleName, models, pathToMigrations }) {
|
||||
logger ??= console as unknown as Logger
|
||||
|
||||
const dbData = loadDatabaseConfig(moduleName, options)!
|
||||
const entities = Object.values(models) as unknown as EntitySchema[]
|
||||
const entities = Object.values(models).map((model) => {
|
||||
if (DmlEntity.isDmlEntity(model)) {
|
||||
return toMikroORMEntity(model)
|
||||
}
|
||||
|
||||
return model
|
||||
}) as unknown as EntitySchema[]
|
||||
|
||||
const orm = await mikroOrmCreateConnection(
|
||||
dbData,
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import { MikroORMOptions } from "@mikro-orm/core/utils/Configuration"
|
||||
import { IDmlEntity } from "@medusajs/types"
|
||||
import { DmlEntity, toMikroORMEntity } from "../dml"
|
||||
import { TSMigrationGenerator } from "../dal"
|
||||
|
||||
type Options = Partial<MikroORMOptions> & {
|
||||
entities: (MikroORMOptions["entities"] | IDmlEntity<any>)[]
|
||||
databaseName: string
|
||||
}
|
||||
|
||||
type ReturnedOptions = Partial<MikroORMOptions> & {
|
||||
entities: MikroORMOptions["entities"]
|
||||
clientUrl: string
|
||||
type: MikroORMOptions["type"]
|
||||
migrations: MikroORMOptions["migrations"]
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 options
|
||||
*/
|
||||
export function defineMikroOrmCliConfig(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)
|
||||
}
|
||||
|
||||
return entity
|
||||
})
|
||||
|
||||
if (!options.databaseName) {
|
||||
throw new Error(
|
||||
"defineMikroOrmCliConfig failed with: databaseName is required"
|
||||
)
|
||||
}
|
||||
|
||||
let databaseName
|
||||
if (options.databaseName) {
|
||||
databaseName = options.databaseName
|
||||
// @ts-ignore
|
||||
delete options.databaseName
|
||||
}
|
||||
|
||||
return {
|
||||
clientUrl: `postgres://postgres@localhost/${databaseName}`,
|
||||
type: "postgresql",
|
||||
...options,
|
||||
entities,
|
||||
migrations: {
|
||||
generator: TSMigrationGenerator,
|
||||
...options.migrations,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user