Skip to content

Commit

Permalink
Merge branch 'chore/utils-repository-base-composite-support' of githu…
Browse files Browse the repository at this point in the history
…b.com:medusajs/medusa into chore/utils-repository-base-composite-support
  • Loading branch information
adrien2p committed Jan 12, 2024
2 parents 1f6e0ae + 22836dd commit af1c912
Show file tree
Hide file tree
Showing 37 changed files with 538 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/cyan-countries-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@medusajs/authentication": patch
"@medusajs/pricing": patch
"@medusajs/utils": patch
---

chore(utils): Update base repository to infer primary keys and support composite
6 changes: 6 additions & 0 deletions .changeset/wicked-months-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/modules-sdk": patch
"@medusajs/types": patch
---

feat: add Payment module package
17 changes: 17 additions & 0 deletions packages/modules-sdk/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum Modules {
PROMOTION = "promotion",
AUTHENTICATION = "authentication",
CART = "cart",
PAYMENT = "payment",
}

export enum ModuleRegistrationName {
Expand All @@ -28,6 +29,7 @@ export enum ModuleRegistrationName {
PROMOTION = "promotionModuleService",
AUTHENTICATION = "authenticationModuleService",
CART = "cartModuleService",
PAYMENT = "paymentModuleService",
}

export const MODULE_PACKAGE_NAMES = {
Expand All @@ -40,6 +42,7 @@ export const MODULE_PACKAGE_NAMES = {
[Modules.PROMOTION]: "@medusajs/promotion",
[Modules.AUTHENTICATION]: "@medusajs/authentication",
[Modules.CART]: "@medusajs/cart",
[Modules.PAYMENT]: "@medusajs/payment",
}

export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
Expand Down Expand Up @@ -171,6 +174,20 @@ export const ModulesDefinition: { [key: string | Modules]: ModuleDefinition } =
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
[Modules.PAYMENT]: {
key: Modules.PAYMENT,
registrationName: ModuleRegistrationName.PAYMENT,
defaultPackage: false,
label: upperCaseFirst(ModuleRegistrationName.PAYMENT),
isRequired: false,
canOverride: true,
isQueryable: true,
dependencies: ["logger"],
defaultModuleDeclaration: {
scope: MODULE_SCOPE.INTERNAL,
resources: MODULE_RESOURCE_TYPE.SHARED,
},
},
}

export const MODULE_DEFINITIONS: ModuleDefinition[] =
Expand Down
6 changes: 6 additions & 0 deletions packages/payment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/dist
node_modules
.DS_store
.env*
.env
*.sql
Empty file added packages/payment/CHANGELOG.md
Empty file.
3 changes: 3 additions & 0 deletions packages/payment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Payment Module

A Payment is the result of a series of financial transactions performed as part of collecting a payment of a purchase. The Payment holds information about the payment provider as well as the state of it. You can associate a Payment with a Cart, an Order, an OrderEdit, or any other resource you’d like to have your customers pay for.
5 changes: 5 additions & 0 deletions packages/payment/integration-tests/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("Noop test", () => {
it("noop check", async () => {
expect(true).toBe(true)
})
})
6 changes: 6 additions & 0 deletions packages/payment/integration-tests/setup-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if (typeof process.env.DB_TEMP_NAME === "undefined") {
const tempName = parseInt(process.env.JEST_WORKER_ID || "1")
process.env.DB_TEMP_NAME = `medusa-payment-integration-${tempName}`
}

process.env.MEDUSA_PAYMENT_DB_SCHEMA = "public"
3 changes: 3 additions & 0 deletions packages/payment/integration-tests/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { JestUtils } from "medusa-test-utils"

JestUtils.afterAllHookDropDatabase()
6 changes: 6 additions & 0 deletions packages/payment/integration-tests/utils/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ModuleServiceInitializeOptions } from "@medusajs/types"

export const databaseOptions: ModuleServiceInitializeOptions["database"] = {
schema: "public",
clientUrl: "medusa-payment-test",
}
18 changes: 18 additions & 0 deletions packages/payment/integration-tests/utils/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TestDatabaseUtils } from "medusa-test-utils"

