sedentary 0.0.19 → 0.0.23
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 +2 -3
- package/{lib/db.d.ts → db.d.ts} +21 -25
- package/{lib/db.js → db.js} +21 -11
- package/index.d.ts +56 -64
- package/index.js +83 -221
- package/package.json +21 -5
- package/lib/log.d.ts +0 -4
- package/lib/log.js +0 -20
- package/lib/log.ts +0 -22
- package/lib/minidb.d.ts +0 -17
- package/lib/minidb.js +0 -190
- package/requirements.txt +0 -2
package/README.md
CHANGED
|
@@ -81,10 +81,9 @@ $ npm install --save sedentary
|
|
|
81
81
|
|
|
82
82
|
# Disclaimer
|
|
83
83
|
|
|
84
|
-
**Do not use this package itself
|
|
85
|
-
purposes.**
|
|
84
|
+
**Do not use this package itself! It does not support any DB engine.**
|
|
86
85
|
|
|
87
|
-
|
|
86
|
+
A _DB engine dedicated extension_ must be used:
|
|
88
87
|
|
|
89
88
|
- MySQL: planned
|
|
90
89
|
- PostgreSQL: [sedentary-pg](https://github.com/iccicci/sedentary-pg#readme)
|
package/{lib/db.d.ts → db.d.ts}
RENAMED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
export declare type Natural = Date | Record<string, unknown> | boolean | number | string;
|
|
2
2
|
export declare class EntryBase {
|
|
3
|
+
constructor(from?: Partial<EntryBase> | "load");
|
|
4
|
+
construct(): void;
|
|
5
|
+
postLoad(): void;
|
|
6
|
+
postSave(): void;
|
|
7
|
+
preLoad(): void;
|
|
8
|
+
preSave(): void;
|
|
3
9
|
save(): Promise<boolean>;
|
|
4
10
|
}
|
|
5
11
|
export declare type ForeignKeyActions = "cascade" | "no action" | "restrict" | "set default" | "set null";
|
|
@@ -7,7 +13,7 @@ export interface ForeignKeyOptions {
|
|
|
7
13
|
onDelete?: ForeignKeyActions;
|
|
8
14
|
onUpdate?: ForeignKeyActions;
|
|
9
15
|
}
|
|
10
|
-
export
|
|
16
|
+
export interface Type<N extends Natural, E> {
|
|
11
17
|
base: unknown;
|
|
12
18
|
entry?: E;
|
|
13
19
|
native?: N;
|
|
@@ -19,34 +25,20 @@ export declare class Type<N extends Natural, E> {
|
|
|
19
25
|
options?: ForeignKeyOptions;
|
|
20
26
|
tableName: string;
|
|
21
27
|
};
|
|
22
|
-
constructor(from: Type<N, E>);
|
|
23
28
|
}
|
|
24
|
-
export declare class
|
|
25
|
-
|
|
26
|
-
[key: string]: unknown;
|
|
27
|
-
};
|
|
28
|
-
foreignKeys: {
|
|
29
|
-
[key: string]: unknown;
|
|
30
|
-
};
|
|
31
|
-
init: () => void;
|
|
32
|
-
isModel?: () => boolean;
|
|
33
|
-
methods: {
|
|
34
|
-
[key: string]: () => unknown;
|
|
35
|
-
};
|
|
36
|
-
modelName: string;
|
|
37
|
-
parent?: Meta<Natural, E>;
|
|
38
|
-
primaryKey: string;
|
|
39
|
-
tableName: string;
|
|
40
|
-
constructor(from: Meta<N, E>);
|
|
29
|
+
export declare class Type<N extends Natural, E> {
|
|
30
|
+
constructor(from: Type<N, E>);
|
|
41
31
|
}
|
|
42
|
-
export
|
|
32
|
+
export interface Attribute<N extends Natural, E> extends Type<N, E> {
|
|
43
33
|
attributeName: string;
|
|
44
|
-
defaultValue?:
|
|
34
|
+
defaultValue?: Natural;
|
|
45
35
|
fieldName: string;
|
|
46
36
|
modelName: string;
|
|
47
37
|
notNull: boolean;
|
|
48
38
|
tableName: string;
|
|
49
39
|
unique?: boolean;
|
|
40
|
+
}
|
|
41
|
+
export declare class Attribute<N extends Natural, E> extends Type<N, E> {
|
|
50
42
|
constructor(from: Attribute<N, E>);
|
|
51
43
|
}
|
|
52
44
|
export interface Constraint {
|
|
@@ -65,11 +57,11 @@ interface ITable {
|
|
|
65
57
|
autoIncrement: boolean;
|
|
66
58
|
constraints: Constraint[];
|
|
67
59
|
indexes: Index[];
|
|
68
|
-
parent
|
|
60
|
+
parent?: Attribute<Natural, unknown>;
|
|
69
61
|
sync: boolean;
|
|
70
62
|
tableName: string;
|
|
71
63
|
}
|
|
72
|
-
declare const Table_base: new (defaults?: ITable) => ITable;
|
|
64
|
+
declare const Table_base: new (defaults?: ITable | undefined) => ITable;
|
|
73
65
|
export declare class Table extends Table_base {
|
|
74
66
|
autoIncrementOwn?: boolean;
|
|
75
67
|
oid?: number;
|
|
@@ -77,7 +69,7 @@ export declare class Table extends Table_base {
|
|
|
77
69
|
}
|
|
78
70
|
export declare abstract class DB {
|
|
79
71
|
tables: Table[];
|
|
80
|
-
protected log: (
|
|
72
|
+
protected log: (message: string) => void;
|
|
81
73
|
protected sync: boolean;
|
|
82
74
|
abstract connect(): Promise<void>;
|
|
83
75
|
abstract end(): Promise<void>;
|
|
@@ -85,7 +77,9 @@ export declare abstract class DB {
|
|
|
85
77
|
findTable(name: string): Table;
|
|
86
78
|
protected indexesEq(a: Index, b: Index): boolean;
|
|
87
79
|
syncDataBase(): Promise<void>;
|
|
88
|
-
protected syncLog(
|
|
80
|
+
protected syncLog(message: string): void;
|
|
81
|
+
abstract escape(value: Natural): string;
|
|
82
|
+
abstract save(tableName: string, attributes: Attribute<Natural, unknown>[]): (this: Record<string, Natural>) => Promise<boolean>;
|
|
89
83
|
abstract dropConstraints(table: Table): Promise<number[]>;
|
|
90
84
|
abstract dropFields(table: Table): Promise<void>;
|
|
91
85
|
abstract dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
|
@@ -95,4 +89,6 @@ export declare abstract class DB {
|
|
|
95
89
|
abstract syncSequence(table: Table): Promise<void>;
|
|
96
90
|
abstract syncTable(table: Table): Promise<void>;
|
|
97
91
|
}
|
|
92
|
+
export declare class Transaction {
|
|
93
|
+
}
|
|
98
94
|
export {};
|
package/{lib/db.js → db.js}
RENAMED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
/**/
|
|
3
|
+
exports.Transaction = exports.DB = exports.Table = exports.Attribute = exports.Type = exports.EntryBase = void 0;
|
|
5
4
|
class EntryBase {
|
|
5
|
+
constructor(from) {
|
|
6
|
+
if (from === "load")
|
|
7
|
+
this.preLoad();
|
|
8
|
+
else {
|
|
9
|
+
if (from)
|
|
10
|
+
Object.assign(this, from);
|
|
11
|
+
this.construct();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
construct() { }
|
|
15
|
+
postLoad() { }
|
|
16
|
+
postSave() { }
|
|
17
|
+
preLoad() { }
|
|
18
|
+
preSave() { }
|
|
6
19
|
async save() {
|
|
7
20
|
return false;
|
|
8
21
|
}
|
|
@@ -14,12 +27,6 @@ class Type {
|
|
|
14
27
|
}
|
|
15
28
|
}
|
|
16
29
|
exports.Type = Type;
|
|
17
|
-
class Meta extends Type {
|
|
18
|
-
constructor(from) {
|
|
19
|
-
super(from);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.Meta = Meta;
|
|
23
30
|
class Attribute extends Type {
|
|
24
31
|
constructor(from) {
|
|
25
32
|
super(from);
|
|
@@ -42,6 +49,7 @@ exports.Table = Table;
|
|
|
42
49
|
class DB {
|
|
43
50
|
constructor(log) {
|
|
44
51
|
this.tables = [];
|
|
52
|
+
this.sync = true;
|
|
45
53
|
this.log = log;
|
|
46
54
|
}
|
|
47
55
|
findTable(name) {
|
|
@@ -72,9 +80,11 @@ class DB {
|
|
|
72
80
|
await this.syncIndexes(table);
|
|
73
81
|
}
|
|
74
82
|
}
|
|
75
|
-
syncLog(
|
|
76
|
-
|
|
77
|
-
this.log(...args);
|
|
83
|
+
syncLog(message) {
|
|
84
|
+
this.log(this.sync ? message : "NOT SYNCING: " + message);
|
|
78
85
|
}
|
|
79
86
|
}
|
|
80
87
|
exports.DB = DB;
|
|
88
|
+
class Transaction {
|
|
89
|
+
}
|
|
90
|
+
exports.Transaction = Transaction;
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DB, EntryBase, ForeignKeyOptions,
|
|
2
|
-
export { EntryBase, ForeignKeyActions, ForeignKeyOptions, Natural, Type } from "./
|
|
1
|
+
import { Attribute, DB, EntryBase, ForeignKeyOptions, Natural, Type } from "./db";
|
|
2
|
+
export { EntryBase, ForeignKeyActions, ForeignKeyOptions, Natural, Type } from "./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;
|
|
@@ -16,13 +16,9 @@ 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 Methods<E> = {
|
|
20
|
-
[key: string]: (this: E) => unknown;
|
|
21
|
-
};
|
|
22
19
|
declare type Native__<T> = T extends Type<infer N, unknown> ? N : never;
|
|
23
20
|
declare type Native_<T> = T extends () => Type<infer N, infer E> ? Native__<Type<N, E>> : Native__<T>;
|
|
24
21
|
declare type Native<T> = T extends AttributeOptions<infer N, infer E> ? Native__<Type<N, E>> : Native_<T>;
|
|
25
|
-
declare type Parent<T> = T extends Meta<Natural, infer E> ? E : never;
|
|
26
22
|
export declare type IndexAttributes = string[] | string;
|
|
27
23
|
export interface IndexOptions {
|
|
28
24
|
attributes: IndexAttributes;
|
|
@@ -33,93 +29,89 @@ export declare type IndexDefinition = IndexAttributes | IndexOptions;
|
|
|
33
29
|
export declare type IndexesDefinition = {
|
|
34
30
|
[key: string]: IndexDefinition;
|
|
35
31
|
};
|
|
36
|
-
|
|
32
|
+
interface BaseModelOptions {
|
|
37
33
|
indexes?: IndexesDefinition;
|
|
38
|
-
init?: (this: T) => void;
|
|
39
|
-
int8id?: B;
|
|
40
34
|
sync?: boolean;
|
|
41
35
|
tableName?: string;
|
|
42
|
-
}
|
|
43
|
-
export
|
|
44
|
-
methods?: M;
|
|
45
|
-
parent?: P;
|
|
46
|
-
primaryKey?: K;
|
|
47
|
-
};
|
|
48
|
-
declare type ModelBaseOptions = {
|
|
49
|
-
indexes?: IndexesDefinition;
|
|
50
|
-
sync?: boolean;
|
|
51
|
-
tableName?: string;
|
|
52
|
-
};
|
|
53
|
-
export declare type ModelOptions2 = ModelBaseOptions & {
|
|
36
|
+
}
|
|
37
|
+
export interface ModelOptions extends BaseModelOptions {
|
|
54
38
|
int8id?: boolean;
|
|
55
|
-
parent?:
|
|
39
|
+
parent?: Attribute<Natural, EntryBase>;
|
|
56
40
|
primaryKey?: string;
|
|
41
|
+
}
|
|
42
|
+
declare type ConditionAttribute<N extends Natural> = N | [">" | "<" | ">=" | "<=" | "<>", N] | ["IN", ...N[]] | ["IS NULL"] | ["LIKE", string];
|
|
43
|
+
declare type ConditionBase<A extends AttributesDefinition> = string | {
|
|
44
|
+
[a in keyof A]?: ConditionAttribute<Native<A[a]>>;
|
|
57
45
|
};
|
|
46
|
+
declare type Condition<A extends AttributesDefinition> = ConditionBase<A> | ["NOT", Condition<A>] | ["AND", ...Condition<A>[]] | ["OR", ...Condition<A>[]];
|
|
58
47
|
declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
59
48
|
declare type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
|
|
60
|
-
declare type
|
|
61
|
-
|
|
62
|
-
id?: EntryIdNatural<B>;
|
|
63
|
-
};
|
|
49
|
+
declare type BaseKeyType<B extends boolean> = IsUnion<B> extends true ? number : B extends true ? string : number;
|
|
50
|
+
declare type KeyType<B extends boolean, P extends ModelStd> = P extends new () => EntryBase ? (P extends Attribute<infer T, EntryBase> ? T : never) : BaseKeyType<B>;
|
|
64
51
|
declare type ForeignKey<A> = A extends AttributeDefinition<Natural, infer E> ? () => Promise<E> : never;
|
|
65
|
-
declare type
|
|
66
|
-
[a in ForeignKeys<A> & string as `${a}Load`]?: ForeignKey<A[a]>;
|
|
67
|
-
};
|
|
68
|
-
declare type ModelBaseAttributes<A extends AttributesDefinition> = {
|
|
52
|
+
declare type EntryBaseAttributes<A extends AttributesDefinition> = {
|
|
69
53
|
[a in keyof A]?: Native<A[a]>;
|
|
70
54
|
};
|
|
71
|
-
declare type
|
|
72
|
-
declare type
|
|
73
|
-
[a in
|
|
55
|
+
declare type EntryMethodsBase<P extends ModelStd> = P extends new () => EntryBase ? P["methods"] : EntryBase;
|
|
56
|
+
declare type EntryMethodsFK<A extends AttributesDefinition> = {
|
|
57
|
+
[a in ForeignKeys<A> & string as `${a}Load`]: ForeignKey<A[a]>;
|
|
58
|
+
};
|
|
59
|
+
declare type EntryMethods<A extends AttributesDefinition, P extends ModelStd> = keyof EntryMethodsFK<A> extends never ? EntryMethodsBase<P> : EntryMethodsBase<P> & EntryMethodsFK<A>;
|
|
60
|
+
declare type ModelAttributesIf<A extends AttributesDefinition, T> = keyof A extends never ? T : T & A;
|
|
61
|
+
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"] : {
|
|
62
|
+
id: Type<BaseKeyType<B>, unknown>;
|
|
63
|
+
}>;
|
|
64
|
+
declare type ModelBase<N extends Natural, A extends AttributesDefinition, EA extends Record<string, Natural | undefined>, EM extends EntryBase, E extends EntryBase> = (new (from?: EA) => E) & Attribute<N, E> & {
|
|
65
|
+
attributes: A;
|
|
66
|
+
foreignKeys: Record<string, boolean>;
|
|
67
|
+
methods: EM;
|
|
68
|
+
parent?: ModelStd;
|
|
69
|
+
tableName: string;
|
|
70
|
+
load: (where?: Condition<A>) => Promise<E[]>;
|
|
74
71
|
} & {
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
72
|
+
[a in keyof A]: Attribute<Native<A[a]>, E>;
|
|
73
|
+
};
|
|
74
|
+
declare type Model<N extends Natural, A extends AttributesDefinition, EM extends EntryBase> = ModelBase<N, A, EntryBaseAttributes<A>, EM, EntryBaseAttributes<A> & EM>;
|
|
75
|
+
declare type ModelStd = Attribute<Natural, EntryBase> & {
|
|
76
|
+
attributes: AttributesDefinition;
|
|
77
|
+
foreignKeys: Record<string, boolean>;
|
|
78
|
+
methods: EntryBase;
|
|
79
|
+
parent?: ModelStd;
|
|
78
80
|
};
|
|
79
|
-
declare type ModelBase<E extends EntryBase> = new () => E;
|
|
80
81
|
export declare type Entry<M> = M extends new () => infer E ? E : never;
|
|
82
|
+
export declare type Where<M> = M extends {
|
|
83
|
+
load: (where: infer T) => void;
|
|
84
|
+
} ? Exclude<T, undefined> : never;
|
|
81
85
|
export interface SedentaryOptions {
|
|
86
|
+
autoSync?: boolean;
|
|
82
87
|
log?: ((message: string) => void) | null;
|
|
83
|
-
serverless?: boolean;
|
|
84
88
|
sync?: boolean;
|
|
85
89
|
}
|
|
86
90
|
export declare class Sedentary {
|
|
91
|
+
protected autoSync: boolean;
|
|
87
92
|
protected db: DB;
|
|
88
93
|
protected log: (...data: unknown[]) => void;
|
|
89
94
|
protected sync: boolean;
|
|
90
95
|
private models;
|
|
91
|
-
constructor(
|
|
96
|
+
constructor(options?: SedentaryOptions);
|
|
92
97
|
DATETIME(): Type<Date, unknown>;
|
|
93
|
-
FKEY<N extends Natural, E extends EntryBase>(attribute:
|
|
98
|
+
FKEY<N extends Natural, E extends EntryBase>(attribute: Attribute<N, E>, options?: ForeignKeyOptions): Type<N, E>;
|
|
94
99
|
INT(size?: number): Type<number, unknown>;
|
|
95
100
|
INT8(): Type<string, unknown>;
|
|
96
101
|
VARCHAR(size?: number): Type<string, unknown>;
|
|
102
|
+
checkDB(): void;
|
|
97
103
|
checkSize(size: number, message: string): number;
|
|
98
|
-
connect(): Promise<void>;
|
|
104
|
+
connect(sync?: boolean): Promise<void>;
|
|
99
105
|
end(): Promise<void>;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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> & {
|
|
106
|
+
escape(value: Natural): string;
|
|
107
|
+
model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EM extends EntryMethods<A, P>>(modelName: string, attributes: A, options?: BaseModelOptions & {
|
|
108
|
+
int8id?: B;
|
|
108
109
|
parent?: P;
|
|
109
|
-
primaryKey?: K;
|
|
110
|
-
}):
|
|
111
|
-
model<A extends AttributesDefinition, B extends boolean,
|
|
112
|
-
|
|
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> & {
|
|
110
|
+
primaryKey?: K | keyof A;
|
|
111
|
+
}): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM>;
|
|
112
|
+
model<A extends AttributesDefinition, B extends boolean, K extends string, P extends ModelStd, EA extends EntryBaseAttributes<ModelAttributes<A, B, K, P>>, EM extends EntryMethods<A, P>, M extends Record<string, <S extends M>(this: EA & EM & S, ...args: any[]) => void>>(modelName: string, attributes: A, options: BaseModelOptions & {
|
|
113
|
+
int8id?: B;
|
|
117
114
|
parent?: P;
|
|
118
|
-
primaryKey?: K;
|
|
119
|
-
}, methods: M & Record<
|
|
120
|
-
}
|
|
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>;
|
|
115
|
+
primaryKey?: K | keyof A;
|
|
116
|
+
}, methods: M & Record<keyof M, (this: EA & EM & M, ...args: any[]) => void>): Model<K extends keyof A ? Native<A[K]> : KeyType<B, P>, ModelAttributes<A, B, K, P>, EM & M>;
|
|
125
117
|
}
|
package/index.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const db_1 = require("./
|
|
5
|
-
|
|
6
|
-
const minidb_1 = require("./lib/minidb");
|
|
7
|
-
var db_2 = require("./lib/db");
|
|
3
|
+
exports.Sedentary = exports.Type = exports.EntryBase = void 0;
|
|
4
|
+
const db_1 = require("./db");
|
|
5
|
+
var db_2 = require("./db");
|
|
8
6
|
Object.defineProperty(exports, "EntryBase", { enumerable: true, get: function () { return db_2.EntryBase; } });
|
|
9
7
|
Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return db_2.Type; } });
|
|
10
|
-
const allowedOption = ["indexes", "
|
|
8
|
+
const allowedOption = ["indexes", "int8id", "parent", "primaryKey", "sync", "tableName"];
|
|
11
9
|
const reservedNames = [
|
|
12
|
-
...["attributeName", "base", "class", "constructor", "defaultValue", "entry", "fieldName", "
|
|
13
|
-
...["
|
|
10
|
+
...["attributeName", "attributes", "base", "class", "construct", "constructor", "defaultValue", "entry", "fieldName", "foreignKeys", "load"],
|
|
11
|
+
...["loaded", "methods", "name", "postLoad", "postSave", "preLoad", "preSave", "primaryKey", "prototype", "save", "size", "tableName", "tx", "type"]
|
|
14
12
|
];
|
|
15
13
|
class Sedentary {
|
|
16
|
-
constructor(
|
|
14
|
+
constructor(options) {
|
|
17
15
|
this.sync = true;
|
|
18
16
|
this.models = {};
|
|
19
|
-
if (typeof filename !== "string")
|
|
20
|
-
throw new Error("new Sedentary: 'filename' argument: Wrong type, expected 'string'");
|
|
21
17
|
if (!options)
|
|
22
18
|
options = {};
|
|
23
19
|
if (!(options instanceof Object))
|
|
24
20
|
throw new Error("new Sedentary: 'options' argument: Wrong type, expected 'Object'");
|
|
25
21
|
for (const k in options)
|
|
26
|
-
if (!["log", "sync"].includes(k))
|
|
22
|
+
if (!["autoSync", "log", "sync"].includes(k))
|
|
27
23
|
throw new Error(`new Sedentary: 'options' argument: Unknown '${k}' option`);
|
|
28
|
-
const { log, sync } = Object.assign({ sync: true }, options);
|
|
24
|
+
const { autoSync, log, sync } = Object.assign({ autoSync: true, sync: true }, options);
|
|
25
|
+
if (typeof autoSync !== "boolean")
|
|
26
|
+
throw new Error("new Sedentary: 'autoSync' option: Wrong type, expected 'boolean'");
|
|
29
27
|
if (log !== null && log !== undefined && !(log instanceof Function))
|
|
30
28
|
throw new Error("new Sedentary: 'log' option: Wrong type, expected 'null' or 'Function'");
|
|
31
29
|
if (typeof sync !== "boolean")
|
|
32
30
|
throw new Error("new Sedentary: 'sync' option: Wrong type, expected 'boolean'");
|
|
33
|
-
this.
|
|
34
|
-
this.db =
|
|
31
|
+
this.autoSync = autoSync;
|
|
32
|
+
this.db = null;
|
|
33
|
+
// eslint-disable-next-line no-console
|
|
34
|
+
this.log = log ? log : log === null ? () => { } : console.log;
|
|
35
35
|
this.sync = sync;
|
|
36
36
|
}
|
|
37
37
|
DATETIME() {
|
|
@@ -56,6 +56,10 @@ 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
|
+
checkDB() {
|
|
60
|
+
if (!this.db)
|
|
61
|
+
throw new Error("Package sedentary can't be used directly. Please check: https://www.npmjs.com/package/sedentary#disclaimer");
|
|
62
|
+
}
|
|
59
63
|
checkSize(size, message) {
|
|
60
64
|
const str = size.toString();
|
|
61
65
|
const parsed = parseInt(str, 10);
|
|
@@ -63,16 +67,20 @@ class Sedentary {
|
|
|
63
67
|
throw new Error(message);
|
|
64
68
|
return parsed;
|
|
65
69
|
}
|
|
66
|
-
async connect() {
|
|
70
|
+
async connect(sync) {
|
|
67
71
|
try {
|
|
72
|
+
this.checkDB();
|
|
68
73
|
this.log("Connecting...");
|
|
69
74
|
await this.db.connect();
|
|
70
|
-
this.log("Connected
|
|
71
|
-
|
|
72
|
-
|
|
75
|
+
this.log("Connected");
|
|
76
|
+
if (this.autoSync || sync) {
|
|
77
|
+
this.log("Syncing...");
|
|
78
|
+
await this.db.syncDataBase();
|
|
79
|
+
this.log("Synced");
|
|
80
|
+
}
|
|
73
81
|
}
|
|
74
82
|
catch (e) {
|
|
75
|
-
this.log("Connecting:"
|
|
83
|
+
this.log("Connecting: " + (e instanceof Error ? e.message : JSON.stringify(e)));
|
|
76
84
|
throw e;
|
|
77
85
|
}
|
|
78
86
|
}
|
|
@@ -81,16 +89,12 @@ class Sedentary {
|
|
|
81
89
|
await this.db.end();
|
|
82
90
|
this.log("Connection closed");
|
|
83
91
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this.save();
|
|
87
|
-
};
|
|
88
|
-
Object.defineProperty(ret, "name", { value: name });
|
|
89
|
-
if (methods)
|
|
90
|
-
Object.assign(ret.prototype, methods);
|
|
91
|
-
return ret;
|
|
92
|
+
escape(value) {
|
|
93
|
+
return this.db.escape(value);
|
|
92
94
|
}
|
|
95
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
93
96
|
model(modelName, attributes, options, methods) {
|
|
97
|
+
this.checkDB();
|
|
94
98
|
if (typeof modelName !== "string")
|
|
95
99
|
throw new Error("Sedentary.model: 'name' argument: Wrong type, expected 'string'");
|
|
96
100
|
if (this.models[modelName])
|
|
@@ -120,19 +124,13 @@ class Sedentary {
|
|
|
120
124
|
let constraints = [{ attribute: aarray[0], constraintName: `${tableName}_id_unique`, type: "u" }];
|
|
121
125
|
const iarray = [];
|
|
122
126
|
const pk = aarray[0];
|
|
123
|
-
if (
|
|
127
|
+
if (!methods)
|
|
128
|
+
methods = {};
|
|
129
|
+
if (!(methods instanceof Object))
|
|
124
130
|
throw new Error(`Sedentary.model: '${modelName}' model: 'methods' option: Wrong type, expected 'Object'`);
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
try {
|
|
128
|
-
if (!parent.isModel())
|
|
129
|
-
throw new Error();
|
|
130
|
-
}
|
|
131
|
-
catch (e) {
|
|
131
|
+
if (parent)
|
|
132
|
+
if (!parent.attributes)
|
|
132
133
|
throw new Error(`Sedentary.model: '${modelName}' model: 'parent' option: Wrong type, expected 'Model'`);
|
|
133
|
-
}
|
|
134
|
-
methods = (methods ? Object.assign(Object.assign({}, (parent.methods || {})), methods) : parent.methods);
|
|
135
|
-
}
|
|
136
134
|
if (primaryKey && typeof primaryKey !== "string")
|
|
137
135
|
throw new Error(`Sedentary.model: '${modelName}' model: 'primaryKey' option: Wrong type, expected 'string'`);
|
|
138
136
|
if (primaryKey && !Object.keys(attributes).includes(primaryKey))
|
|
@@ -191,21 +189,21 @@ class Sedentary {
|
|
|
191
189
|
return ret;
|
|
192
190
|
})();
|
|
193
191
|
if (foreignKey) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (!(foreignKey.options instanceof Object))
|
|
192
|
+
const options = foreignKey.options || {};
|
|
193
|
+
if (foreignKey.options !== undefined && !(foreignKey.options instanceof Object))
|
|
197
194
|
throw new Error(`Sedentary.FKEY: '${modelName}' model: '${attributeName}' attribute: Wrong options type, expected 'Object'`);
|
|
198
|
-
for (const k in
|
|
195
|
+
for (const k in options)
|
|
199
196
|
if (!["onDelete", "onUpdate"].includes(k))
|
|
200
197
|
throw new Error(`Sedentary.FKEY: '${modelName}' model: '${attributeName}' attribute: Unknown option '${k}'`);
|
|
201
198
|
for (const onChange of ["onDelete", "onUpdate"]) {
|
|
202
199
|
const actions = ["cascade", "no action", "restrict", "set default", "set null"];
|
|
203
|
-
let action =
|
|
200
|
+
let action = options[onChange];
|
|
204
201
|
if (!action)
|
|
205
|
-
action =
|
|
202
|
+
action = options[onChange] = "no action";
|
|
206
203
|
if (action && !actions.includes(action))
|
|
207
204
|
throw new Error(`Sedentary.FKEY: '${modelName}' model: '${attributeName}' attribute: '${onChange}' option: Wrong value, expected ${actions.map(_ => `'${_}'`).join(" | ")}`);
|
|
208
205
|
}
|
|
206
|
+
foreignKey.options = options;
|
|
209
207
|
}
|
|
210
208
|
if (primaryKey === attributeName) {
|
|
211
209
|
notNull = true;
|
|
@@ -274,208 +272,72 @@ class Sedentary {
|
|
|
274
272
|
}
|
|
275
273
|
this.db.tables.push(new db_1.Table({ autoIncrement, constraints, attributes: aarray, indexes: iarray, parent, sync, tableName }));
|
|
276
274
|
this.models[modelName] = true;
|
|
277
|
-
const
|
|
278
|
-
? options.init
|
|
279
|
-
? function () {
|
|
280
|
-
parent.init.call(this);
|
|
281
|
-
options.init.call(this);
|
|
282
|
-
}
|
|
283
|
-
: parent.init
|
|
284
|
-
: options.init;
|
|
285
|
-
class Class {
|
|
286
|
-
constructor() {
|
|
287
|
-
if (init)
|
|
288
|
-
init.call(this);
|
|
289
|
-
}
|
|
290
|
-
save() {
|
|
291
|
-
return new Promise((resolve, reject) => {
|
|
292
|
-
const save = () => reject(new Error("eh no"));
|
|
293
|
-
Object.defineProperty(save, "name", { value: modelName + ".save" });
|
|
294
|
-
setTimeout(save, 10);
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
const load = (boh) => new Promise((resolve, reject) => setTimeout(() => {
|
|
299
|
-
if (boh)
|
|
300
|
-
return resolve([new Class()]);
|
|
301
|
-
reject(new Error("boh"));
|
|
302
|
-
}, 10));
|
|
303
|
-
Object.defineProperty(load, "name", { value: modelName + ".load" });
|
|
304
|
-
const metaAttributes = aarray.reduce((ret, curr) => {
|
|
305
|
-
ret[curr.attributeName] = curr;
|
|
306
|
-
return ret;
|
|
307
|
-
}, {});
|
|
308
|
-
const metaForeignKeys = aarray
|
|
275
|
+
const foreignKeys = aarray
|
|
309
276
|
.filter(_ => _.foreignKey)
|
|
310
277
|
.reduce((ret, curr) => {
|
|
311
|
-
ret[curr.attributeName] =
|
|
278
|
+
ret[curr.attributeName] = true;
|
|
312
279
|
return ret;
|
|
313
280
|
}, {});
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
if (foreignKey + "Load" in metaAttributes)
|
|
281
|
+
for (const foreignKey in foreignKeys) {
|
|
282
|
+
if (foreignKey + "Load" in attributes)
|
|
317
283
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute`);
|
|
318
|
-
if (
|
|
284
|
+
if (foreignKey + "Load" in methods)
|
|
319
285
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method`);
|
|
320
286
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute`);
|
|
287
|
+
for (const method in methods)
|
|
288
|
+
if (method in attributes)
|
|
289
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute`);
|
|
325
290
|
const checkParent = (parent) => {
|
|
326
291
|
if (!parent)
|
|
327
292
|
return;
|
|
328
|
-
for (const attribute in
|
|
293
|
+
for (const attribute in attributes) {
|
|
329
294
|
if (attribute in parent.attributes)
|
|
330
295
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an attribute of '${parent.modelName}' model`);
|
|
331
|
-
if (
|
|
296
|
+
if (attribute in parent.methods)
|
|
332
297
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with a method of '${parent.modelName}' model`);
|
|
333
298
|
for (const foreignKey in parent.foreignKeys)
|
|
334
299
|
if (attribute === foreignKey + "Load")
|
|
335
300
|
throw new Error(`Sedentary.model: '${modelName}' model: '${attribute}' attribute: conflicts with an inferred methods of '${parent.modelName}' model`);
|
|
336
301
|
}
|
|
337
|
-
for (const foreignKey in
|
|
302
|
+
for (const foreignKey in foreignKeys) {
|
|
338
303
|
if (foreignKey + "Load" in parent.attributes)
|
|
339
304
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with an attribute of '${parent.modelName}' model`);
|
|
340
|
-
if (
|
|
305
|
+
if (foreignKey + "Load" in parent.methods)
|
|
341
306
|
throw new Error(`Sedentary.model: '${modelName}' model: '${foreignKey}' attribute: '${foreignKey}Load' inferred methods conflicts with a method of '${parent.modelName}' model`);
|
|
342
307
|
}
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with a method of '${parent.modelName}' model`);
|
|
352
|
-
}
|
|
308
|
+
for (const method in methods) {
|
|
309
|
+
if (method in parent.attributes)
|
|
310
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an attribute of '${parent.modelName}' model`);
|
|
311
|
+
for (const foreignKey in parent.foreignKeys)
|
|
312
|
+
if (foreignKey + "Load" === method)
|
|
313
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with an inferred methods of '${parent.modelName}' model`);
|
|
314
|
+
if (method in parent.methods)
|
|
315
|
+
throw new Error(`Sedentary.model: '${modelName}' model: '${method}' method: conflicts with a method of '${parent.modelName}' model`);
|
|
353
316
|
}
|
|
354
317
|
checkParent(parent.parent);
|
|
355
318
|
};
|
|
356
319
|
checkParent(parent);
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
Object.defineProperty(Class, attribute.attributeName, { value: attribute });
|
|
366
|
-
for (const key of ["attributeName", "base", "fieldName", "modelName", "size", "type", "unique"])
|
|
367
|
-
Object.defineProperty(Class, key, { value: pk[key] });
|
|
368
|
-
return Class;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
exports.Sedentary = Sedentary;
|
|
372
|
-
exports.Package = Sedentary;
|
|
373
|
-
const db = new Sedentary("gino");
|
|
374
|
-
const Users = db.model("User", { foo: db.INT(), bar: { type: db.VARCHAR(), unique: true } }, {});
|
|
375
|
-
class Item extends db.model("Item", {
|
|
376
|
-
num: db.FKEY(Users),
|
|
377
|
-
str: db.VARCHAR()
|
|
378
|
-
}, {
|
|
379
|
-
init: function () {
|
|
380
|
-
this.num = "0";
|
|
381
|
-
this.str = "0";
|
|
382
|
-
},
|
|
383
|
-
int8id: true
|
|
384
|
-
/*
|
|
385
|
-
methods: {
|
|
386
|
-
prova: (): string => "ok"
|
|
387
|
-
}
|
|
388
|
-
*/
|
|
389
|
-
}) {
|
|
390
|
-
}
|
|
391
|
-
class Super extends db.model("Super", {
|
|
392
|
-
a: db.INT,
|
|
393
|
-
n: db.FKEY(Item),
|
|
394
|
-
s: db.FKEY(Users.bar)
|
|
395
|
-
}, {
|
|
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();
|
|
405
|
-
}
|
|
406
|
-
*/
|
|
407
|
-
}) {
|
|
408
|
-
}
|
|
409
|
-
class Next extends db.model("Next", { a: db.INT, b: db.INT }, {
|
|
410
|
-
init: function () {
|
|
411
|
-
this.a = 23;
|
|
412
|
-
},
|
|
413
|
-
primaryKey: "a"
|
|
414
|
-
}) {
|
|
415
|
-
}
|
|
416
|
-
class Current extends db.model("Current", { b: { type: db.FKEY(Next), unique: true } }, {
|
|
417
|
-
init: function () {
|
|
418
|
-
this.b = 24;
|
|
419
|
-
}
|
|
420
|
-
}) {
|
|
421
|
-
}
|
|
422
|
-
class Last extends db.model("Last", { c: db.FKEY(Current.b) }, {
|
|
423
|
-
init: function () {
|
|
424
|
-
this.c = 24;
|
|
425
|
-
},
|
|
426
|
-
parent: Next
|
|
427
|
-
}) {
|
|
428
|
-
}
|
|
429
|
-
(async function () {
|
|
430
|
-
const item = new Super();
|
|
431
|
-
try {
|
|
432
|
-
await item.save();
|
|
433
|
-
}
|
|
434
|
-
catch (e) {
|
|
435
|
-
console.log(Item.load, item.save, await Item.load(true), item, e.message);
|
|
436
|
-
//console.log(new Next(), Next.load, await Next.load(true), new Last(), item.prova());
|
|
437
|
-
}
|
|
438
|
-
return true;
|
|
439
|
-
})();
|
|
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 () { };
|
|
320
|
+
const ret = class extends (parent || db_1.EntryBase) {
|
|
321
|
+
};
|
|
322
|
+
const load = (boh) => new Promise((resolve, reject) => setTimeout(() => {
|
|
323
|
+
if (boh)
|
|
324
|
+
return resolve([new ret()]);
|
|
325
|
+
reject(new Error("boh"));
|
|
326
|
+
}, 10));
|
|
327
|
+
Object.defineProperty(load, "name", { value: modelName + ".load" });
|
|
466
328
|
Object.defineProperty(ret, "name", { value: modelName });
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
329
|
+
Object.defineProperty(ret, "load", { value: load });
|
|
330
|
+
Object.defineProperty(ret, "attributes", { value: attributes });
|
|
331
|
+
Object.defineProperty(ret, "foreignKeys", { value: foreignKeys });
|
|
332
|
+
Object.defineProperty(ret, "methods", { value: methods });
|
|
333
|
+
Object.assign(ret.prototype, methods);
|
|
334
|
+
ret.prototype.save = this.db.save(tableName, aarray);
|
|
335
|
+
Object.defineProperty(ret.prototype.save, "name", { value: modelName + ".save" });
|
|
336
|
+
for (const attribute of aarray)
|
|
337
|
+
Object.defineProperty(ret, attribute.attributeName, { value: attribute });
|
|
338
|
+
for (const key of ["attributeName", "base", "fieldName", "modelName", "size", "tableName", "type", "unique"])
|
|
339
|
+
Object.defineProperty(ret, key, { value: pk[key] });
|
|
470
340
|
return ret;
|
|
471
341
|
}
|
|
472
342
|
}
|
|
473
|
-
exports.
|
|
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
|
-
});
|
|
343
|
+
exports.Sedentary = Sedentary;
|
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Daniele Ricci <daniele.icc@gmail.com> (https://github.com/iccicci)",
|
|
3
3
|
"bugs": "https://github.com/iccicci/sedentary/issues",
|
|
4
|
+
"contributors": [
|
|
5
|
+
"Daniele Ricci <daniele.icc@gmail.com> (https://github.com/iccicci)",
|
|
6
|
+
"yossarian <sergiybiluk@gmail.com> (https://github.com/captain-yossarian)"
|
|
7
|
+
],
|
|
4
8
|
"dependencies": {},
|
|
5
9
|
"description": "The ORM which never needs to migrate",
|
|
6
10
|
"devDependencies": {
|
|
7
11
|
"@types/mocha": "9.0.0",
|
|
8
|
-
"@types/node": "17.0.
|
|
12
|
+
"@types/node": "17.0.5",
|
|
9
13
|
"@types/yamljs": "0.2.31",
|
|
10
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
11
|
-
"@typescript-eslint/parser": "5.
|
|
14
|
+
"@typescript-eslint/eslint-plugin": "5.8.1",
|
|
15
|
+
"@typescript-eslint/parser": "5.8.1",
|
|
12
16
|
"eslint": "8.5.0",
|
|
13
17
|
"mocha": "9.1.3",
|
|
14
18
|
"nyc": "15.1.0",
|
|
@@ -58,9 +62,21 @@
|
|
|
58
62
|
},
|
|
59
63
|
"tsd": {
|
|
60
64
|
"compilerOptions": {
|
|
61
|
-
"
|
|
65
|
+
"alwaysStrict": true,
|
|
66
|
+
"declaration": true,
|
|
67
|
+
"esModuleInterop": true,
|
|
68
|
+
"module": "commonjs",
|
|
69
|
+
"noImplicitAny": true,
|
|
70
|
+
"noImplicitReturns": true,
|
|
71
|
+
"noImplicitThis": true,
|
|
72
|
+
"strict": true,
|
|
73
|
+
"strictBindCallApply": true,
|
|
74
|
+
"strictFunctionTypes": true,
|
|
75
|
+
"strictNullChecks": true,
|
|
76
|
+
"strictPropertyInitialization": true,
|
|
77
|
+
"target": "es2017"
|
|
62
78
|
}
|
|
63
79
|
},
|
|
64
80
|
"types": "index.d.ts",
|
|
65
|
-
"version": "0.0.
|
|
81
|
+
"version": "0.0.23"
|
|
66
82
|
}
|
package/lib/log.d.ts
DELETED
package/lib/log.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createLogger = void 0;
|
|
4
|
-
const console_1 = require("console");
|
|
5
|
-
const stream_1 = require("stream");
|
|
6
|
-
class Logger extends stream_1.Writable {
|
|
7
|
-
constructor(log) {
|
|
8
|
-
super();
|
|
9
|
-
this.log = log;
|
|
10
|
-
}
|
|
11
|
-
_write(chunk, encoding, callback) {
|
|
12
|
-
this.log(chunk.toString());
|
|
13
|
-
callback();
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function createLogger(log) {
|
|
17
|
-
// eslint-disable-next-line no-console
|
|
18
|
-
return log ? new console_1.Console(new Logger(log)).log : log === null ? () => { } : console.log;
|
|
19
|
-
}
|
|
20
|
-
exports.createLogger = createLogger;
|
package/lib/log.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { Console } from "console";
|
|
2
|
-
import { Writable } from "stream";
|
|
3
|
-
|
|
4
|
-
class Logger extends Writable {
|
|
5
|
-
private log: (message: string) => void;
|
|
6
|
-
|
|
7
|
-
constructor(log: (message: string) => void) {
|
|
8
|
-
super();
|
|
9
|
-
|
|
10
|
-
this.log = log;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
_write(chunk: Buffer, encoding: string, callback: () => void) {
|
|
14
|
-
this.log(chunk.toString());
|
|
15
|
-
callback();
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function createLogger(log: ((message: string) => void) | null | undefined) {
|
|
20
|
-
// eslint-disable-next-line no-console
|
|
21
|
-
return log ? new Console(new Logger(log)).log : log === null ? () => {} : console.log;
|
|
22
|
-
}
|
package/lib/minidb.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { DB, Table } from "./db";
|
|
2
|
-
export declare class MiniDB extends DB {
|
|
3
|
-
private body;
|
|
4
|
-
private file;
|
|
5
|
-
constructor(filename: string, log: (message: string) => void);
|
|
6
|
-
connect(): Promise<void>;
|
|
7
|
-
dropConstraints(table: Table): Promise<number[]>;
|
|
8
|
-
dropFields(table: Table): Promise<void>;
|
|
9
|
-
dropIndexes(table: Table, constraintIndexes: number[]): Promise<void>;
|
|
10
|
-
end(): Promise<void>;
|
|
11
|
-
save(): Promise<void>;
|
|
12
|
-
syncConstraints(table: Table): Promise<void>;
|
|
13
|
-
syncIndexes(table: Table): Promise<void>;
|
|
14
|
-
syncFields(table: Table): Promise<void>;
|
|
15
|
-
syncSequence(table: Table): Promise<void>;
|
|
16
|
-
syncTable(table: Table): Promise<void>;
|
|
17
|
-
}
|
package/lib/minidb.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.MiniDB = void 0;
|
|
5
|
-
const db_1 = require("./db");
|
|
6
|
-
const fs_1 = require("fs");
|
|
7
|
-
const { readFile, writeFile } = fs_1.promises;
|
|
8
|
-
class MiniDB extends db_1.DB {
|
|
9
|
-
constructor(filename, log) {
|
|
10
|
-
super(log);
|
|
11
|
-
this.file = filename;
|
|
12
|
-
}
|
|
13
|
-
async connect() {
|
|
14
|
-
this.body = { next: {}, tables: {} };
|
|
15
|
-
try {
|
|
16
|
-
this.body = JSON.parse((await readFile(this.file)).toString());
|
|
17
|
-
}
|
|
18
|
-
catch (e) {
|
|
19
|
-
if (e.code !== "ENOENT")
|
|
20
|
-
throw e;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
async dropConstraints(table) {
|
|
24
|
-
const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
|
|
25
|
-
for (const constraint of Object.keys(constraints.f).sort()) {
|
|
26
|
-
const arr = table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "f");
|
|
27
|
-
const dbOptions = arr.length ? arr[0].attribute.foreignKey.options : { onDelete: "delete", onUpdate: "delete" };
|
|
28
|
-
const inOptions = constraints.f[constraint].options;
|
|
29
|
-
if (dbOptions.onDelete !== inOptions.onDelete || dbOptions.onUpdate !== inOptions.onUpdate) {
|
|
30
|
-
this.syncLog(`'${table.tableName}': Removing foreign key: '${constraint}'`);
|
|
31
|
-
if (this.sync)
|
|
32
|
-
delete constraints.f[constraint];
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
for (const constraint of Object.keys(constraints.u).sort()) {
|
|
36
|
-
if (!table.constraints.filter(({ constraintName, type }) => constraintName === constraint && type === "u").length) {
|
|
37
|
-
this.syncLog(`'${table.tableName}': Removing unique constraint from field: '${constraints.u[constraint].on}'`);
|
|
38
|
-
if (this.sync)
|
|
39
|
-
delete constraints.u[constraint];
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
await this.save();
|
|
43
|
-
return [];
|
|
44
|
-
}
|
|
45
|
-
async dropFields(table) {
|
|
46
|
-
const { fields } = this.body.tables[table.tableName] || { fields: {} };
|
|
47
|
-
for (const attribute of Object.keys(fields).sort()) {
|
|
48
|
-
if (!table.findField(attribute)) {
|
|
49
|
-
this.syncLog(`'${table.tableName}': Removing field: '${attribute}'`);
|
|
50
|
-
if (this.sync)
|
|
51
|
-
delete fields[attribute];
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
await this.save();
|
|
55
|
-
}
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
57
|
-
async dropIndexes(table, constraintIndexes) {
|
|
58
|
-
const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
|
|
59
|
-
for (const name of Object.keys(indexes).sort()) {
|
|
60
|
-
const index = table.indexes.filter(_ => _.indexName === name);
|
|
61
|
-
if (index.length === 0 || !this.indexesEq(indexes[name], index[0])) {
|
|
62
|
-
this.syncLog(`'${table.tableName}': Removing index: '${name}'`);
|
|
63
|
-
if (this.sync)
|
|
64
|
-
delete indexes[name];
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
await this.save();
|
|
68
|
-
}
|
|
69
|
-
async end() { }
|
|
70
|
-
async save() {
|
|
71
|
-
await writeFile(this.file, JSON.stringify(this.body));
|
|
72
|
-
}
|
|
73
|
-
async syncConstraints(table) {
|
|
74
|
-
const { constraints } = this.body.tables[table.tableName] || { constraints: { f: {}, u: {} } };
|
|
75
|
-
for (const constraint of table.constraints) {
|
|
76
|
-
const { attribute, constraintName, type } = constraint;
|
|
77
|
-
if (!constraints[type][constraintName]) {
|
|
78
|
-
if (type === "f") {
|
|
79
|
-
const { fieldName, options, tableName } = attribute.foreignKey;
|
|
80
|
-
const onDelete = options.onDelete !== "no action" ? ` on delete ${options.onDelete}` : "";
|
|
81
|
-
const onUpdate = options.onUpdate !== "no action" ? ` on update ${options.onUpdate}` : "";
|
|
82
|
-
this.syncLog(`'${table.tableName}': Adding foreign key '${constraint.constraintName}' on field: '${attribute.fieldName}' references '${tableName}(${fieldName})'${onDelete}${onUpdate}`);
|
|
83
|
-
if (this.sync)
|
|
84
|
-
constraints[type][constraintName] = { on: attribute.fieldName, options, fieldName, tableName };
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
this.syncLog(`'${table.tableName}': Adding unique constraint on field: '${attribute.fieldName}'`);
|
|
88
|
-
if (this.sync)
|
|
89
|
-
constraints[type][constraintName] = { on: attribute.fieldName };
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
await this.save();
|
|
94
|
-
}
|
|
95
|
-
async syncIndexes(table) {
|
|
96
|
-
const { indexes } = this.body.tables[table.tableName] || { indexes: {} };
|
|
97
|
-
for (const index of table.indexes) {
|
|
98
|
-
const { indexName } = index;
|
|
99
|
-
if (!(indexName in indexes)) {
|
|
100
|
-
this.syncLog(`'${table.tableName}': Adding index: '${indexName}' on (${index.fields.map(_ => `'${_}'`).join(", ")}) type '${index.type}'${index.unique ? " unique" : ""}`);
|
|
101
|
-
if (this.sync)
|
|
102
|
-
indexes[indexName] = index;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
await this.save();
|
|
106
|
-
}
|
|
107
|
-
async syncFields(table) {
|
|
108
|
-
for (const attribute of table.attributes) {
|
|
109
|
-
const { fields } = this.body.tables[table.tableName] || { fields: {} };
|
|
110
|
-
const { defaultValue, fieldName, notNull, size, type } = attribute;
|
|
111
|
-
let field = fields[fieldName];
|
|
112
|
-
if (!field) {
|
|
113
|
-
this.syncLog(`'${table.tableName}': Adding field: '${fieldName}' '${type}' '${size || ""}'`);
|
|
114
|
-
if (this.sync)
|
|
115
|
-
field = fields[fieldName] = { size, type };
|
|
116
|
-
else
|
|
117
|
-
field = {};
|
|
118
|
-
}
|
|
119
|
-
if (field.size !== size || field.type !== type) {
|
|
120
|
-
this.syncLog(`'${table.tableName}': Changing field type: '${fieldName}' '${type}' '${size || ""}'`);
|
|
121
|
-
if (this.sync)
|
|
122
|
-
field = fields[fieldName] = Object.assign(Object.assign({}, field), { size, type });
|
|
123
|
-
}
|
|
124
|
-
if (field.default) {
|
|
125
|
-
if (!defaultValue) {
|
|
126
|
-
this.syncLog(`'${table.tableName}': Dropping default value for field: '${fieldName}'`);
|
|
127
|
-
if (this.sync)
|
|
128
|
-
delete field.default;
|
|
129
|
-
}
|
|
130
|
-
else if (field.default !== defaultValue) {
|
|
131
|
-
this.syncLog(`'${table.tableName}': Changing default value to '${defaultValue}' for field: '${fieldName}'`);
|
|
132
|
-
if (this.sync)
|
|
133
|
-
field.default = defaultValue;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
else if (defaultValue) {
|
|
137
|
-
this.syncLog(`'${table.tableName}': Setting default value '${defaultValue instanceof Date ? defaultValue.toISOString() : defaultValue}' for field: '${fieldName}'`);
|
|
138
|
-
if (this.sync)
|
|
139
|
-
field.default = defaultValue;
|
|
140
|
-
}
|
|
141
|
-
if (field.notNull) {
|
|
142
|
-
if (!notNull) {
|
|
143
|
-
this.syncLog(`'${table.tableName}': Dropping not null for field: '${fieldName}'`);
|
|
144
|
-
if (this.sync)
|
|
145
|
-
delete field.notNull;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
else if (notNull) {
|
|
149
|
-
this.syncLog(`'${table.tableName}': Setting not null for field: '${fieldName}'`);
|
|
150
|
-
if (this.sync)
|
|
151
|
-
field.notNull = true;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
await this.save();
|
|
155
|
-
}
|
|
156
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
157
|
-
async syncSequence(table) { }
|
|
158
|
-
async syncTable(table) {
|
|
159
|
-
if (this.body.tables[table.tableName]) {
|
|
160
|
-
(() => {
|
|
161
|
-
if (table.parent) {
|
|
162
|
-
if (this.body.tables[table.tableName].parent === table.parent.tableName)
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
else if (!this.body.tables[table.tableName].parent)
|
|
166
|
-
return;
|
|
167
|
-
this.syncLog(`Removing table: '${table.tableName}'`);
|
|
168
|
-
if (this.sync)
|
|
169
|
-
delete this.body.tables[table.tableName];
|
|
170
|
-
})();
|
|
171
|
-
}
|
|
172
|
-
if (!this.body.tables[table.tableName]) {
|
|
173
|
-
this.syncLog(`Adding table: '${table.tableName}'`);
|
|
174
|
-
if (this.sync)
|
|
175
|
-
this.body.tables[table.tableName] = { constraints: { f: {}, u: {} }, fields: {}, indexes: {} };
|
|
176
|
-
if (table.parent) {
|
|
177
|
-
this.syncLog(`Setting parent: '${table.parent.tableName}' - to table: '${table.tableName}'`);
|
|
178
|
-
if (this.sync)
|
|
179
|
-
this.body.tables[table.tableName].parent = table.parent.tableName;
|
|
180
|
-
}
|
|
181
|
-
if (table.autoIncrement && !this.body.next[table.tableName]) {
|
|
182
|
-
this.syncLog(`Setting auto increment: '${table.tableName}'`);
|
|
183
|
-
if (this.sync)
|
|
184
|
-
this.body.next[table.tableName] = 1;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
await this.save();
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
exports.MiniDB = MiniDB;
|
package/requirements.txt
DELETED