@samet-it/be-db-common 1.0.11
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/LICENSE +24 -0
- package/README.md +74 -0
- package/dist/assets/.gitkeep +0 -0
- package/dist/connection/db.connection.d.ts +74 -0
- package/dist/connection/db.connection.js +173 -0
- package/dist/connection/index.d.ts +2 -0
- package/dist/connection/index.js +18 -0
- package/dist/connection/index.types.d.ts +294 -0
- package/dist/connection/index.types.js +2 -0
- package/dist/error/db-execute.error.d.ts +5 -0
- package/dist/error/db-execute.error.js +10 -0
- package/dist/error/db-invalid-value.error.d.ts +5 -0
- package/dist/error/db-invalid-value.error.js +10 -0
- package/dist/error/db-not-supported.error.d.ts +4 -0
- package/dist/error/db-not-supported.error.js +10 -0
- package/dist/error/db.error.d.ts +7 -0
- package/dist/error/db.error.js +10 -0
- package/dist/error/index.d.ts +5 -0
- package/dist/error/index.js +21 -0
- package/dist/error/index.types.d.ts +1 -0
- package/dist/error/index.types.js +2 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +20 -0
- package/dist/line/db-lines.impl.d.ts +7 -0
- package/dist/line/db-lines.impl.js +141 -0
- package/dist/line/index.d.ts +2 -0
- package/dist/line/index.js +18 -0
- package/dist/line/index.types.d.ts +103 -0
- package/dist/line/index.types.js +2 -0
- package/dist/repo/db.repo.d.ts +147 -0
- package/dist/repo/db.repo.js +924 -0
- package/dist/repo/index.d.ts +2 -0
- package/dist/repo/index.js +18 -0
- package/dist/repo/index.types.d.ts +701 -0
- package/dist/repo/index.types.js +2 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Samet Global
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
Web: https://sametglobal.com
|
|
24
|
+
Responsible: Mustafa Yelmer (mustafay@samet.com.tr)
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Samet IT : DB Common
|
|
2
|
+
`ts` `component` `backend`
|
|
3
|
+
```
|
|
4
|
+
____
|
|
5
|
+
____/ / /_ _________ ____ ___ ____ ___ ____ ____
|
|
6
|
+
/ __ / __ \ / ___/ __ \/ __ `__ \/ __ `__ \/ __ \/ __ \
|
|
7
|
+
/ /_/ / /_/ / / /__/ /_/ / / / / / / / / / / / /_/ / / / /
|
|
8
|
+
\__,_/_.___/ \___/\____/_/ /_/ /_/_/ /_/ /_/\____/_/ /_/
|
|
9
|
+
```
|
|
10
|
+
<!-- https://patorjk.com/software/taag/#p=display&f=Slant&t=db+common&x=none&v=4&h=3&w=80&we=false -->
|
|
11
|
+
- `EN`: It's db component
|
|
12
|
+
- `TR`: Nodejs projeleri veritabanı temel bileşeni
|
|
13
|
+
|
|
14
|
+
## Prerequisite
|
|
15
|
+
- [Help](https://github.com/samet-digital/help)
|
|
16
|
+
- [1 - Install Git](https://github.com/samet-digital/help/blob/main//install-git.MD)
|
|
17
|
+
- [2 - Build Up a Git Connection](https://github.com/samet-digital/help/blob/main//build-git-connection.MD)
|
|
18
|
+
- [3 - Install Node/NPM](https://github.com/samet-digital/help/blob/main//install-npm.MD)
|
|
19
|
+
- [4 - Create Project Folder](https://github.com/samet-digital/help/blob/main//create-project-folder.MD)
|
|
20
|
+
- [5 - Bind Projects Folder to Git](https://github.com/samet-digital/help/blob/main//bind-project-folder.MD)
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
- `EN`: How to add into your project?
|
|
24
|
+
- `TR`: Projene nasıl eklersin?
|
|
25
|
+
|
|
26
|
+
```shell
|
|
27
|
+
npm i @samet-it/be-db-common
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Environments
|
|
31
|
+
- ~~Nothing~~
|
|
32
|
+
|
|
33
|
+
## Contents
|
|
34
|
+
- [abstract class DbConnection](./src/connection/db-connection.ts)
|
|
35
|
+
- [function dbLines()](./src/line/db-lines.impl.ts)
|
|
36
|
+
- [abstract class DbRepo](./src/repo/db.repo.ts)
|
|
37
|
+
|
|
38
|
+
## Development
|
|
39
|
+
> You can start to develop on it
|
|
40
|
+
>
|
|
41
|
+
### Step 1
|
|
42
|
+
- [Go to project folder](https://github.com/samet-digital/help/blob/main/go-to-project-folder.MD)
|
|
43
|
+
|
|
44
|
+
### Step 2
|
|
45
|
+
- `EN`: Clone the project
|
|
46
|
+
- `TR`: Projeyi bilgisayarına çek
|
|
47
|
+
```shell
|
|
48
|
+
git clone https://github.com/samet-digital/be-db-common.git -b development
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Dependencies
|
|
52
|
+
- `@samet-it/be-base-common`: **samet** base common
|
|
53
|
+
- `@leyyo/*`: leyyo utilities
|
|
54
|
+
|
|
55
|
+
## Commands
|
|
56
|
+
- [Component Commands](https://github.com/samet-digital/help/blob/main/commands-component.MD)
|
|
57
|
+
|
|
58
|
+
## Standards
|
|
59
|
+
```diff
|
|
60
|
+
+ language: TS
|
|
61
|
+
+ lint: eslint
|
|
62
|
+
+ inspections: intelli-j code inspections
|
|
63
|
+
+ ddd: domain driven development
|
|
64
|
+
+ edd: exception driven development
|
|
65
|
+
! tdd: test driven development
|
|
66
|
+
+ ldd: log driven development
|
|
67
|
+
+ ddd: document driven development
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## History
|
|
71
|
+
| Date | Developer | Info | Ticket |
|
|
72
|
+
|------------|----------------|--------------|----------------|
|
|
73
|
+
| 2026-01-20 | Mustafa Yelmer | _Created_ | ~~[none](./)~~ |
|
|
74
|
+
| 2025-01-24 | Mustafa Yelmer | _Refactored_ | ~~[none](./)~~ |
|
|
File without changes
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { DbConnectionLike, DbExecOpt, DbQueryResultMore, DbQueryResultOne, DbConnOpt, DbMeta, DbOnAfter, DbConnProps } from "./index.types";
|
|
2
|
+
import { LoggerLike } from "@samet-it/be-base-common";
|
|
3
|
+
import type { DbLines } from "../line";
|
|
4
|
+
import type { DbRepoDef } from "../repo";
|
|
5
|
+
import type { Opt } from "@leyyo/common";
|
|
6
|
+
/**
|
|
7
|
+
* DB connection abstract class
|
|
8
|
+
* */
|
|
9
|
+
export declare abstract class DbConnection<REPO extends DbRepoDef, META extends DbMeta, OPT extends DbExecOpt> implements DbConnectionLike<REPO, META, OPT> {
|
|
10
|
+
/**
|
|
11
|
+
* Logger
|
|
12
|
+
* */
|
|
13
|
+
protected logger: LoggerLike;
|
|
14
|
+
/**
|
|
15
|
+
* On connected callbacks
|
|
16
|
+
* */
|
|
17
|
+
protected _onConnected: Array<DbOnAfter>;
|
|
18
|
+
/**
|
|
19
|
+
* On first connected callbacks
|
|
20
|
+
* */
|
|
21
|
+
protected _onFirstConnected: Array<DbOnAfter>;
|
|
22
|
+
/**
|
|
23
|
+
* On disconnected callbacks
|
|
24
|
+
* */
|
|
25
|
+
protected _onDisconnected: Array<DbOnAfter>;
|
|
26
|
+
/**
|
|
27
|
+
* Option of in-body usage in place of constructor param
|
|
28
|
+
* */
|
|
29
|
+
protected _opt: DbConnOpt;
|
|
30
|
+
/**
|
|
31
|
+
* Produced setting from option
|
|
32
|
+
* @see _opt
|
|
33
|
+
* */
|
|
34
|
+
protected _props: DbConnProps;
|
|
35
|
+
protected constructor(opt?: DbConnOpt);
|
|
36
|
+
checkError(err: Error, opt: DbExecOpt | Opt): void;
|
|
37
|
+
protected _on(items: Array<DbOnAfter>, fn: DbOnAfter, throwable: boolean): void;
|
|
38
|
+
/** {@inheritDoc} */
|
|
39
|
+
get props(): Readonly<DbConnProps>;
|
|
40
|
+
/** {@inheritDoc} */
|
|
41
|
+
f(field: string): string;
|
|
42
|
+
/** {@inheritDoc} */
|
|
43
|
+
abstract field(field: string): string;
|
|
44
|
+
v(value: unknown): string;
|
|
45
|
+
abstract value(value: unknown): string;
|
|
46
|
+
/** {@inheritDoc} */
|
|
47
|
+
rows<T>(rows: Array<T>): Array<T>;
|
|
48
|
+
/** {@inheritDoc} */
|
|
49
|
+
row<T>(row: T): T | undefined;
|
|
50
|
+
/** {@inheritDoc} */
|
|
51
|
+
first<T>(rows: Array<T>): T | undefined;
|
|
52
|
+
/** {@inheritDoc} */
|
|
53
|
+
buildOpt(p1?: Partial<OPT> | string, name?: string): OPT;
|
|
54
|
+
/** {@inheritDoc} */
|
|
55
|
+
onConnected(fn: DbOnAfter): void;
|
|
56
|
+
/** {@inheritDoc} */
|
|
57
|
+
onDisconnected(fn: DbOnAfter): void;
|
|
58
|
+
/** {@inheritDoc} */
|
|
59
|
+
onFirstConnected(fn: DbOnAfter): void;
|
|
60
|
+
/** {@inheritDoc} */
|
|
61
|
+
exec<T>(fn: Promise<T>, p1?: string | Omit<OPT, 'printSql'>): Promise<T>;
|
|
62
|
+
/** {@inheritDoc} */
|
|
63
|
+
abstract more<T>(repo: REPO, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultMore<T, META>>;
|
|
64
|
+
/** {@inheritDoc} */
|
|
65
|
+
abstract one<T>(repo: REPO, sql: string | DbLines, name?: string | DbExecOpt): Promise<DbQueryResultOne<T, META>>;
|
|
66
|
+
/**
|
|
67
|
+
* It will be called during module based systems
|
|
68
|
+
* */
|
|
69
|
+
onModuleInit(): Promise<void>;
|
|
70
|
+
/** {@inheritDoc} */
|
|
71
|
+
abstract ping(next?: boolean): Promise<boolean>;
|
|
72
|
+
/** {@inheritDoc} */
|
|
73
|
+
abstract connect(): Promise<boolean>;
|
|
74
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DbConnection = void 0;
|
|
13
|
+
const be_base_common_1 = require("@samet-it/be-base-common");
|
|
14
|
+
const type_1 = require("@leyyo/type");
|
|
15
|
+
const error_1 = require("../error");
|
|
16
|
+
// noinspection JSUnusedGlobalSymbols
|
|
17
|
+
/**
|
|
18
|
+
* DB connection abstract class
|
|
19
|
+
* */
|
|
20
|
+
class DbConnection {
|
|
21
|
+
// endregion protected-property
|
|
22
|
+
constructor(opt) {
|
|
23
|
+
/**
|
|
24
|
+
* On connected callbacks
|
|
25
|
+
* */
|
|
26
|
+
this._onConnected = [];
|
|
27
|
+
/**
|
|
28
|
+
* On first connected callbacks
|
|
29
|
+
* */
|
|
30
|
+
this._onFirstConnected = [];
|
|
31
|
+
/**
|
|
32
|
+
* On disconnected callbacks
|
|
33
|
+
* */
|
|
34
|
+
this._onDisconnected = [];
|
|
35
|
+
/**
|
|
36
|
+
* Option of in-body usage in place of constructor param
|
|
37
|
+
* */
|
|
38
|
+
this._opt = {};
|
|
39
|
+
if (!(0, type_1.isObjectBare)(this._opt)) {
|
|
40
|
+
this._opt = {};
|
|
41
|
+
}
|
|
42
|
+
if ((0, type_1.isObjectBare)(opt)) {
|
|
43
|
+
this._opt = Object.assign(Object.assign({}, this._opt), opt);
|
|
44
|
+
}
|
|
45
|
+
this._props = Object.assign({}, this._opt);
|
|
46
|
+
delete this._opt;
|
|
47
|
+
}
|
|
48
|
+
// region protected-method
|
|
49
|
+
checkError(err, opt) {
|
|
50
|
+
var _a, _b, _c;
|
|
51
|
+
const size = be_base_common_1.errorHandler.addStat(err);
|
|
52
|
+
if ((_a = opt === null || opt === void 0 ? void 0 : opt.ignoredErrors) === null || _a === void 0 ? void 0 : _a.includes(err.name)) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (opt === null || opt === void 0 ? void 0 : opt.silent) {
|
|
56
|
+
if (size < 100) {
|
|
57
|
+
this.logger.warn(`[${size}] ${(_b = opt.name) !== null && _b !== void 0 ? _b : 'Database error'} <${err.name}> ${err.message}`, err);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (size < 100) {
|
|
62
|
+
this.logger.error(`[${size}] ${(_c = opt.name) !== null && _c !== void 0 ? _c : 'Database error'} <${err.name}> ${err.message}`, err);
|
|
63
|
+
}
|
|
64
|
+
if (err instanceof error_1.DbError) {
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
throw be_base_common_1.errorHandler.common.castForClass(error_1.DbError, err, { queryName: opt.name });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
_on(items, fn, throwable) {
|
|
72
|
+
if (typeof fn !== 'function') {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (!this._props.isConnected) {
|
|
76
|
+
items.push(fn);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
fn().then().catch(e => {
|
|
80
|
+
this.logger.error(`[${e.name}] => ${e.message}`, e);
|
|
81
|
+
if (throwable) {
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// endregion protected-method
|
|
88
|
+
// region getter
|
|
89
|
+
/** {@inheritDoc} */
|
|
90
|
+
get props() {
|
|
91
|
+
return this._props;
|
|
92
|
+
}
|
|
93
|
+
// endregion getter
|
|
94
|
+
// region field-value
|
|
95
|
+
/** {@inheritDoc} */
|
|
96
|
+
f(field) {
|
|
97
|
+
return this.field(field);
|
|
98
|
+
}
|
|
99
|
+
v(value) {
|
|
100
|
+
return this.value(value);
|
|
101
|
+
}
|
|
102
|
+
/** {@inheritDoc} */
|
|
103
|
+
rows(rows) {
|
|
104
|
+
if (rows && Array.isArray(rows) && (rows.length > 0)) {
|
|
105
|
+
return rows.filter(row => row['_trashId'] === undefined);
|
|
106
|
+
}
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
/** {@inheritDoc} */
|
|
110
|
+
row(row) {
|
|
111
|
+
if (row && row['_trashId'] === undefined) {
|
|
112
|
+
return row;
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
/** {@inheritDoc} */
|
|
117
|
+
first(rows) {
|
|
118
|
+
if (rows && Array.isArray(rows) && (rows.length > 0)) {
|
|
119
|
+
return rows[0];
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
// endregion field-value
|
|
124
|
+
// region option
|
|
125
|
+
/** {@inheritDoc} */
|
|
126
|
+
buildOpt(p1, name) {
|
|
127
|
+
const opt = (typeof p1 === 'string' ? { name: p1 } : Object.assign({}, p1));
|
|
128
|
+
if (typeof opt.name === undefined && typeof name !== undefined) {
|
|
129
|
+
opt.name = name;
|
|
130
|
+
}
|
|
131
|
+
opt.name = opt.name ? `[${opt.name}]` : '';
|
|
132
|
+
return opt;
|
|
133
|
+
}
|
|
134
|
+
// endregion option
|
|
135
|
+
// region callback
|
|
136
|
+
/** {@inheritDoc} */
|
|
137
|
+
onConnected(fn) {
|
|
138
|
+
this._on(this._onConnected, fn, false);
|
|
139
|
+
}
|
|
140
|
+
/** {@inheritDoc} */
|
|
141
|
+
onDisconnected(fn) {
|
|
142
|
+
this._on(this._onDisconnected, fn, false);
|
|
143
|
+
}
|
|
144
|
+
/** {@inheritDoc} */
|
|
145
|
+
onFirstConnected(fn) {
|
|
146
|
+
this._on(this._onFirstConnected, fn, true);
|
|
147
|
+
}
|
|
148
|
+
// endregion callback
|
|
149
|
+
// region query
|
|
150
|
+
/** {@inheritDoc} */
|
|
151
|
+
exec(fn, p1) {
|
|
152
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
const opt = this.buildOpt(p1);
|
|
154
|
+
try {
|
|
155
|
+
return yield fn;
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
this.checkError(err, opt);
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
// endregion query
|
|
164
|
+
/**
|
|
165
|
+
* It will be called during module based systems
|
|
166
|
+
* */
|
|
167
|
+
onModuleInit() {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
yield this.connect();
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.DbConnection = DbConnection;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
18
|
+
__exportStar(require("./db.connection"), exports);
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import type { DbLines } from "../line";
|
|
2
|
+
import type { DbRepoDef } from "../repo";
|
|
3
|
+
import type { DbErrorOmit } from "../error";
|
|
4
|
+
import { Opt } from "@leyyo/common";
|
|
5
|
+
/**
|
|
6
|
+
* DB query result
|
|
7
|
+
* */
|
|
8
|
+
export interface DbQueryResult<META> {
|
|
9
|
+
/**
|
|
10
|
+
* Success or failure
|
|
11
|
+
*/
|
|
12
|
+
success: boolean;
|
|
13
|
+
/**
|
|
14
|
+
* [when success=true] The meta-data which has been returned by the query.
|
|
15
|
+
*/
|
|
16
|
+
meta?: META;
|
|
17
|
+
/**
|
|
18
|
+
* [when success=false] Couchbase error
|
|
19
|
+
*/
|
|
20
|
+
error?: DbErrorOmit;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* DB query result as a row
|
|
24
|
+
* */
|
|
25
|
+
export interface DbQueryResultOne<E = any, META = unknown> extends DbQueryResult<META> {
|
|
26
|
+
/**
|
|
27
|
+
* The rows which have been returned by the query.
|
|
28
|
+
*/
|
|
29
|
+
row: E | undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* DB query result as rows
|
|
33
|
+
* */
|
|
34
|
+
export interface DbQueryResultMore<E = any, META = unknown> extends DbQueryResult<META> {
|
|
35
|
+
/**
|
|
36
|
+
* The rows which have been returned by the query.
|
|
37
|
+
*/
|
|
38
|
+
rows: E[];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* DB query option
|
|
42
|
+
* */
|
|
43
|
+
export interface DbExecOpt {
|
|
44
|
+
/**
|
|
45
|
+
* Name of SQL
|
|
46
|
+
* */
|
|
47
|
+
name?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Ignored errors
|
|
50
|
+
* */
|
|
51
|
+
ignoredErrors?: Array<string>;
|
|
52
|
+
/**
|
|
53
|
+
* Print sql?
|
|
54
|
+
* */
|
|
55
|
+
printSql?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Ignore any error
|
|
58
|
+
* */
|
|
59
|
+
silent?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Trash id for only trash case
|
|
62
|
+
* */
|
|
63
|
+
trashId?: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* DB connection
|
|
67
|
+
* */
|
|
68
|
+
export interface DbConnectionBase<OPT extends DbExecOpt = DbExecOpt> {
|
|
69
|
+
/**
|
|
70
|
+
* Field name
|
|
71
|
+
*
|
|
72
|
+
* @param {string} field - raw field name
|
|
73
|
+
* @return {string} - formatted field name
|
|
74
|
+
* */
|
|
75
|
+
field(field: string): string;
|
|
76
|
+
/**
|
|
77
|
+
* Field name
|
|
78
|
+
* @alias #field
|
|
79
|
+
*
|
|
80
|
+
* @param {string} field - raw field name
|
|
81
|
+
* @return {string} - formatted field name
|
|
82
|
+
* */
|
|
83
|
+
f(field: string): string;
|
|
84
|
+
/**
|
|
85
|
+
* Format value
|
|
86
|
+
*
|
|
87
|
+
* @param {string} value - raw value
|
|
88
|
+
* @return {string} - formatted value
|
|
89
|
+
* */
|
|
90
|
+
value(value: unknown): string;
|
|
91
|
+
/**
|
|
92
|
+
* Format value
|
|
93
|
+
* @alias #value
|
|
94
|
+
*
|
|
95
|
+
* @param {string} value - raw value
|
|
96
|
+
* @return {string} - formatted value
|
|
97
|
+
* */
|
|
98
|
+
v(value: unknown): string;
|
|
99
|
+
/**
|
|
100
|
+
* Check rows and discard trashed
|
|
101
|
+
*
|
|
102
|
+
* @param {Array<any>} rows - rows
|
|
103
|
+
* @return {Array<any>} - checked rows
|
|
104
|
+
* */
|
|
105
|
+
rows<T>(rows: Array<T>): Array<T>;
|
|
106
|
+
/**
|
|
107
|
+
* Check row and discard trashed
|
|
108
|
+
*
|
|
109
|
+
* @param {any} row - row
|
|
110
|
+
* @return {any} - checked row
|
|
111
|
+
* */
|
|
112
|
+
row<T>(row: T): T | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Return first row of rows
|
|
115
|
+
*
|
|
116
|
+
* @param {Array<any>} rows - rows
|
|
117
|
+
* @return {any} - first row if exists
|
|
118
|
+
* */
|
|
119
|
+
first<T>(rows: Array<T>): T | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* Build option
|
|
122
|
+
*
|
|
123
|
+
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
124
|
+
* @param {string?} name - query name, if first param is option
|
|
125
|
+
* @return {DbExecOpt} - formatted option
|
|
126
|
+
* */
|
|
127
|
+
buildOpt(opt?: Partial<OPT> | string, name?: string): OPT;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* DB connection interface
|
|
131
|
+
* */
|
|
132
|
+
export interface DbConnectionLike<REPO extends DbRepoDef, META extends DbMeta, OPT extends DbExecOpt> extends DbConnectionBase {
|
|
133
|
+
/**
|
|
134
|
+
* Props on connection
|
|
135
|
+
* */
|
|
136
|
+
get props(): Readonly<DbConnProps>;
|
|
137
|
+
/**
|
|
138
|
+
* Add an async function into queue
|
|
139
|
+
*
|
|
140
|
+
* @param {function} fn - callback
|
|
141
|
+
* */
|
|
142
|
+
onFirstConnected(fn: DbOnAfter): void;
|
|
143
|
+
/**
|
|
144
|
+
* Add an async function into queue
|
|
145
|
+
*
|
|
146
|
+
* @param {function} fn - callback
|
|
147
|
+
* */
|
|
148
|
+
onConnected(fn: DbOnAfter): void;
|
|
149
|
+
/**
|
|
150
|
+
* Add an async function into queue
|
|
151
|
+
*
|
|
152
|
+
* @param {function} fn - callback
|
|
153
|
+
* */
|
|
154
|
+
onDisconnected(fn: DbOnAfter): void;
|
|
155
|
+
/**
|
|
156
|
+
* Check error
|
|
157
|
+
*
|
|
158
|
+
* @param {Error} err - error
|
|
159
|
+
* @param {DbExecOpt} opt - option
|
|
160
|
+
* */
|
|
161
|
+
checkError(err: Error, opt: DbExecOpt | Opt): void;
|
|
162
|
+
/**
|
|
163
|
+
* Execute a lambda
|
|
164
|
+
*
|
|
165
|
+
* @param {function} fn - promise callback
|
|
166
|
+
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
167
|
+
* @return {Promise} - generic type
|
|
168
|
+
* @async
|
|
169
|
+
* */
|
|
170
|
+
exec<T>(fn: Promise<T>, opt?: string | Omit<OPT, 'printSql'>): Promise<T>;
|
|
171
|
+
/**
|
|
172
|
+
* Execute a sql and return rows
|
|
173
|
+
*
|
|
174
|
+
* @param {DbRepoLike} repo - repository instance
|
|
175
|
+
* @param {(string|DbLines)} sql - sql or sql lines
|
|
176
|
+
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
177
|
+
* @return {Promise<DbQueryResultMore>} - rows
|
|
178
|
+
* @async
|
|
179
|
+
* */
|
|
180
|
+
more<T>(repo: REPO, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultMore<T, META>>;
|
|
181
|
+
/**
|
|
182
|
+
* Execute a sql and return a row
|
|
183
|
+
*
|
|
184
|
+
* @param {DbRepoLike} repo - repository instance
|
|
185
|
+
* @param {(string|DbLines)} sql - sql or sql lines
|
|
186
|
+
* @param {(DbExecOpt|string)?} opt - option or query name
|
|
187
|
+
* @return {Promise<DbQueryResultOne>} - row
|
|
188
|
+
* @async
|
|
189
|
+
* */
|
|
190
|
+
one<T>(repo: REPO, sql: string | DbLines, opt?: string | OPT): Promise<DbQueryResultOne<T, META>>;
|
|
191
|
+
/**
|
|
192
|
+
* It will be called during module based systems
|
|
193
|
+
*
|
|
194
|
+
* @return {Promise<void>}
|
|
195
|
+
* @async
|
|
196
|
+
* */
|
|
197
|
+
onModuleInit(): Promise<void>;
|
|
198
|
+
/**
|
|
199
|
+
* Connect to DB
|
|
200
|
+
*
|
|
201
|
+
* @return {Promise<boolean>} - is success
|
|
202
|
+
* @async
|
|
203
|
+
* */
|
|
204
|
+
connect(): Promise<boolean>;
|
|
205
|
+
/**
|
|
206
|
+
* Ping to DB
|
|
207
|
+
* - Note: please give `true` fo only internal usage
|
|
208
|
+
*
|
|
209
|
+
* @param {boolean?} next - if true then it will continue to ping
|
|
210
|
+
* @return {Promise<boolean>} - is success?
|
|
211
|
+
* @async
|
|
212
|
+
* */
|
|
213
|
+
ping(next?: boolean): Promise<boolean>;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* DB meta
|
|
217
|
+
* */
|
|
218
|
+
export interface DbMeta {
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* DB connection option
|
|
222
|
+
* */
|
|
223
|
+
export interface DbConnOpt {
|
|
224
|
+
/**
|
|
225
|
+
* Protocol
|
|
226
|
+
*
|
|
227
|
+
* @type {string}
|
|
228
|
+
* */
|
|
229
|
+
protocol?: string;
|
|
230
|
+
/**
|
|
231
|
+
* Host name or ip address
|
|
232
|
+
*
|
|
233
|
+
* @type {string}
|
|
234
|
+
* */
|
|
235
|
+
host?: string;
|
|
236
|
+
/**
|
|
237
|
+
* Port number
|
|
238
|
+
*
|
|
239
|
+
* @type {number}
|
|
240
|
+
* */
|
|
241
|
+
port?: number;
|
|
242
|
+
/**
|
|
243
|
+
* Username
|
|
244
|
+
*
|
|
245
|
+
* @type {string}
|
|
246
|
+
* */
|
|
247
|
+
username?: string;
|
|
248
|
+
/**
|
|
249
|
+
* Password
|
|
250
|
+
*
|
|
251
|
+
* @type {string}
|
|
252
|
+
* */
|
|
253
|
+
password?: string;
|
|
254
|
+
/**
|
|
255
|
+
* Environment variant to use other env keys in dependency injection
|
|
256
|
+
*
|
|
257
|
+
* @default `empty`
|
|
258
|
+
* @type {string}
|
|
259
|
+
* */
|
|
260
|
+
envVariant?: string;
|
|
261
|
+
/**
|
|
262
|
+
* User fetcher lambda
|
|
263
|
+
*
|
|
264
|
+
* @default `undefined`
|
|
265
|
+
* @type {function}
|
|
266
|
+
* */
|
|
267
|
+
userFetcher?: () => Promise<string>;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* DB connection props
|
|
271
|
+
* */
|
|
272
|
+
export interface DbConnProps extends DbConnOpt {
|
|
273
|
+
/**
|
|
274
|
+
* Is connected?
|
|
275
|
+
*
|
|
276
|
+
* @type {boolean}
|
|
277
|
+
* */
|
|
278
|
+
isConnected: boolean;
|
|
279
|
+
/**
|
|
280
|
+
* Is first connected?
|
|
281
|
+
*
|
|
282
|
+
* @type {boolean}
|
|
283
|
+
* */
|
|
284
|
+
isFirst: boolean;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* DB on after lambda
|
|
288
|
+
* @function
|
|
289
|
+
* */
|
|
290
|
+
export type DbOnAfter = () => Promise<void>;
|
|
291
|
+
/**
|
|
292
|
+
* DB connection events
|
|
293
|
+
* */
|
|
294
|
+
export type DbEvent = 'connected' | 'disconnected' | 'first-connected';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DbExecuteError = void 0;
|
|
4
|
+
const db_error_1 = require("./db.error");
|
|
5
|
+
class DbExecuteError extends db_error_1.DbError {
|
|
6
|
+
constructor(err, name, sql, opt) {
|
|
7
|
+
super(`${name ? name + ' : ' : ''}<${err.name}> ${err.message}`, Object.assign(Object.assign({}, opt), { sql, sqlName: name }));
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.DbExecuteError = DbExecuteError;
|