sedentary 0.0.18 → 0.0.19

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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { DB, Entry, ForeignKeyOptions, Meta, Natural, Type } from "./lib/db";
2
- export { Entry, ForeignKeyActions, ForeignKeyOptions, Natural, Type } from "./lib/db";
1
+ import { DB, EntryBase, ForeignKeyOptions, Meta, Natural, Type } from "./lib/db";
2
+ export { EntryBase, ForeignKeyActions, ForeignKeyOptions, Natural, Type } from "./lib/db";
3
3
  export declare type TypeDefinition<N extends Natural, E> = (() => Type<N, E>) | Type<N, E>;
4
4
  export interface AttributeOptions<N extends Natural, E> {
5
5
  defaultValue?: N;
@@ -12,12 +12,12 @@ export declare type AttributeDefinition<N extends Natural, E> = TypeDefinition<N
12
12
  export declare type AttributesDefinition = {
13
13
  [key: string]: AttributeDefinition<Natural, unknown>;
14
14
  };
15
- declare type KeysAttributes<T, k> = T extends AttributeDefinition<Natural, infer E> ? (E extends Entry ? k : never) : never;
16
- declare type Keys<F extends AttributesDefinition> = {
17
- [f in keyof F]?: KeysAttributes<F[f], f>;
18
- }[keyof F];
19
- declare type Methods<T> = {
20
- [key: string]: (this: T) => unknown;
15
+ declare type ForeignKeysAttributes<T, k> = T extends AttributeDefinition<Natural, infer E> ? (E extends EntryBase ? k : never) : never;
16
+ declare type ForeignKeys<A extends AttributesDefinition> = {
17
+ [a in keyof A]?: ForeignKeysAttributes<A[a], a>;
18
+ }[keyof A];
19
+ declare type Methods<E> = {
20
+ [key: string]: (this: E) => unknown;
21
21
  };
22
22
  declare type Native__<T> = T extends Type<infer N, unknown> ? N : never;
23
23
  declare type Native_<T> = T extends () => Type<infer N, infer E> ? Native__<Type<N, E>> : Native__<T>;
@@ -33,34 +33,51 @@ export declare type IndexDefinition = IndexAttributes | IndexOptions;
33
33
  export declare type IndexesDefinition = {
34
34
  [key: string]: IndexDefinition;
35
35
  };
36
- declare type BaseModelOptions<T> = {
36
+ declare type BaseModelOptions<B extends boolean, T> = {
37
37
  indexes?: IndexesDefinition;
38
38
  init?: (this: T) => void;
39
+ int8id?: B;
39
40
  sync?: boolean;
40
41
  tableName?: string;
41
42
  };
42
- export declare type ModelOptions<K extends string, M extends Methods<T>, P extends Meta<Natural, Entry>, T extends Entry> = BaseModelOptions<T> & {
43
- int8id?: boolean;
43
+ export declare type ModelOptions<B extends boolean, K extends string, M extends Methods<T>, P extends Meta<Natural, EntryBase>, T extends EntryBase> = BaseModelOptions<B, T> & {
44
44
  methods?: M;
45
45
  parent?: P;
46
46
  primaryKey?: K;
47
47
  };
48
- declare type ForeignKey<T> = T extends AttributeDefinition<Natural, infer E> ? () => Promise<E> : never;
49
- declare type ModelWithMetods<A extends AttributesDefinition, M> = {
50
- [a in keyof A]?: Native<A[a]>;
51
- } & {
52
- [a in Keys<A> & string as `${a}Load`]?: ForeignKey<A[a]>;
53
- } & M;
54
- declare type Model<A extends AttributesDefinition> = {
48
+ declare type ModelBaseOptions = {
49
+ indexes?: IndexesDefinition;
50
+ sync?: boolean;
51
+ tableName?: string;
52
+ };
53
+ export declare type ModelOptions2 = ModelBaseOptions & {
54
+ int8id?: boolean;
55
+ parent?: Type<Natural, EntryBase>;
56
+ primaryKey?: string;
57
+ };
58
+ declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
59
+ declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
60
+ declare type EntryIdNatural<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
61
+ export declare type EntryId<B extends boolean> = {
62
+ id?: EntryIdNatural<B>;
63
+ };
64
+ declare type ForeignKey<A> = A extends AttributeDefinition<Natural, infer E> ? () => Promise<E> : never;
65
+ declare type ModelFKAttributes<A extends AttributesDefinition> = {
66
+ [a in ForeignKeys<A> & string as `${a}Load`]?: ForeignKey<A[a]>;
67
+ };
68
+ declare type ModelBaseAttributes<A extends AttributesDefinition> = {
55
69
  [a in keyof A]?: Native<A[a]>;
56
- } & {
57
- [a in Keys<A> & string as `${a}Load`]?: ForeignKey<A[a]>;
58
70
  };
59
- declare type Ancestor<A, N extends Natural, T extends Entry> = (new () => T) & {
71
+ declare type ModelAttributes<A extends AttributesDefinition> = keyof ModelFKAttributes<A> extends never ? ModelBaseAttributes<A> : ModelBaseAttributes<A> & ModelFKAttributes<A>;
72
+ declare type Ancestor<A, N extends Natural, T extends EntryBase> = (new () => T) & {
60
73
  [a in keyof A]?: Meta<Native<A[a]>, T>;
61
74
  } & {
62
75
  load: (boh: boolean) => Promise<T[]>;
63
- } & Meta<N, T>;
76
+ } & Meta<N, T> & {
77
+ tmp: number;
78
+ };
79
+ declare type ModelBase<E extends EntryBase> = new () => E;
80
+ export declare type Entry<M> = M extends new () => infer E ? E : never;
64
81
  export interface SedentaryOptions {
65
82
  log?: ((message: string) => void) | null;
66
83
  serverless?: boolean;
@@ -73,45 +90,36 @@ export declare class Sedentary {
73
90
  private models;
74
91
  constructor(filename: string, options?: SedentaryOptions);
75
92
  DATETIME(): Type<Date, unknown>;
76
- FKEY<N extends Natural, E extends Entry>(attribute: Type<N, E>, options?: ForeignKeyOptions): Type<N, E>;
93
+ FKEY<N extends Natural, E extends EntryBase>(attribute: Type<N, E>, options?: ForeignKeyOptions): Type<N, E>;
77
94
  INT(size?: number): Type<number, unknown>;
78
95
  INT8(): Type<string, unknown>;
79
96
  VARCHAR(size?: number): Type<string, unknown>;
97
+ checkSize(size: number, message: string): number;
80
98
  connect(): Promise<void>;
81
99
  end(): Promise<void>;
82
- model<A extends AttributesDefinition, M extends Methods<T>, T extends Entry & {
83
- id?: string;
84
- } & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
85
- int8id: true;
86
- methods: M;
87
- }): Ancestor<A, string, T>;
88
- model<A extends AttributesDefinition, K extends keyof A, M extends Methods<T>, N extends K extends keyof A ? Native<A[K]> : never, T extends Entry & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
89
- methods: M;
90
- primaryKey: K;
91
- }): Ancestor<A, N, T>;
92
- model<A extends AttributesDefinition, M extends Methods<T>, P extends Meta<Natural, Entry>, N extends P extends Meta<infer N, Entry> ? N : never, T extends Parent<P> & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
93
- methods: M;
94
- parent: P;
95
- }): Ancestor<A, N, T>;
96
- model<A extends AttributesDefinition, M extends Methods<T>, T extends Entry & {
97
- id?: number;
98
- } & ModelWithMetods<A, M>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
99
- methods: M;
100
- }): Ancestor<A, number, T>;
101
- model<A extends AttributesDefinition, T extends Entry & {
102
- id?: string;
103
- } & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
104
- int8id: true;
105
- }): Ancestor<A, string, T>;
106
- model<A extends AttributesDefinition, K extends keyof A, N extends K extends keyof A ? Native<A[K]> : never, T extends Entry & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
107
- primaryKey: K;
108
- }): Ancestor<A, N, T>;
109
- model<A extends AttributesDefinition, P extends Meta<Natural, Entry>, N extends P extends Meta<infer N, Entry> ? N : never, T extends Parent<P> & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T> & {
110
- parent: P;
111
- }): Ancestor<A, N, T>;
112
- model<A extends AttributesDefinition, T extends Entry & {
113
- id?: number;
114
- } & Model<A>>(modelName: string, attributes: A, options?: BaseModelOptions<T>): Ancestor<A, number, T>;
115
- checkSize(size: number, message: string): number;
100
+ model2<E extends EntryBase>(modelName: string): ModelBase<E>;
101
+ model2<E extends EntryBase, M extends Record<string, <S extends M>(this: E & S) => unknown>>(modelName: string, methods: M & Record<string, (this: E & M) => void>): ModelBase<E & M>;
102
+ model<A extends AttributesDefinition, B extends boolean, E extends EntryBase & EntryId<B> & ModelAttributes<A>>(modelName: string, attributes: A, options?: BaseModelOptions<B, E>): Ancestor<A, string, E>;
103
+ model<A extends AttributesDefinition, B extends boolean, K extends keyof A, P extends Meta<Natural, EntryBase>, N extends P extends {
104
+ tmp: number;
105
+ } ? (P extends Meta<infer N, EntryBase> ? N : never) : Native<A[K]>, E extends (P extends {
106
+ tmp: number;
107
+ } ? Parent<P> : EntryBase) & ModelAttributes<A>>(modelName: string, attributes: A, options?: BaseModelOptions<B, E> & {
108
+ parent?: P;
109
+ primaryKey?: K;
110
+ }): Ancestor<A, N, E>;
111
+ model<A extends AttributesDefinition, B extends boolean, E extends EntryBase & EntryId<B> & ModelAttributes<A>, M extends Record<string, <S extends M>(this: E & S) => unknown>>(modelName: string, attributes: A, options: BaseModelOptions<B, E>, methods: M & Record<string, (this: E & M) => unknown>): Ancestor<A, string, E & M>;
112
+ model<A extends AttributesDefinition, B extends boolean, K extends keyof A, P extends Meta<Natural, EntryBase>, N extends P extends {
113
+ tmp: number;
114
+ } ? (P extends Meta<infer N, EntryBase> ? N : never) : Native<A[K]>, E extends (P extends {
115
+ tmp: number;
116
+ } ? Parent<P> : EntryBase) & ModelAttributes<A>, M extends Record<string, <S extends M>(this: E & S) => unknown>>(modelName: string, attributes: A, options: BaseModelOptions<B, E> & {
117
+ parent?: P;
118
+ primaryKey?: K;
119
+ }, methods: M & Record<string, (this: E & M) => unknown>): Ancestor<A, N, E & M>;
116
120
  }
