sedentary 0.0.38 → 0.0.41
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/dist/cjs/index.js +40 -11
- package/dist/es/index.js +40 -11
- package/dist/types/db.d.ts +2 -2
- package/dist/types/index.d.ts +21 -12
- package/package.json +18 -14
package/dist/cjs/index.js
CHANGED
|
@@ -72,12 +72,17 @@ class Sedentary {
|
|
|
72
72
|
throw new Error("Package sedentary can't be used directly. Please check: https://www.npmjs.com/package/sedentary#disclaimer");
|
|
73
73
|
}
|
|
74
74
|
checkOrderBy(order, attributes, modelName) {
|
|
75
|
+
let array = [];
|
|
75
76
|
if (!order)
|
|
76
77
|
return true;
|
|
77
|
-
if (
|
|
78
|
+
if (typeof order === "string")
|
|
79
|
+
array = [order];
|
|
80
|
+
else if (order instanceof Array)
|
|
81
|
+
array = order;
|
|
82
|
+
else
|
|
78
83
|
return false;
|
|
79
84
|
const provided = {};
|
|
80
|
-
for (const attribute of
|
|
85
|
+
for (const attribute of array) {
|
|
81
86
|
if (typeof attribute !== "string")
|
|
82
87
|
return false;
|
|
83
88
|
const attributeName = attribute.startsWith("-") ? attribute.substring(1) : attribute;
|
|
@@ -436,17 +441,41 @@ class Sedentary {
|
|
|
436
441
|
const table = new db_1.Table({ autoIncrement, constraints, attributes: aarray, indexes: iarray, model: ret, parent, pk, sync, tableName });
|
|
437
442
|
this.db.tables.push(table);
|
|
438
443
|
const load_ = this.db.load(tableName, attr2field, pk, ret, table);
|
|
439
|
-
const load = async (where,
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
444
|
+
const load = async (where, ...args) => {
|
|
445
|
+
let order = undefined;
|
|
446
|
+
let limit = undefined;
|
|
447
|
+
let tx = undefined;
|
|
448
|
+
let lock = undefined;
|
|
449
|
+
const checkArgs = (first) => {
|
|
450
|
+
if (!args.length)
|
|
451
|
+
return;
|
|
452
|
+
if (args[0] instanceof db_1.Transaction) {
|
|
453
|
+
if (first)
|
|
454
|
+
order = undefined;
|
|
455
|
+
limit = undefined;
|
|
456
|
+
[tx, lock] = args;
|
|
457
|
+
}
|
|
458
|
+
else if (typeof args[0] === "number") {
|
|
459
|
+
if (first)
|
|
460
|
+
order = undefined;
|
|
461
|
+
[limit, tx, lock] = args;
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
if (first) {
|
|
465
|
+
order = args.shift();
|
|
466
|
+
checkArgs(false);
|
|
467
|
+
}
|
|
468
|
+
else
|
|
469
|
+
throw new Error(`${modelName}.load: 'limit' argument: Wrong type, expected 'number'`);
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
checkArgs(true);
|
|
446
473
|
if (!this.checkOrderBy(order, attr2field, modelName))
|
|
447
|
-
throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string[]'`);
|
|
474
|
+
throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string | string[]'`);
|
|
475
|
+
if (tx && !(tx instanceof db_1.Transaction))
|
|
476
|
+
throw new Error(`${modelName}.load: 'tx' argument: Wrong type, expected 'Transaction'`);
|
|
448
477
|
const [str] = this.createWhere(modelName, attr2field, where);
|
|
449
|
-
const ret = await load_(str, order, tx, lock);
|
|
478
|
+
const ret = await load_(str, order, limit, tx, lock);
|
|
450
479
|
return ret;
|
|
451
480
|
};
|
|
452
481
|
Object.defineProperty(load, "name", { value: modelName + ".load" });
|
package/dist/es/index.js
CHANGED
|
@@ -66,12 +66,17 @@ export class Sedentary {
|
|
|
66
66
|
throw new Error("Package sedentary can't be used directly. Please check: https://www.npmjs.com/package/sedentary#disclaimer");
|
|
67
67
|
}
|
|
68
68
|
checkOrderBy(order, attributes, modelName) {
|
|
69
|
+
let array = [];
|
|
69
70
|
if (!order)
|
|
70
71
|
return true;
|
|
71
|
-
if (
|
|
72
|
+
if (typeof order === "string")
|
|
73
|
+
array = [order];
|
|
74
|
+
else if (order instanceof Array)
|
|
75
|
+
array = order;
|
|
76
|
+
else
|
|
72
77
|
return false;
|
|
73
78
|
const provided = {};
|
|
74
|
-
for (const attribute of
|
|
79
|
+
for (const attribute of array) {
|
|
75
80
|
if (typeof attribute !== "string")
|
|
76
81
|
return false;
|
|
77
82
|
const attributeName = attribute.startsWith("-") ? attribute.substring(1) : attribute;
|
|
@@ -430,17 +435,41 @@ export class Sedentary {
|
|
|
430
435
|
const table = new Table({ autoIncrement, constraints, attributes: aarray, indexes: iarray, model: ret, parent, pk, sync, tableName });
|
|
431
436
|
this.db.tables.push(table);
|
|
432
437
|
const load_ = this.db.load(tableName, attr2field, pk, ret, table);
|
|
433
|
-
const load = async (where,
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
438
|
+
const load = async (where, ...args) => {
|
|
439
|
+
let order = undefined;
|
|
440
|
+
let limit = undefined;
|
|
441
|
+
let tx = undefined;
|
|
442
|
+
let lock = undefined;
|
|
443
|
+
const checkArgs = (first) => {
|
|
444
|
+
if (!args.length)
|
|
445
|
+
return;
|
|
446
|
+
if (args[0] instanceof Transaction) {
|
|
447
|
+
if (first)
|
|
448
|
+
order = undefined;
|
|
449
|
+
limit = undefined;
|
|
450
|
+
[tx, lock] = args;
|
|
451
|
+
}
|
|
452
|
+
else if (typeof args[0] === "number") {
|
|
453
|
+
if (first)
|
|
454
|
+
order = undefined;
|
|
455
|
+
[limit, tx, lock] = args;
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
if (first) {
|
|
459
|
+
order = args.shift();
|
|
460
|
+
checkArgs(false);
|
|
461
|
+
}
|
|
462
|
+
else
|
|
463
|
+
throw new Error(`${modelName}.load: 'limit' argument: Wrong type, expected 'number'`);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
checkArgs(true);
|
|
440
467
|
if (!this.checkOrderBy(order, attr2field, modelName))
|
|
441
|
-
throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string[]'`);
|
|
468
|
+
throw new Error(`${modelName}.load: 'order' argument: Wrong type, expected 'string | string[]'`);
|
|
469
|
+
if (tx && !(tx instanceof Transaction))
|
|
470
|
+
throw new Error(`${modelName}.load: 'tx' argument: Wrong type, expected 'Transaction'`);
|
|
442
471
|
const [str] = this.createWhere(modelName, attr2field, where);
|
|
443
|
-
const ret = await load_(str, order, tx, lock);
|
|
472
|
+
const ret = await load_(str, order, limit, tx, lock);
|
|
444
473
|
return ret;
|
|
445
474
|
};
|
|
446
475
|
Object.defineProperty(load, "name", { value: modelName + ".load" });
|
package/dist/types/db.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare type Natural =
|
|
1
|
+
export declare type Natural = Date | Record<string, unknown> | bigint | boolean | number | string | null;
|
|
2
2
|
export declare class EntryBase {
|
|
3
3
|
constructor(from?: Partial<EntryBase>);
|
|
4
4
|
construct(): void;
|
|
@@ -87,7 +87,7 @@ export declare abstract class DB<T extends Transaction> {
|
|
|
87
87
|
protected syncLog(message: string): void;
|
|
88
88
|
abstract begin(): Promise<T>;
|
|
89
89
|
abstract escape(value: Natural): string;
|
|
90
|
-
abstract load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new () => EntryBase, table: Table): (where: string, order?: string[], tx?: Transaction, lock?: boolean) => Promise<EntryBase[]>;
|
|
90
|
+
abstract load(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>, model: new () => EntryBase, table: Table): (where: string, order?: string | string[], limit?: number, tx?: Transaction, lock?: boolean) => Promise<EntryBase[]>;
|
|
91
91
|
abstract remove(tableName: string, pk: Attribute<Natural, unknown>): (this: EntryBase & Record<string, Natural>) => Promise<boolean>;
|
|
92
92
|
abstract save(tableName: string, attributes: Record<string, string>, pk: Attribute<Natural, unknown>): (this: EntryBase & Record<string, Natural>) => Promise<boolean>;
|
|
93
93
|
abstract dropConstraints(table: Table): Promise<number[]>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -16,9 +16,12 @@ declare type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<Natural
|
|
|
16
16
|
declare type ForeignKeys<A extends AttributesDefinition> = {
|
|
17
17
|
[a in keyof A]?: ForeignKeysAttributes<A[a], a>;
|
|
18
18
|
}[keyof A];
|
|
19
|
-
declare type
|
|
20
|
-
declare type
|
|
21
|
-
declare type
|
|
19
|
+
declare type Native___<T> = T extends Type<infer N, unknown> ? N : never;
|
|
20
|
+
declare type Native__<T> = T extends () => Type<infer N, infer E> ? Native___<Type<N, E>> : Native___<T>;
|
|
21
|
+
declare type Native_<T, N extends Natural, E> = T extends {
|
|
22
|
+
notNull: true;
|
|
23
|
+
} ? Native___<Type<N, E>> : Native___<Type<N, E>> | null;
|
|
24
|
+
declare type Native<T> = T extends AttributeOptions<infer N, infer E> ? Native_<T, N, E> : Native__<T> | null;
|
|
22
25
|
export declare type IndexAttributes = string[] | string;
|
|
23
26
|
export interface IndexOptions {
|
|
24
27
|
attributes: IndexAttributes;
|
|
@@ -44,14 +47,15 @@ declare type ConditionBase<A extends AttributesDefinition> = string | {
|
|
|
44
47
|
[a in keyof A]?: ConditionAttribute<Native<A[a]>>;
|
|
45
48
|
};
|
|
46
49
|
declare type Condition<A extends AttributesDefinition> = ConditionBase<A> | ["NOT", Condition<A>] | ["AND", ...Condition<A>[]] | ["OR", ...Condition<A>[]];
|
|
47
|
-
declare type
|
|
50
|
+
declare type Order_<A extends AttributesDefinition> = keyof A | `-${string & keyof A}`;
|
|
51
|
+
declare type Order<A extends AttributesDefinition> = Order_<A> | Order_<A>[];
|
|
48
52
|
declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
49
53
|
declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
|
|
50
54
|
declare type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
|
|
51
55
|
declare type KeyType<B extends boolean, P extends ModelStd> = P extends new () => EntryBase ? (P extends Attribute<infer T, EntryBase> ? T : never) : BaseKeyType<B>;
|
|
52
56
|
declare type ForeignKey<A> = A extends AttributeDefinition<Natural, infer E> ? () => Promise<E> : never;
|
|
53
57
|
declare type EntryBaseAttributes<A extends AttributesDefinition> = {
|
|
54
|
-
[a in keyof A]
|
|
58
|
+
[a in keyof A]: Native<A[a]>;
|
|
55
59
|
};
|
|
56
60
|
declare type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P["methods"] : EntryBase;
|
|
57
61
|
declare type EntryMethodsFK<A extends AttributesDefinition> = {
|
|
@@ -60,14 +64,19 @@ declare type EntryMethodsFK<A extends AttributesDefinition> = {
|
|
|
60
64
|
declare type EntryMethods<A extends AttributesDefinition, P extends ModelStd> = keyof EntryMethodsFK<A> extends never ? EntryMethodsBase<P> : EntryMethodsBase<P> & EntryMethodsFK<A>;
|
|
61
65
|
declare type ModelAttributesIf<A extends AttributesDefinition, T> = keyof A extends never ? T : T & A;
|
|
62
66
|
declare type ModelAttributes<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd> = K extends keyof A ? A : ModelAttributesIf<A, P extends new () => EntryBase ? P["attributes"] : {
|
|
63
|
-
id:
|
|
67
|
+
id: {
|
|
68
|
+
notNull: true;
|
|
69
|
+
type: Type<BaseKeyType<B>, unknown>;
|
|
70
|
+
};
|
|
64
71
|
}>;
|
|
65
72
|
export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase> {
|
|
66
|
-
|
|
73
|
+
load(where: Condition<A>, order?: Order<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
|
|
67
74
|
load(where: Condition<A>, order?: Order<A>, tx?: Transaction, lock?: boolean): Promise<E[]>;
|
|
75
|
+
load(where: Condition<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
|
|
68
76
|
load(where: Condition<A>, tx: Transaction, lock?: boolean): Promise<E[]>;
|
|
69
77
|
}
|
|
70
|
-
declare type ModelBase<N extends Natural, A extends AttributesDefinition, EA extends Record<string, Natural
|
|
78
|
+
declare type ModelBase<N extends Natural, A extends AttributesDefinition, EA extends Record<string, Natural>, EM extends EntryBase, E extends EntryBase> = (new (from?: Partial<EA>, tx?: Transaction) => E) & Attribute<N, E> & {
|
|
79
|
+
attributes: A;
|
|
71
80
|
foreignKeys: Record<string, boolean>;
|
|
72
81
|
methods: EM;
|
|
73
82
|
parent?: ModelStd;
|
|
@@ -84,12 +93,12 @@ declare type ModelStd = Attribute<Natural, EntryBase> & {
|
|
|
84
93
|
};
|
|
85
94
|
export declare type Entry<M> = M extends new () => infer E ? E : never;
|
|
86
95
|
export declare type OrderBy<M> = M extends {
|
|
87
|
-
load(where: unknown, order?: infer T
|
|
96
|
+
load(where: unknown, order?: infer T): void;
|
|
97
|
+
load(where: unknown, limit?: number): void;
|
|
88
98
|
load(where: unknown, tx?: Transaction): void;
|
|
89
99
|
} ? Exclude<T, undefined> : never;
|
|
90
100
|
export declare type Where<M> = M extends {
|
|
91
|
-
load(where: infer T
|
|
92
|
-
load(where: unknown, tx?: Transaction): void;
|
|
101
|
+
load(where: infer T): void;
|
|
93
102
|
} ? T : never;
|
|
94
103
|
export interface SedentaryOptions {
|
|
95
104
|
autoSync?: boolean;
|
|
@@ -107,7 +116,7 @@ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
|
|
|
107
116
|
DATETIME(): Type<Date, unknown>;
|
|
108
117
|
FKEY<N extends Natural, E extends EntryBase>(attribute: Attribute<N, E>, options?: ForeignKeyOptions): Type<N, E>;
|
|
109
118
|
INT(size?: number): Type<number, unknown>;
|
|
110
|
-
INT8(): Type<
|
|
119
|
+
INT8(): Type<bigint, unknown>;
|
|
111
120
|
NUMBER(): Type<number, unknown>;
|
|
112
121
|
VARCHAR(size?: number): Type<string, unknown>;
|
|
113
122
|
private checkDB;
|
package/package.json
CHANGED
|
@@ -8,18 +8,19 @@
|
|
|
8
8
|
"dependencies": {},
|
|
9
9
|
"description": "The ORM which never needs to migrate",
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@types/
|
|
12
|
-
"@types/node": "
|
|
11
|
+
"@types/jest": "28.1.3",
|
|
12
|
+
"@types/node": "18.0.0",
|
|
13
13
|
"@types/yamljs": "0.2.31",
|
|
14
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
15
|
-
"@typescript-eslint/parser": "5.
|
|
16
|
-
"eslint": "8.
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"prettier": "2.
|
|
20
|
-
"ts-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
14
|
+
"@typescript-eslint/eslint-plugin": "5.30.0",
|
|
15
|
+
"@typescript-eslint/parser": "5.30.0",
|
|
16
|
+
"eslint": "8.18.0",
|
|
17
|
+
"jest": "28.1.1",
|
|
18
|
+
"jest-environment-node-single-context": "28.0.0",
|
|
19
|
+
"prettier": "2.7.1",
|
|
20
|
+
"ts-jest": "28.0.5",
|
|
21
|
+
"ts-node": "10.8.1",
|
|
22
|
+
"tsd": "0.21.0",
|
|
23
|
+
"typescript": "4.7.4",
|
|
23
24
|
"yamljs": "0.3.0"
|
|
24
25
|
},
|
|
25
26
|
"engines": {
|
|
@@ -42,6 +43,9 @@
|
|
|
42
43
|
"main": "./dist/cjs/index.js",
|
|
43
44
|
"module": "./dist/es/index.js",
|
|
44
45
|
"name": "sedentary",
|
|
46
|
+
"optionalDependencies": {
|
|
47
|
+
"fsevents": "2.3.2"
|
|
48
|
+
},
|
|
45
49
|
"prettier": {
|
|
46
50
|
"arrowParens": "avoid",
|
|
47
51
|
"endOfLine": "lf",
|
|
@@ -53,11 +57,11 @@
|
|
|
53
57
|
"readmeFilename": "README.md",
|
|
54
58
|
"repository": "https://github.com/iccicci/sedentary",
|
|
55
59
|
"scripts": {
|
|
56
|
-
"coverage": "
|
|
60
|
+
"coverage": "jest --coverage --runInBand",
|
|
57
61
|
"gitignore": "node -r ts-node/register utils.ts gitignore",
|
|
58
62
|
"npmignore": "node -r ts-node/register utils.ts npmignore",
|
|
59
63
|
"packagejson": "node -r ts-node/register utils.ts packagejson",
|
|
60
|
-
"test": "
|
|
64
|
+
"test": "jest --runInBand --verbose",
|
|
61
65
|
"travis": "node -r ts-node/register utils.ts travis",
|
|
62
66
|
"tsc": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.es.json && tsc -p tsconfig.types.json",
|
|
63
67
|
"version": "node -r ts-node/register utils.ts version"
|
|
@@ -79,5 +83,5 @@
|
|
|
79
83
|
}
|
|
80
84
|
},
|
|
81
85
|
"types": "./dist/types/index.d.ts",
|
|
82
|
-
"version": "0.0.
|
|
86
|
+
"version": "0.0.41"
|
|
83
87
|
}
|