@samet-it/be-couchbase-common 1.0.9 → 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/dist/adapter/cb-adapter.service.d.ts +2 -19
- package/dist/adapter/cb-adapter.service.js +10 -133
- package/dist/adapter/index.types.d.ts +1 -31
- package/dist/filter/cb-filter-util.impl.js +11 -6
- package/dist/repo/cb.repo.d.ts +17 -0
- package/dist/repo/cb.repo.js +120 -46
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Cluster,
|
|
1
|
+
import { Cluster, QueryResult } from 'couchbase';
|
|
2
2
|
import type { IgnoreFieldsByType, ReplaceType } from "@leyyo/common";
|
|
3
|
-
import type { CbAdapterServiceLike,
|
|
3
|
+
import type { CbAdapterServiceLike, CbExecOpt, CbQueryResultMore, CbQueryResultOne } from "./index.types";
|
|
4
4
|
import type { CbRepoDef } from "../repo";
|
|
5
5
|
export declare class CbAdapterService implements CbAdapterServiceLike {
|
|
6
6
|
private readonly _CLEAR_ERROR_INTERVAL;
|
|
@@ -17,25 +17,14 @@ export declare class CbAdapterService implements CbAdapterServiceLike {
|
|
|
17
17
|
* Ping to DB
|
|
18
18
|
* */
|
|
19
19
|
private _ping;
|
|
20
|
-
/**
|
|
21
|
-
* Execute the creation of index
|
|
22
|
-
* */
|
|
23
|
-
private _createIndex;
|
|
24
|
-
/**
|
|
25
|
-
* Build json format of CB object
|
|
26
|
-
* */
|
|
27
|
-
private _toJson;
|
|
28
20
|
protected _checkError(err: Error, opt: CbExecOpt): void;
|
|
29
21
|
/** {@inheritDoc} */
|
|
30
22
|
f(field: string): string;
|
|
31
23
|
/** {@inheritDoc} */
|
|
32
24
|
field(field: string): string;
|
|
33
|
-
s(value: string): string;
|
|
34
25
|
v(value: unknown): string;
|
|
35
26
|
value(value: unknown): string;
|
|
36
27
|
/** {@inheritDoc} */
|
|
37
|
-
string(value: string): string;
|
|
38
|
-
/** {@inheritDoc} */
|
|
39
28
|
flatten<T>(result: QueryResult<T>): Array<T>;
|
|
40
29
|
/** {@inheritDoc} */
|
|
41
30
|
rows<T>(rows: Array<T>): Array<T>;
|
|
@@ -44,12 +33,6 @@ export declare class CbAdapterService implements CbAdapterServiceLike {
|
|
|
44
33
|
/** {@inheritDoc} */
|
|
45
34
|
first<T>(rows: Array<T>): T | undefined;
|
|
46
35
|
/** {@inheritDoc} */
|
|
47
|
-
createIndex<T = Record<string, unknown>>(coll: Collection, p1: (keyof T | string) | Array<keyof T | string>, name?: string): Promise<void>;
|
|
48
|
-
/** {@inheritDoc} */
|
|
49
|
-
createBaseIndices(coll: Collection, ...keys: Array<keyof CbEntity>): Promise<void>;
|
|
50
|
-
/** {@inheritDoc} */
|
|
51
|
-
createPk(coll: Collection): Promise<void>;
|
|
52
|
-
/** {@inheritDoc} */
|
|
53
36
|
castDates<T>(given: ReplaceType<T, Date, string> | T, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
|
|
54
37
|
/** {@inheritDoc} */
|
|
55
38
|
castDatesForList<T>(given: Array<ReplaceType<T, Date, string> | T>, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
|
|
@@ -54,9 +54,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
54
54
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
55
55
|
exports.CbAdapterService = void 0;
|
|
56
56
|
const couchbase = __importStar(require("couchbase"));
|
|
57
|
-
const couchbase_1 = require("couchbase");
|
|
58
57
|
const common_1 = require("@nestjs/common");
|
|
59
58
|
const config_1 = require("../config");
|
|
59
|
+
const common_2 = require("@leyyo/common");
|
|
60
60
|
// noinspection JSUnusedGlobalSymbols
|
|
61
61
|
let CbAdapterService = class CbAdapterService {
|
|
62
62
|
constructor() {
|
|
@@ -86,68 +86,6 @@ let CbAdapterService = class CbAdapterService {
|
|
|
86
86
|
setTimeout(() => this._ping(), 30000);
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Execute the creation of index
|
|
91
|
-
* */
|
|
92
|
-
_createIndex(scope, sql) {
|
|
93
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
-
var _a;
|
|
95
|
-
try {
|
|
96
|
-
return yield scope.query(sql);
|
|
97
|
-
}
|
|
98
|
-
catch (e) {
|
|
99
|
-
if (e instanceof couchbase_1.IndexExistsError) {
|
|
100
|
-
return undefined;
|
|
101
|
-
}
|
|
102
|
-
this.logger.error(`Index error => [${e.name}: ${e.message}] ${JSON.stringify({
|
|
103
|
-
scope: scope === null || scope === void 0 ? void 0 : scope.name,
|
|
104
|
-
bucket: (_a = scope === null || scope === void 0 ? void 0 : scope.bucket) === null || _a === void 0 ? void 0 : _a.name
|
|
105
|
-
})}`);
|
|
106
|
-
// throw e; // TODO
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Build json format of CB object
|
|
112
|
-
* */
|
|
113
|
-
_toJson(val) {
|
|
114
|
-
if ([undefined, null].includes(val)) {
|
|
115
|
-
return 'null';
|
|
116
|
-
}
|
|
117
|
-
switch (typeof val) {
|
|
118
|
-
case "string":
|
|
119
|
-
case "number":
|
|
120
|
-
case "bigint":
|
|
121
|
-
case "boolean":
|
|
122
|
-
return JSON.stringify(val);
|
|
123
|
-
case "object":
|
|
124
|
-
if (Array.isArray(val)) {
|
|
125
|
-
return '[' + val
|
|
126
|
-
.filter(v => v !== undefined)
|
|
127
|
-
.map(v => this._toJson(v))
|
|
128
|
-
.join(', ') + ']';
|
|
129
|
-
}
|
|
130
|
-
else if (val instanceof Map) {
|
|
131
|
-
return this._toJson(Object.fromEntries(val.entries()));
|
|
132
|
-
}
|
|
133
|
-
else if (val instanceof Set) {
|
|
134
|
-
return this._toJson(Array.from(val.values()));
|
|
135
|
-
}
|
|
136
|
-
else {
|
|
137
|
-
return '{' + Array.from(Object.entries(val))
|
|
138
|
-
.filter(item => item[1] !== undefined)
|
|
139
|
-
.map(item => {
|
|
140
|
-
const [k, v] = item;
|
|
141
|
-
return '`' + k + '`: ' + this._toJson(v);
|
|
142
|
-
}).join(', ') + '}';
|
|
143
|
-
}
|
|
144
|
-
case "symbol":
|
|
145
|
-
return JSON.stringify(val.description);
|
|
146
|
-
case "function":
|
|
147
|
-
case "undefined":
|
|
148
|
-
return 'null';
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
89
|
_checkError(err, opt) {
|
|
152
90
|
var _a, _b, _c;
|
|
153
91
|
let size = 1;
|
|
@@ -162,38 +100,26 @@ let CbAdapterService = class CbAdapterService {
|
|
|
162
100
|
this.logger.warn(`[${size}] ${(_b = opt.name) !== null && _b !== void 0 ? _b : 'Ignored error'} => [${err.name}: ${err.message}]`);
|
|
163
101
|
return;
|
|
164
102
|
}
|
|
165
|
-
this.logger.error(`[${size}] ${(_c = opt.name) !== null && _c !== void 0 ? _c : '
|
|
103
|
+
this.logger.error(`[${size}] ${(_c = opt.name) !== null && _c !== void 0 ? _c : 'Database error'} => [${err.name}: ${err.message}]`, JSON.stringify(err.context));
|
|
166
104
|
throw err;
|
|
167
105
|
}
|
|
168
106
|
/** {@inheritDoc} */
|
|
169
107
|
f(field) {
|
|
170
|
-
|
|
171
|
-
return field.split('.').map(part => this.f(part)).join('.');
|
|
172
|
-
}
|
|
173
|
-
else if (field.startsWith('`')) {
|
|
174
|
-
return field;
|
|
175
|
-
}
|
|
176
|
-
return `\`${field}\``;
|
|
108
|
+
return this.field(field);
|
|
177
109
|
}
|
|
178
110
|
/** {@inheritDoc} */
|
|
179
111
|
field(field) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (typeof value === 'string') {
|
|
184
|
-
return value.includes('"') ? this._toJson(value) : `"${value}"`;
|
|
112
|
+
field = field.trim();
|
|
113
|
+
if (field.includes('.')) {
|
|
114
|
+
return field.split('.').map(part => this.field(part)).join('.');
|
|
185
115
|
}
|
|
186
|
-
return
|
|
116
|
+
return field.startsWith('`') ? field : `\`${field}\``;
|
|
187
117
|
}
|
|
188
118
|
v(value) {
|
|
189
|
-
return this.
|
|
119
|
+
return this.value(value);
|
|
190
120
|
}
|
|
191
121
|
value(value) {
|
|
192
|
-
return
|
|
193
|
-
}
|
|
194
|
-
/** {@inheritDoc} */
|
|
195
|
-
string(value) {
|
|
196
|
-
return this.s(value);
|
|
122
|
+
return common_2.$dev.secureJson(value);
|
|
197
123
|
}
|
|
198
124
|
/** {@inheritDoc} */
|
|
199
125
|
flatten(result) {
|
|
@@ -224,55 +150,6 @@ let CbAdapterService = class CbAdapterService {
|
|
|
224
150
|
return undefined;
|
|
225
151
|
}
|
|
226
152
|
/** {@inheritDoc} */
|
|
227
|
-
createIndex(coll, p1, name) {
|
|
228
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
-
if (Array.isArray(p1)) {
|
|
230
|
-
const fields = p1.map(f => f.split('.').map(part => '`' + part + '`').join('.'));
|
|
231
|
-
name = name.split('.').join('_').split('-').join('_');
|
|
232
|
-
if (!name.startsWith('idx_')) {
|
|
233
|
-
name = 'idx_' + name;
|
|
234
|
-
}
|
|
235
|
-
yield this._createIndex(coll.scope, `CREATE INDEX \`${name}\` ON \`${coll.name}\` (${fields.join(', ')})`);
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
const field = p1;
|
|
239
|
-
if (!name) {
|
|
240
|
-
name = 'idx_' + field.split('.').join('_').split('-').join('_');
|
|
241
|
-
}
|
|
242
|
-
else if (!name.startsWith('idx_')) {
|
|
243
|
-
name = 'idx_' + name;
|
|
244
|
-
}
|
|
245
|
-
yield this._createIndex(coll.scope, `CREATE INDEX \`${name}\` ON \`${coll.name}\` (${field.split('.').map(part => '`' + part + '`').join('.')})`);
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
/** {@inheritDoc} */
|
|
250
|
-
createBaseIndices(coll, ...keys) {
|
|
251
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
252
|
-
if (keys.includes('id')) {
|
|
253
|
-
yield this.createIndex(coll, 'id');
|
|
254
|
-
}
|
|
255
|
-
if (keys.includes('createdAt')) {
|
|
256
|
-
yield this.createIndex(coll, 'createdAt');
|
|
257
|
-
}
|
|
258
|
-
if (keys.includes('updatedAt')) {
|
|
259
|
-
yield this.createIndex(coll, 'updatedAt');
|
|
260
|
-
}
|
|
261
|
-
if (keys.includes('_trashId')) {
|
|
262
|
-
yield this.createIndex(coll, '_trashId', 'trashId');
|
|
263
|
-
}
|
|
264
|
-
if (keys.includes('_urn')) {
|
|
265
|
-
yield this.createIndex(coll, '_urn', 'urn');
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
/** {@inheritDoc} */
|
|
270
|
-
createPk(coll) {
|
|
271
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
272
|
-
yield this._createIndex(coll.scope, `CREATE PRIMARY INDEX \`pk\` ON \`${coll.name}\``);
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
/** {@inheritDoc} */
|
|
276
153
|
castDates(given, ...fields) {
|
|
277
154
|
if (fields.length < 1) {
|
|
278
155
|
return;
|
|
@@ -386,7 +263,7 @@ let CbAdapterService = class CbAdapterService {
|
|
|
386
263
|
return this._cluster;
|
|
387
264
|
}
|
|
388
265
|
catch (e) {
|
|
389
|
-
this.logger.
|
|
266
|
+
this.logger.error(`Connection error => [${e.name}: ${e.message}]`);
|
|
390
267
|
throw e;
|
|
391
268
|
}
|
|
392
269
|
});
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Cluster, Collection, CouchbaseError, QueryMetaData, QueryOptions, QueryResult, Scope } from "couchbase";
|
|
2
2
|
import type { IgnoreFieldsByType, ReplaceType } from "@leyyo/common";
|
|
3
3
|
import type { CbRepoDef } from "../repo";
|
|
4
|
-
import { CouchbaseError } from "couchbase/dist/errors";
|
|
5
4
|
export interface CbAdapterServiceLike {
|
|
6
5
|
/**
|
|
7
6
|
* Field name
|
|
@@ -11,14 +10,6 @@ export interface CbAdapterServiceLike {
|
|
|
11
10
|
* Field name
|
|
12
11
|
* */
|
|
13
12
|
f(field: string): string;
|
|
14
|
-
/**
|
|
15
|
-
* Basic value: string
|
|
16
|
-
* */
|
|
17
|
-
string(value: string): string;
|
|
18
|
-
/**
|
|
19
|
-
* Basic value: string
|
|
20
|
-
* */
|
|
21
|
-
s(value: string): string;
|
|
22
13
|
/**
|
|
23
14
|
* Complex value
|
|
24
15
|
* */
|
|
@@ -43,22 +34,6 @@ export interface CbAdapterServiceLike {
|
|
|
43
34
|
* Return first row of rows
|
|
44
35
|
* */
|
|
45
36
|
first<T>(rows: Array<T>): T | undefined;
|
|
46
|
-
/**
|
|
47
|
-
* Create index for multiple fields
|
|
48
|
-
* */
|
|
49
|
-
createIndex<T = Record<string, unknown>>(coll: Collection, fields: Array<keyof T | string>, name: string): Promise<void>;
|
|
50
|
-
/**
|
|
51
|
-
* Create index for a field
|
|
52
|
-
* */
|
|
53
|
-
createIndex<T = Record<string, unknown>>(coll: Collection, field: keyof T | string, name?: string): Promise<void>;
|
|
54
|
-
/**
|
|
55
|
-
* Create base indices for system fields
|
|
56
|
-
* */
|
|
57
|
-
createBaseIndices(coll: Collection, ...keys: Array<keyof CbEntity>): Promise<void>;
|
|
58
|
-
/**
|
|
59
|
-
* Create primary key
|
|
60
|
-
* */
|
|
61
|
-
createPk(coll: Collection): Promise<void>;
|
|
62
37
|
/**
|
|
63
38
|
* Cast dates
|
|
64
39
|
* */
|
|
@@ -132,11 +107,6 @@ export interface CbEntity {
|
|
|
132
107
|
* */
|
|
133
108
|
_trashId?: string;
|
|
134
109
|
}
|
|
135
|
-
export interface CbErrorRec {
|
|
136
|
-
count: number;
|
|
137
|
-
log: boolean;
|
|
138
|
-
known?: boolean;
|
|
139
|
-
}
|
|
140
110
|
type CbIgnoreFields<T, I> = {
|
|
141
111
|
[K in keyof T]: T[K] extends I ? K : never;
|
|
142
112
|
}[keyof T];
|
|
@@ -53,17 +53,17 @@ 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)} ${field} =
|
|
56
|
+
return `${this._where(opt)} ${this.cb.f(field)} = ${this.cb.v(items[0])}`;
|
|
57
57
|
}
|
|
58
58
|
else {
|
|
59
|
-
return `${this._where(opt)} ${field} IN
|
|
59
|
+
return `${this._where(opt)} ${this.cb.f(field)} IN ${this.cb.v(items[0])}`;
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
/** @inheritDoc */
|
|
64
64
|
sqlBool(opt, field, value) {
|
|
65
65
|
if (typeof value === 'boolean') {
|
|
66
|
-
return `${this._where(opt)} ${field} = ${value ? 'true' : 'false'}`;
|
|
66
|
+
return `${this._where(opt)} ${this.cb.f(field)} = ${value ? 'true' : 'false'}`;
|
|
67
67
|
}
|
|
68
68
|
return undefined;
|
|
69
69
|
}
|
|
@@ -72,17 +72,22 @@ class CbFilterUtilImpl {
|
|
|
72
72
|
if (Array.isArray(value) && value.length === 2) {
|
|
73
73
|
const [first, last] = value.map(v => (v instanceof Date) ? v.toISOString() : v);
|
|
74
74
|
if (first !== undefined && last !== undefined) {
|
|
75
|
-
return `${this._where(opt)} ${field} BETWEEN
|
|
75
|
+
return `${this._where(opt)} ${this.cb.f(field)} BETWEEN ${this.cb.v(first)} AND ${this.cb.v(last)}`;
|
|
76
76
|
}
|
|
77
77
|
else if (first !== undefined && last === undefined) {
|
|
78
|
-
return `${this._where(opt)} ${field} >=
|
|
78
|
+
return `${this._where(opt)} ${this.cb.f(field)} >= ${this.cb.v(first)}`;
|
|
79
79
|
}
|
|
80
80
|
else if (first === undefined && last !== undefined) {
|
|
81
|
-
return `${this._where(opt)} ${field} <=
|
|
81
|
+
return `${this._where(opt)} ${this.cb.f(field)} <= ${this.cb.v(last)}`;
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
return undefined;
|
|
85
85
|
}
|
|
86
|
+
$set(cb) {
|
|
87
|
+
if (!this.cb) {
|
|
88
|
+
this.cb = cb;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
86
91
|
}
|
|
87
92
|
// noinspection JSUnusedGlobalSymbols
|
|
88
93
|
exports.cbFilterUtil = new CbFilterUtilImpl();
|
package/dist/repo/cb.repo.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLi
|
|
|
8
8
|
private _scopeName?;
|
|
9
9
|
private _bucketName?;
|
|
10
10
|
private _tryCount;
|
|
11
|
+
private _pkIndexed;
|
|
11
12
|
protected logger: Logger;
|
|
12
13
|
/** @inheritDoc */
|
|
13
14
|
readonly fullPath: string;
|
|
@@ -34,6 +35,22 @@ export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLi
|
|
|
34
35
|
* Build update sql as set and unset
|
|
35
36
|
* */
|
|
36
37
|
protected _updateSql<T>(urn: string, doc: T): string;
|
|
38
|
+
/**
|
|
39
|
+
* Execute the creation of index for one field
|
|
40
|
+
* */
|
|
41
|
+
protected _createIndex(field: keyof E | string, name?: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Execute the creation of index for complex fields
|
|
44
|
+
* */
|
|
45
|
+
protected _createIndex(fields: Array<keyof E | string>, name: string): Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Execute the creation of indexes with keys
|
|
48
|
+
* */
|
|
49
|
+
protected _createMoreIndices(...keys: Array<keyof E>): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Execute the creation of primary key
|
|
52
|
+
* */
|
|
53
|
+
protected _createPk(): Promise<void>;
|
|
37
54
|
/**
|
|
38
55
|
* Create indices
|
|
39
56
|
* */
|
package/dist/repo/cb.repo.js
CHANGED
|
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
exports.CbRepo = void 0;
|
|
14
|
+
const couchbase_1 = require("couchbase");
|
|
14
15
|
const common_1 = require("@leyyo/common");
|
|
15
16
|
const config_1 = require("../config");
|
|
16
17
|
const line_1 = require("../line");
|
|
@@ -29,9 +30,9 @@ class CbRepo {
|
|
|
29
30
|
this._tryCount = 0;
|
|
30
31
|
this.logger = new common_2.Logger(this.constructor.name);
|
|
31
32
|
if (!F_ID) {
|
|
32
|
-
F_ID =
|
|
33
|
-
F_URN =
|
|
34
|
-
F_TRASH_ID =
|
|
33
|
+
F_ID = cb.f('id');
|
|
34
|
+
F_URN = cb.f('_urn');
|
|
35
|
+
F_TRASH_ID = cb.f('_trashId');
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
/**
|
|
@@ -57,10 +58,10 @@ class CbRepo {
|
|
|
57
58
|
return keys
|
|
58
59
|
.map(item => {
|
|
59
60
|
switch (typeof item) {
|
|
60
|
-
case
|
|
61
|
+
case 'string':
|
|
61
62
|
const str = item.trim();
|
|
62
63
|
return str ? str : undefined;
|
|
63
|
-
case
|
|
64
|
+
case 'number':
|
|
64
65
|
return item;
|
|
65
66
|
default:
|
|
66
67
|
return undefined;
|
|
@@ -72,22 +73,23 @@ class CbRepo {
|
|
|
72
73
|
* Build update sql as set and unset
|
|
73
74
|
* */
|
|
74
75
|
_updateSql(urn, doc) {
|
|
76
|
+
const cb = this.cb;
|
|
75
77
|
const set = [];
|
|
76
78
|
const unset = [];
|
|
77
79
|
for (const [k, v] of Object.entries(doc)) {
|
|
78
80
|
if (!['_urn', 'id'].includes(k)) {
|
|
79
81
|
if (v === undefined) {
|
|
80
|
-
unset.push(
|
|
82
|
+
unset.push(cb.f(k));
|
|
81
83
|
}
|
|
82
84
|
else {
|
|
83
|
-
set.push([
|
|
85
|
+
set.push([cb.f(k), cb.v(v)]);
|
|
84
86
|
}
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
const lines = (0, line_1.cbLine)();
|
|
88
90
|
lines
|
|
89
91
|
.add(`UPDATE ${this.fullPath}`)
|
|
90
|
-
.add(`USE KEYS ${
|
|
92
|
+
.add(`USE KEYS ${cb.v(urn)}`);
|
|
91
93
|
if (unset.length > 0) {
|
|
92
94
|
lines.add('UNSET ', unset.join(', '));
|
|
93
95
|
}
|
|
@@ -97,6 +99,64 @@ class CbRepo {
|
|
|
97
99
|
lines.add(`RETURNING meta().id ${F_URN}`);
|
|
98
100
|
return lines.end();
|
|
99
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Execute the creation of index
|
|
104
|
+
* */
|
|
105
|
+
_createIndex(p1, name) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
let sql;
|
|
108
|
+
const fields = [];
|
|
109
|
+
if (Array.isArray(p1)) {
|
|
110
|
+
fields.push(...p1.map(f => this.cb.f(f)));
|
|
111
|
+
if (common_1.$is.text(name)) {
|
|
112
|
+
name = fields[0];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
fields.push(p1);
|
|
117
|
+
}
|
|
118
|
+
if (!name.startsWith('idx_')) {
|
|
119
|
+
name = 'idx_' + name;
|
|
120
|
+
}
|
|
121
|
+
sql = `CREATE INDEX ${this.cb.f(name)} ON ${this.fullPath} (${fields.join(', ')})`;
|
|
122
|
+
yield this.cb.one(this.def, sql, { name: `${this.path}.index`, ignoredErrors: [couchbase_1.IndexExistsError] });
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Execute the creation of indexes with keys
|
|
127
|
+
* */
|
|
128
|
+
_createMoreIndices(...keys) {
|
|
129
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
+
const already = [];
|
|
131
|
+
const promises = keys
|
|
132
|
+
.filter(k => {
|
|
133
|
+
if (already.includes(k)) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
already.push(k);
|
|
137
|
+
return true;
|
|
138
|
+
})
|
|
139
|
+
.map(k => this._createIndex(k));
|
|
140
|
+
if (!this._pkIndexed) {
|
|
141
|
+
promises.push(this._createPk());
|
|
142
|
+
}
|
|
143
|
+
if (promises.length > 0) {
|
|
144
|
+
yield Promise.all(promises);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Execute the creation of primary key
|
|
150
|
+
* */
|
|
151
|
+
_createPk() {
|
|
152
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
if (!this._pkIndexed) {
|
|
154
|
+
const sql = `CREATE PRIMARY INDEX \`pk\` ON ${this.fullPath}`;
|
|
155
|
+
yield this.cb.one(this.def, sql, { name: `${this.path}.pk`, ignoredErrors: [couchbase_1.IndexExistsError] });
|
|
156
|
+
this._pkIndexed = true;
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
100
160
|
/** @inheritDoc */
|
|
101
161
|
get def() {
|
|
102
162
|
return this;
|
|
@@ -105,14 +165,15 @@ class CbRepo {
|
|
|
105
165
|
onModuleInit() {
|
|
106
166
|
return __awaiter(this, void 0, void 0, function* () {
|
|
107
167
|
var _a, _b;
|
|
168
|
+
const cb = this.cb;
|
|
108
169
|
const mutable = this;
|
|
109
170
|
try {
|
|
110
|
-
mutable.cluster = yield
|
|
171
|
+
mutable.cluster = yield cb.connectDb();
|
|
111
172
|
mutable.bucket = this.cluster.bucket((_a = this._bucketName) !== null && _a !== void 0 ? _a : config_1.couchbaseCommonConfig.raw.CB_BUCKET);
|
|
112
173
|
mutable.scope = this.bucket.scope((_b = this._scopeName) !== null && _b !== void 0 ? _b : config_1.couchbaseCommonConfig.raw.CB_SCOPE);
|
|
113
174
|
mutable.collection = this.scope.collection(this.colName);
|
|
114
175
|
mutable.path = `${this.bucket.name}.${this.scope.name}.${this.collection.name}`;
|
|
115
|
-
mutable.fullPath =
|
|
176
|
+
mutable.fullPath = cb.f(`${this.bucket.name}.${this.scope.name}.${this.collection.name}`);
|
|
116
177
|
if (config_1.couchbaseCommonConfig.raw.CB_CREATE_INDICES) {
|
|
117
178
|
yield this._createIndices();
|
|
118
179
|
}
|
|
@@ -132,30 +193,32 @@ class CbRepo {
|
|
|
132
193
|
urnById(id, p1) {
|
|
133
194
|
return __awaiter(this, void 0, void 0, function* () {
|
|
134
195
|
var _a;
|
|
135
|
-
const
|
|
196
|
+
const cb = this.cb;
|
|
197
|
+
const opt = cb.buildOpt(p1, 'urnById');
|
|
136
198
|
const lines = (0, line_1.cbLine)();
|
|
137
199
|
lines
|
|
138
200
|
.add(`SELECT meta().id as ${F_URN}`)
|
|
139
201
|
.add(`FROM ${this.fullPath} a`)
|
|
140
|
-
.add(`WHERE (a.${F_ID} = ${
|
|
202
|
+
.add(`WHERE (a.${F_ID} = ${cb.v(id)})`)
|
|
141
203
|
.add(`AND (a.${F_TRASH_ID} is missing)`)
|
|
142
204
|
.add('LIMIT 1');
|
|
143
|
-
return (_a = (yield
|
|
205
|
+
return (_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a._urn;
|
|
144
206
|
});
|
|
145
207
|
}
|
|
146
208
|
/** @inheritDoc */
|
|
147
209
|
existsByUrn(urn, p1) {
|
|
148
210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
149
211
|
var _a;
|
|
150
|
-
const
|
|
212
|
+
const cb = this.cb;
|
|
213
|
+
const opt = cb.buildOpt(p1, 'existsByUrn');
|
|
151
214
|
const lines = (0, line_1.cbLine)();
|
|
152
215
|
lines
|
|
153
216
|
.add(`SELECT meta().id as ${F_URN}`)
|
|
154
217
|
.add(`FROM ${this.fullPath} a`)
|
|
155
|
-
.add(`USE KEYS ${
|
|
218
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
156
219
|
.add(`WHERE (a.${F_TRASH_ID} IS MISSING)`)
|
|
157
220
|
.add('LIMIT 1');
|
|
158
|
-
return ((_a = (yield
|
|
221
|
+
return ((_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a._urn) !== undefined;
|
|
159
222
|
});
|
|
160
223
|
}
|
|
161
224
|
/** @inheritDoc */
|
|
@@ -168,35 +231,38 @@ class CbRepo {
|
|
|
168
231
|
/** @inheritDoc */
|
|
169
232
|
getByUrn(urn, p1) {
|
|
170
233
|
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
-
const
|
|
234
|
+
const cb = this.cb;
|
|
235
|
+
const opt = cb.buildOpt(p1, 'getByUrn');
|
|
172
236
|
const lines = (0, line_1.cbLine)();
|
|
173
237
|
lines
|
|
174
238
|
.add(`SELECT a.*`)
|
|
175
239
|
.add(`FROM ${this.fullPath} a`)
|
|
176
|
-
.add(`USE KEYS ${
|
|
240
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
177
241
|
.add(`WHERE (a.${F_TRASH_ID} IS MISSING)`)
|
|
178
242
|
.add('LIMIT 1');
|
|
179
|
-
return (yield
|
|
243
|
+
return (yield cb.one(this, lines.end(), opt)).row;
|
|
180
244
|
});
|
|
181
245
|
}
|
|
182
246
|
/** @inheritDoc */
|
|
183
247
|
getById(id, p1) {
|
|
184
248
|
return __awaiter(this, void 0, void 0, function* () {
|
|
185
|
-
const
|
|
249
|
+
const cb = this.cb;
|
|
250
|
+
const opt = cb.buildOpt(p1, 'getById');
|
|
186
251
|
const lines = (0, line_1.cbLine)();
|
|
187
252
|
lines
|
|
188
253
|
.add(`SELECT a.*`)
|
|
189
254
|
.add(`FROM ${this.fullPath} a`)
|
|
190
|
-
.add(`WHERE (a.${F_ID} = ${
|
|
255
|
+
.add(`WHERE (a.${F_ID} = ${cb.v(id)})`)
|
|
191
256
|
.add(`AND (a.${F_TRASH_ID} is missing)`)
|
|
192
257
|
.add('LIMIT 1');
|
|
193
|
-
return (yield
|
|
258
|
+
return (yield cb.one(this, lines.end(), opt)).row;
|
|
194
259
|
});
|
|
195
260
|
}
|
|
196
261
|
/** @inheritDoc */
|
|
197
262
|
listByUrn(urnList, p1) {
|
|
198
263
|
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
const
|
|
264
|
+
const cb = this.cb;
|
|
265
|
+
const opt = cb.buildOpt(p1, 'listByUrn');
|
|
200
266
|
urnList = this._checkKeys(urnList);
|
|
201
267
|
if (urnList.length < 1) {
|
|
202
268
|
return [];
|
|
@@ -205,15 +271,16 @@ class CbRepo {
|
|
|
205
271
|
lines
|
|
206
272
|
.add(`SELECT a.*`)
|
|
207
273
|
.add(`FROM ${this.fullPath} a`)
|
|
208
|
-
.add(`USE KEYS
|
|
274
|
+
.add(`USE KEYS ${cb.v(urnList)}`)
|
|
209
275
|
.add(`WHERE (a.${F_TRASH_ID} IS MISSING)`);
|
|
210
|
-
return (yield
|
|
276
|
+
return (yield cb.more(this, lines.end(), opt)).rows;
|
|
211
277
|
});
|
|
212
278
|
}
|
|
213
279
|
/** @inheritDoc */
|
|
214
280
|
listById(ids, p1) {
|
|
215
281
|
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
const
|
|
282
|
+
const cb = this.cb;
|
|
283
|
+
const opt = cb.buildOpt(p1, 'listById');
|
|
217
284
|
ids = this._checkKeys(ids);
|
|
218
285
|
if (ids.length < 1) {
|
|
219
286
|
return [];
|
|
@@ -222,68 +289,73 @@ class CbRepo {
|
|
|
222
289
|
lines
|
|
223
290
|
.add(`SELECT a.*`)
|
|
224
291
|
.add(`FROM ${this.fullPath} a`)
|
|
225
|
-
.add(`WHERE (a.${F_ID}
|
|
292
|
+
.add(`WHERE (a.${F_ID} ${ids.length === 1 ? '=' : 'IN'} ${cb.v(ids)})`)
|
|
226
293
|
.add(`AND (a.${F_TRASH_ID} is missing)`);
|
|
227
|
-
return (yield
|
|
294
|
+
return (yield cb.more(this, lines.end(), opt)).rows;
|
|
228
295
|
});
|
|
229
296
|
}
|
|
230
297
|
/** @inheritDoc */
|
|
231
298
|
removeByUrn(urn, p1) {
|
|
232
299
|
return __awaiter(this, void 0, void 0, function* () {
|
|
233
300
|
var _a;
|
|
234
|
-
const
|
|
301
|
+
const cb = this.cb;
|
|
302
|
+
const opt = cb.buildOpt(p1, 'removeByUrn');
|
|
235
303
|
const lines = (0, line_1.cbLine)();
|
|
236
304
|
lines
|
|
237
305
|
.add(`DELETE
|
|
238
306
|
FROM ${this.fullPath} a`)
|
|
239
|
-
.add(`USE KEYS ${
|
|
307
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
240
308
|
.add(`WHERE (a.${F_TRASH_ID} is missing)`)
|
|
241
309
|
.add(`RETURNING a.${F_ID}`);
|
|
242
|
-
return (_a = (yield
|
|
310
|
+
return (_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a.id;
|
|
243
311
|
});
|
|
244
312
|
}
|
|
245
313
|
/** @inheritDoc */
|
|
246
314
|
removeById(id, p1) {
|
|
247
315
|
return __awaiter(this, void 0, void 0, function* () {
|
|
248
316
|
var _a;
|
|
249
|
-
const
|
|
317
|
+
const cb = this.cb;
|
|
318
|
+
const opt = cb.buildOpt(p1, 'removeById');
|
|
250
319
|
const lines = (0, line_1.cbLine)();
|
|
251
320
|
lines
|
|
252
321
|
.add(`DELETE FROM ${this.fullPath} a`)
|
|
253
|
-
.add(`WHERE (a.${F_ID} = ${
|
|
322
|
+
.add(`WHERE (a.${F_ID} = ${cb.v(id)})`)
|
|
254
323
|
.add(`AND (a.${F_TRASH_ID} is missing)`)
|
|
255
324
|
.add(`RETURNING meta().id ${F_URN}`);
|
|
256
|
-
return (_a = (yield
|
|
325
|
+
return (_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a._urn;
|
|
257
326
|
});
|
|
258
327
|
}
|
|
259
328
|
/** @inheritDoc */
|
|
260
329
|
insert(doc, p1) {
|
|
261
330
|
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
-
const
|
|
331
|
+
const cb = this.cb;
|
|
332
|
+
const opt = cb.buildOpt(p1, 'insert');
|
|
263
333
|
const urn = this._urnFromDoc(doc, 'insert');
|
|
264
334
|
doc.createdAt = new Date().toISOString();
|
|
265
335
|
doc.updatedAt = new Date().toISOString();
|
|
266
|
-
const result = yield
|
|
336
|
+
const result = yield cb.exec(this.collection.insert(urn, doc), opt);
|
|
267
337
|
return (result === null || result === void 0 ? void 0 : result.token) ? result.token.toString() : 'inserted';
|
|
268
338
|
});
|
|
269
339
|
}
|
|
270
340
|
/** @inheritDoc */
|
|
271
341
|
replace(doc, p1) {
|
|
272
342
|
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
-
const
|
|
343
|
+
const cb = this.cb;
|
|
344
|
+
const opt = cb.buildOpt(p1, 'replace');
|
|
274
345
|
const urn = this._urnFromDoc(doc, 'replace');
|
|
275
346
|
doc.updatedAt = new Date().toISOString();
|
|
276
|
-
const result = yield
|
|
347
|
+
const result = yield cb.exec(this.collection.replace(urn, doc), opt);
|
|
277
348
|
return (result === null || result === void 0 ? void 0 : result.token) ? result.token.toString() : 'inserted';
|
|
278
349
|
});
|
|
279
350
|
}
|
|
280
351
|
/** @inheritDoc */
|
|
281
352
|
upsert(doc, p1) {
|
|
282
353
|
return __awaiter(this, void 0, void 0, function* () {
|
|
283
|
-
const
|
|
354
|
+
const cb = this.cb;
|
|
355
|
+
const opt = cb.buildOpt(p1, 'upsert');
|
|
284
356
|
const urn = this._urnFromDoc(doc, 'upsert');
|
|
285
357
|
doc.updatedAt = new Date().toISOString();
|
|
286
|
-
const result = yield
|
|
358
|
+
const result = yield cb.exec(this.collection.upsert(urn, doc), opt);
|
|
287
359
|
return (result === null || result === void 0 ? void 0 : result.token) ? result.token.toString() : 'modified';
|
|
288
360
|
});
|
|
289
361
|
}
|
|
@@ -291,18 +363,20 @@ class CbRepo {
|
|
|
291
363
|
update(doc, p1) {
|
|
292
364
|
return __awaiter(this, void 0, void 0, function* () {
|
|
293
365
|
var _a;
|
|
294
|
-
const
|
|
366
|
+
const cb = this.cb;
|
|
367
|
+
const opt = cb.buildOpt(p1, 'update');
|
|
295
368
|
const urn = this._urnFromDoc(doc, 'update');
|
|
296
369
|
doc.updatedAt = new Date().toISOString();
|
|
297
370
|
doc.updatedAt = new Date().toISOString();
|
|
298
371
|
const sql = this._updateSql(urn, doc);
|
|
299
|
-
return (_a = (yield
|
|
372
|
+
return (_a = (yield cb.one(this, sql, opt)).row) === null || _a === void 0 ? void 0 : _a._urn;
|
|
300
373
|
});
|
|
301
374
|
}
|
|
302
375
|
/** @inheritDoc */
|
|
303
376
|
trash(doc, p1) {
|
|
304
377
|
return __awaiter(this, void 0, void 0, function* () {
|
|
305
|
-
const
|
|
378
|
+
const cb = this.cb;
|
|
379
|
+
const opt = cb.buildOpt(p1, 'trash');
|
|
306
380
|
const urn = this._urnFromDoc(doc, 'trash');
|
|
307
381
|
if (opt.trashId === undefined) {
|
|
308
382
|
opt.trashId = (0, node_crypto_1.randomUUID)();
|
|
@@ -310,10 +384,10 @@ class CbRepo {
|
|
|
310
384
|
const lines = (0, line_1.cbLine)();
|
|
311
385
|
lines
|
|
312
386
|
.add(`UPDATE ${this.fullPath} a`)
|
|
313
|
-
.add(`USE KEYS ${
|
|
314
|
-
.add(`SET a.${F_TRASH_ID} = ${
|
|
387
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
388
|
+
.add(`SET a.${F_TRASH_ID} = ${cb.v(opt.trashId)}`)
|
|
315
389
|
.add(`WHERE (a.${F_TRASH_ID} is missing)`);
|
|
316
|
-
yield
|
|
390
|
+
yield cb.one(this, lines.end(), opt);
|
|
317
391
|
return opt.trashId;
|
|
318
392
|
});
|
|
319
393
|
}
|