import * as PaymentModules from "@models"

const pathToMigrations = "../../src/migrations"
const mikroOrmEntities = PaymentModules as unknown as any[]

export const MikroOrmWrapper = TestDatabaseUtils.getMikroOrmWrapper(
mikroOrmEntities,
pathToMigrations
)

export const MikroOrmConfig = TestDatabaseUtils.getMikroOrmConfig(
mikroOrmEntities,
pathToMigrations
)

export const DB_URL = TestDatabaseUtils.getDatabaseURL()
2 changes: 2 additions & 0 deletions packages/payment/integration-tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./config"
export * from "./database"
21 changes: 21 additions & 0 deletions packages/payment/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
moduleNameMapper: {
"^@models": "<rootDir>/src/models",
"^@services": "<rootDir>/src/services",
"^@repositories": "<rootDir>/src/repositories",
},
transform: {
"^.+\\.[jt]s?$": [
"ts-jest",
{
tsConfig: "tsconfig.spec.json",
isolatedModules: true,
},
],
},
testEnvironment: `node`,
moduleFileExtensions: [`js`, `ts`],
modulePathIgnorePatterns: ["dist/"],
setupFiles: ["<rootDir>/integration-tests/setup-env.js"],
setupFilesAfterEnv: ["<rootDir>/integration-tests/setup.js"],
}
8 changes: 8 additions & 0 deletions packages/payment/mikro-orm.config.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as entities from "./src/models"

module.exports = {
entities: Object.values(entities),
schema: "public",
clientUrl: "postgres://postgres@localhost/medusa-payment",
type: "postgresql",
}
61 changes: 61 additions & 0 deletions packages/payment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "@medusajs/payment",
"version": "0.0.1",
"description": "Medusa Payment module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"engines": {
"node": ">=16"
},
"bin": {
"medusa-payment-seed": "dist/scripts/bin/run-seed.js"
},
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/payment"
},
"publishConfig": {
"access": "public"
},
"author": "Medusa",
"license": "MIT",
"scripts": {
"watch": "tsc --build --watch",
"watch:test": "tsc --build tsconfig.spec.json --watch",
"prepublishOnly": "cross-env NODE_ENV=production tsc --build && tsc-alias -p tsconfig.json",
"build": "rimraf dist && tsc --build && tsc-alias -p tsconfig.json",
"test": "jest --runInBand --bail --forceExit -- src/**/__tests__/**/*.ts",
"test:integration": "jest --runInBand --forceExit -- integration-tests/**/__tests__/**/*.ts",
"migration:generate": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:generate",
"migration:initial": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create --initial",
"migration:create": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:create",
"migration:up": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm migration:up",
"orm:cache:clear": " MIKRO_ORM_CLI=./mikro-orm.config.dev.ts mikro-orm cache:clear"
},
"devDependencies": {
"@mikro-orm/cli": "5.9.7",
"cross-env": "^5.2.1",
"jest": "^29.6.3",
"medusa-test-utils": "^1.1.40",
"rimraf": "^3.0.2",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tsc-alias": "^1.8.6",
"typescript": "^5.1.6"
},
"dependencies": {
"@medusajs/modules-sdk": "^1.12.5",
"@medusajs/types": "^1.11.9",
"@medusajs/utils": "^1.11.2",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"knex": "2.4.2"
}
}
23 changes: 23 additions & 0 deletions packages/payment/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { moduleDefinition } from "./module-definition"
import { Modules } from "@medusajs/modules-sdk"
import { ModulesSdkUtils } from "@medusajs/utils"

import * as PaymentModels from "@models"

export default moduleDefinition

const migrationScriptOptions = {
moduleName: Modules.PAYMENT,
models: PaymentModels,
pathToMigrations: __dirname + "/migrations",
}

export const runMigrations = ModulesSdkUtils.buildMigrationScript(
migrationScriptOptions
)
export const revertMigration = ModulesSdkUtils.buildRevertMigrationScript(
migrationScriptOptions
)

