@samet-it/be-couchbase-common 1.0.7 → 1.0.9

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.
@@ -6,9 +6,7 @@ export declare class CbAdapterService implements CbAdapterServiceLike {
6
6
  private readonly _CLEAR_ERROR_INTERVAL;
7
7
  private logger;
8
8
  private _connected;
9
- private _undefined;
10
9
  private _cluster;
11
- private readonly _cbErrors;
12
10
  private readonly _nativeErrors;
13
11
  constructor();
14
12
  /**
@@ -46,8 +44,6 @@ export declare class CbAdapterService implements CbAdapterServiceLike {
46
44
  /** {@inheritDoc} */
47
45
  first<T>(rows: Array<T>): T | undefined;
48
46
  /** {@inheritDoc} */
49
- checkError(p1: string | Error, p2?: Error): void;
50
- /** {@inheritDoc} */
51
47
  createIndex<T = Record<string, unknown>>(coll: Collection, p1: (keyof T | string) | Array<keyof T | string>, name?: string): Promise<void>;
52
48
  /** {@inheritDoc} */
53
49
  createBaseIndices(coll: Collection, ...keys: Array<keyof CbEntity>): Promise<void>;
@@ -54,25 +54,16 @@ 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 common_1 = require("@nestjs/common");
58
57
  const couchbase_1 = require("couchbase");
58
+ const common_1 = require("@nestjs/common");
59
59
  const config_1 = require("../config");
60
60
  // noinspection JSUnusedGlobalSymbols
61
61
  let CbAdapterService = class CbAdapterService {
62
62
  constructor() {
63
63
  this._CLEAR_ERROR_INTERVAL = 1000 * 60 * 60 * 24; // daily
64
- this._undefined = 0;
65
- this._cbErrors = new Map();
66
64
  this._nativeErrors = new Map();
67
65
  this.logger = new common_1.Logger(this.constructor.name);
68
66
  this._clearErrors();
69
- this._cbErrors.set('AmbiguousTimeoutError', { count: 0, log: true, known: true });
70
- this._cbErrors.set('AuthenticationFailureError', { count: 0, log: true, known: true });
71
- this._cbErrors.set('ConnectionClosedError', { count: 0, log: true, known: true });
72
- this._cbErrors.set('InternalServerFailureError', { count: 0, log: true, known: true });
73
- this._cbErrors.set('ServiceNotAvailableError', { count: 0, log: true, known: true });
74
- this._cbErrors.set('TimeoutError', { count: 0, log: true, known: true });
75
- this._cbErrors.set('UnambiguousTimeoutError', { count: 0, log: true, known: true });
76
67
  }
77
68
  /**
78
69
  * Clear native errors
@@ -90,6 +81,7 @@ let CbAdapterService = class CbAdapterService {
90
81
  yield this.exec(this._cluster.ping(), { name: 'ping' });
91
82
  }
92
83
  catch (err) {
84
+ this.logger.warn(`Ping error => [${err.name}: ${err.message}]`);
93
85
  }
94
86
  setTimeout(() => this._ping(), 30000);
95
87
  });
@@ -107,7 +99,10 @@ let CbAdapterService = class CbAdapterService {
107
99
  if (e instanceof couchbase_1.IndexExistsError) {
108
100
  return undefined;
109
101
  }
110
- this.logger.error(`!index[${(_a = e.name) !== null && _a !== void 0 ? _a : ''}] #${scope.name}] => ${sql}`, e);
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
+ })}`);
111
106
  // throw e; // TODO
112
107
  }
113
108
  });
@@ -154,18 +149,20 @@ let CbAdapterService = class CbAdapterService {
154
149
  }
155
150
  }
156
151
  _checkError(err, opt) {
157
- var _a;
152
+ var _a, _b, _c;
153
+ let size = 1;
158
154
  if (!this._nativeErrors.has(err)) {
159
- this._nativeErrors.set(err, 1);
155
+ this._nativeErrors.set(err, size);
160
156
  }
161
157
  else {
162
- this._nativeErrors.set(err, this._nativeErrors.get(err) + 1);
158
+ size = this._nativeErrors.get(err) + 1;
159
+ this._nativeErrors.set(err, size);
163
160
  }
164
161
  if ((_a = opt === null || opt === void 0 ? void 0 : opt.ignoredErrors) === null || _a === void 0 ? void 0 : _a.includes(err)) {
165
- this.logger.warn(`${opt.name} => (${err.name})${err.message}`);
162
+ this.logger.warn(`[${size}] ${(_b = opt.name) !== null && _b !== void 0 ? _b : 'Ignored error'} => [${err.name}: ${err.message}]`);
166
163
  return;
167
164
  }
168
- this.logger.debug(`${opt.name} => (${err.name})${err.message}`, JSON.stringify(err.context));
165
+ this.logger.error(`[${size}] ${(_c = opt.name) !== null && _c !== void 0 ? _c : 'Ignored error'} => [${err.name}: ${err.message}]`, JSON.stringify(err.context));
169
166
  throw err;
170
167
  }
171
168
  /** {@inheritDoc} */
@@ -227,42 +224,6 @@ let CbAdapterService = class CbAdapterService {
227
224
  return undefined;
228
225
  }
229
226
  /** {@inheritDoc} */
230
- checkError(p1, p2) {
231
- var _a;
232
- let op;
233
- let err;
234
- if (typeof p1 === 'string') {
235
- p1 = p1.trim();
236
- op = p1 ? `(${p1})` : '';
237
- err = p2;
238
- }
239
- else {
240
- op = '';
241
- err = p1;
242
- }
243
- const name = (_a = err === null || err === void 0 ? void 0 : err.constructor) === null || _a === void 0 ? void 0 : _a.name;
244
- if (name) {
245
- if (!this._cbErrors.has(name)) {
246
- this.logger.warn(`!first[${name}] #${op} [1]`, err);
247
- this._cbErrors.set(name, { count: 1, log: true });
248
- }
249
- else {
250
- const obj = this._cbErrors.get(name);
251
- obj.count++;
252
- if (obj.known) {
253
- this.logger.debug(`!known[${name}] #${op} [${obj.count}]`, err);
254
- }
255
- else {
256
- this.logger.debug(`!unknown[${name}] #${op} [${obj.count}]`, err);
257
- }
258
- }
259
- }
260
- else {
261
- this._undefined++;
262
- this.logger.warn(`!undefined[] #${op} [${this._undefined}]`, err);
263
- }
264
- }
265
- /** {@inheritDoc} */
266
227
  createIndex(coll, p1, name) {
267
228
  return __awaiter(this, void 0, void 0, function* () {
268
229
  if (Array.isArray(p1)) {
@@ -410,7 +371,6 @@ let CbAdapterService = class CbAdapterService {
410
371
  /** {@inheritDoc} */
411
372
  connectDb() {
412
373
  return __awaiter(this, void 0, void 0, function* () {
413
- var _a;
414
374
  if (this._connected) {
415
375
  return this._cluster;
416
376
  }
@@ -426,7 +386,7 @@ let CbAdapterService = class CbAdapterService {
426
386
  return this._cluster;
427
387
  }
428
388
  catch (e) {
429
- this.logger.error(`!connection[${(_a = e.name) !== null && _a !== void 0 ? _a : ''}]`, e);
389
+ this.logger.warn(`Connection error => [${e.name}: ${e.message}]`);
430
390
  throw e;
431
391
  }
432
392
  });
@@ -43,14 +43,6 @@ export interface CbAdapterServiceLike {
43
43
  * Return first row of rows
44
44
  * */
45
45
  first<T>(rows: Array<T>): T | undefined;
46
- /**
47
- * Check error
48
- * */
49
- checkError(err: Error): void;
50
- /**
51
- * Check error with operation
52
- * */
53
- checkError(op: string, err: Error): void;
54
46
  /**
55
47
  * Create index for multiple fields
56
48
  * */
@@ -1,11 +1,14 @@
1
1
  import { CbAdapterService, type CbEntity, type CbExecOpt } from "../adapter";
2
2
  import { Bucket, Cluster, Collection, Scope } from "couchbase";
3
3
  import type { CbRepoDef, CbRepoLike } from "./index.types";
4
+ import { Logger } from "@nestjs/common";
4
5
  export declare abstract class CbRepo<E extends CbEntity, ID> implements CbRepoLike<E, ID> {
5
6
  protected colName: string;
6
7
  protected cb: CbAdapterService;
7
8
  private _scopeName?;
8
9
  private _bucketName?;
10
+ private _tryCount;
11
+ protected logger: Logger;
9
12
  /** @inheritDoc */
10
13
  readonly fullPath: string;
11
14
  /** @inheritDoc */
@@ -15,6 +15,7 @@ const common_1 = require("@leyyo/common");
15
15
  const config_1 = require("../config");
16
16
  const line_1 = require("../line");
17
17
  const node_crypto_1 = require("node:crypto");
18
+ const common_2 = require("@nestjs/common");
18
19
  let F_ID;
19
20
  let F_URN;
20
21
  let F_TRASH_ID;
@@ -25,6 +26,8 @@ class CbRepo {
25
26
  this.cb = cb;
26
27
  this._scopeName = _scopeName;
27
28
  this._bucketName = _bucketName;
29
+ this._tryCount = 0;
30
+ this.logger = new common_2.Logger(this.constructor.name);
28
31
  if (!F_ID) {
29
32
  F_ID = this.cb.f('id');
30
33
  F_URN = this.cb.f('_urn');
@@ -103,14 +106,25 @@ class CbRepo {
103
106
  return __awaiter(this, void 0, void 0, function* () {
104
107
  var _a, _b;
105
108
  const mutable = this;
106
- mutable.cluster = yield this.cb.connectDb();
107
- mutable.bucket = this.cluster.bucket((_a = this._bucketName) !== null && _a !== void 0 ? _a : config_1.couchbaseCommonConfig.raw.CB_BUCKET);
108
- mutable.scope = this.bucket.scope((_b = this._scopeName) !== null && _b !== void 0 ? _b : config_1.couchbaseCommonConfig.raw.CB_SCOPE);
109
- mutable.collection = this.scope.collection(this.colName);
110
- mutable.path = `${this.bucket.name}.${this.scope.name}.${this.collection.name}`;
111
- mutable.fullPath = this.cb.f(`${this.bucket.name}.${this.scope.name}.${this.collection.name}`);
112
- if (config_1.couchbaseCommonConfig.raw.CB_CREATE_INDICES) {
113
- yield this._createIndices();
109
+ try {
110
+ mutable.cluster = yield this.cb.connectDb();
111
+ mutable.bucket = this.cluster.bucket((_a = this._bucketName) !== null && _a !== void 0 ? _a : config_1.couchbaseCommonConfig.raw.CB_BUCKET);
112
+ mutable.scope = this.bucket.scope((_b = this._scopeName) !== null && _b !== void 0 ? _b : config_1.couchbaseCommonConfig.raw.CB_SCOPE);
113
+ mutable.collection = this.scope.collection(this.colName);
114
+ mutable.path = `${this.bucket.name}.${this.scope.name}.${this.collection.name}`;
115
+ mutable.fullPath = this.cb.f(`${this.bucket.name}.${this.scope.name}.${this.collection.name}`);
116
+ if (config_1.couchbaseCommonConfig.raw.CB_CREATE_INDICES) {
117
+ yield this._createIndices();
118
+ }
119
+ }
120
+ catch (e) {
121
+ if (this._tryCount > 100) {
122
+ throw e;
123
+ }
124
+ this._tryCount++;
125
+ setTimeout(() => this.onModuleInit().then().catch(e2 => {
126
+ throw e2;
127
+ }), 1000);
114
128
  }
115
129
  });
116
130
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@samet-it/be-couchbase-common",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "Couchbase common component",
5
5
  "keywords": [
6
6
  "Couchbase"