feat(pricing, types, utils, medusa-sdk): Pricing Module Setup + Currency (#4860)
What: - Setups the skeleton for pricing module - Creates service/model/repository for currency model - Setups types - Setups DB - Moved some utils to a common place RESOLVES CORE-1477 RESOLVES CORE-1476
This commit is contained in:
1 parent
470379e631
commit
460161a69f
70 files changed
+3022
-75
No files matched your search
@@ -0,0 +1,111 @@
|
||||
import { TSMigrationGenerator } from "@mikro-orm/migrations"
|
||||
import { MikroORM, Options, SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import * as process from "process"
|
||||
|
||||
export function getDatabaseURL(): string {
|
||||
const DB_HOST = process.env.DB_HOST ?? "localhost"
|
||||
const DB_USERNAME = process.env.DB_USERNAME ?? ""
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD
|
||||
const DB_NAME = process.env.DB_TEMP_NAME
|
||||
|
||||
return `postgres://${DB_USERNAME}${
|
||||
DB_PASSWORD ? `:${DB_PASSWORD}` : ""
|
||||
}@${DB_HOST}/${DB_NAME}`
|
||||
}
|
||||
|
||||
export function getMikroOrmConfig(
|
||||
mikroOrmEntities: any[],
|
||||
pathToMigrations: string
|
||||
): Options {
|
||||
const DB_URL = getDatabaseURL()
|
||||
|
||||
return {
|
||||
type: "postgresql",
|
||||
clientUrl: DB_URL,
|
||||
entities: Object.values(mikroOrmEntities),
|
||||
schema: process.env.MEDUSA_DB_SCHEMA,
|
||||
debug: false,
|
||||
migrations: {
|
||||
path: pathToMigrations,
|
||||
pathTs: pathToMigrations,
|
||||
glob: "!(*.d).{js,ts}",
|
||||
silent: true,
|
||||
dropTables: true,
|
||||
transactional: true,
|
||||
allOrNothing: true,
|
||||
safe: false,
|
||||
generator: TSMigrationGenerator,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export interface TestDatabase {
|
||||
orm: MikroORM | null
|
||||
manager: SqlEntityManager | null
|
||||
|
||||
setupDatabase(): Promise<void>
|
||||
clearDatabase(): Promise<void>
|
||||
getManager(): SqlEntityManager
|
||||
forkManager(): SqlEntityManager
|
||||
getOrm(): MikroORM
|
||||
}
|
||||
|
||||
export function getMikroOrmWrapper(
|
||||
mikroOrmEntities: any[],
|
||||
pathToMigrations: string
|
||||
): TestDatabase {
|
||||
return {
|
||||
orm: null,
|
||||
manager: null,
|
||||
|
||||
getManager() {
|
||||
if (this.manager === null) {
|
||||
throw new Error("manager entity not available")
|
||||
}
|
||||
|
||||
return this.manager
|
||||
},
|
||||
|
||||
forkManager() {
|
||||
if (this.manager === null) {
|
||||
throw new Error("manager entity not available")
|
||||
}
|
||||
|
||||
return this.manager.fork()
|
||||
},
|
||||
|
||||
getOrm() {
|
||||
if (this.orm === null) {
|
||||
throw new Error("orm entity not available")
|
||||
}
|
||||
|
||||
return this.orm
|
||||
},
|
||||
|
||||
async setupDatabase() {
|
||||
const OrmConfig = getMikroOrmConfig(mikroOrmEntities, pathToMigrations)
|
||||
|
||||
// Initializing the ORM
|
||||
this.orm = await MikroORM.init(OrmConfig)
|
||||
|
||||
if (this.orm === null) {
|
||||
throw new Error("ORM not configured")
|
||||
}
|
||||
|
||||
this.manager = await this.orm.em
|
||||
|
||||
await this.orm.schema.refreshDatabase() // ensure db exists and is fresh
|
||||
},
|
||||
|
||||
async clearDatabase() {
|
||||
if (this.orm === null) {
|
||||
throw new Error("ORM not configured")
|
||||
}
|
||||
|
||||
await this.orm.close()
|
||||
|
||||
this.orm = null
|
||||
this.manager = null
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
import randomize from "randomatic";
|
||||
import randomize from "randomatic"
|
||||
|
||||
class IdMap {
|
||||
ids = {};
|
||||
ids = {}
|
||||
|
||||
getId(key, prefix = "", length = 10) {
|
||||
if (this.ids[key]) {
|
||||
return this.ids[key];
|
||||
return this.ids[key]
|
||||
}
|
||||
|
||||
const id = `${prefix && prefix + "_"}${randomize("Aa0", length)}`;
|
||||
this.ids[key] = id;
|
||||
const id = `${prefix && prefix + "_"}${randomize("Aa0", length)}`
|
||||
this.ids[key] = id
|
||||
|
||||
return id;
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
const instance = new IdMap();
|
||||
export default instance;
|
||||
const instance = new IdMap()
|
||||
export default instance
|
||||
@@ -1,3 +0,0 @@
|
||||
export { default as IdMap } from "./id-map";
|
||||
export { default as MockRepository } from "./mock-repository";
|
||||
export { default as MockManager } from "./mock-manager";
|
||||
@@ -0,0 +1,5 @@
|
||||
export * as TestDatabaseUtils from "./database"
|
||||
export { default as IdMap } from "./id-map"
|
||||
export * as JestUtils from "./jest"
|
||||
export { default as MockManager } from "./mock-manager"
|
||||
export { default as MockRepository } from "./mock-repository"
|
||||
@@ -0,0 +1,24 @@
|
||||
import { dropDatabase } from "pg-god"
|
||||
|
||||
export function afterAllHookDropDatabase() {
|
||||
const DB_HOST = process.env.DB_HOST ?? "localhost"
|
||||
const DB_USERNAME = process.env.DB_USERNAME ?? "postgres"
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD ?? ""
|
||||
const DB_NAME = process.env.DB_TEMP_NAME || ""
|
||||
|
||||
const pgGodCredentials = {
|
||||
user: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
host: DB_HOST,
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
try {
|
||||
await dropDatabase({ databaseName: DB_NAME }, pgGodCredentials)
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`This might fail if it is run during the unit tests since there is no database to drop. Otherwise, please check what is the issue. ${e.message}`
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user