@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
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  \___/\____/\__,_/\___/_/ /_/_.___/\__,_/____/\___/ \___/\____/_/ /_/ /_/_/ /_/ /_/\____/_/ /_/
9
9
  ```
10
10
  <!-- https://patorjk.com/software/taag/#p=display&f=Slant&t=couchbase+common&x=none&v=4&h=3&w=80&we=false -->
11
- - `EN`: Use it for Couchbase connections
11
+ - `EN`: Use it for couchbase connections
12
12
  - `TR`: Couchbase bağlantıları için bu bileşeni kullanınız
13
13
 
14
14
  ## Prerequisite
@@ -28,7 +28,7 @@ npm i @samet-it/be-couchbase-common
28
28
  ```
29
29
 
30
30
  ## Environments
31
- - [CouchbaseCommon](./src/config/couchbase-common.config.ts)
31
+ - [CouchbaseCommonConf](./src/config/couchbase-common.config.ts)
32
32
 
33
33
  | Name | Type | Default | Required | Secret | Description |
34
34
  |---------------------|---------|---------|----------|--------|---------------------------------------------|
@@ -39,14 +39,13 @@ npm i @samet-it/be-couchbase-common
39
39
  | `CB_SCOPE` | string | | ✅ | | Scope/Schema Name |
40
40
  | `CB_CREATE_INDICES` | boolean | _false_ | | | Does it create the indices when it deploys? |
41
41
 
42
-
43
42
  ## Contents
44
- - [CbAdapterService](./src/adapter/cb-adapter.service.ts) - Injectable service
45
- - [CbAdapterModule](./src/adapter/cb-adapter.module.ts) - Nestjs Module
46
- - [couchbaseCommonConfig](./src/config/couchbase-common.config.ts) - config
47
- - [cbFilterUtil](./src/filter/cb-filter-util.impl.ts) - query filter utility
48
- - [cbLine()](./src/line/cb-line.impl.ts) - sql line utility
49
- - [CbRepo](./src/repo/cb.repo.ts) - abstract repository
43
+ - `abstract` [CbConnection](src/connection/cb.connection.ts) - abstract connection class
44
+ - `class` [CbDirectConnection](src/connection/cb-direct.connection.ts) - Direct connection
45
+ - `function` [cbConnection()](src/connection/cb-connection.fn.ts) - Connection function
46
+ - `abstract` [CbRepo](./src/repo/cb.repo.ts) - abstract repository
47
+ - `class` [CbDirectRepo](./src/repo/cb-direct.repo.ts) - direct repository
48
+ - `instance` [cbFilterUtil](./src/filter/cb-filter-util.impl.ts) - query filter utility
50
49
 
51
50
 
52
51
  ## Development
@@ -64,8 +63,9 @@ git clone https://github.com/samet-digital/be-couchbase-common.git -b developmen
64
63
 
65
64
  ## Dependencies
66
65
  - `@samet-it/be-base-common`: **samet** base common
66
+ - `@samet-it/be-db-common`: **samet** db common
67
+ - `@samet-it/be-cache-common`: **samet** cache common
67
68
  - `@leyyo/*`: leyyo utilities
68
- - `@nestjs/*`: nestjs framework
69
69
  - `couchbase`: couchbase client
70
70
 
71
71
  ## Commands
@@ -1,2 +1,6 @@
1
1
  import type { CouchbaseCommonConf } from "./index.types";
2
- export declare const couchbaseCommonConfig: import("@leyyo/env").EnvScopeRuntime<CouchbaseCommonConf>;
2
+ /**
3
+ * Couchbase common config
4
+ * @config
5
+ * */
6
+ export declare const couchbaseCommonConfig: import("@leyyo/env").EnvScopeRuntime<CouchbaseCommonConf, "CB", undefined>;
@@ -2,13 +2,19 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.couchbaseCommonConfig = void 0;
4
4
  const env_1 = require("@leyyo/env");
5
+ /**
6
+ * Couchbase common config
7
+ * @config
8
+ * */
5
9
  exports.couchbaseCommonConfig = env_1.envCore.configure
