sedentary 0.0.39 → 0.0.40
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 +1 -1
- package/dist/types/index.d.ts +9 -6
- package/jest.config.js +8 -0
- package/package.json +7 -6
- package/testSequencer.js +20 -0
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
|
@@ -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
|
@@ -47,7 +47,8 @@ declare type ConditionBase<A extends AttributesDefinition> = string | {
|
|
|
47
47
|
[a in keyof A]?: ConditionAttribute<Native<A[a]>>;
|
|
48
48
|
};
|
|
49
49
|
declare type Condition<A extends AttributesDefinition> = ConditionBase<A> | ["NOT", Condition<A>] | ["AND", ...Condition<A>[]] | ["OR", ...Condition<A>[]];
|
|
50
|
-
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>[];
|
|
51
52
|
declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
52
53
|
declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
|
|
53
54
|
declare type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
|
|
@@ -69,11 +70,13 @@ declare type ModelAttributes<A extends AttributesDefinition, B extends boolean,
|
|
|
69
70
|
};
|
|
70
71
|
}>;
|
|
71
72
|
export interface ModelLoad<A extends AttributesDefinition, E extends EntryBase> {
|
|
72
|
-
|
|
73
|
+
load(where: Condition<A>, order?: Order<A>, limit?: number, tx?: Transaction, lock?: boolean): Promise<E[]>;
|
|
73
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[]>;
|
|
74
76
|
load(where: Condition<A>, tx: Transaction, lock?: boolean): Promise<E[]>;
|
|
75
77
|
}
|
|
76
|
-
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;
|
|
77
80
|
foreignKeys: Record<string, boolean>;
|
|
78
81
|
methods: EM;
|
|
79
82
|
parent?: ModelStd;
|
|
@@ -90,12 +93,12 @@ declare type ModelStd = Attribute<Natural, EntryBase> & {
|
|
|
90
93
|
};
|
|
91
94
|
export declare type Entry<M> = M extends new () => infer E ? E : never;
|
|
92
95
|
export declare type OrderBy<M> = M extends {
|
|
93
|
-
load(where: unknown, order?: infer T
|
|
96
|
+
load(where: unknown, order?: infer T): void;
|
|
97
|
+
load(where: unknown, limit?: number): void;
|
|
94
98
|
load(where: unknown, tx?: Transaction): void;
|
|
95
99
|
} ? Exclude<T, undefined> : never;
|
|
96
100
|
export declare type Where<M> = M extends {
|
|
97
|
-
load(where: infer T
|
|
98
|
-
load(where: unknown, tx?: Transaction): void;
|
|
101
|
+
load(where: infer T): void;
|
|
99
102
|
} ? T : never;
|
|
100
103
|
export interface SedentaryOptions {
|
|
101
104
|
autoSync?: boolean;
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
|
2
|
+
module.exports = {
|
|
3
|
+
collectCoverageFrom: ["db.ts", "index.ts"],
|
|
4
|
+
preset: "ts-jest",
|
|
5
|
+
testEnvironment: "jest-environment-node-single-context",
|
|
6
|
+
testPathIgnorePatterns: ["/node_modules/", "/sedentary-pg/"],
|
|
7
|
+
testSequencer: "./testSequencer.js"
|
|
8
|
+
};
|
package/package.json
CHANGED
|
@@ -8,15 +8,16 @@
|
|
|
8
8
|
"dependencies": {},
|
|
9
9
|
"description": "The ORM which never needs to migrate",
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@types/
|
|
11
|
+
"@types/jest": "28.1.3",
|
|
12
12
|
"@types/node": "18.0.0",
|
|
13
13
|
"@types/yamljs": "0.2.31",
|
|
14
14
|
"@typescript-eslint/eslint-plugin": "5.30.0",
|
|
15
15
|
"@typescript-eslint/parser": "5.30.0",
|
|
16
16
|
"eslint": "8.18.0",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"jest": "28.1.1",
|
|
18
|
+
"jest-environment-node-single-context": "28.0.0",
|
|
19
19
|
"prettier": "2.7.1",
|
|
20
|
+
"ts-jest": "28.0.5",
|
|
20
21
|
"ts-node": "10.8.1",
|
|
21
22
|
"tsd": "0.21.0",
|
|
22
23
|
"typescript": "4.7.4",
|
|
@@ -53,11 +54,11 @@
|
|
|
53
54
|
"readmeFilename": "README.md",
|
|
54
55
|
"repository": "https://github.com/iccicci/sedentary",
|
|
55
56
|
"scripts": {
|
|
56
|
-
"coverage": "
|
|
57
|
+
"coverage": "jest --coverage --runInBand",
|
|
57
58
|
"gitignore": "node -r ts-node/register utils.ts gitignore",
|
|
58
59
|
"npmignore": "node -r ts-node/register utils.ts npmignore",
|
|
59
60
|
"packagejson": "node -r ts-node/register utils.ts packagejson",
|
|
60
|
-
"test": "
|
|
61
|
+
"test": "jest --runInBand --verbose",
|
|
61
62
|
"travis": "node -r ts-node/register utils.ts travis",
|
|
62
63
|
"tsc": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.es.json && tsc -p tsconfig.types.json",
|
|
63
64
|
"version": "node -r ts-node/register utils.ts version"
|
|
@@ -79,5 +80,5 @@
|
|
|
79
80
|
}
|
|
80
81
|
},
|
|
81
82
|
"types": "./dist/types/index.d.ts",
|
|
82
|
-
"version": "0.0.
|
|
83
|
+
"version": "0.0.40"
|
|
83
84
|
}
|
package/testSequencer.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
2
|
+
const Sequencer = require("@jest/test-sequencer").default;
|
|
3
|
+
|
|
4
|
+
class CustomSequencer extends Sequencer {
|
|
5
|
+
shard(tests, { shardIndex, shardCount }) {
|
|
6
|
+
const shardSize = Math.ceil(tests.length / shardCount);
|
|
7
|
+
const shardStart = shardSize * (shardIndex - 1);
|
|
8
|
+
const shardEnd = shardSize * shardIndex;
|
|
9
|
+
|
|
10
|
+
return [...tests].sort((a, b) => (a.path > b.path ? 1 : -1)).slice(shardStart, shardEnd);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
sort(tests) {
|
|
14
|
+
const copyTests = Array.from(tests);
|
|
15
|
+
|
|
16
|
+
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = CustomSequencer;
|