117
121
  export declare const Package: typeof Sedentary;
122
+ export declare class Sedentary2 {
123
+ model<E extends EntryBase>(modelName: string): ModelBase<E>;
124
+ model<E extends EntryBase, M extends Record<string, <S extends M>(this: E & S) => unknown>>(modelName: string, methods: M & Record<string, (this: E & M) => void>): ModelBase<E & M>;
125
+ }
package/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Package = exports.Sedentary = exports.Type = exports.Entry = void 0;
3
+ exports.Sedentary2 = exports.Package = exports.Sedentary = exports.Type = exports.EntryBase = void 0;
4
4
  const db_1 = require("./lib/db");
5
5
  const log_1 = require("./lib/log");
6
6
  const minidb_1 = require("./lib/minidb");
7
7
  var db_2 = require("./lib/db");
8
- Object.defineProperty(exports, "Entry", { enumerable: true, get: function () { return db_2.Entry; } });
8
+ Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return db_2.EntryBase; } });
9
9
  Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return db_2.Type; } });
10
10
  const allowedOption = ["indexes", "init", "int8id", "methods", "parent", "primaryKey", "sync", "tableName", "type"];
11
11
  const reservedNames = [
@@ -56,6 +56,13 @@ class Sedentary {
56
56
  size = size ? this.checkSize(size, message) : undefined;
57
57
  return new db_1.Type({ base: String, size, type: "VARCHAR" });
58
58
  }
59
+ checkSize(size, message) {
60
+ const str = size.toString();
61
+ const parsed = parseInt(str, 10);
62
+ if (str !== parsed.toString())
63
+ throw new Error(message);
64
+ return parsed;
65
+ }
59
66
  async connect() {
60
67
  try {
61
68
  this.log("Connecting...");
@@ -74,7 +81,16 @@ class Sedentary {
74
81
  await this.db.end();
75
82
  this.log("Connection closed");
76
83
  }
77
- model(modelName, attributes, options) {
84
+ model2(modelName, methods) {
85
+ const ret = function () {
86
+ this.save();
87
+ };
88
+ Object.defineProperty(ret, "name", { value: name });
89
+ if (methods)
90
+ Object.assign(ret.prototype, methods);
91
+ return ret;
92
+ }
93
+ model(modelName, attributes, options, methods) {
78
94
  if (typeof modelName !== "string")
79
95
  throw new Error("Sedentary.model: 'name' argument: Wrong type, expected 'string'");
80
96
  if (this.models[modelName])
@@ -98,7 +114,6 @@ class Sedentary {
98
114
  throw new Error(`Sedentary.model: '${modelName}' model: 'parent' and 'primaryKey' options conflict each other`);
99
115
  let autoIncrement = true;
100
116
  const { indexes, int8id, parent, primaryKey, sync, tableName } = Object.assign({ sync: this.sync, tableName: modelName }, options);
101
- let { methods } = options;
102
117
  let aarray = int8id
103
118
  ? [new db_1.Attribute(Object.assign(Object.assign({}, this.INT8()), { attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true }))]
104
119
  : [new db_1.Attribute(Object.assign(Object.assign({}, this.INT(4)), { attributeName: "id", fieldName: "id", modelName, notNull: true, tableName, unique: true }))];
@@ -352,13 +367,6 @@ class Sedentary {
352
367
  Object.defineProperty(Class, key, { value: pk[key] });
353
368
  return Class;
354
369
  }
355
- checkSize(size, message) {
356
- const str = size.toString();
357
- const parsed = parseInt(str, 10);
358
- if (str !== parsed.toString())
359
- throw new Error(message);
360
- return parsed;
361
- }
362
370
  }
363
371
  exports.Sedentary = Sedentary;
364
372
  exports.Package = Sedentary;
@@ -369,13 +377,15 @@ class Item extends db.model("Item", {
369
377
  str: db.VARCHAR()
370
378
  }, {
371
379
  init: function () {
372
- this.num = 0;
380
+ this.num = "0";
373
381
  this.str = "0";
374
382
  },
375
- int8id: true,
383
+ int8id: true
384
+ /*
376
385
  methods: {
377
- prova: () => "ok"
386
+ prova: (): string => "ok"
378
387
  }
388
+ */
379
389
  }) {
380
390
  }
381
391
  class Super extends db.model("Super", {
@@ -383,15 +393,17 @@ class Super extends db.model("Super", {
383
393
  n: db.FKEY(Item),
384
394
  s: db.FKEY(Users.bar)
385
395
  }, {
386
- parent: Item,
387
- init: async function () {
388
- this.n = "23";
389
- this.id = "0";
390
- this.num = 0;
391
- const a = this.nLoad ? await this.nLoad() : { prova: () => null };
392
- a.prova();
393
- this.prova();
396
+ parent: Item
397
+ /*
398
+ init: async function() {
399
+ this.n = "23";
400
+ this.id = 0;
401
+ this.num = 0;
402
+ const a = this.nLoad ? await this.nLoad() : { prova: (): null => null };
403
+ a.prova();
404
+ this.prova();
394
405
  }
406
+ */
395
407
  }) {
396
408
  }
397
409
  class Next extends db.model("Next", { a: db.INT, b: db.INT }, {
@@ -421,33 +433,49 @@ class Last extends db.model("Last", { c: db.FKEY(Current.b) }, {
421
433
  }
422
434
  catch (e) {
423
435
  console.log(Item.load, item.save, await Item.load(true), item, e.message);
424
- console.log(new Next(), Next.load, await Next.load(true), new Last(), item.prova());
436
+ //console.log(new Next(), Next.load, await Next.load(true), new Last(), item.prova());
425
437
  }
426
438
  return true;
427
439
  })();
428
- /*
429
- export let factory = () => {
430
- class Foo {
431
- a = 3;
432
- b = 'bar'
433
- c?: boolean
440
+ function model(modelName, attributes, options, methods) {
441
+ const model = function () { };
442
+ Object.defineProperty(model, "name", { value: modelName });
443
+ if (methods)
444
+ Object.assign(model.prototype, methods);
445
+ return model;
446
+ }
447
+ const T1 = model("T1", {});
448
+ const t1 = new T1();
449
+ t1.id = 0;
450
+ console.log(t1);
451
+ const T2 = model("T2", { a: db.INT, b: db.VARCHAR }, { int8id: true }, {
452
+ test: function (repeat = true) {
453
+ if (repeat)
454
+ this.b = this.test(false);
455
+ this.c = this.a = 0;
456
+ return "test";
457
+ }
458
+ });
459
+ const t2 = new T2();
460
+ //t2.id = "0";
461
+ const tt2 = (t) => console.log(t, t.test(), t.a, t.b);
462
+ tt2(t2);
463
+ class Sedentary2 {
464
+ model(modelName, methods) {
465
+ const ret = function () { };
466
+ Object.defineProperty(ret, "name", { value: modelName });
467
+ if (methods)
468
+ Object.assign(ret.prototype, methods);
469
+ ret.prototype.save = () => new Promise(resolve => resolve(false));
470
+ return ret;
434
471
  }
435
- return Foo as (new () => { [key in keyof Foo]: Foo[key] })
436
- };
437
-
438
- export const FooConstr = factory()
439
- export type TypeExtractor<T> = T extends (new() => infer E) ? E : never;
440
- export type FooType = TypeExtractor<typeof FooConstr>;
441
-
442
- const foo = new FooConstr()
443
-
444
- function rino(a:FooType){
445
- if(a instanceof FooConstr){console.log("sisi")}
446
- if(a instanceof FooType){console.log("sisi")}
447
472
  }
448
-
449
- rino(foo);
450
-
451
-
452
- B.prototype instanceof A
453
- */
473
+ exports.Sedentary2 = Sedentary2;
474
+ const db2 = new Sedentary2();
475
+ const T22 = db2.model("T2", {
476
+ c: function () {
477
+ this.id = "0";
478
+ this.c();
479
+ return 0;
480
+ }
481
+ });
package/lib/db.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export declare type Natural = Date | Record<string, unknown> | boolean | number | string;
2
- export declare class Entry {
3
- init(): void;
2
+ export declare class EntryBase {
4
3
  save(): Promise<boolean>;
5
4
  }
6
5
  export declare type ForeignKeyActions = "cascade" | "no action" | "restrict" | "set default" | "set null";
package/lib/db.js CHANGED
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DB = exports.Table = exports.Attribute = exports.Meta = exports.Type = exports.Entry = void 0;
4
- class Entry {
5
- init() { }
3
+ exports.DB = exports.Table = exports.Attribute = exports.Meta = exports.Type = exports.EntryBase = void 0;
4
+ /**/
5
+ class EntryBase {
6
6
  async save() {
7
7
  return false;
8
8
  }
9
9
  }
10
- exports.Entry = Entry;
10
+ exports.EntryBase = EntryBase;
11
11
  class Type {
12
12
  constructor(from) {
13
13
  Object.assign(this, from);
package/package.json CHANGED
@@ -5,16 +5,17 @@
5
5
  "description": "The ORM which never needs to migrate",
6
6
  "devDependencies": {
7
7
  "@types/mocha": "9.0.0",
8
- "@types/node": "16.11.10",
8
+ "@types/node": "17.0.0",
9
9
  "@types/yamljs": "0.2.31",
10
- "@typescript-eslint/eslint-plugin": "5.4.0",
11
- "@typescript-eslint/parser": "5.4.0",
12
- "eslint": "8.3.0",
10
+ "@typescript-eslint/eslint-plugin": "5.7.0",
11
+ "@typescript-eslint/parser": "5.7.0",
12
+ "eslint": "8.5.0",
13
13
  "mocha": "9.1.3",
14
14
  "nyc": "15.1.0",
15
- "prettier": "2.5.0",
15
+ "prettier": "2.5.1",
16
16
  "ts-node": "10.4.0",
17
- "typescript": "4.5.2",
17
+ "tsd": "0.19.0",
18
+ "typescript": "4.5.4",
18
19
  "yamljs": "0.3.0"
19
20
  },
20
21
  "engines": {
@@ -55,6 +56,11 @@
55
56
  "tsc": "tsc --declaration",
56
57
  "version": "node -r ts-node/register utils.ts version"
57
58
  },
59
+ "tsd": {
60
+ "compilerOptions": {
61
+ "strict": false
62
+ }
63
+ },
58
64
  "types": "index.d.ts",
59
- "version": "0.0.18"
65
+ "version": "0.0.19"
60
66
  }