@worldsibu/convector-core-model
Advanced tools
Comparing version 1.2.1-0-alpha.f15268c7 to 1.3.0-next.0
import * as yup from 'yup'; | ||
export declare type RequiredKeys<T> = { | ||
[K in keyof T]-?: string extends K ? never : number extends K ? never : {} extends Pick<T, K> ? never : K; | ||
} extends { | ||
[_ in keyof T]-?: infer U; | ||
} ? U extends keyof T ? U : never : never; | ||
export declare type OptionalKeys<T> = { | ||
[K in keyof T]-?: string extends K ? never : number extends K ? never : {} extends Pick<T, K> ? K : never; | ||
} extends { | ||
[_ in keyof T]-?: infer U; | ||
} ? U extends keyof T ? U : never : never; | ||
export declare type FlatConvectorModel<T> = { | ||
[L in Exclude<keyof T, keyof ConvectorModel<any>>]: T[L]; | ||
[L in Exclude<OptionalKeys<T>, keyof ConvectorModel<any>>]?: T[L]; | ||
} & { | ||
[L in Exclude<RequiredKeys<T>, keyof ConvectorModel<any>>]: T[L]; | ||
}; | ||
@@ -11,6 +23,10 @@ export interface History<T> { | ||
export declare abstract class ConvectorModel<T extends ConvectorModel<any>> { | ||
static schema<T extends ConvectorModel<any>>(this: new (...args: any[]) => T): yup.ObjectSchema<FlatConvectorModel<T> & { | ||
private static type; | ||
static schema<T extends ConvectorModel<any>>(this: Function & { | ||
prototype: T; | ||
}): yup.ObjectSchema<FlatConvectorModel<T> & { | ||
id: string; | ||
type: string; | ||
}>; | ||
static getOne<T extends ConvectorModel<any>>(this: new (content: any) => T, id: string, type?: new (content: any) => T): Promise<T>; | ||
static getOne<T extends ConvectorModel<any>>(this: new (content: any) => T, id: string, type?: new (content: any) => T, storageOptions?: any): Promise<T>; | ||
static query<T>(type: new (content: any) => T, ...args: any[]): Promise<T | T[]>; | ||
@@ -28,5 +44,5 @@ static getAll<T extends ConvectorModel<any>>(this: new (content: any) => T, type?: string): Promise<T[]>; | ||
}): Promise<void>; | ||
fetch(): Promise<void>; | ||
fetch(storageOptions?: any): Promise<void>; | ||
history(): Promise<History<T>[]>; | ||
save(): Promise<void>; | ||
save(storageOptions?: any): Promise<void>; | ||
clone(): T; | ||
@@ -36,4 +52,4 @@ toJSON(skipEmpty?: boolean): { | ||
}; | ||
delete(): Promise<void>; | ||
delete(storageOptions?: any): Promise<void>; | ||
private assign(content, defaults?); | ||
} |
@@ -23,8 +23,7 @@ "use strict"; | ||
ConvectorModel.schema = function () { | ||
var instance = new this(); | ||
return yup.object().shape(tslib_1.__assign({ id: yup.string().required(), key: yup.string() }, validate_decorator_2.getPropertiesValidation(instance))); | ||
return yup.object().shape(tslib_1.__assign({ id: yup.string().required(), type: yup.string() }, validate_decorator_2.getPropertiesValidation(this.prototype))); | ||
}; | ||
ConvectorModel.getOne = function (id, type) { | ||
ConvectorModel.getOne = function (id, type, storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var content; | ||
var content, model; | ||
return tslib_1.__generator(this, function (_a) { | ||
@@ -34,6 +33,10 @@ switch (_a.label) { | ||
type = type || this; | ||
return [4, convector_core_storage_1.BaseStorage.current.get(id)]; | ||
return [4, convector_core_storage_1.BaseStorage.current.get(id, storageOptions)]; | ||
case 1: | ||
content = _a.sent(); | ||
return [2, new type(content)]; | ||
model = new type(content); | ||
if ((content && model) && content.type !== model.type) { | ||
throw new Error("Possible ID collision, element " + id + " of type " + content.type + " is not " + model.type); | ||
} | ||
return [2, model]; | ||
} | ||
@@ -54,3 +57,3 @@ }); | ||
type = this; | ||
if (args[0] && args[0].prototype.__proto__.constructor === ConvectorModel) { | ||
if (args[0] && 'type' in args[0] && args[0].type === ConvectorModel.type) { | ||
type = args.shift(); | ||
@@ -92,3 +95,3 @@ } | ||
}; | ||
ConvectorModel.prototype.fetch = function () { | ||
ConvectorModel.prototype.fetch = function (storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
@@ -98,3 +101,3 @@ var content; | ||
switch (_a.label) { | ||
case 0: return [4, convector_core_storage_1.BaseStorage.current.get(this.id)]; | ||
case 0: return [4, convector_core_storage_1.BaseStorage.current.get(this.id, storageOptions)]; | ||
case 1: | ||
@@ -129,3 +132,3 @@ content = _a.sent(); | ||
}; | ||
ConvectorModel.prototype.save = function () { | ||
ConvectorModel.prototype.save = function (storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
@@ -137,6 +140,11 @@ return tslib_1.__generator(this, function (_a) { | ||
if (!required_decorator_1.ensureRequired(this)) { | ||
throw new Error("Model " + this.type + " is not complete\n" + JSON.stringify(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) + ".\n Check your model definition for more details."); | ||
} | ||
} | ||
convector_core_errors_1.InvalidIdError.test(this.id); | ||
return [4, convector_core_storage_1.BaseStorage.current.set(this.id, this)]; | ||
return [4, convector_core_storage_1.BaseStorage.current.set(this.id, this, storageOptions)]; | ||
case 1: | ||
@@ -155,6 +163,13 @@ _a.sent(); | ||
if (skipEmpty === void 0) { skipEmpty = false; } | ||
var proto = Object.getPrototypeOf(this); | ||
var protos = []; | ||
var children = this; | ||
do { | ||
children = Object.getPrototypeOf(children); | ||
protos.push(children); | ||
} while (children['__proto__'].constructor.name !== ConvectorModel.name); | ||
var descriptors = protos.reduce(function (result, proto) { return result.concat(Object.keys(proto) | ||
.map(function (key) { return [key, Object.getOwnPropertyDescriptor(proto, key)]; })); }, []); | ||
var base = Object.keys(this).concat('id') | ||
.filter(function (k) { return !k.startsWith('_'); }) | ||
.filter(function (k) { return !skipEmpty || _this[k] !== undefined || _this[k] !== null; }) | ||
.filter(function (k) { return !skipEmpty || !(_this[k] === undefined || _this[k] === null); }) | ||
.reduce(function (result, key) { | ||
@@ -164,5 +179,5 @@ return (tslib_1.__assign({}, result, (_a = {}, _a[key] = _this[key], _a))); | ||
}, {}); | ||
return Object.keys(proto) | ||
.reduce(function (result, key) { | ||
var desc = Object.getOwnPropertyDescriptor(proto, key); | ||
return descriptors | ||
.reduce(function (result, _a) { | ||
var key = _a[0], desc = _a[1]; | ||
var hasGetter = desc && typeof desc.get === 'function'; | ||
@@ -175,10 +190,13 @@ if (hasGetter) { | ||
} | ||
if (result[key] instanceof ConvectorModel) { | ||
result[key] = result[key].toJSON(true); | ||
} | ||
return result; | ||
}, base); | ||
}; | ||
ConvectorModel.prototype.delete = function () { | ||
ConvectorModel.prototype.delete = function (storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, convector_core_storage_1.BaseStorage.current.delete(this.id)]; | ||
case 0: return [4, convector_core_storage_1.BaseStorage.current.delete(this.id, storageOptions)]; | ||
case 1: | ||
@@ -204,2 +222,3 @@ _a.sent(); | ||
}; | ||
ConvectorModel.type = 'io.convector.model'; | ||
tslib_1.__decorate([ | ||
@@ -206,0 +225,0 @@ required_decorator_1.Required(), |
@@ -24,5 +24,11 @@ "use strict"; | ||
return Object.keys(required) | ||
.every(function (k) { return !required[k] || obj[k] !== undefined; }); | ||
.every(function (k) { | ||
var res = !required[k] || obj[k] !== undefined; | ||
if (!res) { | ||
console.log("Validation error. Field " + k + " is required but was not found."); | ||
} | ||
return res; | ||
}); | ||
} | ||
exports.ensureRequired = ensureRequired; | ||
//# sourceMappingURL=required.decorator.js.map |
@@ -11,3 +11,3 @@ "use strict"; | ||
if ('schema' in input) { | ||
schema = input.schema; | ||
schema = input.schema(); | ||
} | ||
@@ -22,3 +22,9 @@ return function (target, key) { | ||
setter = !setter || setter === getSet.set ? (function (v) { return v; }) : setter; | ||
this["_" + key] = schema.validateSync(setter.call(this, newVal)); | ||
try { | ||
this["_" + key] = schema.validateSync(setter.call(this, newVal)); | ||
} | ||
catch (ex) { | ||
ex.message = "Error for field '" + key + "' with val '" + JSON.stringify(newVal) + "' " + ex.message; | ||
throw ex; | ||
} | ||
}, | ||
@@ -25,0 +31,0 @@ enumerable: true, |
{ | ||
"name": "@worldsibu/convector-core-model", | ||
"version": "1.2.1-0-alpha.f15268c7", | ||
"version": "1.3.0-next.0", | ||
"description": "Convector Model base class", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/hyperledger-labs/convector/tree/develop/%40worldsibu/convector-core-model" | ||
}, | ||
"homepage": "https://worldsibu.tech/convector/convector-smart-contracts/", | ||
"main": "./dist/src/index.js", | ||
@@ -35,6 +40,6 @@ "typings": "./dist/src/index.d.ts", | ||
"dependencies": { | ||
"@worldsibu/convector-core-errors": "1.2.1-0-alpha.f15268c7", | ||
"@worldsibu/convector-core-storage": "1.2.1-0-alpha.f15268c7", | ||
"@worldsibu/convector-core-errors": "^1.3.0-next.0", | ||
"@worldsibu/convector-core-storage": "^1.3.0-next.0", | ||
"tslib": "^1.9.0", | ||
"yup": "^0.24.1" | ||
"yup": "^0.26.10" | ||
}, | ||
@@ -41,0 +46,0 @@ "devDependencies": { |
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
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
43090
430
1
0
+ Added@babel/runtime@7.0.0(transitive)
+ Added@worldsibu/convector-core-errors@1.3.8(transitive)
+ Added@worldsibu/convector-core-storage@1.3.8(transitive)
+ Addedfn-name@2.0.1(transitive)
+ Addedregenerator-runtime@0.12.1(transitive)
+ Addedsynchronous-promise@2.0.17(transitive)
+ Addedtoposort@2.0.2(transitive)
+ Addedwindow-or-global@1.0.1(transitive)
+ Addedyup@0.26.10(transitive)
- Removed@worldsibu/convector-core-errors@1.2.1-0-alpha.f15268c7(transitive)
- Removed@worldsibu/convector-core-storage@1.2.1-0-alpha.f15268c7(transitive)
- Removedcase@1.6.3(transitive)
- Removedfn-name@1.0.1(transitive)
- Removedsynchronous-promise@1.0.18(transitive)
- Removedtoposort@0.2.12(transitive)
- Removedtype-name@2.0.2(transitive)
- Removedyup@0.24.1(transitive)
Updatedyup@^0.26.10