feat(medusa-interfaces): Adds schema options to base model

This commit is contained in:
Oliver Windall Juhl
2020-10-20 17:07:47 +02:00
committed by GitHub
parent 883e80a038
commit cc23a3b070
2 changed files with 23 additions and 2 deletions

View File

@@ -35,13 +35,30 @@ class BaseModel {
return this.constructor.modelName
}
/**
* Returns the schema options defined in child class.
* @return {object} the schema options
*/
getSchemaOptions() {
if (!this.constructor.schemaOptions) {
return {}
}
return this.constructor.schemaOptions
}
/**
* @private
* Creates a mongoose model based on schema and model name.
* Creates a mongoose model based on schema, schema options and model name.
* @return {Mongooose.Model} the mongoose model
*/
createMongooseModel_() {
return mongoose.model(this.getModelName(), this.getSchema())
const schema = this.getSchema()
const options = this.getSchemaOptions()
const mongooseSchema = new mongoose.Schema(schema, options)
return mongoose.model(this.getModelName(), mongooseSchema)
}
/**

View File

@@ -13,6 +13,10 @@ import DiscountSchema from "./schemas/discount"
class CartModel extends BaseModel {
static modelName = "Cart"
static schemaOptions = {
minimize: false,
}
static schema = {
email: { type: String },
billing_address: { type: AddressSchema },