@samet-it/be-couchbase-common 1.0.17 → 1.1.2

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.
Files changed (40) hide show
  1. package/README.md +10 -10
  2. package/dist/config/couchbase-common.config.d.ts +5 -1
  3. package/dist/config/couchbase-common.config.js +15 -9
  4. package/dist/config/index.types.d.ts +17 -13
  5. package/dist/connection/cb-connection.fn.d.ts +8 -0
  6. package/dist/connection/cb-connection.fn.js +14 -0
  7. package/dist/connection/cb-direct.connection.d.ts +13 -0
  8. package/dist/connection/cb-direct.connection.js +21 -0
  9. package/dist/connection/cb.connection.d.ts +38 -0
  10. package/dist/{adapter/cb-adapter.service.js → connection/cb.connection.js} +98 -126
  11. package/dist/connection/index.d.ts +4 -0
  12. package/dist/{adapter → connection}/index.js +3 -2
  13. package/dist/connection/index.types.d.ts +94 -0
  14. package/dist/filter/cb-filter-util.impl.js +4 -4
  15. package/dist/filter/index.types.d.ts +4 -4
  16. package/dist/index.d.ts +1 -3
  17. package/dist/index.js +1 -6
  18. package/dist/repo/cb-direct.repo.d.ts +11 -0
  19. package/dist/repo/cb-direct.repo.js +37 -0
  20. package/dist/repo/cb-schema.error.d.ts +4 -0
  21. package/dist/repo/cb-schema.error.js +10 -0
  22. package/dist/repo/cb.repo.d.ts +66 -59
  23. package/dist/repo/cb.repo.js +500 -267
  24. package/dist/repo/index.d.ts +2 -0
  25. package/dist/repo/index.js +2 -0
  26. package/dist/repo/index.types.d.ts +133 -87
  27. package/package.json +8 -5
  28. package/dist/adapter/cb-adapter.module.d.ts +0 -2
  29. package/dist/adapter/cb-adapter.module.js +0 -23
  30. package/dist/adapter/cb-adapter.service.d.ts +0 -46
  31. package/dist/adapter/index.d.ts +0 -3
  32. package/dist/adapter/index.types.d.ts +0 -135
  33. package/dist/assets/source.MD +0 -1
  34. package/dist/line/cb-line.impl.d.ts +0 -2
  35. package/dist/line/cb-line.impl.js +0 -119
  36. package/dist/line/index.d.ts +0 -2
  37. package/dist/line/index.js +0 -18
  38. package/dist/line/index.types.d.ts +0 -62
  39. package/dist/line/index.types.js +0 -2
  40. /package/dist/{adapter → connection}/index.types.js +0 -0