6
- .scope('CouchbaseCommon')
7
- .begin
8
- .field('CB_HOST').text().required().end
9
- .field('CB_USER').text().required().end
10
- .field('CB_PASS').text().required().end
11
- .field('CB_BUCKET').text().required().end
12
- .field('CB_SCOPE').text().required().end
13
- .field('CB_CREATE_INDICES').boolean().def(false).end
14
- .end;
10
+ .scope('CouchbaseCommon', 'CB')
11
+ .start()
12
+ .field('PROTOCOL').text().def('couchbase').end()
13
+ .field('HOST').text().required().end()
14
+ .field('PORT').integer().end()
15
+ .field('USER').text().required().end()
16
+ .field('PASS').text().required().end()
17
+ .field('BUCKET').text().required().end()
18
+ .field('SCOPE').text().required().end()
19
+ .field('CREATE_INDICES').boolean().def(false).end()
20
+ .finish();
@@ -1,31 +1,35 @@
1
1
  import type { EnvBase } from "@leyyo/env";
2
- export declare namespace NodeJS {
3
- interface ProcessEnv extends CouchbaseCommonConf {
4
- }
5
- }
6
2
  export interface CouchbaseCommonConf extends EnvBase {
7
3
  /**
8
- * DB Host
4
+ * Protocol
5
+ * */
6
+ readonly PROTOCOL: string;
7
+ /**
8
+ * Host name or ip address
9
+ * */
10
+ readonly HOST: string;
11
+ /**
12
+ * Port number
9
13
  * */
10
- readonly CB_HOST: string;
14
+ readonly PORT: number;
11
15
  /**
12
- * DB User
16
+ * Username
13
17
  * */
14
- readonly CB_USER: string;
18
+ readonly USER: string;
15
19
  /**
16
- * DB Password
20
+ * Password
17
21
  * */
18
- readonly CB_PASS: string;
22
+ readonly PASS: string;
19
23
  /**
20
24
  * Current bucket (database name)
21
25
  * */
22
- readonly CB_BUCKET: string;
26
+ readonly BUCKET: string;
23
27
  /**
24
28
  * Current scope (schema name)
25
29
  * */
26
- readonly CB_SCOPE: string;
30
+ readonly SCOPE: string;
27
31
  /**
28
32
  * Create indices when it's started
29
33
  * */
30
- readonly CB_CREATE_INDICES: boolean;
34
+ readonly CREATE_INDICES: boolean;
31
35
  }
