fix: creates restock functionality

This commit is contained in:
Sebastian Rindom
2021-04-06 12:22:19 +02:00
parent 0a80b098a4
commit 2b2555004e
17 changed files with 2673 additions and 259 deletions
+42 -13
View File
@@ -1,4 +1,5 @@
import glob from "glob"
import { EntitySchema } from "typeorm"
import {
BaseModel,
BaseService,
@@ -48,7 +49,8 @@ export default async ({ rootDirectory, container, app }) => {
await Promise.all(
resolved.map(async pluginDetails => {
registerModels(pluginDetails, container)
// registerModels(pluginDetails, container)
registerRepositories(pluginDetails, container)
await registerServices(pluginDetails, container)
registerMedusaApi(pluginDetails, container)
registerApi(pluginDetails, app, rootDirectory, container)
@@ -288,6 +290,34 @@ function registerSubscribers(pluginDetails, container) {
})
}
/**
* Registers a plugin's models at the right location in our container. Models
* must inherit from BaseModel. Models are registered directly in the container.
* Names are camelCase formatted and namespaced by the folder i.e:
* models/example-person -> examplePersonModel
* @param {object} pluginDetails - the plugin details including plugin options,
* version, id, resolved path, etc. See resolvePlugin
* @param {object} container - the container where the services will be
* registered
* @return {void}
*/
function registerRepositories(pluginDetails, container) {
const files = glob.sync(`${pluginDetails.resolve}/repositories/*.js`, {})
files.forEach(fn => {
const loaded = require(fn)
Object.entries(loaded).map(([key, val]) => {
if (typeof val === "function") {
const name = formatRegistrationName(fn)
console.log(name)
container.register({
[name]: asClass(val),
})
}
})
})
}
/**
* Registers a plugin's models at the right location in our container. Models
* must inherit from BaseModel. Models are registered directly in the container.
@@ -302,20 +332,19 @@ function registerSubscribers(pluginDetails, container) {
function registerModels(pluginDetails, container) {
const files = glob.sync(`${pluginDetails.resolve}/models/*.js`, {})
files.forEach(fn => {
const loaded = require(fn).default
const loaded = require(fn)
if (!(loaded.prototype instanceof BaseModel)) {
const logger = container.resolve("logger")
const message = `Models must inherit from BaseModel, please check ${fn}`
logger.error(message)
throw new Error(message)
}
Object.entries(loaded).map(([key, val]) => {
if (typeof val === "function" || val instanceof EntitySchema) {
if (config.register) {
const name = formatRegistrationName(fn)
container.register({
[name]: asClass(val),
})
const name = formatRegistrationName(fn)
container.register({
[name]: asFunction(
cradle => new loaded(cradle, pluginDetails.options)
).singleton(),
container.registerAdd("db_entities", asValue(val))
}
}
})
})
}