@samet-it/be-couchbase-common 1.1.8 → 1.1.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.
@@ -11,10 +11,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CbRepo = void 0;
13
13
  const couchbase_1 = require("couchbase");
14
+ const common_1 = require("@leyyo/common");
14
15
  const be_db_common_1 = require("@samet-it/be-db-common");
15
16
  const type_1 = require("@leyyo/type");
16
17
  const type_2 = require("@leyyo/type");
17
18
  const cb_schema_error_1 = require("./cb-schema.error");
19
+ const filter_1 = require("../filter");
18
20
  let F_URN;
19
21
  let F_ID;
20
22
  let F_TRASH_ID;
@@ -42,6 +44,10 @@ class CbRepo extends be_db_common_1.DbRepo {
42
44
  constructor(conn, opt) {
43
45
  var _a, _b, _c;
44
46
  super(conn, opt);
47
+ /**
48
+ * Created indexes, it's temporary property
49
+ * */
50
+ this._createdIndices = [];
45
51
  conn.onFirstConnected(() => this._afterConnected());
46
52
  const { _props: props } = this;
47
53
  props.bucketName = this._checkName(props.bucketName);
@@ -60,10 +66,11 @@ class CbRepo extends be_db_common_1.DbRepo {
60
66
  throw new be_db_common_1.DbInvalidValueError('Collection', { where: 'CbRepo', method: 'constructor', type: typeof props.collectionName });
61
67
  }
62
68
  if (!F_URN) {
63
- F_URN = props.conn.f('_urn');
64
- F_ID = props.conn.f('id');
65
- F_TRASH_ID = props.conn.f('_trashId');
69
+ F_URN = this.f('_urn');
70
+ F_ID = this.f('id');
71
+ F_TRASH_ID = this.f('_trashId');
66
72
  }
73
+ filter_1.cbFilterUtil.$set(this);
67
74
  }
68
75
  // region protected-method
69
76
  /**
@@ -92,7 +99,6 @@ class CbRepo extends be_db_common_1.DbRepo {
92
99
  * */
93
100
  _createIndex(p1, name) {
94
101
  return __awaiter(this, void 0, void 0, function* () {
95
- const { conn, full, path } = this._props;
96
102
  let sql;
97
103
  const fields = [];
98
104
  if (Array.isArray(p1)) {
@@ -100,16 +106,16 @@ class CbRepo extends be_db_common_1.DbRepo {
100
106
  if (!(0, type_1.isText)(name)) {
101
107
  name = given.map(item => item.split('.').pop()).join('.');
102
108
  }
103
- fields.push(...given.map(f => conn.f(f)));
109
+ fields.push(...given.map(f => this.f(f)));
104
110
  }
105
111
  else {
106
112
  fields.push(p1);
107
113
  name = p1;
108
114
  }
109
115
  name = this._indexName(name);
110
- sql = `CREATE INDEX ${conn.f(name)} ON ${full} (${fields.join(', ')}) IF NOT EXISTS`;
111
- yield conn.one(this._props, sql, {
112
- name: `${path}.index`,
116
+ sql = `CREATE INDEX ${this.f(name)} ON ${this.table} (${fields.join(', ')}) IF NOT EXISTS`;
117
+ yield this.one(sql, {
118
+ name: `${this.path}.index`,
113
119
  ignoredErrors: [couchbase_1.IndexExistsError.name]
114
120
  });
115
121
  });
@@ -122,13 +128,12 @@ class CbRepo extends be_db_common_1.DbRepo {
122
128
  * */
123
129
  _createMoreIndices(...keys) {
124
130
  return __awaiter(this, void 0, void 0, function* () {
125
- const already = [];
126
131
  const promises = keys
127
132
  .filter(k => {
128
- if (already.includes(k)) {
133
+ if (this._createdIndices.includes(k)) {
129
134
  return false;
130
135
  }
131
- already.push(k);
136
+ this._createdIndices.push(k);
132
137
  return true;
133
138
  })
134
139
  .map(k => this._createIndex(k));
@@ -143,26 +148,33 @@ class CbRepo extends be_db_common_1.DbRepo {
143
148
  * */
144
149
  _createPk() {
145
150
  return __awaiter(this, void 0, void 0, function* () {
146
- const { full, path, conn } = this._props;
147
- const sql = `CREATE PRIMARY INDEX \`pk\` ON ${full} IF NOT EXISTS`;
148
- yield conn.one(this._props, sql, {
149
- name: `${path}.pk`,
150
- ignoredErrors: [couchbase_1.IndexExistsError.name]
151
+ if (this._createdIndices.includes('pk')) {
152
+ return;
153
+ }
154
+ const sql = `CREATE PRIMARY INDEX \`pk\` ON ${this.table} IF NOT EXISTS`;
155
+ yield this.one(sql, {
156
+ name: `${this.path}.pk`,
157
+ ignoredErrors: [couchbase_1.IndexExistsError]
151
158
  });
159
+ this._createdIndices.push('pk');
152
160
  });
153
161
  }
154
162
  /** @inheritDoc */
155
163
  _afterConnected() {
156
164
  return __awaiter(this, void 0, void 0, function* () {
165
+ var _a;
157
166
  const { _props: props } = this;
158
- props.cluster = props.conn.props.cluster;
159
167
  yield this._loadBucket();
160
168
  yield this._loadScope();
161
169
  yield this._loadCollection();
162
- props.path = `${props.bucket.name}.${props.scope.name}.${props.collection.name}`;
163
- props.full = props.conn.f(props.path);
170
+ props.path = `${this.bucket.name}.${this.scope.name}.${this.collection.name}`;
171
+ props.table = this.f(props.path);
164
172
  if (props.createIndices) {
165
173
  yield this.$createIndices();
174
+ if (((_a = this.props.indexedFields) === null || _a === void 0 ? void 0 : _a.length) > 0) {
175
+ yield this._createMoreIndices(...this.props.indexedFields);
176
+ }
177
+ this._createdIndices = [];
166
178
  }
167
179
  });
168
180
  }
@@ -176,7 +188,7 @@ class CbRepo extends be_db_common_1.DbRepo {
176
188
  let spec = Array.from(CbRepo._specs.keys())
177
189
  .find(s => s.name === props.bucketName);
178
190
  if (!spec) {
179
- const bucketManager = props.cluster.buckets();
191
+ const bucketManager = this.cluster.buckets();
180
192
  const allBuckets = yield bucketManager.getAllBuckets();
181
193
  spec = allBuckets.find(b => b.name === props.bucketName);
182
194
  if (!spec) {
@@ -184,7 +196,7 @@ class CbRepo extends be_db_common_1.DbRepo {
184
196
  }
185
197
  CbRepo._specs.set(spec, []);
186
198
  }
187
- const ins = props.cluster.bucket(props.bucketName);
199
+ const ins = this.cluster.bucket(props.bucketName);
188
200
  ins.$spec = spec;
189
201
  props.bucket = ins;
190
202
  });
@@ -245,63 +257,229 @@ class CbRepo extends be_db_common_1.DbRepo {
245
257
  get props() {
246
258
  return this._props;
247
259
  }
260
+ /**
261
+ * Shortcut to {@link CbConnectionLike}
262
+ * */
263
+ get conn() {
264
+ return this._props.conn;
265
+ }
266
+ /**
267
+ * Shortcut to {@link CbConnProps#cluster}
268
+ * */
269
+ get cluster() {
270
+ return this._props.conn.cluster;
271
+ }
272
+ /**
273
+ * Bucket instance
274
+ *
275
+ * @type {Bucket}
276
+ * */
277
+ get bucket() {
278
+ return this._props.bucket;
279
+ }
280
+ /**
281
+ * Scope instance
282
+ *
283
+ * @type {Scope}
284
+ * */
285
+ get scope() {
286
+ return this._props.scope;
287
+ }
288
+ /**
289
+ * Collection instance
290
+ *
291
+ * @type {Collection}
292
+ * */
293
+ get collection() {
294
+ return this._props.collection;
295
+ }
248
296
  // endregion getter
297
+ // region field-value
298
+ /** {@inheritDoc} */
299
+ field(field) {
300
+ field = field.trim();
301
+ if (field.includes('.')) {
302
+ return field.split('.').map(part => this.field(part)).join('.');
303
+ }
304
+ return field.startsWith('`') ? field : `\`${field}\``;
305
+ }
306
+ /** {@inheritDoc} */
307
+ value(value) {
308
+ var _a;
309
+ switch (typeof value) {
310
+ case "string":
311
+ return JSON.stringify(value);
312
+ case "number":
313
+ return `${value}`;
314
+ case "boolean":
315
+ return value ? `true` : 'false';
316
+ case "object":
317
+ if (value) {
318
+ return (0, common_1.secureJson)(value);
319
+ }
320
+ else {
321
+ return 'null';
322
+ }
323
+ case "bigint":
324
+ return `"${value.toString(10)}"`;
325
+ case "symbol":
326
+ return JSON.stringify(value.description);
327
+ case "function":
328
+ return JSON.stringify(`${(_a = value.name) !== null && _a !== void 0 ? _a : '~fn~'}[${value.length}]()`);
329
+ }
330
+ return 'null';
331
+ }
332
+ /** {@inheritDoc} */
333
+ flatten(result) {
334
+ if (result && Array.isArray(result.rows) && (result.rows.length > 0)) {
335
+ return result.rows;
336
+ }
337
+ return [];
338
+ }
339
+ // endregion field-value
340
+ // region transcoders
341
+ /** {@inheritDoc} */
342
+ castDates(given, ...fields) {
343
+ if (fields.length < 1) {
344
+ return;
345
+ }
346
+ if (!given || typeof given !== 'object' && Array.isArray(given)) {
347
+ return;
348
+ }
349
+ const doc = given;
350
+ fields.forEach(field => {
351
+ const val = doc[field];
352
+ if (typeof val === 'string' || (typeof val === 'number' && val > 0 && Number.isInteger(val))) {
353
+ doc[field] = new Date(val);
354
+ }
355
+ });
356
+ }
357
+ /** {@inheritDoc} */
358
+ castDatesForList(given, ...fields) {
359
+ if (fields.length < 1) {
360
+ return;
361
+ }
362
+ if (!given || !Array.isArray(given) || given.length < 1) {
363
+ return;
364
+ }
365
+ const arr = given;
366
+ arr.forEach(item => this.castDates(item, ...fields));
367
+ }
368
+ // endregion transcoders
369
+ // region query
370
+ /** {@inheritDoc} */
371
+ more(lines, p1) {
372
+ return __awaiter(this, void 0, void 0, function* () {
373
+ if (!this.isEnabled) {
374
+ return { success: true, rows: [] };
375
+ }
376
+ const opt = this.buildOpt(p1);
377
+ const sql = (0, type_1.isObjectValid)(lines) ? lines.end() : lines;
378
+ if (opt.printSql) {
379
+ this.logger.info(`${this.path} > SQL${opt.name}`, sql);
380
+ }
381
+ try {
382
+ const result = yield this.scope.query(sql, opt);
383
+ return {
384
+ success: true,
385
+ rows: result.rows,
386
+ meta: result.meta,
387
+ };
388
+ }
389
+ catch (err) {
390
+ this.checkError(err, Object.assign(Object.assign({}, opt), { sql }));
391
+ return {
392
+ success: false,
393
+ rows: [],
394
+ error: err,
395
+ };
396
+ }
397
+ });
398
+ }
399
+ /** {@inheritDoc} */
400
+ one(lines, p1) {
401
+ return __awaiter(this, void 0, void 0, function* () {
402
+ if (!this.isEnabled) {
403
+ return { success: true, row: undefined };
404
+ }
405
+ const opt = this.buildOpt(p1);
406
+ const sql = (0, type_1.isObjectValid)(lines) ? lines.end() : lines;
407
+ if (opt.printSql) {
408
+ this.logger.info(`${this.path} > SQL${opt.name}`, sql);
409
+ }
410
+ try {
411
+ const result = yield this.scope.query(sql, opt);
412
+ return {
413
+ success: true,
414
+ row: (Array.isArray(result.rows) && result.rows.length > 0) ? result.rows[0] : undefined,
415
+ meta: result.meta,
416
+ };
417
+ }
418
+ catch (err) {
419
+ this.checkError(err, Object.assign(Object.assign({}, opt), { sql }));
420
+ return {
421
+ success: false,
422
+ row: undefined,
423
+ error: err,
424
+ };
425
+ }
426
+ });
427
+ }
428
+ // endregion query
249
429
  // region get
250
430
  /** @inheritDoc */
251
431
  $getByPrimary(key, p1, ignoreCheck) {
252
432
  return __awaiter(this, void 0, void 0, function* () {
253
- const { conn, useSoftDelete, collection, full } = this._props;
254
- if (!conn.props.enabled) {
433
+ if (!this.isEnabled) {
255
434
  return undefined;
256
435
  }
257
- const opt = conn.buildOpt(p1, '$getById');
436
+ const opt = this.buildOpt(p1, '$getById');
258
437
  if (!ignoreCheck) {
259
438
  (0, type_2.assertText)(key, { field: 'id', where: 'CbRepo', method: '$getByPrimary' });
260
439
  }
261
- if (!useSoftDelete) {
440
+ if (!this.hasSoftDelete) {
262
441
  if (!Array.isArray(opt.ignoredErrors)) {
263
442
  opt.ignoredErrors = [];
264
443
  }
265
444
  opt.ignoredErrors.push(couchbase_1.DocumentNotFoundError.name);
266
- const result = yield conn.exec(collection.get(key, opt), opt);
445
+ const result = yield this.exec(this.collection.get(key, opt), opt);
267
446
  return result === null || result === void 0 ? void 0 : result.content;
268
447
  }
269
448
  const lines = (0, be_db_common_1.dbLines)();
270
449
  lines
271
450
  .add(`SELECT a.*`)
272
- .add(`FROM ${full} a`)
451
+ .add(`FROM ${this.table} a`)
273
452
  .add(`USE KEYS $key`)
274
453
  .add(`WHERE (a.${F_TRASH_ID} is missing)`)
275
454
  .add('LIMIT 1');
276
455
  const parameters = { key };
277
- return (yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).row;
456
+ return (yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }))).row;
278
457
  });
279
458
  }
280
459
  /** @inheritDoc */
281
460
  $getBySecondary(key, p1, ignoreCheck) {
282
461
  return __awaiter(this, void 0, void 0, function* () {
283
- const { conn, useSoftDelete, useNumericId, full } = this._props;
284
- if (!conn.props.enabled) {
462
+ if (!this.isEnabled) {
285
463
  return undefined;
286
464
  }
287
465
  if (!ignoreCheck) {
288
- if (useNumericId) {
466
+ if (this.hasNumericId) {
289
467
  (0, type_1.assertInteger)(key, { field: 'key', where: 'CbRepo', method: '$getBySecondary' });
290
468
  }
291
469
  else {
292
470
  (0, type_2.assertText)(key, { field: 'key', where: 'CbRepo', method: '$getBySecondary' });
293
471
  }
294
472
  }
295
- const opt = conn.buildOpt(p1, '$getBySecondary');
473
+ const opt = this.buildOpt(p1, '$getBySecondary');
296
474
  const lines = (0, be_db_common_1.dbLines)();
297
475
  lines
298
476
  .add(`SELECT a.*`)
299
- .add(`FROM ${full} a`)
477
+ .add(`FROM ${this.table} a`)
300
478
  .add(`WHERE (a.${F_ID} = $key)`)
301
- .no(useSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`)
479
+ .yes(this.hasSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`)
302
480
  .add('LIMIT 1');
303
481
  const parameters = { key };
304
- return (yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).row;
482
+ return (yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }))).row;
305
483
  });
306
484
  }
307
485
  // endregion get
@@ -310,54 +488,52 @@ class CbRepo extends be_db_common_1.DbRepo {
310
488
  $existsByPrimary(key, p1, ignoreCheck) {
311
489
  return __awaiter(this, void 0, void 0, function* () {
312
490
  var _a, _b, _c;
313
- const { useSoftDelete, conn, collection, full } = this._props;
314
- if (!conn.props.enabled) {
491
+ if (!this.isEnabled) {
315
492
  return false;
316
493
  }
317
494
  if (!ignoreCheck) {
318
495
  (0, type_2.assertText)(key, { field: 'key', where: 'CbRepo', method: '$existsByPrimary' });
319
496
  }
320
- const opt = conn.buildOpt(p1, '$existsByPrimary');
321
- if (!useSoftDelete) {
322
- return (_b = (_a = (yield conn.exec(collection.exists(key, opt), opt))) === null || _a === void 0 ? void 0 : _a.exists) !== null && _b !== void 0 ? _b : false;
497
+ const opt = this.buildOpt(p1, '$existsByPrimary');
498
+ if (!this.hasSoftDelete) {
499
+ return (_b = (_a = (yield this.exec(this.collection.exists(key, opt), opt))) === null || _a === void 0 ? void 0 : _a.exists) !== null && _b !== void 0 ? _b : false;
323
500
  }
324
501
  const lines = (0, be_db_common_1.dbLines)();
325
502
  lines
326
503
  .add(`SELECT meta().id \`x\``)
327
- .add(`FROM ${full} a`)
504
+ .add(`FROM ${this.table} a`)
328
505
  .add(`USE KEYS $key`)
329
506
  .add(`WHERE (a.${F_TRASH_ID} is missing)`)
330
507
  .add('LIMIT 1');
331
508
  const parameters = { key };
332
- return ((_c = (yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _c === void 0 ? void 0 : _c.x) === undefined;
509
+ return ((_c = (yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _c === void 0 ? void 0 : _c.x) === undefined;
333
510
  });
334
511
  }
335
512
  /** @inheritDoc */
336
513
  $existsBySecondary(key, p1, ignoreCheck) {
337
514
  return __awaiter(this, void 0, void 0, function* () {
338
515
  var _a;
339
- const { useSoftDelete, useNumericId, conn, full } = this._props;
340
- if (!conn.props.enabled) {
516
+ if (!this.isEnabled) {
341
517
  return false;
342
518
  }
343
519
  if (!ignoreCheck) {
344
- if (useNumericId) {
520
+ if (this.hasNumericId) {
345
521
  (0, type_1.assertInteger)(key, { field: 'key', where: 'CbRepo', method: '$existsBySecondary' });
346
522
  }
347
523
  else {
348
524
  (0, type_2.assertText)(key, { field: 'key', where: 'CbRepo', method: '$existsBySecondary' });
349
525
  }
350
526
  }
351
- const opt = conn.buildOpt(p1, '$existsBySecondary');
527
+ const opt = this.buildOpt(p1, '$existsBySecondary');
352
528
  const lines = (0, be_db_common_1.dbLines)();
353
529
  lines
354
530
  .add(`SELECT meta().id \`x\``)
355
- .add(`FROM ${full} a`)
531
+ .add(`FROM ${this.table} a`)
356
532
  .add(`USE KEYS $key`)
357
- .yes(useSoftDelete, `WHERE (a.${F_TRASH_ID} IS MISSING)`)
533
+ .yes(this.hasSoftDelete, `WHERE (a.${F_TRASH_ID} IS MISSING)`)
358
534
  .add('LIMIT 1');
359
535
  const parameters = { key };
360
- return ((_a = (yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x) === undefined;
536
+ return ((_a = (yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x) === undefined;
361
537
  });
362
538
  }
363
539
  // endregion exists
@@ -365,8 +541,7 @@ class CbRepo extends be_db_common_1.DbRepo {
365
541
  /** @inheritDoc */
366
542
  $listByPrimary(keys, p1, ignoreCheck) {
367
543
  return __awaiter(this, void 0, void 0, function* () {
368
- const { useSoftDelete, conn, full } = this._props;
369
- if (!conn.props.enabled) {
544
+ if (!this.isEnabled) {
370
545
  return [];
371
546
  }
372
547
  if (!ignoreCheck) {
@@ -375,28 +550,27 @@ class CbRepo extends be_db_common_1.DbRepo {
375
550
  (0, type_2.assertText)(key, { field: 'key', where: 'CbRepo', method: '$listByPrimary', index });
376
551
  });
377
552
  }
378
- const opt = conn.buildOpt(p1, '$listByIds');
553
+ const opt = this.buildOpt(p1, '$listByIds');
379
554
  const lines = (0, be_db_common_1.dbLines)();
380
555
  lines
381
556
  .add(`SELECT a.*`)
382
- .add(`FROM ${full} a`)
557
+ .add(`FROM ${this.table} a`)
383
558
  .add(`USE KEYS $keys`)
384
- .yes(useSoftDelete, `WHERE (a.${F_TRASH_ID} is missing)`);
559
+ .yes(this.hasSoftDelete, `WHERE (a.${F_TRASH_ID} is missing)`);
385
560
  const parameters = { keys };
386
- return (yield conn.more(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).rows;
561
+ return (yield this.more(lines, Object.assign(Object.assign({}, opt), { parameters }))).rows;
387
562
  });
388
563
  }
389
564
  /** @inheritDoc */
390
565
  $listBySecondary(keys, p1, ignoreCheck) {
391
566
  return __awaiter(this, void 0, void 0, function* () {
392
- const { useSoftDelete, useNumericId, conn, full } = this._props;
393
- if (!conn.props.enabled) {
567
+ if (!this.isEnabled) {
394
568
  return [];
395
569
  }
396
570
  if (!ignoreCheck) {
397
571
  (0, type_2.assertArray)(keys, { field: 'keys', where: 'CbRepo', method: '$listBySecondary' });
398
572
  keys.forEach((key, index) => {
399
- if (useNumericId) {
573
+ if (this.hasNumericId) {
400
574
  (0, type_1.assertInteger)(key, { field: 'key', where: 'CbRepo', method: '$listBySecondary', index });
401
575
  }
402
576
  else {
@@ -404,15 +578,15 @@ class CbRepo extends be_db_common_1.DbRepo {
404
578
  }
405
579
  });
406
580
  }
407
- const opt = conn.buildOpt(p1, '$listBySecondary');
581
+ const opt = this.buildOpt(p1, '$listBySecondary');
408
582
  const lines = (0, be_db_common_1.dbLines)();
409
583
  lines
410
584
  .add(`SELECT a.*`)
411
- .add(`FROM ${full} a`)
585
+ .add(`FROM ${this.table} a`)
412
586
  .add(`WHERE (a.${F_ID} IN $keys)`)
413
- .yes(useSoftDelete, ` AND (a.${F_TRASH_ID} IS MISSING)`);
587
+ .yes(this.hasSoftDelete, ` AND (a.${F_TRASH_ID} IS MISSING)`);
414
588
  const parameters = { keys };
415
- return (yield conn.more(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).rows;
589
+ return (yield this.more(lines, Object.assign(Object.assign({}, opt), { parameters }))).rows;
416
590
  });
417
591
  }
418
592
  // endregion list
@@ -420,18 +594,17 @@ class CbRepo extends be_db_common_1.DbRepo {
420
594
  /** @inheritDoc */
421
595
  $filter(query, p1) {
422
596
  return __awaiter(this, void 0, void 0, function* () {
423
- const { useSoftDelete, conn, full } = this._props;
424
- if (!conn.props.enabled) {
597
+ if (!this.isEnabled) {
425
598
  return [];
426
599
  }
427
- const opt = conn.buildOpt(p1, '$filter');
600
+ const opt = this.buildOpt(p1, '$filter');
428
601
  // @todo - work for query builder
429
602
  const lines = (0, be_db_common_1.dbLines)();
430
603
  lines
431
604
  .add(`SELECT a.*`)
432
- .add(`FROM ${full} a`)
433
- .both(useSoftDelete, `WHERE (a.${F_TRASH_ID} is missing)`, `WHERE (meta().id is not missing)`);
434
- return (yield conn.more(this._props, lines.end(), opt)).rows;
605
+ .add(`FROM ${this.table} a`)
606
+ .both(this.hasSoftDelete, `WHERE (a.${F_TRASH_ID} is missing)`, `WHERE (meta().id is not missing)`);
607
+ return (yield this.more(lines, opt)).rows;
435
608
  });
436
609
  }
437
610
  // endregion filter
@@ -439,27 +612,26 @@ class CbRepo extends be_db_common_1.DbRepo {
439
612
  /** @inheritDoc */
440
613
  $insert(doc, p1, ignoreCheck) {
441
614
  return __awaiter(this, void 0, void 0, function* () {
442
- const { conn, collection, noInsert, useNumericId } = this._props;
443
- if (!conn.props.enabled) {
615
+ if (!this.isEnabled) {
444
616
  return undefined;
445
617
  }
446
- const opt = conn.buildOpt(p1, '$insert');
618
+ const opt = this.buildOpt(p1, '$insert');
447
619
  const param = { where: 'CbRepo', method: '$insert', queryName: opt.name };
448
- if (noInsert) {
620
+ if (this.isNoInsert) {
449
621
  throw new be_db_common_1.DbNotSupportedError('Insert', param);
450
622
  }
451
623
  if (!ignoreCheck) {
452
624
  (0, type_2.assertObject)(doc, Object.assign({ field: 'doc' }, param));
453
- (0, type_2.assertText)(doc._urn, Object.assign({ field: 'doc._urn' }, param));
454
- if (useNumericId) {
625
+ (0, type_2.assertText)(doc.urn, Object.assign({ field: 'doc.urn' }, param));
626
+ if (this.hasNumericId) {
455
627
  (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
456
628
  }
457
629
  else {
458
630
  (0, type_2.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
459
631
  }
460
632
  }
461
- yield conn.exec(collection.insert(doc._urn, doc), opt);
462
- return doc._urn;
633
+ yield this.exec(this.collection.insert(doc.urn, doc), opt);
634
+ return doc.urn;
463
635
  });
464
636
  }
465
637
  // endregion insert
@@ -467,27 +639,26 @@ class CbRepo extends be_db_common_1.DbRepo {
467
639
  /** @inheritDoc */
468
640
  $replace(doc, p1, ignoreCheck) {
469
641
  return __awaiter(this, void 0, void 0, function* () {
470
- const { conn, collection, noUpdate, useNumericId } = this._props;
471
- if (!conn.props.enabled) {
642
+ if (!this.isEnabled) {
472
643
  return undefined;
473
644
  }
474
- const opt = conn.buildOpt(p1, '$replace');
645
+ const opt = this.buildOpt(p1, '$replace');
475
646
  const param = { where: 'CbRepo', method: '$replace', queryName: opt.name };
476
- if (noUpdate) {
647
+ if (this.isNoUpdate) {
477
648
  throw new be_db_common_1.DbNotSupportedError('Update', param);
478
649
  }
479
650
  if (!ignoreCheck) {
480
651
  (0, type_2.assertObject)(doc, Object.assign({ field: 'doc' }, param));
481
- (0, type_2.assertText)(doc._urn, Object.assign({ field: 'doc._urn' }, param));
482
- if (useNumericId) {
652
+ (0, type_2.assertText)(doc.urn, Object.assign({ field: 'doc._urn' }, param));
653
+ if (this.hasNumericId) {
483
654
  (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
484
655
  }
485
656
  else {
486
657
  (0, type_2.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
487
658
  }
488
659
  }
489
- yield conn.exec(collection.replace(doc._urn, doc), opt);
490
- return doc._urn;
660
+ yield this.exec(this.collection.replace(doc.urn, doc), opt);
661
+ return doc.urn;
491
662
  });
492
663
  }
493
664
  // endregion replace
@@ -495,27 +666,26 @@ class CbRepo extends be_db_common_1.DbRepo {
495
666
  /** @inheritDoc */
496
667
  $upsert(doc, p1, ignoreCheck) {
497
668
  return __awaiter(this, void 0, void 0, function* () {
498
- const { conn, collection, noUpdate, noInsert, useNumericId } = this._props;
499
- if (!conn.props.enabled) {
669
+ if (!this.isEnabled) {
500
670
  return undefined;
501
671
  }
502
- const opt = conn.buildOpt(p1, '$upsert');
672
+ const opt = this.buildOpt(p1, '$upsert');
503
673
  const param = { where: 'CbRepo', method: '$upsert', queryName: opt.name };
504
- if (noUpdate || noInsert) {
674
+ if (this.isNoUpdate || this.isNoInsert) {
505
675
  throw new be_db_common_1.DbNotSupportedError('Upsert', param);
506
676
  }
507
677
  if (!ignoreCheck) {
508
678
  (0, type_2.assertObject)(doc, Object.assign({ field: 'doc' }, param));
509
- (0, type_2.assertText)(doc._urn, Object.assign({ field: 'doc._urn' }, param));
510
- if (useNumericId) {
679
+ (0, type_2.assertText)(doc.urn, Object.assign({ field: 'doc._urn' }, param));
680
+ if (this.hasNumericId) {
511
681
  (0, type_1.assertInteger)(doc.id, Object.assign({ field: 'doc.id' }, param));
512
682
  }
513
683
  else {
514
684
  (0, type_2.assertText)(doc.id, Object.assign({ field: 'doc.id' }, param));
515
685
  }
516
686
  }
517
- yield conn.exec(collection.upsert(doc._urn, doc), opt);
518
- return doc._urn;
687
+ yield this.exec(this.collection.upsert(doc.urn, doc), opt);
688
+ return doc.urn;
519
689
  });
520
690
  }
521
691
  // endregion upsert
@@ -523,13 +693,12 @@ class CbRepo extends be_db_common_1.DbRepo {
523
693
  /** @inheritDoc */
524
694
  $updateByPrimary(key, doc, p1, ignoreCheck) {
525
695
  return __awaiter(this, void 0, void 0, function* () {
526
- const { conn, full, noUpdate, useSoftDelete } = this._props;
527
- if (!conn.props.enabled) {
696
+ if (!this.isEnabled) {
528
697
  return undefined;
529
698
  }
530
- const opt = conn.buildOpt(p1, '$updateByPrimary');
699
+ const opt = this.buildOpt(p1, '$updateByPrimary');
531
700
  const param = { where: 'CbRepo', method: '$updateByPrimary', queryName: opt.name };
532
- if (noUpdate) {
701
+ if (this.isNoUpdate) {
533
702
  throw new be_db_common_1.DbNotSupportedError('Update', param);
534
703
  }
535
704
  if (!ignoreCheck) {
@@ -542,17 +711,17 @@ class CbRepo extends be_db_common_1.DbRepo {
542
711
  const unset = [];
543
712
  for (const [k, v] of Object.entries(doc)) {
544
713
  if (v === undefined) {
545
- unset.push('a.' + conn.f(k));
714
+ unset.push('a.' + this.f(k));
546
715
  }
547
716
  else {
548
- set.push(['a.' + conn.f(k), '$param_' + paramId]);
717
+ set.push(['a.' + this.f(k), '$param_' + paramId]);
549
718
  parameters['param_' + paramId] = v;
550
719
  paramId++;
551
720
  }
552
721
  }
553
722
  if (set.length > 0 || unset.length > 0) {
554
723
  const lines = (0, be_db_common_1.dbLines)();
555
- lines.add(`UPDATE ${full} a`);
724
+ lines.add(`UPDATE ${this.table} a`);
556
725
  lines.add(`USE KEYS $key`);
557
726
  parameters['key'] = key;
558
727
  if (set.length > 0) {
@@ -562,8 +731,8 @@ class CbRepo extends be_db_common_1.DbRepo {
562
731
  lines.add('UNSET').add(unset.join(', '));
563
732
  }
564
733
  lines
565
- .yes(useSoftDelete, `WHERE (a.${F_TRASH_ID} is missing)`);
566
- yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }));
734
+ .yes(this.hasSoftDelete, `WHERE (a.${F_TRASH_ID} is missing)`);
735
+ yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }));
567
736
  return key;
568
737
  }
569
738
  return undefined;
@@ -573,17 +742,16 @@ class CbRepo extends be_db_common_1.DbRepo {
573
742
  $updateBySecondary(key, doc, p1, ignoreCheck) {
574
743
  return __awaiter(this, void 0, void 0, function* () {
575
744
  var _a;
576
- const { useNumericId, noUpdate, useSoftDelete, conn, full } = this._props;
577
- if (!conn.props.enabled) {
745
+ if (!this.isEnabled) {
578
746
  return undefined;
579
747
  }
580
- const opt = conn.buildOpt(p1, '$updateBySecondary');
748
+ const opt = this.buildOpt(p1, '$updateBySecondary');
581
749
  const param = { where: 'CbRepo', method: '$updateBySecondary', queryName: opt.name };
582
- if (noUpdate) {
750
+ if (this.isNoUpdate) {
583
751
  throw new be_db_common_1.DbNotSupportedError('Update', param);
584
752
  }
585
753
  if (!ignoreCheck) {
586
- if (useNumericId) {
754
+ if (this.hasNumericId) {
587
755
  (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
588
756
  }
589
757
  else {
@@ -597,17 +765,17 @@ class CbRepo extends be_db_common_1.DbRepo {
597
765
  const unset = [];
598
766
  for (const [k, v] of Object.entries(doc)) {
599
767
  if (v === undefined) {
600
- unset.push('a.' + conn.f(k));
768
+ unset.push('a.' + this.f(k));
601
769
  }
602
770
  else {
603
- set.push(['a.' + conn.f(k), '$param_' + paramId]);
771
+ set.push(['a.' + this.f(k), '$param_' + paramId]);
604
772
  parameters['param_' + paramId] = v;
605
773
  paramId++;
606
774
  }
607
775
  }
608
776
  if (set.length > 0 || unset.length > 0) {
609
777
  const lines = (0, be_db_common_1.dbLines)();
610
- lines.add(`UPDATE ${full} a`);
778
+ lines.add(`UPDATE ${this.table} a`);
611
779
  if (set.length > 0) {
612
780
  lines.add('SET').add(set.join(',\n '));
613
781
  }
@@ -616,10 +784,10 @@ class CbRepo extends be_db_common_1.DbRepo {
616
784
  }
617
785
  lines
618
786
  .add(`WHERE (a.${F_ID} = $key)`)
619
- .yes(useSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`);
787
+ .yes(this.hasSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`);
620
788
  parameters['key'] = key;
621
789
  lines.add('RETURNING meta().id \`x\`');
622
- return (_a = (yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x;
790
+ return (_a = (yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x;
623
791
  }
624
792
  return undefined;
625
793
  });
@@ -629,30 +797,29 @@ class CbRepo extends be_db_common_1.DbRepo {
629
797
  /** @inheritDoc */
630
798
  $removeByPrimary(key, p1, ignoreCheck) {
631
799
  return __awaiter(this, void 0, void 0, function* () {
632
- const { useSoftDelete, noRemove, conn, full, collection } = this._props;
633
- if (!conn.props.enabled) {
800
+ if (!this.isEnabled) {
634
801
  return undefined;
635
802
  }
636
- const opt = conn.buildOpt(p1, '$removeByPrimary');
803
+ const opt = this.buildOpt(p1, '$removeByPrimary');
637
804
  const param = { where: 'CbRepo', method: '$removeByPrimary', queryName: opt.name };
638
- if (noRemove) {
805
+ if (this.isNoRemove) {
639
806
  throw new be_db_common_1.DbNotSupportedError('Remove', param);
640
807
  }
641
808
  if (!ignoreCheck) {
642
809
  (0, type_2.assertText)(key, Object.assign({ field: 'key' }, param));
643
810
  }
644
- if (!useSoftDelete) {
645
- yield conn.exec(collection.remove(key, opt), opt);
811
+ if (!this.hasSoftDelete) {
812
+ yield this.exec(this.collection.remove(key, opt), opt);
646
813
  return key;
647
814
  }
648
815
  const lines = (0, be_db_common_1.dbLines)();
649
816
  lines
650
817
  .add('DELETE')
651
- .add(`FROM ${full} a`)
818
+ .add(`FROM ${this.table} a`)
652
819
  .add(`USE KEYS $key`)
653
- .yes(useSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`);
820
+ .yes(this.hasSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`);
654
821
  const parameters = { key };
655
- yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }));
822
+ yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }));
656
823
  return key;
657
824
  });
658
825
  }
@@ -660,17 +827,16 @@ class CbRepo extends be_db_common_1.DbRepo {
660
827
  $removeBySecondary(key, p1, ignoreCheck) {
661
828
  return __awaiter(this, void 0, void 0, function* () {
662
829
  var _a;
663
- const { useNumericId, noRemove, useSoftDelete, conn, full } = this._props;
664
- if (!conn.props.enabled) {
830
+ if (!this.isEnabled) {
665
831
  return undefined;
666
832
  }
667
- const opt = conn.buildOpt(p1, '$removeBySecondary');
833
+ const opt = this.buildOpt(p1, '$removeBySecondary');
668
834
  const param = { where: 'CbRepo', method: '$removeBySecondary', queryName: opt.name };
669
- if (noRemove) {
835
+ if (this.isNoRemove) {
670
836
  throw new be_db_common_1.DbNotSupportedError('Remove', param);
671
837
  }
672
838
  if (!ignoreCheck) {
673
- if (useNumericId) {
839
+ if (this.hasNumericId) {
674
840
  (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
675
841
  }
676
842
  else {
@@ -680,12 +846,12 @@ class CbRepo extends be_db_common_1.DbRepo {
680
846
  const lines = (0, be_db_common_1.dbLines)();
681
847
  lines
682
848
  .add('DELETE')
683
- .add(`FROM ${full} a`)
849
+ .add(`FROM ${this.table} a`)
684
850
  .add(`WHERE (a.${F_ID} = $key)`)
685
- .yes(useSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`)
851
+ .yes(this.hasSoftDelete, ` AND (a.${F_TRASH_ID} is missing)`)
686
852
  .add('RETURNING meta().id \`x\`');
687
853
  const parameters = { key };
688
- return (_a = (yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x;
854
+ return (_a = (yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x;
689
855
  });
690
856
  }
691
857
  // endregion remove
@@ -693,16 +859,15 @@ class CbRepo extends be_db_common_1.DbRepo {
693
859
  /** @inheritDoc */
694
860
  $trashByPrimary(key, p1, ignoreCheck) {
695
861
  return __awaiter(this, void 0, void 0, function* () {
696
- const { useSoftDelete, noTrash, conn, full } = this._props;
697
- if (!conn.props.enabled) {
862
+ if (!this.isEnabled) {
698
863
  return undefined;
699
864
  }
700
- const opt = conn.buildOpt(p1, '$trashByPrimary');
865
+ const opt = this.buildOpt(p1, '$trashByPrimary');
701
866
  const param = { where: 'CbRepo', method: '$trashByPrimary', queryName: opt.name };
702
- if (noTrash) {
867
+ if (this.isNoTrash) {
703
868
  throw new be_db_common_1.DbNotSupportedError('Trash', param);
704
869
  }
705
- if (!useSoftDelete) {
870
+ if (!this.hasSoftDelete) {
706
871
  throw new be_db_common_1.DbNotSupportedError('Soft delete', param);
707
872
  }
708
873
  if (!ignoreCheck) {
@@ -710,7 +875,7 @@ class CbRepo extends be_db_common_1.DbRepo {
710
875
  }
711
876
  const lines = (0, be_db_common_1.dbLines)();
712
877
  lines
713
- .add(`UPDATE ${full} a`)
878
+ .add(`UPDATE ${this.table} a`)
714
879
  .add(`SET`)
715
880
  .add(` \`updatedAt\` = $updated,`)
716
881
  .add(` ${F_TRASH_ID} = $trashId`)
@@ -721,7 +886,7 @@ class CbRepo extends be_db_common_1.DbRepo {
721
886
  trashId: opt.trashId,
722
887
  key,
723
888
  };
724
- yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }));
889
+ yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }));
725
890
  return key;
726
891
  });
727
892
  }
@@ -729,20 +894,19 @@ class CbRepo extends be_db_common_1.DbRepo {
729
894
  $trashBySecondary(key, p1, ignoreCheck) {
730
895
  return __awaiter(this, void 0, void 0, function* () {
731
896
  var _a;
732
- const { useSoftDelete, noTrash, useNumericId, conn, full } = this._props;
733
- if (!conn.props.enabled) {
897
+ if (!this.isEnabled) {
734
898
  return undefined;
735
899
  }
736
- const opt = conn.buildOpt(p1, '$trashBySecondary');
900
+ const opt = this.buildOpt(p1, '$trashBySecondary');
737
901
  const param = { where: 'CbRepo', method: '$trashBySecondary', queryName: opt.name };
738
- if (noTrash) {
902
+ if (this.isNoTrash) {
739
903
  throw new be_db_common_1.DbNotSupportedError('Trash', param);
740
904
  }
741
- if (!useSoftDelete) {
905
+ if (!this.hasSoftDelete) {
742
906
  throw new be_db_common_1.DbNotSupportedError('Soft delete', param);
743
907
  }
744
908
  if (!ignoreCheck) {
745
- if (useNumericId) {
909
+ if (this.hasNumericId) {
746
910
  (0, type_1.assertInteger)(key, Object.assign({ field: 'key' }, param));
747
911
  }
748
912
  else {
@@ -751,7 +915,7 @@ class CbRepo extends be_db_common_1.DbRepo {
751
915
  }
752
916
  const lines = (0, be_db_common_1.dbLines)();
753
917
  lines
754
- .add(`UPDATE ${full} a`)
918
+ .add(`UPDATE ${this.table} a`)
755
919
  .add(`SET`)
756
920
  .add(` \`updatedAt\` = $updated,`)
757
921
  .add(` ${F_TRASH_ID} = $trashId`)
@@ -763,7 +927,7 @@ class CbRepo extends be_db_common_1.DbRepo {
763
927
  trashId: opt.trashId,
764
928
  key,
765
929
  };
766
- return (_a = (yield conn.one(this._props, lines.end(), Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x;
930
+ return (_a = (yield this.one(lines, Object.assign(Object.assign({}, opt), { parameters }))).row) === null || _a === void 0 ? void 0 : _a.x;
767
931
  });
768
932
  }
769
933
  }