@@ -0,0 +1,8 @@
1
+ import type { CbDirectConnectionLike, CbDirectConnOpt } from "./index.types";
2
+ /**
3
+ * Crete new couchbase connection
4
+ *
5
+ * @param {CbDirectConnOpt?} opt - option
6
+ * @return {CbDirectConnectionLike} - new connection instance
7
+ * */
8
+ export declare function cbConnection(opt?: CbDirectConnOpt): CbDirectConnectionLike;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cbConnection = cbConnection;
4
+ const cb_direct_connection_1 = require("./cb-direct.connection");
5
+ // noinspection JSUnusedGlobalSymbols
6
+ /**
7
+ * Crete new couchbase connection
8
+ *
9
+ * @param {CbDirectConnOpt?} opt - option
10
+ * @return {CbDirectConnectionLike} - new connection instance
11
+ * */
12
+ function cbConnection(opt) {
13
+ return new cb_direct_connection_1.CbDirectConnection(opt);
14
+ }
@@ -0,0 +1,13 @@
1
+ import { CbConnection } from "./cb.connection";
2
+ import type { DefDims, Entity, UrnDocLike, Pair, Portion, View } from "@samet-it/be-base-common";
3
+ import { type CbRepoDirectOpt, type CbRepoLike } from "../repo";
4
+ import type { CbDirectConnectionLike, CbDirectConnOpt } from "./index.types";
5
+ import type { KeyValue } from "@leyyo/common";
6
+ /**
7
+ * Couchbase connection direct class
8
+ * */
9
+ export declare class CbDirectConnection extends CbConnection implements CbDirectConnectionLike {
10
+ constructor(opt?: CbDirectConnOpt);
11
+ /** @inheritDoc */
12
+ 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>;
13
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CbDirectConnection = void 0;
4
+ const cb_connection_1 = require("./cb.connection");
5
+ const be_base_common_1 = require("@samet-it/be-base-common");
6
+ const repo_1 = require("../repo");
7
+ // noinspection JSUnusedGlobalSymbols
8
+ /**
9
+ * Couchbase connection direct class
10
+ * */
11
+ class CbDirectConnection extends cb_connection_1.CbConnection {
12
+ constructor(opt) {
13
+ super(opt);
14
+ this.logger = be_base_common_1.logger.of(`CbConnection${(opt === null || opt === void 0 ? void 0 : opt.name) ? '#' + (opt === null || opt === void 0 ? void 0 : opt.name) : ''}`);
15
+ }
16
+ /** @inheritDoc */
17
+ newRepo(opt) {
18
+ return new repo_1.CbDirectRepo(this, opt);
19
+ }
20
+ }
21
+ exports.CbDirectConnection = CbDirectConnection;
@@ -0,0 +1,38 @@
1
+ import type { QueryResult, QueryMetaData } from 'couchbase';
2
+ import { type IgnoreFieldsByType, type ReplaceType } from "@leyyo/common";
3
+ import { DbConnection, type DbLines } from "@samet-it/be-db-common";
4
+ import type { CbConnectionLike, CbConnOpt, CbExecOpt, CbConnProps } from "./index.types";
5
+ import type { CbRepoDef } from "../repo";
6
+ import type { DbQueryResultMore, DbQueryResultOne } from "@samet-it/be-db-common";
7
+ /**
8
+ * Couchbase connection abstract class
9
+ * */
10
+ export declare abstract class CbConnection extends DbConnection<CbRepoDef, QueryMetaData, CbExecOpt> implements CbConnectionLike {
11
+ /** {@inheritDoc} */
12
+ protected _props: CbConnProps;
13
+ protected constructor(opt?: CbConnOpt);
14
+ /**
15
+ * Read configuration by variant
16
+ * */
17
+ protected _readEnv(): void;
18
+ /** {@inheritDoc} */
19
+ get props(): Readonly<CbConnProps>;
20
+ /** {@inheritDoc} */
21
+ field(field: string): string;
22
+ /** {@inheritDoc} */
23
+ value(value: unknown): string;
24
+ /** {@inheritDoc} */
25
+ flatten<T>(result: QueryResult<T>): Array<T>;
26
+ /** {@inheritDoc} */
27
+ castDates<T>(given: ReplaceType<T, Date, string> | T, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
28
+ /** {@inheritDoc} */
29
+ castDatesForList<T>(given: Array<ReplaceType<T, Date, string> | T>, ...fields: Array<IgnoreFieldsByType<T, Date>>): void;
30
+ /** {@inheritDoc} */
31
+ more<T>(repo: CbRepoDef, lines: string | DbLines, p1?: string | CbExecOpt): Promise<DbQueryResultMore<T, QueryMetaData>>;
32
+ /** {@inheritDoc} */
33
+ one<T>(repo: CbRepoDef, lines: string | DbLines, p1?: string | CbExecOpt): Promise<DbQueryResultOne<T, QueryMetaData>>;
34
+ /** {@inheritDoc} */
35
+ connect(): Promise<boolean>;
36
+ /** {@inheritDoc} */
37
+ ping(next?: boolean): Promise<boolean>;
38
+ }
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- // ~~console.log(__filename);
3
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
3
  if (k2 === undefined) k2 = k;
5
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -16,12 +15,6 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
16
15
  }) : function(o, v) {
17
16
  o["default"] = v;
18
17
  });
19
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
20
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
21
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
22
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
23
- return c > 3 && r && Object.defineProperty(target, key, r), r;
24
- };
25
18
  var __importStar = (this && this.__importStar) || (function () {
26
19
  var ownKeys = function(o) {
27
20
  ownKeys = Object.getOwnPropertyNames || function (o) {
@@ -39,9 +32,6 @@ var __importStar = (this && this.__importStar) || (function () {
39
32
  return result;
40
33
  };
41
34
  })();
42
- var __metadata = (this && this.__metadata) || function (k, v) {
43
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
44
- };
45
35
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
46
36
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
47
37
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -52,63 +42,51 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
52
42
  });
53
43
  };
54
44
  Object.defineProperty(exports, "__esModule", { value: true });
55
- exports.CbAdapterService = void 0;
45
+ exports.CbConnection = void 0;
56
46
  const couchbase = __importStar(require("couchbase"));
57
- const common_1 = require("@nestjs/common");
58
47
  const config_1 = require("../config");
59
- const common_2 = require("@leyyo/common");
48
+ const common_1 = require("@leyyo/common");
60
49
  const filter_1 = require("../filter");
