@apex-pub/convector-core-model
Advanced tools
Comparing version 1.4.1-2.0 to 1.4.1-4-alpha.6064f94c
@@ -1,13 +0,10 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ConvectorModel = void 0; | ||
const tslib_1 = require("tslib"); | ||
const yup = require("yup"); | ||
const convector_core_errors_1 = require("@apex-pub/convector-core-errors"); | ||
const convector_core_storage_1 = require("@apex-pub/convector-core-storage"); | ||
const validate_decorator_1 = require("../src/validate.decorator"); | ||
const default_decorator_1 = require("../src/default.decorator"); | ||
const required_decorator_1 = require("../src/required.decorator"); | ||
const validate_decorator_2 = require("../src/validate.decorator"); | ||
class ConvectorModel { | ||
import { __decorate } from "tslib"; | ||
import * as yup from 'yup'; | ||
import { InvalidIdError } from '@apex-pub/convector-core-errors'; | ||
import { BaseStorage } from '@apex-pub/convector-core-storage'; | ||
import { Validate } from '../src/validate.decorator'; | ||
import { getDefaults } from '../src/default.decorator'; | ||
import { Required, ensureRequired } from '../src/required.decorator'; | ||
import { getPropertiesValidation, getValidatedProperties } from '../src/validate.decorator'; | ||
export class ConvectorModel { | ||
constructor(content) { | ||
@@ -24,76 +21,66 @@ if (!content) { | ||
static schema() { | ||
return yup.object().shape(Object.assign({ id: yup.string().required(), type: yup.string() }, (0, validate_decorator_2.getPropertiesValidation)(this.prototype))); | ||
} | ||
static getOne(id, type, storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
type = type || this; | ||
const content = yield convector_core_storage_1.BaseStorage.current.get(id, storageOptions); | ||
const model = new type(content); | ||
if ((content && model) && content.type !== new type('').type) { | ||
if (storageOptions && storageOptions.enforceType) { | ||
throw new Error(`Possible ID collision, element ${id} of type ${content.type} is not ${new type('').type}`); | ||
} | ||
else { | ||
console.warn(`Possible ID collision, element ${id} of type ${content.type} is not ${new type('').type}`); | ||
} | ||
} | ||
return model; | ||
return yup.object().shape({ | ||
id: yup.string().required(), | ||
type: yup.string(), | ||
...getPropertiesValidation(this.prototype) | ||
}); | ||
} | ||
static query(...args) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
let type = this; | ||
if (args[0] && 'type' in args[0] && args[0].type === ConvectorModel.type) { | ||
type = args.shift(); | ||
static async getOne(id, type, storageOptions) { | ||
type = type || this; | ||
const content = await BaseStorage.current.get(id, storageOptions); | ||
const model = new type(content); | ||
if ((content && model) && content.type !== new type('').type) { | ||
if (storageOptions && storageOptions.enforceType) { | ||
throw new Error(`Possible ID collision, element ${id} of type ${content.type} is not ${new type('').type}`); | ||
} | ||
const content = yield convector_core_storage_1.BaseStorage.current.query(...args); | ||
return Array.isArray(content) ? content.map(c => new type(c)) : new type(content); | ||
}); | ||
else { | ||
console.warn(`Possible ID collision, element ${id} of type ${content.type} is not ${new type('').type}`); | ||
} | ||
} | ||
return model; | ||
} | ||
static getAll(type) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
type = type || new this('').type; | ||
return yield ConvectorModel.query(this, { selector: { type } }); | ||
}); | ||
static async query(...args) { | ||
let type = this; | ||
if (args[0] && 'type' in args[0] && args[0].type === ConvectorModel.type) { | ||
type = args.shift(); | ||
} | ||
const content = await BaseStorage.current.query(...args); | ||
return Array.isArray(content) ? content.map(c => new type(c)) : new type(content); | ||
} | ||
update(content) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
this.assign(content); | ||
yield this.save(); | ||
}); | ||
static async getAll(type) { | ||
type = type || new this('').type; | ||
return await ConvectorModel.query(this, { selector: { type } }); | ||
} | ||
fetch(storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const content = yield convector_core_storage_1.BaseStorage.current.get(this.id, storageOptions); | ||
if (content.type !== this.type) { | ||
throw new Error(`Possible ID collision, element ${this.id} of type ${content.type} is not ${this.type}`); | ||
} | ||
this.assign(content); | ||
}); | ||
async update(content) { | ||
this.assign(content); | ||
await this.save(); | ||
} | ||
history() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
const history = yield convector_core_storage_1.BaseStorage.current.history(this.id); | ||
return history.map(item => ({ | ||
txId: item.tx_id, | ||
value: new this.constructor(item.value), | ||
timestamp: item.timestamp | ||
})); | ||
}); | ||
async fetch(storageOptions) { | ||
const content = await BaseStorage.current.get(this.id, storageOptions); | ||
if (content.type !== this.type) { | ||
throw new Error(`Possible ID collision, element ${this.id} of type ${content.type} is not ${this.type}`); | ||
} | ||
this.assign(content); | ||
} | ||
save(storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
this.assign((0, default_decorator_1.getDefaults)(this), true); | ||
if (!(0, required_decorator_1.ensureRequired)(this)) { | ||
if (!this.id) { | ||
throw new Error(`Model ${this.type} is missing the 'id' property \n${JSON.stringify(this)}`); | ||
} | ||
else { | ||
throw new Error(`Model ${this.type} is not complete\n${JSON.stringify(this)}. | ||
async history() { | ||
const history = await BaseStorage.current.history(this.id); | ||
return history.map(item => ({ | ||
txId: item.tx_id, | ||
value: new this.constructor(item.value), | ||
timestamp: item.timestamp | ||
})); | ||
} | ||
async save(storageOptions) { | ||
this.assign(getDefaults(this), true); | ||
if (!ensureRequired(this)) { | ||
if (!this.id) { | ||
throw new Error(`Model ${this.type} is missing the 'id' property \n${JSON.stringify(this)}`); | ||
} | ||
else { | ||
throw new Error(`Model ${this.type} is not complete\n${JSON.stringify(this)}. | ||
Check your model definition for more details.`); | ||
} | ||
} | ||
convector_core_errors_1.InvalidIdError.test(this.id); | ||
yield convector_core_storage_1.BaseStorage.current.set(this.id, this, storageOptions); | ||
}); | ||
} | ||
InvalidIdError.test(this.id); | ||
await BaseStorage.current.set(this.id, this, storageOptions); | ||
} | ||
@@ -118,3 +105,3 @@ clone() { | ||
.filter(k => !skipEmpty || !(this[k] === undefined || this[k] === null)) | ||
.reduce((result, key) => (Object.assign(Object.assign({}, result), { [key]: this[key] })), {}); | ||
.reduce((result, key) => ({ ...result, [key]: this[key] }), {}); | ||
return descriptors | ||
@@ -135,13 +122,14 @@ .reduce((result, [key, desc]) => { | ||
} | ||
delete(storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
yield convector_core_storage_1.BaseStorage.current.delete(this.id, storageOptions); | ||
}); | ||
async delete(storageOptions) { | ||
await BaseStorage.current.delete(this.id, storageOptions); | ||
} | ||
assign(content, defaults = false) { | ||
const validated = ['id', 'type', ...(0, validate_decorator_2.getValidatedProperties)(this)]; | ||
const validated = ['id', 'type', ...getValidatedProperties(this)]; | ||
const filteredContent = Object.keys(content) | ||
.map(key => key.replace(/^_/, '')) | ||
.filter(key => validated.indexOf(key) >= 0) | ||
.reduce((result, key) => (Object.assign(Object.assign({}, result), { [key]: content[key] !== undefined ? content[key] : content['_' + key] })), {}); | ||
.reduce((result, key) => ({ | ||
...result, | ||
[key]: content[key] !== undefined ? content[key] : content['_' + key] | ||
}), {}); | ||
const afterDefaults = defaults ? this.toJSON(true) : {}; | ||
@@ -152,7 +140,6 @@ Object.assign(this, filteredContent, afterDefaults); | ||
ConvectorModel.type = 'io.convector.model'; | ||
tslib_1.__decorate([ | ||
(0, required_decorator_1.Required)(), | ||
(0, validate_decorator_1.Validate)(yup.string()) | ||
__decorate([ | ||
Required(), | ||
Validate(yup.string()) | ||
], ConvectorModel.prototype, "id", void 0); | ||
exports.ConvectorModel = ConvectorModel; | ||
//# sourceMappingURL=convector-model.js.map |
@@ -1,21 +0,22 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDefaults = exports.Default = exports.defaultMetadataKey = void 0; | ||
const g = require("window-or-global"); | ||
require("reflect-metadata"); | ||
exports.defaultMetadataKey = g.ConvectorDefaultMetadataKey || Symbol('default'); | ||
g.ConvectorDefaultMetadataKey = exports.defaultMetadataKey; | ||
function Default(defaultValue) { | ||
import * as g from 'window-or-global'; | ||
import 'reflect-metadata'; | ||
export const defaultMetadataKey = g.ConvectorDefaultMetadataKey || Symbol('default'); | ||
g.ConvectorDefaultMetadataKey = defaultMetadataKey; | ||
export function Default(defaultValue) { | ||
return (target, key) => { | ||
const defaults = Reflect.getMetadata(exports.defaultMetadataKey, target) || {}; | ||
Reflect.defineMetadata(exports.defaultMetadataKey, Object.assign(Object.assign({}, defaults), { [key]: defaultValue }), target); | ||
const defaults = Reflect.getMetadata(defaultMetadataKey, target) || {}; | ||
Reflect.defineMetadata(defaultMetadataKey, { | ||
...defaults, | ||
[key]: defaultValue | ||
}, target); | ||
}; | ||
} | ||
exports.Default = Default; | ||
function getDefaults(obj) { | ||
const defaults = Reflect.getMetadata(exports.defaultMetadataKey, obj); | ||
export function getDefaults(obj) { | ||
const defaults = Reflect.getMetadata(defaultMetadataKey, obj); | ||
return !defaults ? {} : Object.keys(defaults) | ||
.reduce((result, k) => (Object.assign(Object.assign({}, result), { [k]: typeof defaults[k] === 'function' ? defaults[k]() : defaults[k] })), {}); | ||
.reduce((result, k) => ({ | ||
...result, | ||
[k]: typeof defaults[k] === 'function' ? defaults[k]() : defaults[k] | ||
}), {}); | ||
} | ||
exports.getDefaults = getDefaults; | ||
//# sourceMappingURL=default.decorator.js.map |
@@ -1,9 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./convector-model"), exports); | ||
tslib_1.__exportStar(require("./default.decorator"), exports); | ||
tslib_1.__exportStar(require("./required.decorator"), exports); | ||
tslib_1.__exportStar(require("./validate.decorator"), exports); | ||
tslib_1.__exportStar(require("./read-only.decorator"), exports); | ||
export * from './convector-model'; | ||
export * from './default.decorator'; | ||
export * from './required.decorator'; | ||
export * from './validate.decorator'; | ||
export * from './read-only.decorator'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ReadOnly = void 0; | ||
function ReadOnly() { | ||
export function ReadOnly() { | ||
return function (target, key) { | ||
@@ -22,3 +19,2 @@ const getSet = { | ||
} | ||
exports.ReadOnly = ReadOnly; | ||
//# sourceMappingURL=read-only.decorator.js.map |
@@ -1,19 +0,18 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ensureRequired = exports.Required = exports.requiredMetadataKey = void 0; | ||
const g = require("window-or-global"); | ||
require("reflect-metadata"); | ||
exports.requiredMetadataKey = g.ConvectorRequiredMetadataKey || Symbol('required'); | ||
g.ConvectorRequiredMetadataKey = exports.requiredMetadataKey; | ||
function Required() { | ||
import * as g from 'window-or-global'; | ||
import 'reflect-metadata'; | ||
export const requiredMetadataKey = g.ConvectorRequiredMetadataKey || Symbol('required'); | ||
g.ConvectorRequiredMetadataKey = requiredMetadataKey; | ||
export function Required() { | ||
return (target, key) => { | ||
const required = Reflect.getMetadata(exports.requiredMetadataKey, target); | ||
Reflect.defineMetadata(exports.requiredMetadataKey, Object.assign(Object.assign({}, required), { [key]: true }), target); | ||
const required = Reflect.getMetadata(requiredMetadataKey, target); | ||
Reflect.defineMetadata(requiredMetadataKey, { | ||
...required, | ||
[key]: true | ||
}, target); | ||
}; | ||
} | ||
exports.Required = Required; | ||
function ensureRequired(obj) { | ||
export function ensureRequired(obj) { | ||
let required = {}; | ||
try { | ||
required = Reflect.getMetadata(exports.requiredMetadataKey, obj) || {}; | ||
required = Reflect.getMetadata(requiredMetadataKey, obj) || {}; | ||
} | ||
@@ -31,3 +30,2 @@ catch (e) { | ||
} | ||
exports.ensureRequired = ensureRequired; | ||
//# sourceMappingURL=required.decorator.js.map |
@@ -1,9 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getValidatedProperties = exports.getPropertiesValidation = exports.Validate = exports.validateMetadataKey = void 0; | ||
const g = require("window-or-global"); | ||
require("reflect-metadata"); | ||
exports.validateMetadataKey = g.ConvectorValidateMetadataKey || Symbol('validate'); | ||
g.ConvectorValidateMetadataKey = exports.validateMetadataKey; | ||
function Validate(input) { | ||
import * as g from 'window-or-global'; | ||
import 'reflect-metadata'; | ||
export const validateMetadataKey = g.ConvectorValidateMetadataKey || Symbol('validate'); | ||
g.ConvectorValidateMetadataKey = validateMetadataKey; | ||
export function Validate(input) { | ||
let schema = input; | ||
@@ -33,11 +30,13 @@ if ('schema' in input) { | ||
Object.defineProperty(target, key, getSet); | ||
const validated = Reflect.getMetadata(exports.validateMetadataKey, target); | ||
Reflect.defineMetadata(exports.validateMetadataKey, Object.assign(Object.assign({}, validated), { [key]: schema }), target); | ||
const validated = Reflect.getMetadata(validateMetadataKey, target); | ||
Reflect.defineMetadata(validateMetadataKey, { | ||
...validated, | ||
[key]: schema | ||
}, target); | ||
}; | ||
} | ||
exports.Validate = Validate; | ||
function getPropertiesValidation(obj) { | ||
export function getPropertiesValidation(obj) { | ||
let validated = {}; | ||
try { | ||
validated = Reflect.getMetadata(exports.validateMetadataKey, obj) || {}; | ||
validated = Reflect.getMetadata(validateMetadataKey, obj) || {}; | ||
} | ||
@@ -48,7 +47,5 @@ catch (e) { | ||
} | ||
exports.getPropertiesValidation = getPropertiesValidation; | ||
function getValidatedProperties(obj) { | ||
export function getValidatedProperties(obj) { | ||
return Object.keys(getPropertiesValidation(obj)); | ||
} | ||
exports.getValidatedProperties = getValidatedProperties; | ||
//# sourceMappingURL=validate.decorator.js.map |
{ | ||
"name": "@apex-pub/convector-core-model", | ||
"version": "1.4.1-2.0", | ||
"version": "1.4.1-4-alpha.6064f94c", | ||
"description": "Convector Model base class", | ||
@@ -53,4 +53,4 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"@apex-pub/convector-core-errors": "^1.4.1-2.0", | ||
"@apex-pub/convector-core-storage": "^1.4.1-2.0", | ||
"@apex-pub/convector-core-errors": "1.4.1-4-alpha.6064f94c", | ||
"@apex-pub/convector-core-storage": "1.4.1-4-alpha.6064f94c", | ||
"window-or-global": "^1.0.1", | ||
@@ -57,0 +57,0 @@ "yup": "^0.26.10" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37968
335
+ Added@apex-pub/convector-core-errors@1.4.1-4-alpha.6064f94c(transitive)
+ Added@apex-pub/convector-core-storage@1.4.1-4-alpha.6064f94c(transitive)
- Removed@apex-pub/convector-core-errors@1.5.0(transitive)
- Removed@apex-pub/convector-core-storage@1.5.0(transitive)
- Removedtslib@1.14.1(transitive)
Updated@apex-pub/convector-core-errors@1.4.1-4-alpha.6064f94c
Updated@apex-pub/convector-core-storage@1.4.1-4-alpha.6064f94c