@nu-art/ts-common 0.202.74 → 0.202.76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db/types.d.ts +29 -8
- package/mem-storage/MemStorage.d.ts +1 -0
- package/mem-storage/MemStorage.js +10 -0
- package/package.json +2 -2
- package/utils/db-object-tools.js +1 -1
- package/utils/types.d.ts +6 -2
- package/validator/type-validators.d.ts +1 -1
- package/validator/type-validators.js +9 -3
- package/validator/validators.d.ts +1 -0
- package/validator/validators.js +2 -0
package/db/types.d.ts
CHANGED
|
@@ -25,6 +25,15 @@ export type InnerDependencies<T extends DB_Object, K extends SubsetKeys<keyof T,
|
|
|
25
25
|
type Exact<T, Shape> = T & {
|
|
26
26
|
[K in Exclude<keyof Shape, keyof T>]?: never;
|
|
27
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Defines the base structure and constraints for a database object.
|
|
30
|
+
*
|
|
31
|
+
* @template T The base type of the database object.
|
|
32
|
+
* @template GeneratedKeys Keys that are auto-generated and should not be manually modified.
|
|
33
|
+
* @template Versions Versioning information for the database object.
|
|
34
|
+
* @template UniqueKeys Keys that are unique to each instance of the database object.
|
|
35
|
+
* @template Dependencies (Future Use) Defines dependencies or relationships to other database objects.
|
|
36
|
+
*/
|
|
28
37
|
export type Proto_DB_Object<T extends DB_Object, GeneratedKeys extends keyof T | never, Versions extends VersionsDeclaration<VersionType[]>, UniqueKeys extends keyof T = Default_UniqueKey, Dependencies extends Exact<{
|
|
29
38
|
[K in SubsetKeys<keyof T, T, string | string[]>]?: DBProto<any>;
|
|
30
39
|
}, Dependencies> = never> = {
|
|
@@ -34,6 +43,13 @@ export type Proto_DB_Object<T extends DB_Object, GeneratedKeys extends keyof T |
|
|
|
34
43
|
uniqueKeys: UniqueKeys;
|
|
35
44
|
dependencies: Dependencies;
|
|
36
45
|
};
|
|
46
|
+
/**
|
|
47
|
+
* Extends Proto_DB_Object with additional UI and validation details.
|
|
48
|
+
*
|
|
49
|
+
* @template P The Proto_DB_Object this DBProto is based on.
|
|
50
|
+
* @template ModifiableSubType The subset of P's type that is modifiable.
|
|
51
|
+
* @template GeneratedSubType The subset of P's type that is auto-generated.
|
|
52
|
+
*/
|
|
37
53
|
export type DBProto<P extends Proto_DB_Object<any, any, VersionsDeclaration<VersionType[]>, any, any>, ModifiableSubType = Omit<P['type'], P['generatedKeys'] | keyof DB_Object>, GeneratedSubType = SubsetObjectByKeys<P['type'], P['generatedKeys']>> = {
|
|
38
54
|
uiType: ModifiableSubType & Partial<GeneratedSubType> & Partial<DB_Object>;
|
|
39
55
|
preDbType: ModifiableSubType & Partial<GeneratedSubType>;
|
|
@@ -50,19 +66,24 @@ export type DBProto<P extends Proto_DB_Object<any, any, VersionsDeclaration<Vers
|
|
|
50
66
|
metadata?: Metadata<OmitDBObject<P['type']>>;
|
|
51
67
|
lockKeys?: (keyof P['type'])[];
|
|
52
68
|
};
|
|
53
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Represents the definition of a database entity with metadata and validation rules.
|
|
71
|
+
*
|
|
72
|
+
* @template Proto The DBProto type that this definition is based on.
|
|
73
|
+
*/
|
|
74
|
+
export type DBDef_V3<Proto extends DBProto<any, any, any>> = {
|
|
54
75
|
dbName: string;
|
|
55
76
|
entityName: string;
|
|
56
77
|
TTL?: number;
|
|
57
78
|
lastUpdatedTTL?: number;
|
|
58
79
|
upgradeChunksSize?: number;
|
|
59
|
-
generatedPropsValidator:
|
|
60
|
-
modifiablePropsValidator:
|
|
61
|
-
uniqueKeys?:
|
|
62
|
-
versions?:
|
|
63
|
-
indices?:
|
|
64
|
-
lockKeys?:
|
|
65
|
-
metadata?:
|
|
80
|
+
generatedPropsValidator: Proto['generatedPropsValidator'];
|
|
81
|
+
modifiablePropsValidator: Proto['modifiablePropsValidator'];
|
|
82
|
+
uniqueKeys?: Proto['uniqueKeys'];
|
|
83
|
+
versions?: Proto['versions']['versions'];
|
|
84
|
+
indices?: Proto['indices'];
|
|
85
|
+
lockKeys?: Proto['lockKeys'];
|
|
86
|
+
metadata?: Proto['metadata'];
|
|
66
87
|
};
|
|
67
88
|
/**
|
|
68
89
|
* @field version - First item in the array is current version, Must pass all past versions with the current, default version is 1.0.0
|
|
@@ -13,6 +13,7 @@ export declare class MemKey<T> {
|
|
|
13
13
|
constructor(key: string, unique?: boolean);
|
|
14
14
|
setResolver: (resolver?: ((storage: MemStorage) => T) | undefined) => this;
|
|
15
15
|
resolve: (storage: MemStorage) => Promise<void>;
|
|
16
|
+
assert: (value?: T) => T;
|
|
16
17
|
get: (value?: T) => T;
|
|
17
18
|
set: (value: T) => T;
|
|
18
19
|
merge: (value: T) => any;
|
|
@@ -14,6 +14,7 @@ const tools_1 = require("../utils/tools");
|
|
|
14
14
|
const exceptions_1 = require("../core/exceptions/exceptions");
|
|
15
15
|
const async_hooks_1 = require("async_hooks");
|
|
16
16
|
const random_tools_1 = require("../utils/random-tools");
|
|
17
|
+
const validator_core_1 = require("../validator/validator-core");
|
|
17
18
|
const asyncLocalStorage = new async_hooks_1.AsyncLocalStorage();
|
|
18
19
|
class MemStorage {
|
|
19
20
|
constructor() {
|
|
@@ -59,6 +60,15 @@ class MemKey {
|
|
|
59
60
|
return;
|
|
60
61
|
this.set(value);
|
|
61
62
|
});
|
|
63
|
+
this.assert = (value) => {
|
|
64
|
+
const storedValue = this.get();
|
|
65
|
+
if (!(0, tools_1.exists)(value))
|
|
66
|
+
return storedValue;
|
|
67
|
+
if (value !== storedValue) {
|
|
68
|
+
throw new validator_core_1.ValidationException(`Asserting MemKey(${this.key}) ${value} !== ${storedValue}!`);
|
|
69
|
+
}
|
|
70
|
+
return storedValue;
|
|
71
|
+
};
|
|
62
72
|
this.get = (value) => {
|
|
63
73
|
// @ts-ignore
|
|
64
74
|
const memValue = asyncLocalStorage.getStore().get(this, value);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nu-art/ts-common",
|
|
3
|
-
"version": "0.202.
|
|
3
|
+
"version": "0.202.76",
|
|
4
4
|
"description": "js and ts infra",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"TacB0sS",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"fast-csv": "^4.3.6",
|
|
36
36
|
"export-to-csv": "0.2.1",
|
|
37
|
-
"moment": "^2.
|
|
37
|
+
"moment": "^2.29.4",
|
|
38
38
|
"node-forge": "^1.2.1",
|
|
39
39
|
"uuid": "^9.0.0",
|
|
40
40
|
"csv-parser": "^2.3.3"
|
package/utils/db-object-tools.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.keepPartialObject = exports.keepDBObjectKeys = exports.deleteKeysObject = exports.removeDBObjectKeys = exports.dbObjectToId = exports.KeysOfDB_Object = void 0;
|
|
4
4
|
const object_tools_1 = require("./object-tools");
|
|
5
5
|
const tools_1 = require("./tools");
|
|
6
|
-
exports.KeysOfDB_Object = ['_id', '_v', '__created', '__updated'];
|
|
6
|
+
exports.KeysOfDB_Object = ['_id', '_v', '__created', '__updated', '__metadata1'];
|
|
7
7
|
function dbObjectToId(i) {
|
|
8
8
|
return i._id;
|
|
9
9
|
}
|
package/utils/types.d.ts
CHANGED
|
@@ -95,11 +95,12 @@ export type DB_BaseObject = {
|
|
|
95
95
|
_id: string;
|
|
96
96
|
};
|
|
97
97
|
export type DB_Object = DB_BaseObject & {
|
|
98
|
-
|
|
99
|
-
_originDocId?: UniqueId;
|
|
98
|
+
__metadata1?: any;
|
|
100
99
|
__hardDelete?: boolean;
|
|
101
100
|
__created: number;
|
|
102
101
|
__updated: number;
|
|
102
|
+
_v?: string;
|
|
103
|
+
_originDocId?: UniqueId;
|
|
103
104
|
};
|
|
104
105
|
export type UniqueId = string;
|
|
105
106
|
export type PreDB<T extends DB_Object, K extends keyof T = never> = PartialProperties<T, keyof DB_Object | K>;
|
|
@@ -151,6 +152,9 @@ export type AssetValueType<T, K extends keyof T, Ex> = T[K] extends Ex ? K : nev
|
|
|
151
152
|
export type RecursiveOmit<T, OmitKey extends keyof any> = {
|
|
152
153
|
[K in Exclude<keyof T, OmitKey>]: T[K] extends object ? RecursiveOmit<T[K], OmitKey> : T[K];
|
|
153
154
|
};
|
|
155
|
+
export type RecursivePartial<T> = {
|
|
156
|
+
[P in keyof T]?: T[P] extends any[] ? T[P] : T[P] extends object ? RecursivePartial<T[P]> : T[P];
|
|
157
|
+
};
|
|
154
158
|
/**
|
|
155
159
|
* Constructs a union of string paths representing the properties and nested properties of an object type.
|
|
156
160
|
*
|
|
@@ -10,7 +10,7 @@ type validatorObject<T> = {
|
|
|
10
10
|
};
|
|
11
11
|
export declare const tsValidateUnionV3: <T extends unknown>(validatorObject: validatorObject<T>, mandatory?: boolean) => ((input?: T | undefined) => any)[];
|
|
12
12
|
export declare const tsValidateArray: <T extends any[], I extends ArrayType<T> = ArrayType<T>>(validator: ValidatorTypeResolver<I>, mandatory?: boolean, minimumLength?: number) => Validator<I[]>;
|
|
13
|
-
export declare const tsValidateString: (length?: number, mandatory?: boolean) => Validator<string>;
|
|
13
|
+
export declare const tsValidateString: (length?: number | [number, number], mandatory?: boolean) => Validator<string>;
|
|
14
14
|
export declare const tsValidateStringMinLength: (length: number, mandatory?: boolean) => Validator<string>;
|
|
15
15
|
export declare const tsValidateNumber: (mandatory?: boolean) => Validator<number>;
|
|
16
16
|
export declare const tsValidateEnum: (enumType: TypedMap<number | string>, mandatory?: boolean) => Validator<number | string>;
|
|
@@ -79,9 +79,15 @@ const tsValidateString = (length = -1, mandatory = true) => {
|
|
|
79
79
|
return `input is not a string`;
|
|
80
80
|
if (length === -1)
|
|
81
81
|
return;
|
|
82
|
-
if (
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
if (Array.isArray(length)) {
|
|
83
|
+
if (length[0] > input.length)
|
|
84
|
+
return `input length is lesser than ${length}`;
|
|
85
|
+
if (input.length > length[1])
|
|
86
|
+
return `input length is longer than ${length}`;
|
|
87
|
+
}
|
|
88
|
+
else if (input.length > length)
|
|
89
|
+
return `input length is longer than ${length}`;
|
|
90
|
+
return;
|
|
85
91
|
}];
|
|
86
92
|
};
|
|
87
93
|
exports.tsValidateString = tsValidateString;
|
|
@@ -27,6 +27,7 @@ export declare const tsValidator_LowerUpperStringWithDashesAndUnderscore: Valida
|
|
|
27
27
|
export declare const tsValidator_InternationalPhoneNumber: Validator<string>;
|
|
28
28
|
export declare const tsValidator_AuditableV2: ValidatorTypeResolver<AuditableV2>;
|
|
29
29
|
export declare const DB_Object_validator: {
|
|
30
|
+
__metadata1: import("./validator-core").ValidatorImpl<any>;
|
|
30
31
|
_id: Validator<string>;
|
|
31
32
|
_v: Validator<string>;
|
|
32
33
|
_originDocId: Validator<string>;
|
package/validator/validators.js
CHANGED
|
@@ -37,6 +37,8 @@ exports.tsValidator_LowerUpperStringWithDashesAndUnderscore = (0, type_validator
|
|
|
37
37
|
exports.tsValidator_InternationalPhoneNumber = (0, type_validators_1.tsValidateRegexp)(/^\+(?:[0-9] ?){6,14}[0-9]$/);
|
|
38
38
|
exports.tsValidator_AuditableV2 = { _auditorId: (0, type_validators_1.tsValidateString)() };
|
|
39
39
|
exports.DB_Object_validator = {
|
|
40
|
+
// this will be the way to handle app level context via proto.. need to rename this to __metadata once done
|
|
41
|
+
__metadata1: exports.tsValidateOptional,
|
|
40
42
|
_id: exports.tsValidateUniqueId,
|
|
41
43
|
_v: exports.tsValidateVersion,
|
|
42
44
|
_originDocId: exports.tsValidateOptionalId,
|