@worldsibu/convector-core-model
Advanced tools
Comparing version 1.2.1-0-alpha.38c096fa to 1.2.1-0-alpha.3fab4fce
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,2 +23,3 @@ export interface History<T> { | ||
export declare abstract class ConvectorModel<T extends ConvectorModel<any>> { | ||
private static type; | ||
static schema<T extends ConvectorModel<any>>(this: Function & { | ||
@@ -18,3 +31,3 @@ prototype: T; | ||
}>; | ||
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[]>; | ||
@@ -32,5 +45,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; | ||
@@ -40,4 +53,4 @@ toJSON(skipEmpty?: boolean): { | ||
}; | ||
delete(): Promise<void>; | ||
delete(storageOptions?: any): Promise<void>; | ||
private assign(content, defaults?); | ||
} |
@@ -25,5 +25,5 @@ "use strict"; | ||
}; | ||
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) { | ||
@@ -33,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]; | ||
} | ||
@@ -53,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(); | ||
@@ -91,3 +95,3 @@ } | ||
}; | ||
ConvectorModel.prototype.fetch = function () { | ||
ConvectorModel.prototype.fetch = function (storageOptions) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
@@ -97,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: | ||
@@ -128,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 () { | ||
@@ -144,3 +148,3 @@ return tslib_1.__generator(this, function (_a) { | ||
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: | ||
@@ -169,3 +173,3 @@ _a.sent(); | ||
.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) { | ||
@@ -185,10 +189,13 @@ return (tslib_1.__assign({}, result, (_a = {}, _a[key] = _this[key], _a))); | ||
} | ||
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: | ||
@@ -214,2 +221,3 @@ _a.sent(); | ||
}; | ||
ConvectorModel.type = 'io.convector.model'; | ||
tslib_1.__decorate([ | ||
@@ -216,0 +224,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 |
@@ -21,3 +21,9 @@ "use strict"; | ||
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; | ||
} | ||
}, | ||
@@ -24,0 +30,0 @@ enumerable: true, |
{ | ||
"name": "@worldsibu/convector-core-model", | ||
"version": "1.2.1-0-alpha.38c096fa", | ||
"version": "1.2.1-0-alpha.3fab4fce", | ||
"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,4 +40,4 @@ "typings": "./dist/src/index.d.ts", | ||
"dependencies": { | ||
"@worldsibu/convector-core-errors": "1.2.1-0-alpha.38c096fa", | ||
"@worldsibu/convector-core-storage": "1.2.1-0-alpha.38c096fa", | ||
"@worldsibu/convector-core-errors": "1.2.1-0-alpha.3fab4fce", | ||
"@worldsibu/convector-core-storage": "1.2.1-0-alpha.3fab4fce", | ||
"tslib": "^1.9.0", | ||
@@ -39,0 +44,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
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
43118
430
1
0
+ Added@worldsibu/convector-core-errors@1.2.1-0-alpha.3fab4fce(transitive)
+ Added@worldsibu/convector-core-storage@1.2.1-0-alpha.3fab4fce(transitive)
- Removed@worldsibu/convector-core-errors@1.2.1-0-alpha.38c096fa(transitive)
- Removed@worldsibu/convector-core-storage@1.2.1-0-alpha.38c096fa(transitive)
Updated@worldsibu/convector-core-errors@1.2.1-0-alpha.3fab4fce
Updated@worldsibu/convector-core-storage@1.2.1-0-alpha.3fab4fce