61
- // noinspection JSUnusedGlobalSymbols
62
- let CbAdapterService = class CbAdapterService {
63
- constructor() {
64
- this._CLEAR_ERROR_INTERVAL = 1000 * 60 * 60 * 24; // daily
65
- this._nativeErrors = new Map();
66
- this.logger = new common_1.Logger(this.constructor.name);
67
- this._clearErrors();
50
+ const be_db_common_1 = require("@samet-it/be-db-common");
51
+ const type_1 = require("@leyyo/type");
52
+ /**
53
+ * Couchbase connection abstract class
54
+ * */
55
+ class CbConnection extends be_db_common_1.DbConnection {
56
+ // endregion protected-property
57
+ constructor(opt) {
58
+ super(opt);
68
59
  filter_1.cbFilterUtil.$set(this);
60
+ this._readEnv();
69
61
  }
62
+ // region protected-method
70
63
  /**
71
- * Clear native errors
64
+ * Read configuration by variant
72
65
  * */
73
- _clearErrors() {
74
- this._nativeErrors.clear();
75
- setTimeout(() => this._clearErrors(), this._CLEAR_ERROR_INTERVAL);
76
- }
77
- /**
78
- * Ping to DB
79
- * */
80
- _ping() {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- try {
83
- yield this.exec(this._cluster.ping(), { name: 'ping' });
84
- }
85
- catch (err) {
86
- this.logger.warn(`Ping error => [${err.name}: ${err.message}]`);
87
- }
88
- setTimeout(() => this._ping(), 30000);
89
- });
90
- }
91
- _checkError(err, opt) {
92
- var _a, _b, _c;
93
- let size = 1;
94
- if (!this._nativeErrors.has(err)) {
95
- this._nativeErrors.set(err, size);
96
- }
97
- else {
98
- size = this._nativeErrors.get(err) + 1;
99
- this._nativeErrors.set(err, size);
66
+ _readEnv() {
67
+ var _a, _b, _c, _d, _e, _f, _g, _h;
68
+ const { _props: props } = this;
69
+ let env = config_1.couchbaseCommonConfig.valueShort;
70
+ if (props.envVariant) {
71
+ env = config_1.couchbaseCommonConfig.configure.getVariation(props.envVariant).valueShort;
100
72
  }
101
- if ((_a = opt === null || opt === void 0 ? void 0 : opt.ignoredErrors) === null || _a === void 0 ? void 0 : _a.includes(err)) {
102
- this.logger.warn(`[${size}] ${(_b = opt.name) !== null && _b !== void 0 ? _b : 'Ignored error'} => [${err.name}: ${err.message}]`);
103
- return;
104
- }
105
- this.logger.error(`[${size}] ${(_c = opt.name) !== null && _c !== void 0 ? _c : 'Database error'} => [${err.name}: ${err.message}]`, JSON.stringify(err.context));
106
- throw err;
73
+ props.protocol = (_a = props.protocol) !== null && _a !== void 0 ? _a : env.PROTOCOL;
74
+ props.host = (_b = props.host) !== null && _b !== void 0 ? _b : env.HOST;
75
+ props.port = (_c = props.port) !== null && _c !== void 0 ? _c : env.PORT;
76
+ props.username = (_d = props.username) !== null && _d !== void 0 ? _d : env.USER;
77
+ props.password = (_e = props.password) !== null && _e !== void 0 ? _e : env.PASS;
78
+ props.bucketName = (_f = props.bucketName) !== null && _f !== void 0 ? _f : env.BUCKET;
79
+ props.scopeName = (_g = props.scopeName) !== null && _g !== void 0 ? _g : env.SCOPE;
80
+ props.createIndices = (_h = props.createIndices) !== null && _h !== void 0 ? _h : env.CREATE_INDICES;
107
81
  }
82
+ // endregion protected-method
83
+ // region getter
108
84
  /** {@inheritDoc} */
109
- f(field) {
110
- return this.field(field);
85
+ get props() {
86
+ return this._props;
111
87
  }
88
+ // endregion getter
89
+ // region field-value
112
90
  /** {@inheritDoc} */
113
91
  field(field) {
114
92
  field = field.trim();
@@ -117,9 +95,7 @@ let CbAdapterService = class CbAdapterService {
117
95
  }
118
96
  return field.startsWith('`') ? field : `\`${field}\``;
119
97
  }
120
- v(value) {
121
- return this.value(value);
122
- }
98
+ /** {@inheritDoc} */
123
99
  value(value) {
124
100
  var _a;
125
101
  switch (typeof value) {
@@ -131,7 +107,7 @@ let CbAdapterService = class CbAdapterService {
131
107
  return value ? `true` : 'false';
132
108
  case "object":
133
109
  if (value) {
134
- return common_2.$dev.secureJson(value, true);
110
+ return (0, common_1.secureJson)(value);
135
111
  }
136
112
  else {
137
113
  return 'null';
@@ -152,27 +128,8 @@ let CbAdapterService = class CbAdapterService {
152
128
  }
153
129
  return [];
154
130
  }
155
- /** {@inheritDoc} */
156
- rows(rows) {
157
- if (rows && Array.isArray(rows) && (rows.length > 0)) {
158
- return rows.filter(row => row['_trashId'] === undefined);
159
- }
160
- return [];
161
- }
162
- /** {@inheritDoc} */
163
- row(row) {
164
- if (row && row['_trashId'] === undefined) {
165
- return row;
166
- }
167
- return undefined;
168
- }
169
- /** {@inheritDoc} */
170
- first(rows) {
171
- if (rows && Array.isArray(rows) && (rows.length > 0)) {
172
- return rows[0];
173
- }
174
- return undefined;
175
- }
131
+ // endregion field-value
132
+ // region transcoders
176
133
  /** {@inheritDoc} */
177
134
  castDates(given, ...fields) {
178
135
  if (fields.length < 1) {
@@ -200,35 +157,18 @@ let CbAdapterService = class CbAdapterService {
200
157
  const arr = given;
201
158
  arr.forEach(item => this.castDates(item, ...fields));
202
159
  }
203
- buildOpt(p1, name) {
204
- const opt = typeof p1 === 'string' ? { name: p1 } : Object.assign({}, p1);
205
- if (typeof opt.name === undefined && typeof name !== undefined) {
206
- opt.name = name;
207
- }
208
- opt.name = opt.name ? `[${opt.name}]` : '';
209
- return opt;
210
- }
160
+ // endregion transcoders
161
+ // region query
211
162
  /** {@inheritDoc} */
212
- exec(fn, p1) {
213
- return __awaiter(this, void 0, void 0, function* () {
214
- const opt = this.buildOpt(p1);
215
- try {
216
- return yield fn;
217
- }
218
- catch (err) {
219
- this._checkError(err, opt);
220
- return undefined;
221
- }
222
- });
223
- }
224
- more(repo, sql, p1) {
163
+ more(repo, lines, p1) {
225
164
  return __awaiter(this, void 0, void 0, function* () {
226
165
  const opt = this.buildOpt(p1);
166
+ const sql = (0, type_1.isObjectValid)(lines) ? lines.end() : lines;
227
167
  if (opt.printSql) {
228
- this.logger.log(`${repo.path} > SQL${opt.name}`, sql);
168
+ this.logger.log(`${repo.props.path} > SQL${opt.name}`, sql);
229
169
  }
230
170
  try {
231
- const result = yield repo.scope.query(sql, opt);
171
+ const result = yield repo.props.scope.query(sql, opt);
232
172
  return {
233
173
  success: true,
234
174
  rows: result.rows,
@@ -236,7 +176,7 @@ let CbAdapterService = class CbAdapterService {
236
176
  };
237
177
  }
238
178
  catch (err) {
239
- this._checkError(err, opt);
179
+ this.checkError(err, Object.assign(Object.assign({}, opt), { sql }));
240
180
  return {
241
181
  success: false,
242
182
  rows: [],
@@ -245,14 +185,16 @@ let CbAdapterService = class CbAdapterService {
245
185
  }
246
186
  });
247
187
  }
248
- one(repo, sql, p1) {
188
+ /** {@inheritDoc} */
189
+ one(repo, lines, p1) {
249
190
  return __awaiter(this, void 0, void 0, function* () {
250
191
  const opt = this.buildOpt(p1);
192
+ const sql = (0, type_1.isObjectValid)(lines) ? lines.end() : lines;
251
193
  if (opt.printSql) {
252
- this.logger.log(`${repo.path} > SQL${opt.name}`, sql);
194
+ this.logger.log(`${repo.props.path} > SQL${opt.name}`, sql);
253
195
  }
254
196
  try {
255
- const result = yield repo.scope.query(sql, opt);
197
+ const result = yield repo.props.scope.query(sql, opt);
256
198
  return {
257
199
  success: true,
258
200
  row: (Array.isArray(result.rows) && result.rows.length > 0) ? result.rows[0] : undefined,
@@ -260,7 +202,7 @@ let CbAdapterService = class CbAdapterService {
260
202
  };
261
203
  }
262
204
  catch (err) {
263
- this._checkError(err, opt);
205
+ this.checkError(err, Object.assign(Object.assign({}, opt), { sql }));
264
206
  return {
265
207
  success: false,
266
208
  row: undefined,
@@ -269,32 +211,62 @@ let CbAdapterService = class CbAdapterService {
269
211
  }
270
212
  });
271
213
  }
214
+ // endregion query
215
+ // region connect
272
216
  /** {@inheritDoc} */
273
- connectDb() {
217
+ connect() {
274
218
  return __awaiter(this, void 0, void 0, function* () {
275
- if (this._connected) {
276
- return this._cluster;
219
+ const { _props: props } = this;
220
+ if (props.isConnected) {
221
+ return true;
277
222
  }
278
- const opt = config_1.couchbaseCommonConfig.raw;
279
223
  try {
280
- this._cluster = yield couchbase.connect(`couchbase://${opt.CB_HOST}`, {
281
- username: opt.CB_USER,
282
- password: opt.CB_PASS,
224
+ props.cluster = yield couchbase.connect(`${props.protocol}://${props.host}`, {
225
+ username: props.username,
226
+ password: props.password,
283
227
  });
284
- this._connected = true;
228
+ props.isConnected = true;
285
229
  this.logger.log('Connected');
286
- setTimeout(() => this._ping(), 30000);
287
- return this._cluster;
230
+ setTimeout(() => this.ping(true).then(), 30000);
288
231
  }
289
232
  catch (e) {
290
- this.logger.error(`Connection error => [${e.name}: ${e.message}]`);
233
+ this.checkError(e, { name: 'connection' });
291
234
  throw e;
292
235
  }
236
+ if (props.isFirst === undefined) {
237
+ if (this._onFirstConnected.length > 0) {
238
+ yield Promise.all(this._onFirstConnected);
239
+ this._onFirstConnected = [];
240
+ }
241
+ props.isFirst = false;
242
+ }
243
+ if (this._onConnected.length > 0) {
244
+ this._onConnected.forEach(fn => {
245
+ fn().then().catch(e => {
246
+ this.logger.error(`OnConnected <${e.name}> ${e.message}`);
247
+ });
248
+ });
249
+ }
250
+ return true;
293
251
  });
294
252
  }
295
- };
296
- exports.CbAdapterService = CbAdapterService;
297
- exports.CbAdapterService = CbAdapterService = __decorate([
298
- (0, common_1.Injectable)(),
299
- __metadata("design:paramtypes", [])
300
- ], CbAdapterService);
253
+ /** {@inheritDoc} */
254
+ ping(next) {
255
+ return __awaiter(this, void 0, void 0, function* () {
256
+ const { _props: props } = this;
257
+ let result = false;
258
+ try {
259
+ yield this.exec(props.cluster.ping(), { name: 'ping' });
260
+ result = true;
261
+ }
262
+ catch (err) {
263
+ this.checkError(err, { name: 'ping', silent: true });
264
+ }
265
+ if (next) {
266
+ setTimeout(() => this.ping(true).then(), 30000);
267
+ }
268
+ return result;
269
+ });
270
+ }
271
+ }
272
+ exports.CbConnection = CbConnection;
@@ -0,0 +1,4 @@
1
+ export * from './index.types';
2
+ export * from './cb.connection';
3
+ export * from './cb-connection.fn';
4
+ export * from './cb-direct.connection';
@@ -15,5 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./index.types"), exports);
18
- __exportStar(require("./cb-adapter.service"), exports);
19
- __exportStar(require("./cb-adapter.module"), exports);
18
+ __exportStar(require("./cb.connection"), exports);
19
+ __exportStar(require("./cb-connection.fn"), exports);
20
+ __exportStar(require("./cb-direct.connection"), exports);