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 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 (!(order instanceof Array))
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 order) {
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, order, tx, lock) => {
440
- if (order instanceof db_1.Transaction) {
441
- if (typeof tx === "boolean" && tx)
442
- lock = true;
443
- tx = order;
444
- order = undefined;
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 (!(order instanceof Array))
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 order) {
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, order, tx, lock) => {
434
- if (order instanceof Transaction) {
435
- if (typeof tx === "boolean" && tx)
436
- lock = true;
437
- tx = order;
438
- order = undefined;
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" });
@@ -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[]>;
@@ -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 Order<A extends AttributesDefinition> = (keyof A | `-${string & keyof A}`)[];
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
- attributes: A;
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 | undefined>, EM extends EntryBase, E extends EntryBase> = (new (from?: Partial<EA>, tx?: Transaction) => E) & Attribute<N, E> & {
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, tx?: Transaction): void;
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, order?: unknown, tx?: Transaction): void;
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/mocha": "9.1.1",
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
- "mocha": "10.0.0",
18
- "nyc": "15.1.0",
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": "nyc -r lcov -r text -r text-summary -r html mocha -r ts-node/register test/*ts",
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": "mocha -r ts-node/register test/*ts",
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.39"
83
+ "version": "0.0.40"
83
84
  }
@@ -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;