@samet-it/be-couchbase-common 1.1.7 → 1.1.9
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/config/couchbase-common.config.js +28 -8
- package/dist/config/index.types.d.ts +4 -0
- package/dist/connection/cb-direct.connection.js +1 -1
- package/dist/connection/cb.connection.d.ts +45 -17
- package/dist/connection/cb.connection.js +103 -151
- package/dist/connection/index.types.d.ts +14 -34
- package/dist/filter/cb-filter-util.impl.js +9 -9
- package/dist/filter/index.types.d.ts +3 -2
- package/dist/repo/cb-direct.repo.js +3 -23
- package/dist/repo/cb.repo.d.ts +51 -6
- package/dist/repo/cb.repo.js +339 -127
- package/dist/repo/index.types.d.ts +73 -39
- package/dist/repo/index.types.js +1 -0
- package/package.json +7 -6
|
@@ -9,12 +9,32 @@ const env_1 = require("@leyyo/env");
|
|
|
9
9
|
exports.couchbaseCommonConfig = env_1.envCore.configure
|
|
10
10
|
.scope('CouchbaseCommon', 'CB')
|
|
11
11
|
.start()
|
|
12
|
-
|
|
13
|
-
.field('
|
|
14
|
-
.
|
|
15
|
-
.field('
|
|
16
|
-
.
|
|
17
|
-
.
|
|
18
|
-
.field('
|
|
19
|
-
.
|
|
12
|
+
// @formatter:off
|
|
13
|
+
.field('ENABLED')
|
|
14
|
+
.boolean().def(true).end()
|
|
15
|
+
.field('PROTOCOL')
|
|
16
|
+
.off(v => !v.ENABLED)
|
|
17
|
+
.text().def('couchbase').end()
|
|
18
|
+
.field('HOST')
|
|
19
|
+
.off(v => !v.ENABLED)
|
|
20
|
+
.text().required().end()
|
|
21
|
+
.field('PORT')
|
|
22
|
+
.off(v => !v.ENABLED)
|
|
23
|
+
.integer().end()
|
|
24
|
+
.field('USER')
|
|
25
|
+
.off(v => !v.ENABLED)
|
|
26
|
+
.text().required().end()
|
|
27
|
+
.field('PASS')
|
|
28
|
+
.off(v => !v.ENABLED)
|
|
29
|
+
.text().required().end()
|
|
30
|
+
.field('BUCKET')
|
|
31
|
+
.off(v => !v.ENABLED)
|
|
32
|
+
.text().required().end()
|
|
33
|
+
.field('SCOPE')
|
|
34
|
+
.off(v => !v.ENABLED)
|
|
35
|
+
.text().required().end()
|
|
36
|
+
.field('CREATE_INDICES')
|
|
37
|
+
.off(v => !v.ENABLED)
|
|
38
|
+
.boolean().def(false).end()
|
|
39
|
+
// @formatter:on
|
|
20
40
|
.finish();
|
|
@@ -16,7 +16,7 @@ class CbDirectConnection extends cb_connection_1.CbConnection {
|
|
|
16
16
|
* */
|
|
17
17
|
constructor(opt) {
|
|
18
18
|
super(opt);
|
|
19
|
-
this.logger = be_base_common_1.logger.
|
|
19
|
+
this.logger = be_base_common_1.logger.create(`CbConnection${(opt === null || opt === void 0 ? void 0 : opt.name) ? '#' + (opt === null || opt === void 0 ? void 0 : opt.name) : ''}`);
|
|
20
20
|
}
|
|
21
21
|
/** @inheritDoc */
|
|
22
22
|
newRepo(opt) {
|
|
@@ -1,12 +1,46 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type { CbConnectionLike, CbConnOpt, CbExecOpt, CbConnProps, CbRepoLink } from "./index.types";
|
|
5
|
-
import type { DbQueryResultMore, DbQueryResultOne } from "@samet-it/be-db-common";
|
|
1
|
+
import type { Cluster } from 'couchbase';
|
|
2
|
+
import { DbConnection } from "@samet-it/be-db-common";
|
|
3
|
+
import type { CbConnectionLike, CbConnOpt, CbConnProps } from "./index.types";
|
|
6
4
|
/**
|
|
7
5
|
* Couchbase connection abstract class
|
|
8
6
|
* */
|
|
9
|
-
export declare abstract class CbConnection extends DbConnection
|
|
7
|
+
export declare abstract class CbConnection extends DbConnection implements CbConnectionLike {
|
|
8
|
+
/**
|
|
9
|
+
* Base/starting/min delay for connection
|
|
10
|
+
*
|
|
11
|
+
* @type {number} - as seconds
|
|
12
|
+
* */
|
|
13
|
+
private static readonly CONN_BASE_DELAY;
|
|
14
|
+
/**
|
|
15
|
+
* Last/max delay for connection
|
|
16
|
+
*
|
|
17
|
+
* @type {number} - as seconds
|
|
18
|
+
* */
|
|
19
|
+
private static readonly CONN_MAX_DELAY;
|
|
20
|
+
/**
|
|
21
|
+
* Try count
|
|
22
|
+
*
|
|
23
|
+
* @type {number} - times
|
|
24
|
+
* */
|
|
25
|
+
private static readonly CONN_TRY_COUNT;
|
|
26
|
+
/**
|
|
27
|
+
* Base/starting/min delay for ping
|
|
28
|
+
*
|
|
29
|
+
* @type {number} - as seconds
|
|
30
|
+
* */
|
|
31
|
+
private static readonly PING_BASE_DELAY;
|
|
32
|
+
/**
|
|
33
|
+
* Last/max delay ping
|
|
34
|
+
*
|
|
35
|
+
* @type {number} - as seconds
|
|
36
|
+
* */
|
|
37
|
+
private static readonly PING_MAX_DELAY;
|
|
38
|
+
/**
|
|
39
|
+
* Try count
|
|
40
|
+
*
|
|
41
|
+
* @type {number} - times
|
|
42
|
+
* */
|
|
43
|
+
private static readonly PING_TRY_COUNT;
|
|
10
44
|
/** {@inheritDoc} */
|
|
11
45
|
protected _props: CbConnProps;
|
|
12
46
|
/**
|
|
@@ -19,22 +53,16 @@ export declare abstract class CbConnection extends DbConnection<CbRepoLink, Quer
|
|
|
19
53
|
* Read configuration by variant
|
|
20
54
|
* */
|
|
21
55
|
protected _readEnv(): void;
|
|
56
|
+
protected get _connectionDelay(): number;
|
|
57
|
+
protected get _pingDelay(): number;
|
|
22
58
|
/** {@inheritDoc} */
|
|
23
59
|
get props(): Readonly<CbConnProps>;
|
|
24
60
|
/** {@inheritDoc} */
|
|
25
|
-
|
|
26
|
-
/** {@inheritDoc} */
|
|
27
|
-
value(value: unknown): string;
|
|
28
|
-
/** {@inheritDoc} */
|
|
29
|
-
flatten<T>(result: QueryResult<T>): Array<T>;
|
|
30
|
-
/** {@inheritDoc} */
|
|
31
|
-
castDates<T>(given: ReplaceType<T, Date, string> | T, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
|
|
32
|
-
/** {@inheritDoc} */
|
|
33
|
-
castDatesForList<T>(given: Array<ReplaceType<T, Date, string> | T>, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
|
|
61
|
+
get bucketName(): string;
|
|
34
62
|
/** {@inheritDoc} */
|
|
35
|
-
|
|
63
|
+
get scopeName(): string;
|
|
36
64
|
/** {@inheritDoc} */
|
|
37
|
-
|
|
65
|
+
get cluster(): Cluster;
|
|
38
66
|
/** {@inheritDoc} */
|
|
39
67
|
connect(): Promise<boolean>;
|
|
40
68
|
/** {@inheritDoc} */
|
|
@@ -45,10 +45,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
45
45
|
exports.CbConnection = void 0;
|
|
46
46
|
const couchbase = __importStar(require("couchbase"));
|
|
47
47
|
const config_1 = require("../config");
|
|
48
|
-
const common_1 = require("@leyyo/common");
|
|
49
|
-
const filter_1 = require("../filter");
|
|
50
48
|
const be_db_common_1 = require("@samet-it/be-db-common");
|
|
51
|
-
const
|
|
49
|
+
const be_base_common_1 = require("@samet-it/be-base-common");
|
|
52
50
|
/**
|
|
53
51
|
* Couchbase connection abstract class
|
|
54
52
|
* */
|
|
@@ -61,7 +59,6 @@ class CbConnection extends be_db_common_1.DbConnection {
|
|
|
61
59
|
* */
|
|
62
60
|
constructor(opt) {
|
|
63
61
|
super(opt);
|
|
64
|
-
filter_1.cbFilterUtil.$set(this);
|
|
65
62
|
this._readEnv();
|
|
66
63
|
}
|
|
67
64
|
// region protected-method
|
|
@@ -71,10 +68,11 @@ class CbConnection extends be_db_common_1.DbConnection {
|
|
|
71
68
|
_readEnv() {
|
|
72
69
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
73
70
|
const { _props: props } = this;
|
|
74
|
-
let env = config_1.couchbaseCommonConfig.
|
|
71
|
+
let env = config_1.couchbaseCommonConfig.value;
|
|
75
72
|
if (props.envVariant) {
|
|
76
|
-
env = config_1.couchbaseCommonConfig.configure.getVariation(props.envVariant).
|
|
73
|
+
env = config_1.couchbaseCommonConfig.configure.getVariation(props.envVariant).value;
|
|
77
74
|
}
|
|
75
|
+
props.isEnabled = props.isEnabled == env.ENABLED;
|
|
78
76
|
props.protocol = (_a = props.protocol) !== null && _a !== void 0 ? _a : env.PROTOCOL;
|
|
79
77
|
props.host = (_b = props.host) !== null && _b !== void 0 ? _b : env.HOST;
|
|
80
78
|
props.port = (_c = props.port) !== null && _c !== void 0 ? _c : env.PORT;
|
|
@@ -84,173 +82,77 @@ class CbConnection extends be_db_common_1.DbConnection {
|
|
|
84
82
|
props.scopeName = (_g = props.scopeName) !== null && _g !== void 0 ? _g : env.SCOPE;
|
|
85
83
|
props.createIndices = (_h = props.createIndices) !== null && _h !== void 0 ? _h : env.CREATE_INDICES;
|
|
86
84
|
}
|
|
85
|
+
get _connectionDelay() {
|
|
86
|
+
return this._delayWithJitter(this._props.connectTryCount, CbConnection.CONN_BASE_DELAY, CbConnection.CONN_MAX_DELAY);
|
|
87
|
+
}
|
|
88
|
+
get _pingDelay() {
|
|
89
|
+
return this._delayWithJitter(this._props.pingTryCount, CbConnection.PING_BASE_DELAY, CbConnection.PING_MAX_DELAY);
|
|
90
|
+
}
|
|
87
91
|
// endregion protected-method
|
|
88
92
|
// region getter
|
|
89
93
|
/** {@inheritDoc} */
|
|
90
94
|
get props() {
|
|
91
95
|
return this._props;
|
|
92
96
|
}
|
|
93
|
-
// endregion getter
|
|
94
|
-
// region field-value
|
|
95
|
-
/** {@inheritDoc} */
|
|
96
|
-
field(field) {
|
|
97
|
-
field = field.trim();
|
|
98
|
-
if (field.includes('.')) {
|
|
99
|
-
return field.split('.').map(part => this.field(part)).join('.');
|
|
100
|
-
}
|
|
101
|
-
return field.startsWith('`') ? field : `\`${field}\``;
|
|
102
|
-
}
|
|
103
|
-
/** {@inheritDoc} */
|
|
104
|
-
value(value) {
|
|
105
|
-
var _a;
|
|
106
|
-
switch (typeof value) {
|
|
107
|
-
case "string":
|
|
108
|
-
return JSON.stringify(value);
|
|
109
|
-
case "number":
|
|
110
|
-
return `${value}`;
|
|
111
|
-
case "boolean":
|
|
112
|
-
return value ? `true` : 'false';
|
|
113
|
-
case "object":
|
|
114
|
-
if (value) {
|
|
115
|
-
return (0, common_1.secureJson)(value);
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
return 'null';
|
|
119
|
-
}
|
|
120
|
-
case "bigint":
|
|
121
|
-
return `"${value.toString(10)}"`;
|
|
122
|
-
case "symbol":
|
|
123
|
-
return JSON.stringify(value.description);
|
|
124
|
-
case "function":
|
|
125
|
-
return JSON.stringify(`${(_a = value.name) !== null && _a !== void 0 ? _a : '~fn~'}[${value.length}]()`);
|
|
126
|
-
}
|
|
127
|
-
return 'null';
|
|
128
|
-
}
|
|
129
97
|
/** {@inheritDoc} */
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return result.rows;
|
|
133
|
-
}
|
|
134
|
-
return [];
|
|
98
|
+
get bucketName() {
|
|
99
|
+
return this._props.bucketName;
|
|
135
100
|
}
|
|
136
|
-
// endregion field-value
|
|
137
|
-
// region transcoders
|
|
138
101
|
/** {@inheritDoc} */
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
if (!given || typeof given !== 'object' && Array.isArray(given)) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
const doc = given;
|
|
147
|
-
fields.forEach(field => {
|
|
148
|
-
const val = doc[field];
|
|
149
|
-
if (typeof val === 'string' || (typeof val === 'number' && val > 0 && Number.isInteger(val))) {
|
|
150
|
-
doc[field] = new Date(val);
|
|
151
|
-
}
|
|
152
|
-
});
|
|
102
|
+
get scopeName() {
|
|
103
|
+
return this._props.scopeName;
|
|
153
104
|
}
|
|
154
105
|
/** {@inheritDoc} */
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
if (!given || !Array.isArray(given) || given.length < 1) {
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
const arr = given;
|
|
163
|
-
arr.forEach(item => this.castDates(item, ...fields));
|
|
164
|
-
}
|
|
165
|
-
// endregion transcoders
|
|
166
|
-
// region query
|
|
167
|
-
/** {@inheritDoc} */
|
|
168
|
-
more(link, lines, p1) {
|
|
169
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
170
|
-
const opt = this.buildOpt(p1);
|
|
171
|
-
const sql = (0, type_1.isObjectValid)(lines) ? lines.end() : lines;
|
|
172
|
-
if (opt.printSql) {
|
|
173
|
-
this.logger.log(`${link.path} > SQL${opt.name}`, sql);
|
|
174
|
-
}
|
|
175
|
-
try {
|
|
176
|
-
const result = yield link.scope.query(sql, opt);
|
|
177
|
-
return {
|
|
178
|
-
success: true,
|
|
179
|
-
rows: result.rows,
|
|
180
|
-
meta: result.meta,
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
catch (err) {
|
|
184
|
-
this.checkError(err, Object.assign(Object.assign({}, opt), { sql }));
|
|
185
|
-
return {
|
|
186
|
-
success: false,
|
|
187
|
-
rows: [],
|
|
188
|
-
error: err,
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
/** {@inheritDoc} */
|
|
194
|
-
one(link, lines, p1) {
|
|
195
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
196
|
-
const opt = this.buildOpt(p1);
|
|
197
|
-
const sql = (0, type_1.isObjectValid)(lines) ? lines.end() : lines;
|
|
198
|
-
if (opt.printSql) {
|
|
199
|
-
this.logger.log(`${link.path} > SQL${opt.name}`, sql);
|
|
200
|
-
}
|
|
201
|
-
try {
|
|
202
|
-
const result = yield link.scope.query(sql, opt);
|
|
203
|
-
return {
|
|
204
|
-
success: true,
|
|
205
|
-
row: (Array.isArray(result.rows) && result.rows.length > 0) ? result.rows[0] : undefined,
|
|
206
|
-
meta: result.meta,
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
catch (err) {
|
|
210
|
-
this.checkError(err, Object.assign(Object.assign({}, opt), { sql }));
|
|
211
|
-
return {
|
|
212
|
-
success: false,
|
|
213
|
-
row: undefined,
|
|
214
|
-
error: err,
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
});
|
|
106
|
+
get cluster() {
|
|
107
|
+
return this._props.cluster;
|
|
218
108
|
}
|
|
219
|
-
// endregion
|
|
109
|
+
// endregion getter
|
|
220
110
|
// region connect
|
|
221
111
|
/** {@inheritDoc} */
|
|
222
112
|
connect() {
|
|
223
113
|
return __awaiter(this, void 0, void 0, function* () {
|
|
224
114
|
const { _props: props } = this;
|
|
225
|
-
if (
|
|
115
|
+
if (!this.isEnabled) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (this.isConnected) {
|
|
226
119
|
return true;
|
|
227
120
|
}
|
|
121
|
+
if (this._props.connectTryCount > CbConnection.CONN_TRY_COUNT) {
|
|
122
|
+
const err = new be_db_common_1.DbError('Maximum try county', { tryCount: this._props.connectTryCount });
|
|
123
|
+
this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect'));
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
228
126
|
try {
|
|
229
127
|
props.cluster = yield couchbase.connect(`${props.protocol}://${props.host}`, {
|
|
230
128
|
username: props.username,
|
|
231
129
|
password: props.password,
|
|
232
130
|
});
|
|
233
131
|
props.isConnected = true;
|
|
234
|
-
|
|
132
|
+
props.connectTryCount = 0;
|
|
133
|
+
this._triggerOnCase('connected', this._onConnected, false);
|
|
134
|
+
if (props.isFirst === undefined) {
|
|
135
|
+
this._triggerOnCase('first-connected', this._onFirstConnected, true);
|
|
136
|
+
props.isFirst = false;
|
|
137
|
+
}
|
|
138
|
+
// clear stopped ping
|
|
139
|
+
if (this._props.pingTryCount > CbConnection.PING_TRY_COUNT) {
|
|
140
|
+
this._props.pingTryCount = 0;
|
|
141
|
+
setTimeout(() => this.ping(true).then(), 30000);
|
|
142
|
+
}
|
|
143
|
+
props.isConnected = true;
|
|
144
|
+
this.logger.info('Connected');
|
|
235
145
|
setTimeout(() => this.ping(true).then(), 30000);
|
|
236
146
|
}
|
|
237
147
|
catch (e) {
|
|
238
|
-
this.
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (
|
|
243
|
-
|
|
244
|
-
this._onFirstConnected = [];
|
|
148
|
+
this.logger.warn(be_base_common_1.errorHandler.common.logText(e, 'connection', this._props.connectTryCount));
|
|
149
|
+
props.connectTryCount++;
|
|
150
|
+
const old = this.isConnected;
|
|
151
|
+
props.isConnected = false;
|
|
152
|
+
if (old) {
|
|
153
|
+
this._triggerOnCase('disconnected', this._onDisconnected, false);
|
|
245
154
|
}
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
if (this._onConnected.length > 0) {
|
|
249
|
-
this._onConnected.forEach(fn => {
|
|
250
|
-
fn().then().catch(e => {
|
|
251
|
-
this.logger.error(`OnConnected <${e.name}> ${e.message}`);
|
|
252
|
-
});
|
|
253
|
-
});
|
|
155
|
+
setTimeout(() => this.connect().then(), this._connectionDelay);
|
|
254
156
|
}
|
|
255
157
|
return true;
|
|
256
158
|
});
|
|
@@ -258,14 +160,27 @@ class CbConnection extends be_db_common_1.DbConnection {
|
|
|
258
160
|
/** {@inheritDoc} */
|
|
259
161
|
ping(next) {
|
|
260
162
|
return __awaiter(this, void 0, void 0, function* () {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
try {
|
|
264
|
-
yield this.exec(props.cluster.ping(), { name: 'ping' });
|
|
265
|
-
result = true;
|
|
163
|
+
if (!this.isEnabled) {
|
|
164
|
+
return false;
|
|
266
165
|
}
|
|
267
|
-
|
|
268
|
-
|
|
166
|
+
if (this._props.pingTryCount > CbConnection.PING_TRY_COUNT) {
|
|
167
|
+
const err = new be_db_common_1.DbError('Maximum try county', { tryCount: this._props.pingTryCount });
|
|
168
|
+
this.logger.error(be_base_common_1.errorHandler.common.logText(err, 'connect'));
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
let result = false;
|
|
172
|
+
if (this.isConnected) {
|
|
173
|
+
try {
|
|
174
|
+
yield this.cluster.ping();
|
|
175
|
+
this._props.pingTryCount = 0;
|
|
176
|
+
result = true;
|
|
177
|
+
}
|
|
178
|
+
catch (err) {
|
|
179
|
+
this._props.pingTryCount++;
|
|
180
|
+
this.logger.warn(be_base_common_1.errorHandler.common.logText(err, 'ping', this._props.pingTryCount));
|
|
181
|
+
setTimeout(() => this.ping(true).then(), this._pingDelay);
|
|
182
|
+
next = false;
|
|
183
|
+
}
|
|
269
184
|
}
|
|
270
185
|
if (next) {
|
|
271
186
|
setTimeout(() => this.ping(true).then(), 30000);
|
|
@@ -275,3 +190,40 @@ class CbConnection extends be_db_common_1.DbConnection {
|
|
|
275
190
|
}
|
|
276
191
|
}
|
|
277
192
|
exports.CbConnection = CbConnection;
|
|
193
|
+
// region protected-property
|
|
194
|
+
/**
|
|
195
|
+
* Base/starting/min delay for connection
|
|
196
|
+
*
|
|
197
|
+
* @type {number} - as seconds
|
|
198
|
+
* */
|
|
199
|
+
CbConnection.CONN_BASE_DELAY = 500; // 0.5 sec
|
|
200
|
+
/**
|
|
201
|
+
* Last/max delay for connection
|
|
202
|
+
*
|
|
203
|
+
* @type {number} - as seconds
|
|
204
|
+
* */
|
|
205
|
+
CbConnection.CONN_MAX_DELAY = 1000 * 60 * 60; // 60 minutes
|
|
206
|
+
/**
|
|
207
|
+
* Try count
|
|
208
|
+
*
|
|
209
|
+
* @type {number} - times
|
|
210
|
+
* */
|
|
211
|
+
CbConnection.CONN_TRY_COUNT = 100; // 100 times
|
|
212
|
+
/**
|
|
213
|
+
* Base/starting/min delay for ping
|
|
214
|
+
*
|
|
215
|
+
* @type {number} - as seconds
|
|
216
|
+
* */
|
|
217
|
+
CbConnection.PING_BASE_DELAY = 200; // 0.2 sec
|
|
218
|
+
/**
|
|
219
|
+
* Last/max delay ping
|
|
220
|
+
*
|
|
221
|
+
* @type {number} - as seconds
|
|
222
|
+
* */
|
|
223
|
+
CbConnection.PING_MAX_DELAY = 1000 * 60 * 10; // 10 minutes
|
|
224
|
+
/**
|
|
225
|
+
* Try count
|
|
226
|
+
*
|
|
227
|
+
* @type {number} - times
|
|
228
|
+
* */
|
|
229
|
+
CbConnection.PING_TRY_COUNT = 100; // 100 times
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import type { Cluster
|
|
1
|
+
import type { Cluster } from "couchbase";
|
|
2
2
|
import type { IgnoreFieldsByType, KeyValue, ReplaceType, StrKey } from "@leyyo/common";
|
|
3
3
|
import type { CbRepoDirectOpt, CbRepoLike } from "../repo";
|
|
4
|
-
import type { DbConnectionLike, DbConnOpt, DbConnProps
|
|
4
|
+
import type { DbConnDirectOpt, DbConnectionLike, DbConnOpt, DbConnProps } from "@samet-it/be-db-common";
|
|
5
5
|
import type { DefDims, Entity, UrnDocLike, Pair, Portion, View, UrnDef } from "@samet-it/be-base-common";
|
|
6
6
|
/**
|
|
7
7
|
* Couchbase connection interface
|
|
8
8
|
* */
|
|
9
|
-
export interface CbConnectionLike extends DbConnectionLike
|
|
9
|
+
export interface CbConnectionLike extends DbConnectionLike {
|
|
10
10
|
/** @inheritDoc */
|
|
11
11
|
get props(): Readonly<CbConnProps>;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Shortcut to {@link CbConnOpt#bucketName}
|
|
14
14
|
* */
|
|
15
|
-
|
|
15
|
+
get bucketName(): string;
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
17
|
+
* Shortcut to {@link CbConnOpt#scopeName}
|
|
18
18
|
* */
|
|
19
|
-
|
|
19
|
+
get scopeName(): string;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Shortcut to {@link CbConnProps#cluster}
|
|
22
22
|
* */
|
|
23
|
-
|
|
23
|
+
get cluster(): Cluster;
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* Couchbase connection option interface
|
|
@@ -45,17 +45,6 @@ export interface CbConnOpt extends DbConnOpt {
|
|
|
45
45
|
* */
|
|
46
46
|
createIndices?: boolean;
|
|
47
47
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Couchbase direct connection option interface
|
|
50
|
-
* */
|
|
51
|
-
export interface CbDirectConnOpt extends CbConnOpt {
|
|
52
|
-
/**
|
|
53
|
-
* Logger name
|
|
54
|
-
*
|
|
55
|
-
* @type {string}
|
|
56
|
-
* */
|
|
57
|
-
name?: string;
|
|
58
|
-
}
|
|
59
48
|
/**
|
|
60
49
|
* Couchbase connection props interface
|
|
61
50
|
* */
|
|
@@ -68,9 +57,10 @@ export interface CbConnProps extends CbConnOpt, DbConnProps {
|
|
|
68
57
|
cluster: Cluster;
|
|
69
58
|
}
|
|
70
59
|
/**
|
|
71
|
-
* Couchbase
|
|
60
|
+
* Couchbase direct connection option interface
|
|
72
61
|
* */
|
|
73
|
-
export
|
|
62
|
+
export interface CbDirectConnOpt extends CbConnOpt, DbConnDirectOpt {
|
|
63
|
+
}
|
|
74
64
|
/**
|
|
75
65
|
* Direct connection to use outside dependency injection
|
|
76
66
|
* */
|
|
@@ -94,15 +84,5 @@ export interface CbDirectConnectionLike extends CbConnectionLike {
|
|
|
94
84
|
* */
|
|
95
85
|
newRepo<ENT extends Entity<ID>, ID extends KeyValue = KeyValue, URN extends UrnDocLike = UrnDef, VIEW extends View<ID> = ENT, PAIR extends Pair<ID> = Pair<ID>, PORTION extends Portion<ID> = Portion<ID>, KEYS extends string = StrKey<ENT>, DIMS extends DefDims = DefDims, R = unknown>(opt: CbRepoDirectOpt<ENT, ID, URN, DIMS, R>): CbRepoLike<ENT, ID, URN, VIEW, PAIR, PORTION, KEYS, DIMS>;
|
|
96
86
|
}
|
|
97
|
-
export type CbIgnoreDate<T> =
|
|
98
|
-
export type CbIgnoreDateKeys<T> =
|
|
99
|
-
type CbIgnoreFields<T, I> = {
|
|
100
|
-
[K in keyof T]: T[K] extends I ? K : never;
|
|
101
|
-
}[keyof T];
|
|
102
|
-
type CbReplaceType<T, O, N> = {
|
|
103
|
-
[P in keyof T]: T[P] extends O ? N : T[P];
|
|
104
|
-
};
|
|
105
|
-
export interface CbRepoLink extends DbRepoLink {
|
|
106
|
-
scope: Scope;
|
|
107
|
-
}
|
|
108
|
-
export {};
|
|
87
|
+
export type CbIgnoreDate<T> = ReplaceType<T, Date, string>;
|
|
88
|
+
export type CbIgnoreDateKeys<T> = IgnoreFieldsByType<T, Date>;
|
|
@@ -53,10 +53,10 @@ class CbFilterUtilImpl {
|
|
|
53
53
|
if (Array.isArray(items) && items.length > 0) {
|
|
54
54
|
items = items.map(v => (v instanceof Date) ? v.toISOString() : v);
|
|
55
55
|
if (items.length === 1) {
|
|
56
|
-
return `${this._where(opt)} ${this.
|
|
56
|
+
return `${this._where(opt)} ${this.repo.f(field)} = ${this.repo.v(items[0])}`;
|
|
57
57
|
}
|
|
58
58
|
else {
|
|
59
|
-
return `${this._where(opt)} ${this.
|
|
59
|
+
return `${this._where(opt)} ${this.repo.f(field)} IN ${this.repo.v(items[0])}`;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
return undefined;
|
|
@@ -64,7 +64,7 @@ class CbFilterUtilImpl {
|
|
|
64
64
|
/** @inheritDoc */
|
|
65
65
|
sqlBool(opt, field, value) {
|
|
66
66
|
if (typeof value === 'boolean') {
|
|
67
|
-
return `${this._where(opt)} ${this.
|
|
67
|
+
return `${this._where(opt)} ${this.repo.f(field)} = ${value ? 'true' : 'false'}`;
|
|
68
68
|
}
|
|
69
69
|
return undefined;
|
|
70
70
|
}
|
|
@@ -73,20 +73,20 @@ class CbFilterUtilImpl {
|
|
|
73
73
|
if (Array.isArray(value) && value.length > 0) {
|
|
74
74
|
const [first, last] = value.map(v => (v instanceof Date) ? v.toISOString() : v);
|
|
75
75
|
if (first !== undefined && last !== undefined) {
|
|
76
|
-
return `${this._where(opt)} ${this.
|
|
76
|
+
return `${this._where(opt)} ${this.repo.f(field)} BETWEEN ${this.repo.v(first)} AND ${this.repo.v(last)}`;
|
|
77
77
|
}
|
|
78
78
|
else if (first !== undefined && last === undefined) {
|
|
79
|
-
return `${this._where(opt)} ${this.
|
|
79
|
+
return `${this._where(opt)} ${this.repo.f(field)} >= ${this.repo.v(first)}`;
|
|
80
80
|
}
|
|
81
81
|
else if (first === undefined && last !== undefined) {
|
|
82
|
-
return `${this._where(opt)} ${this.
|
|
82
|
+
return `${this._where(opt)} ${this.repo.f(field)} <= ${this.repo.v(last)}`;
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
85
|
return undefined;
|
|
86
86
|
}
|
|
87
|
-
$set(
|
|
88
|
-
if (!this.
|
|
89
|
-
this.
|
|
87
|
+
$set(repo) {
|
|
88
|
+
if (!this.repo) {
|
|
89
|
+
this.repo = repo;
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { CbConnectionLike } from "../connection";
|
|
2
1
|
import type { DbLines } from "@samet-it/be-db-common";
|
|
2
|
+
import { CbRepoLike } from "../repo";
|
|
3
|
+
import { Entity } from "@samet-it/be-base-common";
|
|
3
4
|
export type FilterCastLambda<T> = (v: unknown) => T;
|
|
4
5
|
export interface CbFilterUtil {
|
|
5
6
|
/**
|
|
@@ -26,7 +27,7 @@ export interface CbFilterUtil {
|
|
|
26
27
|
* Builds a sql for between
|
|
27
28
|
* */
|
|
28
29
|
sqlBetween(opt: CbFilterUtilWhereOpt, field: string, value: [unknown, unknown]): string | undefined;
|
|
29
|
-
$set(
|
|
30
|
+
$set(repo: CbRepoLike<Entity>): void;
|
|
30
31
|
}
|
|
31
32
|
export interface CbFilterUtilWhereOpt {
|
|
32
33
|
lines: DbLines;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
exports.CbDirectRepo = void 0;
|
|
13
4
|
const cb_repo_1 = require("./cb.repo");
|
|
@@ -33,21 +24,10 @@ class CbDirectRepo extends cb_repo_1.CbRepo {
|
|
|
33
24
|
* @param {CbRepoDirectOpt} opt - options
|
|
34
25
|
* */
|
|
35
26
|
constructor(conn, opt) {
|
|
27
|
+
var _a;
|
|
36
28
|
super(conn, opt);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
opt.name = this._props.urnPrefix;
|
|
40
|
-
}
|
|
41
|
-
if (typeof opt.toUrnTuple === 'function') {
|
|
42
|
-
this.$toUrnTuple = opt.toUrnTuple;
|
|
43
|
-
}
|
|
44
|
-
if (typeof opt.toDim === 'function') {
|
|
45
|
-
this.$toDim = (doc, dim) => __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
return (yield opt.toDim(doc, dim));
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
this.logger = be_base_common_1.logger.of(`CbRepo${(opt === null || opt === void 0 ? void 0 : opt.name) ? '#' + (opt === null || opt === void 0 ? void 0 : opt.name) : ''}`);
|
|
29
|
+
const logName = (_a = opt === null || opt === void 0 ? void 0 : opt.name) !== null && _a !== void 0 ? _a : this._props.urnPrefix;
|
|
30
|
+
this.logger = be_base_common_1.logger.create(`CbRepo${logName ? '#' + logName : ''}`);
|
|
51
31
|
}
|
|
52
32
|
}
|
|
53
33
|
exports.CbDirectRepo = CbDirectRepo;
|