sedentary 0.0.39 → 0.0.42

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/README.md CHANGED
@@ -109,9 +109,9 @@ To work with the package under Windows, be sure to configure `bash.exe` as your
109
109
  > npm config set script-shell bash.exe
110
110
  ```
111
111
 
112
- # Licence
112
+ # License
113
113
 
114
- [MIT Licence](https://github.com/iccicci/sedentary/blob/master/LICENSE)
114
+ [MIT License](https://github.com/iccicci/sedentary/blob/master/LICENSE)
115
115
 
116
116
  # Bugs
117
117
 
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" });
@@ -1,4 +1,4 @@
1
- export declare type Natural = BigInt | Date | Record<string, unknown> | boolean | number | string | null;
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[]>;
@@ -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;
@@ -113,7 +116,7 @@ export declare class Sedentary<D extends DB<T>, T extends Transaction> {
113
116
  DATETIME(): Type<Date, unknown>;
114
117
  FKEY<N extends Natural, E extends EntryBase>(attribute: Attribute<N, E>, options?: ForeignKeyOptions): Type<N, E>;
115
118
  INT(size?: number): Type<number, unknown>;
116
- INT8(): Type<BigInt, unknown>;
119
+ INT8(): Type<bigint, unknown>;
117
120
  NUMBER(): Type<number, unknown>;
118
121
  VARCHAR(size?: number): Type<string, unknown>;
119
122
  private checkDB;
package/package.json CHANGED
@@ -5,30 +5,14 @@
5
5
  "Daniele Ricci <daniele.icc@gmail.com> (https://github.com/iccicci)",
6
6
  "yossarian <sergiybiluk@gmail.com> (https://github.com/captain-yossarian)"
7
7
  ],
8
- "dependencies": {},
9
8
  "description": "The ORM which never needs to migrate",
10
- "devDependencies": {
11
- "@types/mocha": "9.1.1",
12
- "@types/node": "18.0.0",
13
- "@types/yamljs": "0.2.31",
14
- "@typescript-eslint/eslint-plugin": "5.30.0",
15
- "@typescript-eslint/parser": "5.30.0",
16
- "eslint": "8.18.0",
17
- "mocha": "10.0.0",
18
- "nyc": "15.1.0",
19
- "prettier": "2.7.1",
20
- "ts-node": "10.8.1",
21
- "tsd": "0.21.0",
22
- "typescript": "4.7.4",
23
- "yamljs": "0.3.0"
24
- },
25
9
  "engines": {
26
10
  "node": ">=14.0"
27
11
  },
28
12
  "funding": {
29
13
  "url": "https://blockchain.info/address/1Md9WFAHrXTb3yPBwQWmUfv2RmzrtbHioB"
30
14
  },
31
- "homepage": "https://github.com/iccicci/sedentary#readme",
15
+ "homepage": "https://github.com/iccicci/sedentary/packages/sedentary#readme",
32
16
  "keywords": [
33
17
  "DB",
34
18
  "ORM",
@@ -42,25 +26,19 @@
42
26
  "main": "./dist/cjs/index.js",
43
27
  "module": "./dist/es/index.js",
44
28
  "name": "sedentary",
45
- "prettier": {
46
- "arrowParens": "avoid",
47
- "endOfLine": "lf",
48
- "jsxBracketSameLine": true,
49
- "printWidth": 200,
50
- "trailingComma": "none",
51
- "useTabs": false
29
+ "optionalDependencies": {
30
+ "fsevents": "2.3.2"
52
31
  },
53
32
  "readmeFilename": "README.md",
54
33
  "repository": "https://github.com/iccicci/sedentary",
55
34
  "scripts": {
56
- "coverage": "nyc -r lcov -r text -r text-summary -r html mocha -r ts-node/register test/*ts",
57
- "gitignore": "node -r ts-node/register utils.ts gitignore",
58
- "npmignore": "node -r ts-node/register utils.ts npmignore",
59
- "packagejson": "node -r ts-node/register utils.ts packagejson",
60
- "test": "mocha -r ts-node/register test/*ts",
61
- "travis": "node -r ts-node/register utils.ts travis",
62
- "tsc": "tsc -p tsconfig.cjs.json && tsc -p tsconfig.es.json && tsc -p tsconfig.types.json",
63
- "version": "node -r ts-node/register utils.ts version"
35
+ "build": "make build",
36
+ "coverage": "jest --coverage --runInBand",
37
+ "deploy": "npm_config_registry=\"registry.npmjs.org\" npm publish",
38
+ "precoverage": "make pretest",
39
+ "preinstall": "if [ -f Makefile ] ; then make ; fi",
40
+ "pretest": "make pretest",
41
+ "test": "jest --runInBand"
64
42
  },
65
43
  "tsd": {
66
44
  "compilerOptions": {
@@ -79,5 +57,5 @@
79
57
  }
80
58
  },
81
59
  "types": "./dist/types/index.d.ts",
82
- "version": "0.0.39"
83
- }
60
+ "version": "0.0.42"
61
+ }