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:
Riqwan Thamir authored and GitHub committed 2023-08-29 21:58:34 +00:00
1 parent 470379e631
commit 460161a69f
70 files changed
+3022 -75

No files matched your search

-9
View File
@@ -1,9 +0,0 @@
{
"plugins": ["@babel/plugin-proposal-class-properties"],
"presets": ["@babel/preset-env"],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}
+10 -11
View File
@@ -9,10 +9,11 @@
"directory": "packages/medusa-test-utils"
},
"scripts": {
"prepublishOnly": "cross-env NODE_ENV=production tsc --build",
"prepare": "cross-env NODE_ENV=production yarn run build",
"test": "jest --passWithNoTests src",
"build": "babel src --out-dir dist/ --ignore '**/__tests__','**/__mocks__'",
"watch": "babel -w src --out-dir dist/ --ignore '**/__tests__','**/__mocks__'"
"build": "rimraf dist && tsc --build",
"watch": "tsc --build --watch",
"test": "jest --passWithNoTests src"
},
"files": [
"dist"
@@ -20,18 +21,16 @@
"author": "Sebastian Rindom",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"@babel/runtime": "^7.9.6",
"cross-env": "^5.2.1",
"jest": "^25.5.4"
"jest": "^25.5.4",
"rimraf": "^3.0.2",
"typescript": "^5.1.6"
},
"dependencies": {
"@babel/plugin-transform-classes": "^7.9.5",
"@mikro-orm/migrations": "5.7.12",
"@mikro-orm/postgresql": "5.7.12",
"medusa-core-utils": "^1.2.0",
"pg-god": "^1.0.12",
"randomatic": "^3.1.1"
},
"gitHead": "81a7ff73d012fda722f6e9ef0bd9ba0232d37808"
+111
View File
@@ -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
},
}
}
+8 -8
View File
@@ -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
-3
View File
@@ -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";
+5
View File
@@ -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"
+24
View File
@@ -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}`
)
}
})
}
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"lib": ["es5", "es6", "es2019"],
"target": "es5",
"outDir": "./dist",
"esModuleInterop": true,
"declaration": false,
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"allowJs": true,
"skipLibCheck": true,
"downlevelIteration": true
},
"include": ["src"],
"exclude": ["dist", "node_modules"]
}