UNPKG

final-orm

Version:

> Please check out https://github.com/oknoah/final and https://github.com/oknoah/final/packages/arangolize for similar projects that MAY be more up to date

114 lines (88 loc) 3.48 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _model = require('../models/model'); var _model2 = _interopRequireDefault(_model); var _fieldType = require('../fields/field-type'); var _fieldType2 = _interopRequireDefault(_fieldType); var _fieldTypes = require('../fields/field-types'); var _fieldTypes2 = _interopRequireDefault(_fieldTypes); var _fieldModel = require('../fields/field-model'); var _fieldModel2 = _interopRequireDefault(_fieldModel); var _fieldModels = require('../fields/field-models'); var _fieldModels2 = _interopRequireDefault(_fieldModels); var _fieldSchemas = require('../fields/field-schemas'); var _fieldSchemas2 = _interopRequireDefault(_fieldSchemas); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } class Schema { constructor(userSchema = null, basePath = [], isRootSchema = true) { this.basePath = basePath; this.fields = this.parseUserSchema(userSchema); if (isRootSchema) { this.fields.push(new _fieldType2.default(basePath, ['_id'], String, null, true)); this.fields.push(new _fieldType2.default(basePath, ['_key'], String, null, true)); this.fields.push(new _fieldType2.default(basePath, ['_rev'], String, null, true)); this.fields.push(new _fieldType2.default(basePath, ['_removed'], Boolean, null, true)); this.fields.push(new _fieldType2.default(basePath, ['createdAt'], String, null, true)); this.fields.push(new _fieldType2.default(basePath, ['updatedAt'], String, null, true)); } } parseUserSchema(userSchema, parentPath = []) { const basePath = this.basePath; const fields = []; for (const key in userSchema) { if (userSchema.hasOwnProperty(key)) { const path = [...parentPath, key]; let value = userSchema[key]; if ('$type' in value) { var options = value; value = value.$type; } else { options = {}; } if (typeof value === 'function') { if (value.prototype instanceof _model2.default) { fields.push(new _fieldModel2.default(basePath, path, value, options)); } else { fields.push(new _fieldType2.default(basePath, path, value, options)); } } else if (Array.isArray(value)) { const firstItem = value[0]; if (typeof firstItem === 'function') { if (firstItem.prototype instanceof _model2.default) { fields.push(new _fieldModels2.default(basePath, path, firstItem, options)); } else { fields.push(new _fieldTypes2.default(basePath, path, firstItem, options)); } } else { fields.push(new _fieldSchemas2.default(basePath, path, firstItem, options)); } } else { const subFields = this.parseUserSchema(value, path); fields.push(...subFields); } } } return fields; } validate(data, basePath = []) { this.fields.forEach(field => field.validate(data, basePath)); } documentToModel(model, document) { this.fields.forEach(field => { field.documentToModel(model, document); }); return model; } modelToDocument(model, document) { this.fields.forEach(field => { field.modelToDocument(model, document); }); return document; } [Symbol.iterator]() { return this.fields.values(); } } exports.default = Schema;