@samet-it/be-couchbase-common 1.0.9 → 1.0.10
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 +16 -0
- package/dist/repo/cb.repo.js +104 -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
|
@@ -34,6 +34,22 @@ export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLi
|
|
|
34
34
|
* Build update sql as set and unset
|
|
35
35
|
* */
|
|
36
36
|
protected _updateSql<T>(urn: string, doc: T): string;
|
|
37
|
+
/**
|
|
38
|
+
* Execute the creation of index for one field
|
|
39
|
+
* */
|
|
40
|
+
protected _createIndex(field: keyof E | string, name?: string): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Execute the creation of index for complex fields
|
|
43
|
+
* */
|
|
44
|
+
protected _createIndex(fields: Array<keyof E | string>, name: string): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Execute the creation of indexes with keys
|
|
47
|
+
* */
|
|
48
|
+
protected _createMoreIndices(...keys: Array<keyof CbEntity>): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Execute the creation of primary key
|
|
51
|
+
* */
|
|
52
|
+
protected _createPk(): Promise<void>;
|
|
37
53
|
/**
|
|
38
54
|
* Create indices
|
|
39
55
|
* */
|
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,48 @@ 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 promises = keys.map(k => this._createIndex(k));
|
|
131
|
+
promises.push(this._createPk());
|
|
132
|
+
yield Promise.all(promises);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Execute the creation of primary key
|
|
137
|
+
* */
|
|
138
|
+
_createPk() {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
const sql = `CREATE PRIMARY INDEX \`pk\` ON ${this.fullPath}`;
|
|
141
|
+
yield this.cb.one(this.def, sql, { name: `${this.path}.pk`, ignoredErrors: [couchbase_1.IndexExistsError] });
|
|
142
|
+
});
|
|
143
|
+
}
|
|
100
144
|
/** @inheritDoc */
|
|
101
145
|
get def() {
|
|
102
146
|
return this;
|
|
@@ -105,14 +149,15 @@ class CbRepo {
|
|
|
105
149
|
onModuleInit() {
|
|
106
150
|
return __awaiter(this, void 0, void 0, function* () {
|
|
107
151
|
var _a, _b;
|
|
152
|
+
const cb = this.cb;
|
|
108
153
|
const mutable = this;
|
|
109
154
|
try {
|
|
110
|
-
mutable.cluster = yield
|
|
155
|
+
mutable.cluster = yield cb.connectDb();
|
|
111
156
|
mutable.bucket = this.cluster.bucket((_a = this._bucketName) !== null && _a !== void 0 ? _a : config_1.couchbaseCommonConfig.raw.CB_BUCKET);
|
|
112
157
|
mutable.scope = this.bucket.scope((_b = this._scopeName) !== null && _b !== void 0 ? _b : config_1.couchbaseCommonConfig.raw.CB_SCOPE);
|
|
113
158
|
mutable.collection = this.scope.collection(this.colName);
|
|
114
159
|
mutable.path = `${this.bucket.name}.${this.scope.name}.${this.collection.name}`;
|
|
115
|
-
mutable.fullPath =
|
|
160
|
+
mutable.fullPath = cb.f(`${this.bucket.name}.${this.scope.name}.${this.collection.name}`);
|
|
116
161
|
if (config_1.couchbaseCommonConfig.raw.CB_CREATE_INDICES) {
|
|
117
162
|
yield this._createIndices();
|
|
118
163
|
}
|
|
@@ -132,30 +177,32 @@ class CbRepo {
|
|
|
132
177
|
urnById(id, p1) {
|
|
133
178
|
return __awaiter(this, void 0, void 0, function* () {
|
|
134
179
|
var _a;
|
|
135
|
-
const
|
|
180
|
+
const cb = this.cb;
|
|
181
|
+
const opt = cb.buildOpt(p1, 'urnById');
|
|
136
182
|
const lines = (0, line_1.cbLine)();
|
|
137
183
|
lines
|
|
138
184
|
.add(`SELECT meta().id as ${F_URN}`)
|
|
139
185
|
.add(`FROM ${this.fullPath} a`)
|
|
140
|
-
.add(`WHERE (a.${F_ID} = ${
|
|
186
|
+
.add(`WHERE (a.${F_ID} = ${cb.v(id)})`)
|
|
141
187
|
.add(`AND (a.${F_TRASH_ID} is missing)`)
|
|
142
188
|
.add('LIMIT 1');
|
|
143
|
-
return (_a = (yield
|
|
189
|
+
return (_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a._urn;
|
|
144
190
|
});
|
|
145
191
|
}
|
|
146
192
|
/** @inheritDoc */
|
|
147
193
|
existsByUrn(urn, p1) {
|
|
148
194
|
return __awaiter(this, void 0, void 0, function* () {
|
|
149
195
|
var _a;
|
|
150
|
-
const
|
|
196
|
+
const cb = this.cb;
|
|
197
|
+
const opt = cb.buildOpt(p1, 'existsByUrn');
|
|
151
198
|
const lines = (0, line_1.cbLine)();
|
|
152
199
|
lines
|
|
153
200
|
.add(`SELECT meta().id as ${F_URN}`)
|
|
154
201
|
.add(`FROM ${this.fullPath} a`)
|
|
155
|
-
.add(`USE KEYS ${
|
|
202
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
156
203
|
.add(`WHERE (a.${F_TRASH_ID} IS MISSING)`)
|
|
157
204
|
.add('LIMIT 1');
|
|
158
|
-
return ((_a = (yield
|
|
205
|
+
return ((_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a._urn) !== undefined;
|
|
159
206
|
});
|
|
160
207
|
}
|
|
161
208
|
/** @inheritDoc */
|
|
@@ -168,35 +215,38 @@ class CbRepo {
|
|
|
168
215
|
/** @inheritDoc */
|
|
169
216
|
getByUrn(urn, p1) {
|
|
170
217
|
return __awaiter(this, void 0, void 0, function* () {
|
|
171
|
-
const
|
|
218
|
+
const cb = this.cb;
|
|
219
|
+
const opt = cb.buildOpt(p1, 'getByUrn');
|
|
172
220
|
const lines = (0, line_1.cbLine)();
|
|
173
221
|
lines
|
|
174
222
|
.add(`SELECT a.*`)
|
|
175
223
|
.add(`FROM ${this.fullPath} a`)
|
|
176
|
-
.add(`USE KEYS ${
|
|
224
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
177
225
|
.add(`WHERE (a.${F_TRASH_ID} IS MISSING)`)
|
|
178
226
|
.add('LIMIT 1');
|
|
179
|
-
return (yield
|
|
227
|
+
return (yield cb.one(this, lines.end(), opt)).row;
|
|
180
228
|
});
|
|
181
229
|
}
|
|
182
230
|
/** @inheritDoc */
|
|
183
231
|
getById(id, p1) {
|
|
184
232
|
return __awaiter(this, void 0, void 0, function* () {
|
|
185
|
-
const
|
|
233
|
+
const cb = this.cb;
|
|
234
|
+
const opt = cb.buildOpt(p1, 'getById');
|
|
186
235
|
const lines = (0, line_1.cbLine)();
|
|
187
236
|
lines
|
|
188
237
|
.add(`SELECT a.*`)
|
|
189
238
|
.add(`FROM ${this.fullPath} a`)
|
|
190
|
-
.add(`WHERE (a.${F_ID} = ${
|
|
239
|
+
.add(`WHERE (a.${F_ID} = ${cb.v(id)})`)
|
|
191
240
|
.add(`AND (a.${F_TRASH_ID} is missing)`)
|
|
192
241
|
.add('LIMIT 1');
|
|
193
|
-
return (yield
|
|
242
|
+
return (yield cb.one(this, lines.end(), opt)).row;
|
|
194
243
|
});
|
|
195
244
|
}
|
|
196
245
|
/** @inheritDoc */
|
|
197
246
|
listByUrn(urnList, p1) {
|
|
198
247
|
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
-
const
|
|
248
|
+
const cb = this.cb;
|
|
249
|
+
const opt = cb.buildOpt(p1, 'listByUrn');
|
|
200
250
|
urnList = this._checkKeys(urnList);
|
|
201
251
|
if (urnList.length < 1) {
|
|
202
252
|
return [];
|
|
@@ -205,15 +255,16 @@ class CbRepo {
|
|
|
205
255
|
lines
|
|
206
256
|
.add(`SELECT a.*`)
|
|
207
257
|
.add(`FROM ${this.fullPath} a`)
|
|
208
|
-
.add(`USE KEYS
|
|
258
|
+
.add(`USE KEYS ${cb.v(urnList)}`)
|
|
209
259
|
.add(`WHERE (a.${F_TRASH_ID} IS MISSING)`);
|
|
210
|
-
return (yield
|
|
260
|
+
return (yield cb.more(this, lines.end(), opt)).rows;
|
|
211
261
|
});
|
|
212
262
|
}
|
|
213
263
|
/** @inheritDoc */
|
|
214
264
|
listById(ids, p1) {
|
|
215
265
|
return __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
const
|
|
266
|
+
const cb = this.cb;
|
|
267
|
+
const opt = cb.buildOpt(p1, 'listById');
|
|
217
268
|
ids = this._checkKeys(ids);
|
|
218
269
|
if (ids.length < 1) {
|
|
219
270
|
return [];
|
|
@@ -222,68 +273,73 @@ class CbRepo {
|
|
|
222
273
|
lines
|
|
223
274
|
.add(`SELECT a.*`)
|
|
224
275
|
.add(`FROM ${this.fullPath} a`)
|
|
225
|
-
.add(`WHERE (a.${F_ID}
|
|
276
|
+
.add(`WHERE (a.${F_ID} ${ids.length === 1 ? '=' : 'IN'} ${cb.v(ids)})`)
|
|
226
277
|
.add(`AND (a.${F_TRASH_ID} is missing)`);
|
|
227
|
-
return (yield
|
|
278
|
+
return (yield cb.more(this, lines.end(), opt)).rows;
|
|
228
279
|
});
|
|
229
280
|
}
|
|
230
281
|
/** @inheritDoc */
|
|
231
282
|
removeByUrn(urn, p1) {
|
|
232
283
|
return __awaiter(this, void 0, void 0, function* () {
|
|
233
284
|
var _a;
|
|
234
|
-
const
|
|
285
|
+
const cb = this.cb;
|
|
286
|
+
const opt = cb.buildOpt(p1, 'removeByUrn');
|
|
235
287
|
const lines = (0, line_1.cbLine)();
|
|
236
288
|
lines
|
|
237
289
|
.add(`DELETE
|
|
238
290
|
FROM ${this.fullPath} a`)
|
|
239
|
-
.add(`USE KEYS ${
|
|
291
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
240
292
|
.add(`WHERE (a.${F_TRASH_ID} is missing)`)
|
|
241
293
|
.add(`RETURNING a.${F_ID}`);
|
|
242
|
-
return (_a = (yield
|
|
294
|
+
return (_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a.id;
|
|
243
295
|
});
|
|
244
296
|
}
|
|
245
297
|
/** @inheritDoc */
|
|
246
298
|
removeById(id, p1) {
|
|
247
299
|
return __awaiter(this, void 0, void 0, function* () {
|
|
248
300
|
var _a;
|
|
249
|
-
const
|
|
301
|
+
const cb = this.cb;
|
|
302
|
+
const opt = cb.buildOpt(p1, 'removeById');
|
|
250
303
|
const lines = (0, line_1.cbLine)();
|
|
251
304
|
lines
|
|
252
305
|
.add(`DELETE FROM ${this.fullPath} a`)
|
|
253
|
-
.add(`WHERE (a.${F_ID} = ${
|
|
306
|
+
.add(`WHERE (a.${F_ID} = ${cb.v(id)})`)
|
|
254
307
|
.add(`AND (a.${F_TRASH_ID} is missing)`)
|
|
255
308
|
.add(`RETURNING meta().id ${F_URN}`);
|
|
256
|
-
return (_a = (yield
|
|
309
|
+
return (_a = (yield cb.one(this, lines.end(), opt)).row) === null || _a === void 0 ? void 0 : _a._urn;
|
|
257
310
|
});
|
|
258
311
|
}
|
|
259
312
|
/** @inheritDoc */
|
|
260
313
|
insert(doc, p1) {
|
|
261
314
|
return __awaiter(this, void 0, void 0, function* () {
|
|
262
|
-
const
|
|
315
|
+
const cb = this.cb;
|
|
316
|
+
const opt = cb.buildOpt(p1, 'insert');
|
|
263
317
|
const urn = this._urnFromDoc(doc, 'insert');
|
|
264
318
|
doc.createdAt = new Date().toISOString();
|
|
265
319
|
doc.updatedAt = new Date().toISOString();
|
|
266
|
-
const result = yield
|
|
320
|
+
const result = yield cb.exec(this.collection.insert(urn, doc), opt);
|
|
267
321
|
return (result === null || result === void 0 ? void 0 : result.token) ? result.token.toString() : 'inserted';
|
|
268
322
|
});
|
|
269
323
|
}
|
|
270
324
|
/** @inheritDoc */
|
|
271
325
|
replace(doc, p1) {
|
|
272
326
|
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
-
const
|
|
327
|
+
const cb = this.cb;
|
|
328
|
+
const opt = cb.buildOpt(p1, 'replace');
|
|
274
329
|
const urn = this._urnFromDoc(doc, 'replace');
|
|
275
330
|
doc.updatedAt = new Date().toISOString();
|
|
276
|
-
const result = yield
|
|
331
|
+
const result = yield cb.exec(this.collection.replace(urn, doc), opt);
|
|
277
332
|
return (result === null || result === void 0 ? void 0 : result.token) ? result.token.toString() : 'inserted';
|
|
278
333
|
});
|
|
279
334
|
}
|
|
280
335
|
/** @inheritDoc */
|
|
281
336
|
upsert(doc, p1) {
|
|
282
337
|
return __awaiter(this, void 0, void 0, function* () {
|
|
283
|
-
const
|
|
338
|
+
const cb = this.cb;
|
|
339
|
+
const opt = cb.buildOpt(p1, 'upsert');
|
|
284
340
|
const urn = this._urnFromDoc(doc, 'upsert');
|
|
285
341
|
doc.updatedAt = new Date().toISOString();
|
|
286
|
-
const result = yield
|
|
342
|
+
const result = yield cb.exec(this.collection.upsert(urn, doc), opt);
|
|
287
343
|
return (result === null || result === void 0 ? void 0 : result.token) ? result.token.toString() : 'modified';
|
|
288
344
|
});
|
|
289
345
|
}
|
|
@@ -291,18 +347,20 @@ class CbRepo {
|
|
|
291
347
|
update(doc, p1) {
|
|
292
348
|
return __awaiter(this, void 0, void 0, function* () {
|
|
293
349
|
var _a;
|
|
294
|
-
const
|
|
350
|
+
const cb = this.cb;
|
|
351
|
+
const opt = cb.buildOpt(p1, 'update');
|
|
295
352
|
const urn = this._urnFromDoc(doc, 'update');
|
|
296
353
|
doc.updatedAt = new Date().toISOString();
|
|
297
354
|
doc.updatedAt = new Date().toISOString();
|
|
298
355
|
const sql = this._updateSql(urn, doc);
|
|
299
|
-
return (_a = (yield
|
|
356
|
+
return (_a = (yield cb.one(this, sql, opt)).row) === null || _a === void 0 ? void 0 : _a._urn;
|
|
300
357
|
});
|
|
301
358
|
}
|
|
302
359
|
/** @inheritDoc */
|
|
303
360
|
trash(doc, p1) {
|
|
304
361
|
return __awaiter(this, void 0, void 0, function* () {
|
|
305
|
-
const
|
|
362
|
+
const cb = this.cb;
|
|
363
|
+
const opt = cb.buildOpt(p1, 'trash');
|
|
306
364
|
const urn = this._urnFromDoc(doc, 'trash');
|
|
307
365
|
if (opt.trashId === undefined) {
|
|
308
366
|
opt.trashId = (0, node_crypto_1.randomUUID)();
|
|
@@ -310,10 +368,10 @@ class CbRepo {
|
|
|
310
368
|
const lines = (0, line_1.cbLine)();
|
|
311
369
|
lines
|
|
312
370
|
.add(`UPDATE ${this.fullPath} a`)
|
|
313
|
-
.add(`USE KEYS ${
|
|
314
|
-
.add(`SET a.${F_TRASH_ID} = ${
|
|
371
|
+
.add(`USE KEYS ${cb.v(urn)}`)
|
|
372
|
+
.add(`SET a.${F_TRASH_ID} = ${cb.v(opt.trashId)}`)
|
|
315
373
|
.add(`WHERE (a.${F_TRASH_ID} is missing)`);
|
|
316
|
-
yield
|
|
374
|
+
yield cb.one(this, lines.end(), opt);
|
|
317
375
|
return opt.trashId;
|
|
318
376
|
});
|
|
319
377
|
}
|