@samet-it/be-db-common 1.1.4 → 1.1.7
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/connection/db.connection.d.ts +18 -26
- package/dist/connection/db.connection.js +24 -78
- package/dist/connection/index.types.d.ts +35 -177
- package/dist/error/db.error.d.ts +1 -2
- package/dist/error/index.d.ts +0 -1
- package/dist/error/index.js +0 -1
- package/dist/repo/db.repo.d.ts +100 -19
- package/dist/repo/db.repo.js +354 -196
- package/dist/repo/index.types.d.ts +317 -43
- package/package.json +5 -4
- package/dist/error/index.types.d.ts +0 -1
- package/dist/error/index.types.js +0 -2
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { DbConnectionLike,
|
|
2
|
-
import {
|
|
3
|
-
import type { DbLines } from "../line";
|
|
4
|
-
import type { Opt } from "@leyyo/common";
|
|
1
|
+
import type { DbConnectionLike, DbConnOpt, DbOnAfter, DbConnProps, DbEvent } from "./index.types";
|
|
2
|
+
import type { Logger } from "@leyyo/common";
|
|
5
3
|
/**
|
|
6
4
|
* DB connection abstract class
|
|
5
|
+
*
|
|
6
|
+
* Generics:
|
|
7
|
+
* - 0-`OPT`: db execution options {@link DbExecOpt}
|
|
7
8
|
* */
|
|
8
|
-
export declare abstract class DbConnection
|
|
9
|
+
export declare abstract class DbConnection implements DbConnectionLike {
|
|
9
10
|
/**
|
|
10
11
|
* Logger
|
|
11
12
|
* */
|
|
12
|
-
protected logger:
|
|
13
|
+
protected logger: Logger;
|
|
13
14
|
/**
|
|
14
15
|
* On connected callbacks
|
|
15
16
|
* */
|
|
@@ -54,36 +55,27 @@ export declare abstract class DbConnection<LINK extends DbRepoLink, META extends
|
|
|
54
55
|
* @param {boolean} throwable - throwable? yes: throw the error
|
|
55
56
|
* */
|
|
56
57
|
protected _triggerOnCase(name: DbEvent, items: Array<DbOnAfter>, throwable: boolean): void;
|
|
58
|
+
/**
|
|
59
|
+
* Generate next delay time with exponential & randomized manner
|
|
60
|
+
*
|
|
61
|
+
* @param {number} count - try count
|
|
62
|
+
* @param {number} baseDelay - starting delay interval
|
|
63
|
+
* @param {number} maxDelay - maximum delay interval
|
|
64
|
+
* @return {number}
|
|
65
|
+
* */
|
|
66
|
+
protected _delayWithJitter(count: number, baseDelay: number, maxDelay: number): number;
|
|
57
67
|
/** {@inheritDoc} */
|
|
58
68
|
get props(): Readonly<DbConnProps>;
|
|
59
69
|
/** {@inheritDoc} */
|
|
60
|
-
|
|
61
|
-
/** {@inheritDoc} */
|
|
62
|
-
f(field: string): string;
|
|
63
|
-
/** {@inheritDoc} */
|
|
64
|
-
abstract field(field: string): string;
|
|
65
|
-
v(value: unknown): string;
|
|
66
|
-
abstract value(value: unknown): string;
|
|
67
|
-
/** {@inheritDoc} */
|
|
68
|
-
rows<T>(rows: Array<T>): Array<T>;
|
|
70
|
+
get isEnabled(): boolean;
|
|
69
71
|
/** {@inheritDoc} */
|
|
70
|
-
|
|
71
|
-
/** {@inheritDoc} */
|
|
72
|
-
first<T>(rows: Array<T>): T | undefined;
|
|
73
|
-
/** {@inheritDoc} */
|
|
74
|
-
buildOpt(p1?: Partial<OPT> | string, name?: string): OPT;
|
|
72
|
+
get isConnected(): boolean;
|
|
75
73
|
/** {@inheritDoc} */
|
|
76
74
|
onConnected(fn: DbOnAfter): void;
|
|
77
75
|
/** {@inheritDoc} */
|
|
78
76
|
onDisconnected(fn: DbOnAfter): void;
|
|
79
77
|
/** {@inheritDoc} */
|
|
80
78
|
onFirstConnected(fn: DbOnAfter): void;
|
|
81
|
-
/** {@inheritDoc} */
|
|
82
|
-
exec<T>(fn: Promise<T>, p1?: string | Omit<OPT, 'printSql'>): Promise<T>;
|
|
83
|
-
/** {@inheritDoc} */
|
|
84
|
-
abstract more<T>(link: LINK, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultMore<T, META>>;
|
|
85
|
-
/** {@inheritDoc} */
|
|
86
|
-
abstract one<T>(link: LINK, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultOne<T, META>>;
|
|
87
79
|
/**
|
|
88
80
|
* It will be called during module based systems
|
|
89
81
|
* */
|
|
@@ -12,10 +12,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.DbConnection = void 0;
|
|
13
13
|
const be_base_common_1 = require("@samet-it/be-base-common");
|
|
14
14
|
const type_1 = require("@leyyo/type");
|
|
15
|
-
const error_1 = require("../error");
|
|
16
15
|
// noinspection JSUnusedGlobalSymbols
|
|
17
16
|
/**
|
|
18
17
|
* DB connection abstract class
|
|
18
|
+
*
|
|
19
|
+
* Generics:
|
|
20
|
+
* - 0-`OPT`: db execution options {@link DbExecOpt}
|
|
19
21
|
* */
|
|
20
22
|
class DbConnection {
|
|
21
23
|
// endregion protected-property
|
|
@@ -47,7 +49,7 @@ class DbConnection {
|
|
|
47
49
|
if ((0, type_1.isObjectBare)(opt)) {
|
|
48
50
|
this._opt = Object.assign(Object.assign({}, this._opt), opt);
|
|
49
51
|
}
|
|
50
|
-
this._props = Object.assign({}, this._opt);
|
|
52
|
+
this._props = Object.assign(Object.assign({}, this._opt), { connectTryCount: 0, pingTryCount: 0 });
|
|
51
53
|
delete this._opt;
|
|
52
54
|
}
|
|
53
55
|
// region protected-method
|
|
@@ -94,77 +96,33 @@ class DbConnection {
|
|
|
94
96
|
});
|
|
95
97
|
}
|
|
96
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Generate next delay time with exponential & randomized manner
|
|
101
|
+
*
|
|
102
|
+
* @param {number} count - try count
|
|
103
|
+
* @param {number} baseDelay - starting delay interval
|
|
104
|
+
* @param {number} maxDelay - maximum delay interval
|
|
105
|
+
* @return {number}
|
|
106
|
+
* */
|
|
107
|
+
_delayWithJitter(count, baseDelay, maxDelay) {
|
|
108
|
+
const exp = Math.min(baseDelay * Math.pow(2, count), maxDelay);
|
|
109
|
+
return exp / 2 + Math.random() * (exp / 2);
|
|
110
|
+
}
|
|
97
111
|
// endregion protected-method
|
|
98
112
|
// region getter
|
|
99
113
|
/** {@inheritDoc} */
|
|
100
114
|
get props() {
|
|
101
115
|
return this._props;
|
|
102
116
|
}
|
|
103
|
-
// endregion getter
|
|
104
|
-
// region field-value
|
|
105
|
-
/** {@inheritDoc} */
|
|
106
|
-
checkError(err, opt) {
|
|
107
|
-
var _a, _b, _c;
|
|
108
|
-
const size = be_base_common_1.errorHandler.addStat(err);
|
|
109
|
-
if ((_a = opt === null || opt === void 0 ? void 0 : opt.ignoredErrors) === null || _a === void 0 ? void 0 : _a.includes(err.name)) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (opt === null || opt === void 0 ? void 0 : opt.silent) {
|
|
113
|
-
if (size < 100) {
|
|
114
|
-
this.logger.warn(be_base_common_1.errorHandler.common.logText(err, ((_b = opt.name) !== null && _b !== void 0 ? _b : 'Database error'), size));
|
|
115
|
-
}
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (size < 100) {
|
|
119
|
-
this.logger.error(be_base_common_1.errorHandler.common.logText(err, ((_c = opt.name) !== null && _c !== void 0 ? _c : 'Database error'), size));
|
|
120
|
-
}
|
|
121
|
-
if (err instanceof error_1.DbError) {
|
|
122
|
-
throw err;
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
throw be_base_common_1.errorHandler.common.castForClass(error_1.DbError, err, { queryName: opt.name });
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
117
|
/** {@inheritDoc} */
|
|
129
|
-
|
|
130
|
-
return this.
|
|
131
|
-
}
|
|
132
|
-
v(value) {
|
|
133
|
-
return this.value(value);
|
|
118
|
+
get isEnabled() {
|
|
119
|
+
return this._props.isEnabled;
|
|
134
120
|
}
|
|
135
121
|
/** {@inheritDoc} */
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return rows.filter(row => row['_trashId'] === undefined);
|
|
139
|
-
}
|
|
140
|
-
return [];
|
|
122
|
+
get isConnected() {
|
|
123
|
+
return this._props.isConnected;
|
|
141
124
|
}
|
|
142
|
-
|
|
143
|
-
row(row) {
|
|
144
|
-
if (row && row['_trashId'] === undefined) {
|
|
145
|
-
return row;
|
|
146
|
-
}
|
|
147
|
-
return undefined;
|
|
148
|
-
}
|
|
149
|
-
/** {@inheritDoc} */
|
|
150
|
-
first(rows) {
|
|
151
|
-
if (rows && Array.isArray(rows) && (rows.length > 0)) {
|
|
152
|
-
return rows[0];
|
|
153
|
-
}
|
|
154
|
-
return undefined;
|
|
155
|
-
}
|
|
156
|
-
// endregion field-value
|
|
157
|
-
// region option
|
|
158
|
-
/** {@inheritDoc} */
|
|
159
|
-
buildOpt(p1, name) {
|
|
160
|
-
const opt = (typeof p1 === 'string' ? { name: p1 } : Object.assign({}, p1));
|
|
161
|
-
if (typeof opt.name === undefined && typeof name !== undefined) {
|
|
162
|
-
opt.name = name;
|
|
163
|
-
}
|
|
164
|
-
opt.name = opt.name ? `[${opt.name}]` : '';
|
|
165
|
-
return opt;
|
|
166
|
-
}
|
|
167
|
-
// endregion option
|
|
125
|
+
// endregion getter
|
|
168
126
|
// region callback
|
|
169
127
|
/** {@inheritDoc} */
|
|
170
128
|
onConnected(fn) {
|
|
@@ -179,26 +137,14 @@ class DbConnection {
|
|
|
179
137
|
this._on('first-connected', this._onFirstConnected, fn, true);
|
|
180
138
|
}
|
|
181
139
|
// endregion callback
|
|
182
|
-
// region query
|
|
183
|
-
/** {@inheritDoc} */
|
|
184
|
-
exec(fn, p1) {
|
|
185
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
-
const opt = this.buildOpt(p1);
|
|
187
|
-
try {
|
|
188
|
-
return yield fn;
|
|
189
|
-
}
|
|
190
|
-
catch (err) {
|
|
191
|
-
this.checkError(err, opt);
|
|
192
|
-
return undefined;
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
// endregion query
|
|
197
140
|
/**
|
|
198
141
|
* It will be called during module based systems
|
|
199
142
|
* */
|
|
200
143
|
onModuleInit() {
|
|
201
144
|
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
if (!this.isEnabled) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
202
148
|
yield this.connect();
|
|
203
149
|
});
|
|
204
150
|
}
|
|
@@ -1,142 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type { DbErrorOmit } from "../error";
|
|
3
|
-
import type { Opt } from "@leyyo/common";
|
|
1
|
+
import { KeyValue } from "@leyyo/common";
|
|
4
2
|
/**
|
|
5
|
-
* DB
|
|
6
|
-
* */
|
|
7
|
-
export interface DbQueryResult<META> {
|
|
8
|
-
/**
|
|
9
|
-
* Success or failure
|
|
10
|
-
*/
|
|
11
|
-
success: boolean;
|
|
12
|
-
/**
|
|
13
|
-
* [when success=true] The meta-data which has been returned by the query.
|
|
14
|
-
*/
|
|
15
|
-
meta?: META;
|
|
16
|
-
/**
|
|
17
|
-
* [when success=false] Couchbase error
|
|
18
|
-
*/
|
|
19
|
-
error?: DbErrorOmit;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* DB query result as a row
|
|
23
|
-
* */
|
|
24
|
-
export interface DbQueryResultOne<E = any, META = unknown> extends DbQueryResult<META> {
|
|
25
|
-
/**
|
|
26
|
-
* The rows which have been returned by the query.
|
|
27
|
-
*/
|
|
28
|
-
row: E | undefined;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* DB query result as rows
|
|
32
|
-
* */
|
|
33
|
-
export interface DbQueryResultMore<E = any, META = unknown> extends DbQueryResult<META> {
|
|
34
|
-
/**
|
|
35
|
-
* The rows which have been returned by the query.
|
|
36
|
-
*/
|
|
37
|
-
rows: E[];
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* DB query option
|
|
41
|
-
* */
|
|
42
|
-
export interface DbExecOpt {
|
|
43
|
-
/**
|
|
44
|
-
* Name of SQL
|
|
45
|
-
* */
|
|
46
|
-
name?: string;
|
|
47
|
-
/**
|
|
48
|
-
* do not use cache
|
|
49
|
-
* */
|
|
50
|
-
noCache?: boolean;
|
|
51
|
-
/**
|
|
52
|
-
* Ignored errors
|
|
53
|
-
* */
|
|
54
|
-
ignoredErrors?: Array<string>;
|
|
55
|
-
/**
|
|
56
|
-
* Print sql?
|
|
57
|
-
* */
|
|
58
|
-
printSql?: boolean;
|
|
59
|
-
/**
|
|
60
|
-
* Ignore any error
|
|
61
|
-
* */
|
|
62
|
-
silent?: boolean;
|
|
63
|
-
/**
|
|
64
|
-
* Trash id for only trash case
|
|
65
|
-
* */
|
|
66
|
-
trashId?: string;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* DB connection
|
|
3
|
+
* DB connection interface
|
|
70
4
|
* */
|
|
71
|
-
export interface
|
|
72
|
-
/**
|
|
73
|
-
* Field name
|
|
74
|
-
*
|
|
75
|
-
* @param {string} field - raw field name
|
|
76
|
-
* @return {string} - formatted field name
|
|
77
|
-
* */
|
|
78
|
-
field(field: string): string;
|
|
5
|
+
export interface DbConnectionLike {
|
|
79
6
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @alias #field
|
|
82
|
-
*
|
|
83
|
-
* @param {string} field - raw field name
|
|
84
|
-
* @return {string} - formatted field name
|
|
85
|
-
* */
|
|
86
|
-
f(field: string): string;
|
|
87
|
-
/**
|
|
88
|
-
* Format value
|
|
89
|
-
*
|
|
90
|
-
* @param {string} value - raw value
|
|
91
|
-
* @return {string} - formatted value
|
|
92
|
-
* */
|
|
93
|
-
value(value: unknown): string;
|
|
94
|
-
/**
|
|
95
|
-
* Format value
|
|
96
|
-
* @alias #value
|
|
97
|
-
*
|
|
98
|
-
* @param {string} value - raw value
|
|
99
|
-
* @return {string} - formatted value
|
|
100
|
-
* */
|
|
101
|
-
v(value: unknown): string;
|
|
102
|
-
/**
|
|
103
|
-
* Check rows and discard trashed
|
|
104
|
-
*
|
|
105
|
-
* @param {Array<any>} rows - rows
|
|
106
|
-
* @return {Array<any>} - checked rows
|
|
107
|
-
* */
|
|
108
|
-
rows<T>(rows: Array<T>): Array<T>;
|
|
109
|
-
/**
|
|
110
|
-
* Check row and discard trashed
|
|
111
|
-
*
|
|
112
|
-
* @param {any} row - row
|
|
113
|
-
* @return {any} - checked row
|
|
114
|
-
* */
|
|
115
|
-
row<T>(row: T): T | undefined;
|
|
116
|
-
/**
|
|
117
|
-
* Return first row of rows
|
|
118
|
-
*
|
|
119
|
-
* @param {Array<any>} rows - rows
|
|
120
|
-
* @return {any} - first row if exists
|
|
7
|
+
* Props on connection
|
|
121
8
|
* */
|
|
122
|
-
|
|
9
|
+
get props(): Readonly<DbConnProps>;
|
|
123
10
|
/**
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
127
|
-
* @param {string?} name - query name, if first param is option
|
|
128
|
-
* @return {DbExecOpt} - formatted option
|
|
11
|
+
* Shortcut to {@link DbConnOpt#isEnabled}
|
|
129
12
|
* */
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* DB connection interface
|
|
134
|
-
* */
|
|
135
|
-
export interface DbConnectionLike<LINK extends DbRepoLink, META extends DbMeta, OPT extends DbExecOpt> extends DbConnectionBase {
|
|
13
|
+
get isEnabled(): boolean;
|
|
136
14
|
/**
|
|
137
|
-
*
|
|
15
|
+
* Shortcut to {@link DbConnProps#isConnected}
|
|
138
16
|
* */
|
|
139
|
-
get
|
|
17
|
+
get isConnected(): boolean;
|
|
140
18
|
/**
|
|
141
19
|
* Add an async function into queue
|
|
142
20
|
*
|
|
@@ -155,42 +33,6 @@ export interface DbConnectionLike<LINK extends DbRepoLink, META extends DbMeta,
|
|
|
155
33
|
* @param {function} fn - callback
|
|
156
34
|
* */
|
|
157
35
|
onDisconnected(fn: DbOnAfter): void;
|
|
158
|
-
/**
|
|
159
|
-
* Check error
|
|
160
|
-
*
|
|
161
|
-
* @param {Error} err - error
|
|
162
|
-
* @param {DbExecOpt} opt - option
|
|
163
|
-
* */
|
|
164
|
-
checkError(err: Error, opt: DbExecOpt | Opt): void;
|
|
165
|
-
/**
|
|
166
|
-
* Execute a lambda
|
|
167
|
-
*
|
|
168
|
-
* @param {function} fn - promise callback
|
|
169
|
-
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
170
|
-
* @return {Promise} - generic type
|
|
171
|
-
* @async
|
|
172
|
-
* */
|
|
173
|
-
exec<T>(fn: Promise<T>, opt?: string | Omit<OPT, 'printSql'>): Promise<T>;
|
|
174
|
-
/**
|
|
175
|
-
* Execute a sql and return rows
|
|
176
|
-
*
|
|
177
|
-
* @param {DbRepoLink} link - repository link
|
|
178
|
-
* @param {(string|DbLines)} sql - sql or sql lines
|
|
179
|
-
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
180
|
-
* @return {Promise<DbQueryResultMore>} - rows
|
|
181
|
-
* @async
|
|
182
|
-
* */
|
|
183
|
-
more<T>(link: LINK, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultMore<T, META>>;
|
|
184
|
-
/**
|
|
185
|
-
* Execute a sql and return a row
|
|
186
|
-
*
|
|
187
|
-
* @param {DbRepoLink} link - repository link
|
|
188
|
-
* @param {(string|DbLines)} sql - sql or sql lines
|
|
189
|
-
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
190
|
-
* @return {Promise<DbQueryResultOne>} - row
|
|
191
|
-
* @async
|
|
192
|
-
* */
|
|
193
|
-
one<T>(link: LINK, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultOne<T, META>>;
|
|
194
36
|
/**
|
|
195
37
|
* It will be called during module based systems
|
|
196
38
|
*
|
|
@@ -215,15 +57,16 @@ export interface DbConnectionLike<LINK extends DbRepoLink, META extends DbMeta,
|
|
|
215
57
|
* */
|
|
216
58
|
ping(next?: boolean): Promise<boolean>;
|
|
217
59
|
}
|
|
218
|
-
/**
|
|
219
|
-
* DB meta
|
|
220
|
-
* */
|
|
221
|
-
export interface DbMeta {
|
|
222
|
-
}
|
|
223
60
|
/**
|
|
224
61
|
* DB connection option
|
|
225
62
|
* */
|
|
226
63
|
export interface DbConnOpt {
|
|
64
|
+
/**
|
|
65
|
+
* Is enabled?
|
|
66
|
+
*
|
|
67
|
+
* @type {boolean}
|
|
68
|
+
* */
|
|
69
|
+
isEnabled?: boolean;
|
|
227
70
|
/**
|
|
228
71
|
* Protocol
|
|
229
72
|
*
|
|
@@ -267,7 +110,7 @@ export interface DbConnOpt {
|
|
|
267
110
|
* @default `undefined`
|
|
268
111
|
* @type {function}
|
|
269
112
|
* */
|
|
270
|
-
userFetcher?:
|
|
113
|
+
userFetcher?: UserFetcherLambda;
|
|
271
114
|
}
|
|
272
115
|
/**
|
|
273
116
|
* DB connection props
|
|
@@ -285,6 +128,20 @@ export interface DbConnProps extends DbConnOpt {
|
|
|
285
128
|
* @type {boolean}
|
|
286
129
|
* */
|
|
287
130
|
isFirst: boolean;
|
|
131
|
+
/**
|
|
132
|
+
* Consecutive attempts for connection
|
|
133
|
+
* - It is cleared (zero) when it connected
|
|
134
|
+
*
|
|
135
|
+
* @type {number}
|
|
136
|
+
* */
|
|
137
|
+
connectTryCount: number;
|
|
138
|
+
/**
|
|
139
|
+
* Consecutive attempts for ping
|
|
140
|
+
* - It is cleared (zero) when it pinged
|
|
141
|
+
*
|
|
142
|
+
* @type {number}
|
|
143
|
+
* */
|
|
144
|
+
pingTryCount: number;
|
|
288
145
|
}
|
|
289
146
|
/**
|
|
290
147
|
* DB on after lambda
|
|
@@ -295,7 +152,8 @@ export type DbOnAfter = () => Promise<void>;
|
|
|
295
152
|
* DB connection events
|
|
296
153
|
* */
|
|
297
154
|
export type DbEvent = 'connected' | 'disconnected' | 'first-connected';
|
|
298
|
-
/**
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
155
|
+
/**
|
|
156
|
+
* User fetcher lambda
|
|
157
|
+
* - It uses ASL
|
|
158
|
+
* */
|
|
159
|
+
export type UserFetcherLambda = () => Promise<KeyValue>;
|
package/dist/error/db.error.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { SametError } from "@samet-it/be-base-common";
|
|
2
|
-
import type { DbErrorOmit } from "./index.types";
|
|
3
2
|
/**
|
|
4
3
|
* General database error
|
|
5
4
|
* */
|
|
6
|
-
export declare class DbError extends SametError
|
|
5
|
+
export declare class DbError extends SametError {
|
|
7
6
|
}
|
package/dist/error/index.d.ts
CHANGED
package/dist/error/index.js
CHANGED
|
@@ -14,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./index.types"), exports);
|
|
18
17
|
__exportStar(require("./db.error"), exports);
|
|
19
18
|
__exportStar(require("./db-execute.error"), exports);
|
|
20
19
|
__exportStar(require("./db-invalid-value.error"), exports);
|
package/dist/repo/db.repo.d.ts
CHANGED
|
@@ -1,28 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { DefDims, Entity, IdDocLike,
|
|
3
|
-
import
|
|
4
|
-
import type { KeyValue, StrKey } from "@leyyo/common";
|
|
1
|
+
import { DbCheckKeysResult, DbRepoProps, DbRepoLike, DbRepoOpt, UrnDelimiter, DbCheckKeysTuple, DbExecOpt, DbMeta, DbRepoExtensionLambda, DbQueryResultOne, DbQueryResultMore } from "./index.types";
|
|
2
|
+
import { DefDims, Entity, IdDocLike, Pair, Portion, UrnDocLike, UrnTuple, View } from "@samet-it/be-base-common";
|
|
3
|
+
import { DbConnectionLike, UserFetcherLambda } from "../connection";
|
|
4
|
+
import type { KeyValue, Logger, Opt, StrKey } from "@leyyo/common";
|
|
5
5
|
import { type QueryAny, type QueryRegular } from "@leyyo/query";
|
|
6
|
+
import type { CacheConnectionLike, CacheChannelLike, CacheExecOpt } from "@samet-it/be-cache-common";
|
|
7
|
+
import type { DbLines } from "../line";
|
|
6
8
|
/**
|
|
7
9
|
* DB repository abstract class
|
|
8
10
|
*
|
|
9
11
|
* Generics:
|
|
10
|
-
* - 0-`CONN`: db connection {@link
|
|
12
|
+
* - 0-`CONN`: db connection {@link DbConnectionLike}
|
|
11
13
|
* - 1-`OPT`: db execution options {@link DbExecOpt}
|
|
12
|
-
* - 2-`
|
|
13
|
-
* - 3-`
|
|
14
|
-
* - 4-`
|
|
15
|
-
* - 5-`
|
|
16
|
-
* - 6-`
|
|
17
|
-
* - 7-`
|
|
18
|
-
* - 8-`
|
|
19
|
-
* - 9-`
|
|
14
|
+
* - 2-`META`: query result metadata {@link DbMeta}
|
|
15
|
+
* - 3-`ENT`: entity {@link Entity}
|
|
16
|
+
* - 4-`ID`: id type {@link KeyValue}
|
|
17
|
+
* - 5-`URN`: urn interface {@link UrnDocLike}
|
|
18
|
+
* - 6-`VIEW`: view interface for presentation layer {@link View}
|
|
19
|
+
* - 7-`PAIR`: pair interface for presentation layer {@link Pair}
|
|
20
|
+
* - 8-`PORTION`: portion interface for presentation layer {@link Portion}
|
|
21
|
+
* - 9-`KEYS`: keys to autocomplete, def: keys of {@link Entity}
|
|
22
|
+
* - 10-`DIMS`: dimensions for presentation layer {@link DefDims}
|
|
20
23
|
* */
|
|
21
|
-
export declare abstract class DbRepo<CONN extends
|
|
24
|
+
export declare abstract class DbRepo<CONN extends DbConnectionLike, OPT extends DbExecOpt, META extends DbMeta, ENT extends Entity<ID>, ID extends KeyValue, URN extends UrnDocLike, VIEW extends View<ID>, PAIR extends Pair<ID>, PORTION extends Portion<ID>, KEYS extends string, DIMS extends DefDims> implements DbRepoLike<CONN, OPT, META, ENT, ID, URN, VIEW, PAIR, PORTION, KEYS, DIMS> {
|
|
22
25
|
/**
|
|
23
26
|
* Logger
|
|
24
27
|
* */
|
|
25
|
-
protected logger:
|
|
28
|
+
protected logger: Logger;
|
|
26
29
|
/**
|
|
27
30
|
* Option of in-body usage in place of constructor param
|
|
28
31
|
* */
|
|
@@ -35,17 +38,15 @@ export declare abstract class DbRepo<CONN extends DbConnectionBase, OPT extends
|
|
|
35
38
|
/**
|
|
36
39
|
* Constructor
|
|
37
40
|
*
|
|
38
|
-
* @param {
|
|
41
|
+
* @param {DbConnectionLike} conn - db connection
|
|
39
42
|
* @param {DbRepoOpt} opt - options
|
|
40
43
|
* */
|
|
41
44
|
protected constructor(conn: CONN, opt?: DbRepoOpt<ENT, ID>);
|
|
42
|
-
protected _removeFieldForForbidden(fields: Array<keyof ENT>, flag: keyof DbRepoOpt<ENT, ID>, field: keyof ENT): void;
|
|
43
|
-
protected _checkForbiddenList(given: Array<keyof ENT>, def: Array<keyof ENT>): Array<keyof ENT>;
|
|
44
45
|
protected get _now(): string | number;
|
|
45
46
|
protected get _randomIndexName(): string;
|
|
46
47
|
protected _indexName(name: string): string;
|
|
47
48
|
/**
|
|
48
|
-
* Fetch urn from doc as doc.
|
|
49
|
+
* Fetch urn from doc as doc.urn
|
|
49
50
|
* */
|
|
50
51
|
protected _urnFromDoc(doc: Partial<ENT>, method: string): string;
|
|
51
52
|
protected _checkKey(value: unknown): DbCheckKeysTuple<ID>;
|
|
@@ -60,6 +61,86 @@ export declare abstract class DbRepo<CONN extends DbConnectionBase, OPT extends
|
|
|
60
61
|
/** @inheritDoc */
|
|
61
62
|
$cast<T>(): T;
|
|
62
63
|
/** @inheritDoc */
|
|
64
|
+
get isEnabled(): boolean;
|
|
65
|
+
/** @inheritDoc */
|
|
66
|
+
get userFetcher(): UserFetcherLambda;
|
|
67
|
+
/** @inheritDoc */
|
|
68
|
+
get conn(): CONN;
|
|
69
|
+
/** @inheritDoc */
|
|
70
|
+
get isConnected(): boolean;
|
|
71
|
+
/** @inheritDoc */
|
|
72
|
+
get cache(): CacheChannelLike<CacheConnectionLike, CacheExecOpt, ENT, ID>;
|
|
73
|
+
/** @inheritDoc */
|
|
74
|
+
get modelVersion(): number;
|
|
75
|
+
/** @inheritDoc */
|
|
76
|
+
get urnDelimiter(): UrnDelimiter;
|
|
77
|
+
/** @inheritDoc */
|
|
78
|
+
get urnPrefix(): string;
|
|
79
|
+
/** @inheritDoc */
|
|
80
|
+
get hasNumericId(): boolean;
|
|
81
|
+
/** @inheritDoc */
|
|
82
|
+
get hasIsoDate(): boolean;
|
|
83
|
+
/** @inheritDoc */
|
|
84
|
+
get isIdSame(): boolean;
|
|
85
|
+
/** @inheritDoc */
|
|
86
|
+
get hasSoftDelete(): boolean;
|
|
87
|
+
/** @inheritDoc */
|
|
88
|
+
get hasCreatedAt(): boolean;
|
|
89
|
+
/** @inheritDoc */
|
|
90
|
+
get hasCreatedBy(): boolean;
|
|
91
|
+
/** @inheritDoc */
|
|
92
|
+
get hasUpdatedAt(): boolean;
|
|
93
|
+
/** @inheritDoc */
|
|
94
|
+
get hasUpdatedBy(): boolean;
|
|
95
|
+
/** @inheritDoc */
|
|
96
|
+
get hasVersion(): boolean;
|
|
97
|
+
/** @inheritDoc */
|
|
98
|
+
get hasRevision(): boolean;
|
|
99
|
+
/** @inheritDoc */
|
|
100
|
+
get isNoInsert(): boolean;
|
|
101
|
+
/** @inheritDoc */
|
|
102
|
+
get isNoUpdate(): boolean;
|
|
103
|
+
/** @inheritDoc */
|
|
104
|
+
get isNoTrash(): boolean;
|
|
105
|
+
/** @inheritDoc */
|
|
106
|
+
get isNoRemove(): boolean;
|
|
107
|
+
/** @inheritDoc */
|
|
108
|
+
get alphaFn(): DbRepoExtensionLambda<ENT, ID>;
|
|
109
|
+
/** @inheritDoc */
|
|
110
|
+
get searchFn(): DbRepoExtensionLambda<ENT, ID>;
|
|
111
|
+
/** @inheritDoc */
|
|
112
|
+
get irregularFn(): DbRepoExtensionLambda<ENT, ID>;
|
|
113
|
+
/** @inheritDoc */
|
|
114
|
+
get forbiddenSet(): Array<keyof ENT>;
|
|
115
|
+
/** @inheritDoc */
|
|
116
|
+
get path(): string;
|
|
117
|
+
/** @inheritDoc */
|
|
118
|
+
get table(): string;
|
|
119
|
+
/** @inheritDoc */
|
|
120
|
+
get urnLength(): number;
|
|
121
|
+
/** {@inheritDoc} */
|
|
122
|
+
checkError(err: Error, opt: DbExecOpt | Opt): void;
|
|
123
|
+
/** {@inheritDoc} */
|
|
124
|
+
f(field: string): string;
|
|
125
|
+
/** {@inheritDoc} */
|
|
126
|
+
abstract field(field: string): string;
|
|
127
|
+
v(value: unknown): string;
|
|
128
|
+
abstract value(value: unknown): string;
|
|
129
|
+
/** {@inheritDoc} */
|
|
130
|
+
rows<T>(rows: Array<T>): Array<T>;
|
|
131
|
+
/** {@inheritDoc} */
|
|
132
|
+
row<T>(row: T): T | undefined;
|
|
133
|
+
/** {@inheritDoc} */
|
|
134
|
+
first<T>(rows: Array<T>): T | undefined;
|
|
135
|
+
/** {@inheritDoc} */
|
|
136
|
+
buildOpt(p1?: Partial<OPT> | string, name?: string): OPT;
|
|
137
|
+
/** {@inheritDoc} */
|
|
138
|
+
exec<T>(fn: Promise<T>, p1?: string | Omit<OPT, 'printSql'>): Promise<T>;
|
|
139
|
+
/** {@inheritDoc} */
|
|
140
|
+
abstract more<T>(sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultMore<T, META>>;
|
|
141
|
+
/** {@inheritDoc} */
|
|
142
|
+
abstract one<T>(sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultOne<T, META>>;
|
|
143
|
+
/** @inheritDoc */
|
|
63
144
|
toUrn(urnRec: URN): string;
|
|
64
145
|
/** @inheritDoc */
|
|
65
146
|
$toUrnTuple(urnRec: URN): UrnTuple;
|