@nu-art/ts-common 0.201.36 → 0.201.38

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 CHANGED
@@ -1,4 +1,4 @@
1
- import { DB_Object, OmitDBObject, PartialProperties } from '../utils/types';
1
+ import { DB_Object, OmitDBObject, SubsetObjectByKeys, UniqueId } from '../utils/types';
2
2
  import { ValidatorTypeResolver } from '../validator/validator-core';
3
3
  export type DBIndex<T extends DB_Object> = {
4
4
  id: string;
@@ -10,17 +10,45 @@ export type DBIndex<T extends DB_Object> = {
10
10
  };
11
11
  export type Default_UniqueKey = '_id';
12
12
  export type VersionType = string;
13
- export type DBProto<T extends DB_Object, Ks extends keyof T = Default_UniqueKey, Versions extends VersionType[] = ['1.0.0'], GeneratedKeys extends keyof T = keyof DB_Object> = {
14
- generatedKeys: keyof DB_Object | GeneratedKeys;
15
- uiType: PartialProperties<T, keyof DB_Object | GeneratedKeys>;
16
- dbType: T;
17
- uniqueKeys: Ks;
13
+ export type VersionsDeclaration<T extends DB_Object, Versions extends VersionType[] = ['1.0.0'], Types extends [T, ...DB_Object[]] = [T, ...DB_Object[]]> = {
18
14
  versions: Versions;
19
- validatorBE: ValidatorTypeResolver<OmitDBObject<T>>;
20
- validatorFE: ValidatorTypeResolver<Omit<T, keyof GeneratedKeys>>;
21
- dbIndices: DBIndex<T>;
15
+ types: Types;
16
+ };
17
+ export type Proto_DB_Object<T extends DB_Object, GeneratedKeys extends keyof T, Versions extends VersionsDeclaration<T, any, any>, UniqueKeys extends keyof T = Default_UniqueKey> = {
18
+ type: T;
19
+ generatedKeys: GeneratedKeys;
20
+ versions: Versions;
21
+ uniqueKeys: UniqueKeys;
22
+ };
23
+ export type DBProto<P extends Proto_DB_Object<any, any, any, any>, ModifiableSubType = Omit<P['type'], P['generatedKeys'] | keyof DB_Object>, GeneratedSubType = SubsetObjectByKeys<P['type'], P['generatedKeys']>> = {
24
+ uiType: ModifiableSubType & Partial<GeneratedSubType> & Partial<DB_Object>;
25
+ dbType: P['type'];
26
+ generatedPropsValidator: ValidatorTypeResolver<GeneratedSubType>;
27
+ modifiablePropsValidator: ValidatorTypeResolver<ModifiableSubType>;
28
+ uniqueKeys: P['uniqueKeys'][];
29
+ generatedProps: (P['generatedKeys'] | keyof DB_Object)[];
30
+ versions: P['versions']['versions'];
31
+ indices: DBIndex<P['type']>[];
32
+ uniqueParam: UniqueId | {
33
+ [K in P['uniqueKeys']]: P['type'][K];
34
+ };
35
+ metadata?: Metadata<OmitDBObject<P['type']>>;
36
+ lockKeys?: (keyof P['type'])[];
37
+ };
38
+ export type DBDef_V3<P extends DBProto<any, any, any>> = {
39
+ dbName: string;
40
+ entityName: string;
41
+ TTL?: number;
42
+ lastUpdatedTTL?: number;
43
+ upgradeChunksSize?: number;
44
+ generatedPropsValidator: P['generatedPropsValidator'];
45
+ modifiablePropsValidator: P['modifiablePropsValidator'];
46
+ uniqueKeys?: P['uniqueKeys'];
47
+ versions?: P['versions'];
48
+ indices?: P['indices'];
49
+ lockKeys?: P['lockKeys'];
50
+ metadata?: P['metadata'];
22
51
  };
23
- export type DBDef_V2<Proto extends DBProto<any>> = DBDef<Proto['dbType'], Proto['uniqueKeys']>;
24
52
  /**
25
53
  * @field version - First item in the array is current version, Must pass all past versions with the current, default version is 1.0.0
26
54
  */
@@ -58,5 +86,36 @@ export type MetadataNested<T extends any> = T extends (infer I)[] ? MetadataProp
58
86
  export type Metadata<T extends any> = T extends (infer I)[] ? MetadataProperty<T> & {
59
87
  metadata: Metadata<I>;
60
88
  } : T extends object ? MetadataObject<T> : MetadataProperty<T>;
61
- export declare const DB_Object_Metadata: Metadata<DB_Object>;
89
+ export declare const DB_Object_Metadata: {
90
+ _id: {
91
+ optional: boolean;
92
+ valueType: string;
93
+ description: string;
94
+ };
95
+ _v: {
96
+ optional: boolean;
97
+ valueType: string;
98
+ description: string;
99
+ };
100
+ _originDocId: {
101
+ optional: boolean;
102
+ valueType: string;
103
+ description: string;
104
+ };
105
+ __hardDelete: {
106
+ optional: boolean;
107
+ valueType: string;
108
+ description: string;
109
+ };
110
+ __created: {
111
+ optional: boolean;
112
+ valueType: string;
113
+ description: string;
114
+ };
115
+ __updated: {
116
+ optional: boolean;
117
+ valueType: string;
118
+ description: string;
119
+ };
120
+ };
62
121
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/ts-common",
3
- "version": "0.201.36",
3
+ "version": "0.201.38",
4
4
  "description": "js and ts infra",
5
5
  "keywords": [
6
6
  "TacB0sS",
@@ -1,5 +1,8 @@
1
- import { DB_Object, OmitDBObject } from './types';
1
+ import { DB_Object, OmitDBObject, SubsetObjectByKeys, TS_Object } from './types';
2
+ import { DBProto } from '../db/types';
2
3
  export declare const KeysOfDB_Object: (keyof DB_Object)[];
3
4
  export declare function dbObjectToId(i: DB_Object): string;
5
+ export declare function dbObjectToId_V3<Proto extends DBProto<any>>(i: Proto['dbType']): any;
4
6
  export declare function removeDBObjectKeys<T extends DB_Object>(instance: T): OmitDBObject<T>;
5
7
  export declare function keepDBObjectKeys<T extends DB_Object>(instance: T): DB_Object;
8
+ export declare function keepPartialObject<T extends TS_Object, Ks extends keyof T>(instance: T, keys: Ks[]): SubsetObjectByKeys<T, Ks>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.keepDBObjectKeys = exports.removeDBObjectKeys = exports.dbObjectToId = exports.KeysOfDB_Object = void 0;
3
+ exports.keepPartialObject = exports.keepDBObjectKeys = exports.removeDBObjectKeys = exports.dbObjectToId_V3 = exports.dbObjectToId = exports.KeysOfDB_Object = void 0;
4
4
  const object_tools_1 = require("./object-tools");
5
5
  const tools_1 = require("./tools");
6
6
  exports.KeysOfDB_Object = ['_id', '_v', '__created', '__updated'];
@@ -8,6 +8,10 @@ function dbObjectToId(i) {
8
8
  return i._id;
9
9
  }
10
10
  exports.dbObjectToId = dbObjectToId;
11
+ function dbObjectToId_V3(i) {
12
+ return i._id;
13
+ }
14
+ exports.dbObjectToId_V3 = dbObjectToId_V3;
11
15
  function removeDBObjectKeys(instance) {
12
16
  const _instance = (0, object_tools_1.deepClone)(instance);
13
17
  exports.KeysOfDB_Object.forEach(key => delete _instance[key]);
@@ -15,10 +19,14 @@ function removeDBObjectKeys(instance) {
15
19
  }
16
20
  exports.removeDBObjectKeys = removeDBObjectKeys;
17
21
  function keepDBObjectKeys(instance) {
18
- return exports.KeysOfDB_Object.reduce((objectToRet, key) => {
19
- if ((0, tools_1.exists)(instance[key])) // @ts-ignore
22
+ return keepPartialObject(instance, exports.KeysOfDB_Object);
23
+ }
24
+ exports.keepDBObjectKeys = keepDBObjectKeys;
25
+ function keepPartialObject(instance, keys) {
26
+ return keys.reduce((objectToRet, key) => {
27
+ if ((0, tools_1.exists)(instance[key]))
20
28
  objectToRet[key] = instance[key];
21
29
  return objectToRet;
22
30
  }, {});
23
31
  }
24
- exports.keepDBObjectKeys = keepDBObjectKeys;
32
+ exports.keepPartialObject = keepPartialObject;
package/utils/types.d.ts CHANGED
@@ -148,3 +148,56 @@ export type MergeTypes<T extends unknown[]> = T extends [a: infer A, ...rest: in
148
148
  export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
149
149
  export type NonEmptyArray<T> = [T, ...T[]];
150
150
  export type AssetValueType<T, K extends keyof T, Ex> = T[K] extends Ex ? K : never;
151
+ /**
152
+ * Constructs a union of string paths representing the properties and nested properties of an object type.
153
+ *
154
+ * @typeParam ObjectType - The object type to analyze.
155
+ *
156
+ * @example
157
+ * // Simple Example: Analyzing a flat object
158
+ * type Person = { name: string; age: number };
159
+ * type PersonPaths = DotNotation<Person>; // 'name' | 'age'
160
+ *
161
+ * @example
162
+ * // Nested Example: Analyzing an object with nested properties
163
+ * type User = { name: string; address: { city: string; zip: string } };
164
+ * type UserPaths = DotNotation<User>; // 'name' | 'address' | 'address.city' | 'address.zip'
165
+ *
166
+ * @example
167
+ * // Complex Example: Analyzing an object with multiple levels of nesting
168
+ * type Profile = { name: string; contacts: { email: { primary: string; secondary: string } } };
169
+ * type ProfilePaths = DotNotation<Profile>; // 'name' | 'contacts' | 'contacts.email' | 'contacts.email.primary' | 'contacts.email.secondary'
170
+ */
171
+ export type DotNotation<T> = T extends object ? {
172
+ [K in keyof T]: K extends string ? `${K & string}` | `${K & string}.${DotNotation<T[K]>}` : never;
173
+ }[keyof T] : '';
174
+ /**
175
+ * Replaces the type of nested property within an object, based on a specified path.
176
+ *
177
+ * @typeParam ObjectType - The original object type.
178
+ * @typeParam PropertyPath - The path to the property to replace, expressed as a dot-notation string.
179
+ * @typeParam NewValueType - The new type to replace the old type with.
180
+ *
181
+ * @example
182
+ * // Simple Example: Replace the 'age' property with a string
183
+ * type Person = { name: string; age: number };
184
+ * type NewPerson = ManipulateInnerPropValue<Person, 'age', string>;
185
+ * // Result: { name: string; age: string }
186
+ *
187
+ * @example
188
+ * // Nested Example: Replace the 'address.city' property with a number
189
+ * type User = { name: string; address: { city: string; zip: string } };
190
+ * type NewUser = ManipulateInnerPropValue<User, 'address.city', number>;
191
+ * // Result: { name: string; address: { city: number; zip: string } }
192
+ *
193
+ * @example
194
+ * // Complex Example: Replace the 'contacts.email.primary' property with a boolean
195
+ * type Profile = { name: string; contacts: { email: { primary: string; secondary: string } } };
196
+ * type NewProfile = ManipulateInnerPropValue<Profile, 'contacts.email.primary', boolean>;
197
+ * // Result: { name: string; contacts: { email: { primary: boolean; secondary: string } } }
198
+ */
199
+ export type ManipulateInnerPropValue<ObjectType, PropertyPath extends DotNotation<ObjectType>, NewValueType> = PropertyPath extends `${infer Key}.${infer Rest}` ? Key extends keyof ObjectType ? {
200
+ [Prop in keyof ObjectType]: Prop extends Key ? Rest extends DotNotation<ObjectType[Key]> ? ManipulateInnerPropValue<ObjectType[Key], Rest, NewValueType> : never : ObjectType[Prop];
201
+ } : never : {
202
+ [Prop in keyof ObjectType]: Prop extends PropertyPath ? NewValueType : ObjectType[Prop];
203
+ };
@@ -1,5 +1,5 @@
1
1
  import { Validator, ValidatorTypeResolver } from './validator-core';
2
- import { AuditableV2, DB_Object } from '../utils/types';
2
+ import { AuditableV2 } from '../utils/types';
3
3
  export declare const tsValidate_OptionalArray: <T>(validator: ValidatorTypeResolver<T>) => Validator<T[]>;
4
4
  export declare const tsValidator_nonMandatoryString: Validator<string>;
5
5
  export declare const tsValidateMD5: (mandatory?: boolean) => Validator<string>;
@@ -25,4 +25,11 @@ export declare const tsValidator_LowerUpperStringWithSpaces: Validator<string>;
25
25
  export declare const tsValidator_LowerUpperStringWithDashesAndUnderscore: Validator<string>;
26
26
  export declare const tsValidator_InternationalPhoneNumber: Validator<string>;
27
27
  export declare const tsValidator_AuditableV2: ValidatorTypeResolver<AuditableV2>;
28
- export declare const DB_Object_validator: ValidatorTypeResolver<DB_Object>;
28
+ export declare const DB_Object_validator: {
29
+ _id: Validator<string>;
30
+ _v: Validator<string>;
31
+ _originDocId: Validator<string>;
32
+ __hardDelete: Validator<boolean>;
33
+ __created: Validator<number>;
34
+ __updated: Validator<number>;
35
+ };