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:
co-authored by
Riqwan Thamir
parent
8ac74c1357
commit
8fa43a6db3
@@ -11,7 +11,6 @@ import {
|
|||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
Property,
|
Property,
|
||||||
} from "@mikro-orm/core"
|
} from "@mikro-orm/core"
|
||||||
import { DALUtils } from "../../bundles"
|
|
||||||
import {
|
import {
|
||||||
camelToSnakeCase,
|
camelToSnakeCase,
|
||||||
createPsqlIndexStatementHelper,
|
createPsqlIndexStatementHelper,
|
||||||
@@ -35,6 +34,7 @@ import type {
|
|||||||
RelationshipMetadata,
|
RelationshipMetadata,
|
||||||
RelationshipType,
|
RelationshipType,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
import { mikroOrmSoftDeletableFilterOptions } from "../../dal"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DML entity data types to PostgreSQL data types via
|
* DML entity data types to PostgreSQL data types via
|
||||||
@@ -627,7 +627,7 @@ export function createMikrORMEntity() {
|
|||||||
* Converting class to a MikroORM entity
|
* Converting class to a MikroORM entity
|
||||||
*/
|
*/
|
||||||
return Entity({ tableName })(
|
return Entity({ tableName })(
|
||||||
Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)(MikroORMEntity)
|
Filter(mikroOrmSoftDeletableFilterOptions)(MikroORMEntity)
|
||||||
) as Infer<T>
|
) as Infer<T>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 { upperCaseFirst } from "../../common"
|
||||||
import { loadDatabaseConfig } from "../load-module-database-config"
|
import { loadDatabaseConfig } from "../load-module-database-config"
|
||||||
import { mikroOrmCreateConnection } from "../../dal"
|
import { mikroOrmCreateConnection } from "../../dal"
|
||||||
|
import { DmlEntity, toMikroORMEntity } from "../../dml"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility function to build a migration script that will revert the migrations.
|
* 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
|
logger ??= console as unknown as Logger
|
||||||
|
|
||||||
const dbData = loadDatabaseConfig(moduleName, options)!
|
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(
|
const orm = await mikroOrmCreateConnection(
|
||||||
dbData,
|
dbData,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { EntitySchema } from "@mikro-orm/core"
|
|||||||
import { upperCaseFirst } from "../../common"
|
import { upperCaseFirst } from "../../common"
|
||||||
import { loadDatabaseConfig } from "../load-module-database-config"
|
import { loadDatabaseConfig } from "../load-module-database-config"
|
||||||
import { mikroOrmCreateConnection } from "../../dal"
|
import { mikroOrmCreateConnection } from "../../dal"
|
||||||
|
import { DmlEntity, toMikroORMEntity } from "../../dml"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility function to build a migration script that will run the migrations.
|
* 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
|
logger ??= console as unknown as Logger
|
||||||
|
|
||||||
const dbData = loadDatabaseConfig(moduleName, options)!
|
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(
|
const orm = await mikroOrmCreateConnection(
|
||||||
dbData,
|
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