@@ -0,0 +1,94 @@
1
+ import type { Cluster, QueryMetaData, QueryOptions, QueryResult } from "couchbase";
2
+ import type { IgnoreFieldsByType, KeyValue, ReplaceType } from "@leyyo/common";
3
+ import type { CbRepoDef, CbRepoDirectOpt, CbRepoLike } from "../repo";
4
+ import type { DbConnectionLike, DbConnOpt, DbConnProps, DbExecOpt } from "@samet-it/be-db-common";
5
+ import type { DefDims, Entity, UrnDocLike, Pair, Portion, View } from "@samet-it/be-base-common";
6
+ /**
7
+ * Couchbase connection interface
8
+ * */
9
+ export interface CbConnectionLike extends DbConnectionLike<CbRepoDef, QueryMetaData, CbExecOpt> {
10
+ /** @inheritDoc */
11
+ get props(): Readonly<CbConnProps>;
12
+ /**
13
+ * Flatten query result to rows
14
+ * */
15
+ flatten<T>(result: QueryResult<T>): Array<T>;
16
+ /**
17
+ * Cast dates
18
+ * */
19
+ castDates<T>(given: ReplaceType<T, Date, string> | T, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
20
+ /**
21
+ * Cast dates for an array
22
+ * */
23
+ castDatesForList<T>(given: Array<ReplaceType<T, Date, string> | T>, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
24
+ }
25
+ /**
26
+ * Couchbase connection option interface
27
+ * */
28
+ export interface CbConnOpt extends DbConnOpt {
29
+ /**
30
+ * Current bucket (database name)
31
+ *
32
+ * @type {string}
33
+ * */
34
+ bucketName?: string;
35
+ /**
36
+ * Current scope (schema name)
37
+ *
38
+ * @type {string}
39
+ * */
40
+ scopeName?: string;
41
+ /**
42
+ * Create indices when it's started
43
+ *
44
+ * @type {boolean}
45
+ * */
46
+ createIndices?: boolean;
47
+ }
48
+ /**
49
+ * Couchbase direct connection option interface
50
+ * */
51
+ export interface CbDirectConnOpt extends CbConnOpt {
52
+ /**
53
+ * Logger name
54
+ *
55
+ * @type {string}
56
+ * */
57
+ name?: string;
58
+ }
59
+ /**
60
+ * Couchbase connection props interface
61
+ * */
62
+ export interface CbConnProps extends CbConnOpt, DbConnProps {
63
+ /**
64
+ * Couchbase cluster
65
+ *
66
+ * @type {Cluster}
67
+ * */
68
+ cluster: Cluster;
69
+ }
70
+ /**
71
+ * Couchbase query option
72
+ * */
73
+ export type CbExecOpt = DbExecOpt & QueryOptions;
74
+ /**
75
+ * Direct connection to use outside dependency injection
76
+ * */
77
+ export interface CbDirectConnectionLike extends CbConnectionLike {
78
+ /**
79
+ * Creates new direct repo without dependency injection
80
+ *
81
+ * @param {CbRepoDirectOpt} opt - options
82
+ * @return {CbRepoLike} - created repository
83
+ * */
84
+ newRepo<ID extends KeyValue, ENTITY extends Entity<ID>, URN extends UrnDocLike, KEYS extends string = string, DIMS extends DefDims = DefDims, PAIR extends Pair<ID> = Pair<ID>, VIEW extends View<ID> = View<ID>, PORTION extends Portion<ID> = Portion<ID>, R = unknown>(opt: CbRepoDirectOpt<ID, ENTITY, URN, DIMS, R>): CbRepoLike<ID, ENTITY, URN, KEYS, DIMS, PAIR, VIEW, PORTION>;
85
+ }
86
+ export type CbIgnoreDate<T> = CbReplaceType<T, Date, string>;
87
+ export type CbIgnoreDateKeys<T> = CbIgnoreFields<T, Date>;
88
+ type CbIgnoreFields<T, I> = {
89
+ [K in keyof T]: T[K] extends I ? K : never;
90
+ }[keyof T];
91
+ type CbReplaceType<T, O, N> = {
92
+ [P in keyof T]: T[P] extends O ? N : T[P];
93
+ };
94
+ export {};
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cbFilterUtil = void 0;
4
- const common_1 = require("@leyyo/common");
4
+ const type_1 = require("@leyyo/type");
5
5
  class CbFilterUtilImpl {
6
6
  _where(opt) {
7
7
  const result = opt.whereUsed ? 'AND' : 'WHERE';
@@ -10,7 +10,7 @@ class CbFilterUtilImpl {
10
10
  }
11
11
  /** @inheritDoc */
12
12
  readOne(value, fn) {
13
- if (!common_1.$is.text(value)) {
13
+ if (!(0, type_1.isText)(value)) {
14
14
  return undefined;
15
15
  }
16
16
  if (typeof fn === 'function') {
@@ -20,7 +20,7 @@ class CbFilterUtilImpl {
20
20
  }
21
21
  /** @inheritDoc */
22
22
  readArray(value, fn) {
23
- if (!common_1.$is.text(value)) {
23
+ if (!(0, type_1.isText)(value)) {
24
24
  return undefined;
25
25
  }
26
26
  if (typeof fn !== 'function') {
@@ -33,7 +33,7 @@ class CbFilterUtilImpl {
33
33
  }
34
34
  /** @inheritDoc */
35
35
  readBetween(value, fn) {
36
- if (!common_1.$is.text(value)) {
36
+ if (!(0, type_1.isText)(value)) {
37
37
  return undefined;
38
38
  }
39
39
  if (typeof fn !== 'function') {
@@ -1,5 +1,5 @@
1
- import type { CbLine } from "../line";
2
- import type { CbAdapterServiceLike } from "../adapter";
1
+ import type { CbConnectionLike } from "../connection";
2
+ import type { DbLines } from "@samet-it/be-db-common";
3
3
  export type FilterCastLambda<T> = (v: unknown) => T;
4
4
  export interface CbFilterUtil {
5
5
  /**
@@ -26,9 +26,9 @@ export interface CbFilterUtil {
26
26
  * Builds a sql for between
27
27
  * */
28
28
  sqlBetween(opt: CbFilterUtilWhereOpt, field: string, value: [unknown, unknown]): string | undefined;
29
- $set(cb: CbAdapterServiceLike): void;
29
+ $set(cb: CbConnectionLike): void;
30
30
  }
31
31
  export interface CbFilterUtilWhereOpt {
32
- lines: CbLine;
32
+ lines: DbLines;
33
33
  whereUsed: boolean;
34
34
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- export * from './adapter';
1
+ export * from './connection';
2
2
  export * from './config';
3
3
  export * from './filter';
4
- export * from './line';
5
4
  export * from './repo';
6
- export { QueryResult as CbResult } from 'couchbase';
package/dist/index.js CHANGED
@@ -14,12 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.CbResult = void 0;
18
- __exportStar(require("./adapter"), exports);
17
+ __exportStar(require("./connection"), exports);
19
18
  __exportStar(require("./config"), exports);
20
19
  __exportStar(require("./filter"), exports);
21
- __exportStar(require("./line"), exports);
22
20
  __exportStar(require("./repo"), exports);
23
- // noinspection JSUnusedGlobalSymbols
24
- var couchbase_1 = require("couchbase");
25
- Object.defineProperty(exports, "CbResult", { enumerable: true, get: function () { return couchbase_1.QueryResult; } });
@@ -0,0 +1,11 @@
1
+ import { CbRepo } from "./cb.repo";
2
+ import type { DefDims, Entity, Pair, Portion, UrnDocLike, View } from "@samet-it/be-base-common";
3
+ import type { CbConnectionLike } from "../connection";
4
+ import type { CbRepoDirectOpt } from "./index.types";
5
+ import type { KeyValue } from "@leyyo/common";
6
+ /**
7
+ * Couchbase repository direct class
8
+ * */
9
+ export declare class CbDirectRepo<ID extends KeyValue, ENTITY extends Entity<ID>, URN extends UrnDocLike, KEYS extends string = string, DIMS extends DefDims = DefDims, PAIR extends Pair<ID> = Pair<ID>, VIEW extends View<ID> = View<ID>, PORTION extends Portion<ID> = Portion<ID>> extends CbRepo<ID, ENTITY, URN, KEYS, DIMS, PAIR, VIEW, PORTION> {
10
+ constructor(conn: CbConnectionLike, opt: CbRepoDirectOpt<ID, ENTITY, URN, DIMS>);
11
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.CbDirectRepo = void 0;
13
+ const cb_repo_1 = require("./cb.repo");
14
+ const be_base_common_1 = require("@samet-it/be-base-common");
15
+ /**
16
+ * Couchbase repository direct class
17
+ * */
18
+ class CbDirectRepo extends cb_repo_1.CbRepo {
19
+ constructor(conn, opt) {
20
+ super(conn, opt);
21
+ if (opt) {
22
+ if (!opt.name) {
23
+ opt.name = this._props.urnPrefix;
24
+ }
25
+ if (typeof opt.toUrnTuple === 'function') {
26
+ this.$toUrnTuple = opt.toUrnTuple;
27
+ }
28
+ if (typeof opt.toDim === 'function') {
29
+ this.$toDim = (doc, dim) => __awaiter(this, void 0, void 0, function* () {
30
+ return (yield opt.toDim(doc, dim));
31
+ });
32
+ }
33
+ }
34
+ this.logger = be_base_common_1.logger.of(`CbRepo${(opt === null || opt === void 0 ? void 0 : opt.name) ? '#' + (opt === null || opt === void 0 ? void 0 : opt.name) : ''}`);
35
+ }
36
+ }
37
+ exports.CbDirectRepo = CbDirectRepo;
@@ -0,0 +1,4 @@
1
+ import { DbError } from "@samet-it/be-db-common";
2
+ export declare class CbSchemaError extends DbError {
3
+ constructor(field: string, value: unknown, where: string, method: string);
4
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CbSchemaError = void 0;
4
+ const be_db_common_1 = require("@samet-it/be-db-common");
5
+ class CbSchemaError extends be_db_common_1.DbError {
6
+ constructor(field, value, where, method) {
7
+ super(`${field} could not be found! => ${value}`, { field, where, value, method });
8
+ }
9
+ }
10
+ exports.CbSchemaError = CbSchemaError;
@@ -1,94 +1,101 @@
1
- import { CbAdapterService, type CbEntity, type CbExecOpt } from "../adapter";
2
- import { Bucket, Cluster, Collection, Scope } from "couchbase";
3
- import type { CbRepoDef, CbRepoLike } from "./index.types";
4
- import { Logger } from "@nestjs/common";
5
- export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLike<E, ID> {
6
- protected colName: string;
7
- protected cb: CbAdapterService;
8
- private _scopeName?;
9
- private _bucketName?;
10
- private _tryCount;
11
- private _pkIndexed;
12
- protected logger: Logger;
13
- /** @inheritDoc */
14
- readonly fullPath: string;
15
- /** @inheritDoc */
16
- readonly path: string;
17
- /** @inheritDoc */
18
- readonly cluster: Cluster;
19
- /** @inheritDoc */
20
- readonly bucket: Bucket;
21
- /** @inheritDoc */
22
- readonly scope: Scope;
23
- /** @inheritDoc */
24
- readonly collection: Collection;
25
- protected constructor(colName: string, cb: CbAdapterService, _scopeName?: string, _bucketName?: string);
26
- private get _randomIndexName();
27
- protected _indexName(name: string): string;
28
- /**
29
- * Fetch urn from doc as doc._urn
30
- * */
31
- protected _urnFromDoc(doc: Partial<E>, method: string): string;
1
+ import type { CbConnectionLike, CbExecOpt } from "../connection";
2
+ import type { KeyValue } from "@leyyo/common";
3
+ import type { StrKey } from "@leyyo/common";
4
+ import type { CbExistsOpt, CbGetOpt, CbInsertOpt, CbReplaceOpt, CbRepoDef, CbRepoProps, CbRepoLike, CbRepoOpt } from "./index.types";
5
+ import { DbRepo } from "@samet-it/be-db-common";
6
+ import type { DefDims, Entity, Pair, Portion, UrnDocLike, View } from "@samet-it/be-base-common";
7
+ import type { QueryRegular } from "@leyyo/query";
8
+ /**
9
+ * Couchbase repository abstract class
10
+ * */
11
+ export declare abstract class CbRepo<ID extends KeyValue, ENTITY extends Entity<ID>, URN extends UrnDocLike, KEYS extends string = string, DIMS extends DefDims = DefDims, PAIR extends Pair<ID> = Pair<ID>, VIEW extends View<ID> = View<ID>, PORTION extends Portion<ID> = Portion<ID>> extends DbRepo<CbConnectionLike, CbExecOpt, ID, ENTITY, URN, KEYS, DIMS, PAIR, VIEW, PORTION> implements CbRepoLike<ID, ENTITY, URN, KEYS, DIMS, PAIR, VIEW, PORTION> {
12
+ private static _specs;
13
+ /** @inheritDoc */
14
+ protected _props: CbRepoProps<ID, ENTITY>;
15
+ protected constructor(conn: CbConnectionLike, opt?: CbRepoOpt<ID, ENTITY>);
32
16
  /**
33
- * Check array keys as id list or urn list
17
+ * Check property name
18
+ *
19
+ * @param {string} value
20
+ * @return {string}
34
21
  * */
35
- protected _checkKeys<I extends string | number = string>(keys: I[]): I[];
36
- /**
37
- * Build update sql as set and unset
38
- * */
39
- protected _updateSql<T>(urn: string, doc: T): string;
22
+ protected _checkName(value: string): string;
40
23
  /**
41
24
  * Execute the creation of index for one field
25
+ *
26
+ * @param {string} field - field name
27
+ * @param {string?} name - index name
42
28
  * */
43
- protected _createIndex(field: keyof E | string, name?: string): Promise<void>;
29
+ protected _createIndex(field: keyof ENTITY | string, name?: string): Promise<void>;
44
30
  /**
45
31
  * Execute the creation of index for complex fields
32
+ *
33
+ * @param {Array<string>} fields - field names
34
+ * @param {string} name - index name
46
35
  * */
47
- protected _createIndex(fields: Array<keyof E | string>, name: string): Promise<void>;
36
+ protected _createIndex(fields: Array<keyof ENTITY | string>, name: string): Promise<void>;
48
37
  /**
49
38
  * Execute the creation of indexes with keys
39
+ *
40
+ * @param {...Array<string>} keys - keys of model
50
41
  * */
51
- protected _createMoreIndices(...keys: Array<keyof E>): Promise<void>;
42
+ protected _createMoreIndices(...keys: Array<keyof ENTITY>): Promise<void>;
52
43
  /**
53
44
  * Execute the creation of primary key
54
45
  * */
55
46
  protected _createPk(): Promise<void>;
47
+ /** @inheritDoc */
48
+ protected _afterConnected(): Promise<void>;
49
+ /**
50
+ * Load & check bucket by cluster
51
+ * */
52
+ private _loadBucket;
53
+ /**
54
+ * Load & check scope by bucket
55
+ * */
56
+ private _loadScope;
57
+ /**
58
+ * Load & check collection by scope
59
+ * */
60
+ private _loadCollection;
56
61
  /**
57
- * Create indices
62
+ * Create indices in final repo
63
+ *
64
+ * can be @override
58
65
  * */
59
- protected abstract _createIndices(): Promise<void>;
66
+ protected $createIndices(): Promise<void>;
60
67
  /** @inheritDoc */
61
- get def(): CbRepoDef;
68
+ get $def(): CbRepoDef;
62
69
  /** @inheritDoc */
63
- abstract urn(part: ID): string;
70
+ get props(): Readonly<CbRepoProps<ID, ENTITY>>;
64
71
  /** @inheritDoc */
65
- onModuleInit(): Promise<void>;
72
+ $getByPrimary(key: string, p1?: CbGetOpt | string, ignoreCheck?: boolean): Promise<ENTITY | undefined>;
66
73
  /** @inheritDoc */
67
- urnById(id: string, p1?: string | CbExecOpt): Promise<string | undefined>;
74
+ $getBySecondary(key: KeyValue, p1?: CbGetOpt | string, ignoreCheck?: boolean): Promise<ENTITY | undefined>;
68
75
  /** @inheritDoc */
69
- existsByUrn(urn: string, p1?: string | CbExecOpt): Promise<boolean>;
76
+ $existsByPrimary(key: string, p1?: CbExistsOpt | string, ignoreCheck?: boolean): Promise<boolean>;
70
77
  /** @inheritDoc */
71
- existsById(id: string, p1?: string | CbExecOpt): Promise<boolean>;
78
+ $existsBySecondary(key: KeyValue, p1?: CbExistsOpt | string, ignoreCheck?: boolean): Promise<boolean>;
72
79
  /** @inheritDoc */
73
- getByUrn(urn: string, p1?: string | CbExecOpt): Promise<E | undefined>;
80
+ $listByPrimary(keys: string[], p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<Array<ENTITY>>;
74
81
  /** @inheritDoc */
75
- getById(id: string, p1?: string | CbExecOpt): Promise<E | undefined>;
82
+ $listBySecondary(keys: KeyValue[], p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<Array<ENTITY>>;
76
83
  /** @inheritDoc */
77
- listByUrn(urnList: string[], p1?: string | CbExecOpt): Promise<Array<E>>;
84
+ $filter<R = ENTITY, K2 extends string = StrKey<R>>(query: QueryRegular<K2 | KEYS | StrKey<ENTITY>>, p1?: string | CbExecOpt): Promise<Array<R>>;
78
85
  /** @inheritDoc */
79
- listById(ids: string[], p1?: string | CbExecOpt): Promise<Array<E>>;
86
+ $insert(doc: ENTITY, p1?: CbInsertOpt | string, ignoreCheck?: boolean): Promise<string>;
80
87
  /** @inheritDoc */
81
- removeByUrn(urn: string, p1?: string | CbExecOpt): Promise<string>;
88
+ $replace(doc: ENTITY, p1?: CbReplaceOpt | string, ignoreCheck?: boolean): Promise<string>;
82
89
  /** @inheritDoc */
83
- removeById(id: string, p1?: string | CbExecOpt): Promise<string>;
90
+ $updateByPrimary(key: string, doc: Partial<ENTITY>, p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<string>;
84
91
  /** @inheritDoc */
85
- insert(doc: E, p1?: string | CbExecOpt): Promise<string>;
92
+ $updateBySecondary(key: KeyValue, doc: Partial<ENTITY>, p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<string>;
86
93
  /** @inheritDoc */
87
- replace(doc: E, p1?: string | CbExecOpt): Promise<string>;
94
+ $removeByPrimary(key: string, p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<string>;
88
95
  /** @inheritDoc */
89
- upsert(doc: E, p1?: string | CbExecOpt): Promise<string>;
96
+ $removeBySecondary(key: KeyValue, p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<string>;
90
97
  /** @inheritDoc */
91
- update(doc: Partial<E>, p1?: string | CbExecOpt): Promise<unknown>;
98
+ $trashByPrimary(key: string, p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<string>;
92
99
  /** @inheritDoc */
93
- trash(doc: E, p1?: string | CbExecOpt): Promise<unknown>;
100
+ $trashBySecondary(key: KeyValue, p1?: CbExecOpt | string, ignoreCheck?: boolean): Promise<string>;
94
101
  }