export * from "./initialize"
export * from "./loaders"
27 changes: 27 additions & 0 deletions packages/payment/src/initialize/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
ExternalModuleDeclaration,
InternalModuleDeclaration,
MedusaModule,
MODULE_PACKAGE_NAMES,
Modules,
} from "@medusajs/modules-sdk"
import { IPaymentModuleService, ModulesSdkTypes } from "@medusajs/types"
import { moduleDefinition } from "../module-definition"
import { InitializeModuleInjectableDependencies } from "../types"

export const initialize = async (
options?: ModulesSdkTypes.ModuleBootstrapDeclaration,
injectedDependencies?: InitializeModuleInjectableDependencies
): Promise<IPaymentModuleService> => {
const loaded = await MedusaModule.bootstrap<IPaymentModuleService>({
moduleKey: Modules.PAYMENT,
defaultPath: MODULE_PACKAGE_NAMES[Modules.PAYMENT],
declaration: options as
| InternalModuleDeclaration
| ExternalModuleDeclaration,
injectedDependencies,
moduleExports: moduleDefinition,
})

return loaded[Modules.PAYMENT]
}
31 changes: 31 additions & 0 deletions packages/payment/src/joiner-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Modules } from "@medusajs/modules-sdk"
import { ModuleJoinerConfig } from "@medusajs/types"
import { MapToConfig } from "@medusajs/utils"
import { Payment } from "@models"

export const LinkableKeys = {
payment_id: Payment.name,
}

const entityLinkableKeysMap: MapToConfig = {}
Object.entries(LinkableKeys).forEach(([key, value]) => {
entityLinkableKeysMap[value] ??= []
entityLinkableKeysMap[value].push({
mapTo: key,
valueFrom: key.split("_").pop()!,
})
})

export const entityNameToLinkableKeysMap: MapToConfig = entityLinkableKeysMap

export const joinerConfig: ModuleJoinerConfig = {
serviceName: Modules.PAYMENT,
primaryKeys: ["id"],
linkableKeys: LinkableKeys,
alias: {
name: ["payment", "payments"],
args: {
entity: Payment.name,
},
},
}
34 changes: 34 additions & 0 deletions packages/payment/src/loaders/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
InternalModuleDeclaration,
LoaderOptions,
Modules,
} from "@medusajs/modules-sdk"
import { ModulesSdkTypes } from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import { EntitySchema } from "@mikro-orm/core"
import * as PaymentModels from "../models"

export default async (
{
options,
container,
logger,
}: LoaderOptions<
| ModulesSdkTypes.ModuleServiceInitializeOptions
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
>,
moduleDeclaration?: InternalModuleDeclaration
): Promise<void> => {
const entities = Object.values(PaymentModels) as unknown as EntitySchema[]
const pathToMigrations = __dirname + "/../migrations"

await ModulesSdkUtils.mikroOrmConnectionLoader({
moduleName: Modules.PAYMENT,
entities,
container,
options,
moduleDeclaration,
logger,
pathToMigrations,
})
}
39 changes: 39 additions & 0 deletions packages/payment/src/loaders/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { LoaderOptions } from "@medusajs/modules-sdk"
import { ModulesSdkTypes } from "@medusajs/types"
import { loadCustomRepositories } from "@medusajs/utils"
import { asClass } from "awilix"

import * as defaultRepositories from "@repositories"
import * as defaultServices from "@services"

export default async ({
container,
options,
}: LoaderOptions<
| ModulesSdkTypes.ModuleServiceInitializeOptions
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
>): Promise<void> => {
const customRepositories = (
options as ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
)?.repositories

container.register({
// paymentService: asClass(defaultServices.PaymentService).singleton(),
})

if (customRepositories) {
loadCustomRepositories({
defaultRepositories,
customRepositories,
container,
})
} else {
loadDefaultRepositories({ container })
}
}

function loadDefaultRepositories({ container }) {
container.register({
baseRepository: asClass(defaultRepositories.BaseRepository).singleton(),
})
}
2 changes: 2 additions & 0 deletions packages/payment/src/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./connection"
export * from "./container"
1 change: 1 addition & 0 deletions packages/payment/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Payment } from "./payment"
Loading

0 comments on commit af1c912

Please sign in to comment.