feat(fulfillment): implementation part 2 (#6408)

**What**

> [!NOTE]  
> I can see this pr becoming huge, so I d like to get this partial one merged 👍 


- Fixes shared connection usage (mikro orm compare the instance to its own package and therefore was resulting in not trully reusing the provided connection leading to exhausting the connection pool as multiple connections was created and end up not being all destroyed properly under the hood, discovered in my integration tests)
- Create shipping options method implementation
- DTO's definition and service interface update
- integration tests 
- Re work of the indexes with new util update
- Test runner utils to remove a big chunk of the boilerplate of the packages integrations

FIXES CORE-1742
This commit is contained in:
Adrien de Peretti
2024-02-19 13:33:46 +01:00
committed by GitHub
parent 680dfcdad3
commit 1d91b7429b
59 changed files with 2213 additions and 1741 deletions

View File

@@ -1,24 +1,28 @@
import { MikroORM, Options, SqlEntityManager } from "@mikro-orm/postgresql"
import * as process from "process"
import { Migrator } from "@mikro-orm/migrations"
export function getDatabaseURL(): string {
export function getDatabaseURL(dbName?: string): 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
const DB_NAME = dbName ?? process.env.DB_TEMP_NAME
return `postgres://${DB_USERNAME}${
DB_PASSWORD ? `:${DB_PASSWORD}` : ""
}@${DB_HOST}/${DB_NAME}`
}
export function getMikroOrmConfig(
mikroOrmEntities: any[],
pathToMigrations: string, // deprecated, auto inferred
export function getMikroOrmConfig({
mikroOrmEntities,
pathToMigrations,
clientUrl,
schema,
}: {
mikroOrmEntities: any[]
pathToMigrations?: string
clientUrl?: string
schema?: string
): Options {
const DB_URL = getDatabaseURL()
}): Options {
const DB_URL = clientUrl ?? getDatabaseURL()
return {
type: "postgresql",
@@ -26,14 +30,18 @@ export function getMikroOrmConfig(
entities: Object.values(mikroOrmEntities),
schema: schema ?? process.env.MEDUSA_DB_SCHEMA,
debug: false,
extensions: [Migrator],
migrations: {
pathTs: pathToMigrations,
silent: true,
},
}
}
export interface TestDatabase {
mikroOrmEntities: any[]
pathToMigrations: any // deprecated, auto inferred
pathToMigrations?: string
schema?: string
clientUrl?: string
orm: MikroORM | null
manager: SqlEntityManager | null
@@ -45,14 +53,21 @@ export interface TestDatabase {
getOrm(): MikroORM
}
export function getMikroOrmWrapper(
mikroOrmEntities: any[],
pathToMigrations: string, // deprecated, auto inferred
export function getMikroOrmWrapper({
mikroOrmEntities,
pathToMigrations,
clientUrl,
schema,
}: {
mikroOrmEntities: any[]
pathToMigrations?: string
clientUrl?: string
schema?: string
): TestDatabase {
}): TestDatabase {
return {
mikroOrmEntities,
pathToMigrations, // deprecated, auto inferred
pathToMigrations,
clientUrl: clientUrl ?? getDatabaseURL(),
schema: schema ?? process.env.MEDUSA_DB_SCHEMA,
orm: null,
@@ -83,11 +98,12 @@ export function getMikroOrmWrapper(
},
async setupDatabase() {
const OrmConfig = getMikroOrmConfig(
this.mikroOrmEntities,
this.pathToMigrations,
this.schema
)
const OrmConfig = getMikroOrmConfig({
mikroOrmEntities: this.mikroOrmEntities,
pathToMigrations: this.pathToMigrations,
clientUrl: this.clientUrl,
schema: this.schema,
})
// Initializing the ORM
this.orm = await MikroORM.init(OrmConfig)
@@ -96,7 +112,9 @@ export function getMikroOrmWrapper(
try {
await this.orm.getSchemaGenerator().ensureDatabase()
} catch (err) {}
} catch (err) {
console.log(err)
}
await this.manager?.execute(
`CREATE SCHEMA IF NOT EXISTS "${this.schema ?? "public"}";`