@orion-js/models
Advanced tools
Comparing version 3.0.27 to 3.0.28
@@ -10,3 +10,3 @@ "use strict"; | ||
const clone_1 = __importDefault(require("./clone")); | ||
const modelToSchema_1 = __importDefault(require("./modelToSchema")); | ||
const modelToSchema_1 = require("./modelToSchema"); | ||
const createModel = modelOptions => { | ||
@@ -23,3 +23,3 @@ const name = modelOptions.name; | ||
const schema = (0, resolveParam_1.default)(modelOptions.schema); | ||
resolvedSchema = (0, modelToSchema_1.default)(schema, model); | ||
resolvedSchema = (0, modelToSchema_1.modelToSchemaWithModel)(schema, model); | ||
return resolvedSchema; | ||
@@ -33,3 +33,3 @@ }; | ||
const schema = (0, resolveParam_1.default)(modelOptions.schema); | ||
resolvedCleanSchema = (0, modelToSchema_1.default)(schema); | ||
resolvedCleanSchema = (0, modelToSchema_1.modelToSchema)(schema); | ||
return resolvedCleanSchema; | ||
@@ -36,0 +36,0 @@ }; |
@@ -1,2 +0,9 @@ | ||
import { Model } from '..'; | ||
export default function (schema: any, model?: Model): any; | ||
import { Model, ModelSchema } from '..'; | ||
import { Schema } from '@orion-js/schema'; | ||
export declare function modelToSchema(modelSchema: ModelSchema, { cleanSchema }?: { | ||
cleanSchema?: boolean; | ||
}): Schema; | ||
export declare function modelToSchemaWithModel(modelSchema: ModelSchema, model?: Model): Schema | { | ||
__model: Model; | ||
__clean: (item: any) => Promise<any>; | ||
}; |
@@ -6,17 +6,39 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.modelToSchemaWithModel = exports.modelToSchema = void 0; | ||
const isArray_1 = __importDefault(require("lodash/isArray")); | ||
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep")); | ||
function default_1(schema, model) { | ||
schema = (0, cloneDeep_1.default)(schema); | ||
const keys = Object.keys(schema); | ||
for (const key of keys) { | ||
if ((0, isArray_1.default)(schema[key].type)) { | ||
if (schema[key].type[0].__isModel) { | ||
schema[key].type[0] = schema[key].type[0].getSchema(); | ||
} | ||
function isModelSchema(type) { | ||
return type && typeof type === 'object' && '__isModel' in type; | ||
} | ||
function isModelArraySchema(type) { | ||
return type && (0, isArray_1.default)(type) && typeof type[0] === 'object' && '__isModel' in type[0]; | ||
} | ||
function modelToSchema(modelSchema, { cleanSchema = true } = {}) { | ||
const compiledSchema = {}; | ||
for (const key in modelSchema) { | ||
const fieldSchema = modelSchema[key]; | ||
let currNode; | ||
if (isModelSchema(fieldSchema.type)) { | ||
currNode = { | ||
...fieldSchema, | ||
type: cleanSchema ? fieldSchema.type.getCleanSchema() : fieldSchema.type.getSchema() | ||
}; | ||
} | ||
if (schema[key].type && schema[key].type.__isModel) { | ||
schema[key].type = schema[key].type.getSchema(); | ||
else if (isModelArraySchema(fieldSchema.type)) { | ||
currNode = { | ||
...fieldSchema, | ||
type: cleanSchema | ||
? [fieldSchema.type[0].getCleanSchema()] | ||
: [fieldSchema.type[0].getSchema()] | ||
}; | ||
} | ||
else { | ||
currNode = { ...fieldSchema, type: fieldSchema.type }; | ||
} | ||
compiledSchema[key] = currNode; | ||
} | ||
return compiledSchema; | ||
} | ||
exports.modelToSchema = modelToSchema; | ||
function modelToSchemaWithModel(modelSchema, model) { | ||
const schema = modelToSchema(modelSchema, { cleanSchema: !model }); | ||
if (!model) | ||
@@ -30,2 +52,2 @@ return schema; | ||
} | ||
exports.default = default_1; | ||
exports.modelToSchemaWithModel = modelToSchemaWithModel; |
@@ -6,3 +6,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const __1 = require(".."); | ||
const _1 = __importDefault(require(".")); | ||
const schema_1 = require("@orion-js/schema"); | ||
it('should add the __model field when converting to schema', async () => { | ||
@@ -19,1 +21,31 @@ const model = (0, _1.default)({ | ||
}); | ||
it('can clean a schema with nested models', async () => { | ||
const modelSchema = { | ||
name: { | ||
type: String, | ||
clean: () => 'Model Schema' | ||
} | ||
}; | ||
const model = (0, _1.default)({ | ||
name: 'AModel', | ||
schema: modelSchema | ||
}); | ||
const schema = { | ||
subModel: { | ||
type: model | ||
}, | ||
subModelArray: { | ||
type: [model] | ||
}, | ||
primitive: { | ||
type: String, | ||
clean: () => 'Primitive' | ||
} | ||
}; | ||
const doc = { subModel: { name: 'Joaquin' }, subModelArray: [{ name: 'Roberto' }], primitive: 'hello' }; | ||
expect(await (0, schema_1.clean)((0, __1.modelToSchema)(schema), doc)).toEqual({ | ||
subModel: { name: 'Model Schema' }, | ||
subModelArray: [{ name: 'Model Schema' }], | ||
primitive: 'Primitive' | ||
}); | ||
}); |
import createModel from './createModel'; | ||
import modelToSchema from './createModel/modelToSchema'; | ||
export { createModel, modelToSchema }; | ||
import { modelToSchema, modelToSchemaWithModel } from './createModel/modelToSchema'; | ||
export { createModel, modelToSchema, modelToSchemaWithModel }; | ||
export * from './types'; |
@@ -16,7 +16,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.modelToSchema = exports.createModel = void 0; | ||
exports.modelToSchemaWithModel = exports.modelToSchema = exports.createModel = void 0; | ||
const createModel_1 = __importDefault(require("./createModel")); | ||
exports.createModel = createModel_1.default; | ||
const modelToSchema_1 = __importDefault(require("./createModel/modelToSchema")); | ||
exports.modelToSchema = modelToSchema_1.default; | ||
const modelToSchema_1 = require("./createModel/modelToSchema"); | ||
Object.defineProperty(exports, "modelToSchema", { enumerable: true, get: function () { return modelToSchema_1.modelToSchema; } }); | ||
Object.defineProperty(exports, "modelToSchemaWithModel", { enumerable: true, get: function () { return modelToSchema_1.modelToSchemaWithModel; } }); | ||
__exportStar(require("./types"), exports); |
{ | ||
"name": "@orion-js/models", | ||
"version": "3.0.27", | ||
"version": "3.0.28", | ||
"main": "lib/index.js", | ||
@@ -39,3 +39,3 @@ "types": "lib/index.d.ts", | ||
}, | ||
"gitHead": "084f56bda6733955cdc640b3c8adf1731ec95af4" | ||
"gitHead": "a1d1623ecf7eefbacb87451255c6fa778e748f6a" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24237
664