spice-couchbase 2.0.51 → 2.0.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.js CHANGED
@@ -14,12 +14,6 @@ var _ = require("lodash");
14
14
 
15
15
  var couchbase = require("couchbase");
16
16
 
17
- var {
18
- default: axios
19
- } = require("axios");
20
-
21
- var qs = require("qs");
22
-
23
17
  var {
24
18
  ConflictResolutionType
25
19
  } = require("couchbase");
@@ -166,11 +160,6 @@ module.exports = class Database {
166
160
  console.log(e);
167
161
  }
168
162
  })();
169
- }
170
-
171
- getCouchbaseHost() {
172
- var default_port = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 8091;
173
- return (this.db_config.api_base || "http://".concat(_.last(_.first(this.db_config.nodes.split(",")).split("://")))) + ":" + default_port;
174
163
  } //create wait function
175
164
 
176
165
 
@@ -180,6 +169,21 @@ module.exports = class Database {
180
169
  })();
181
170
  }
182
171
 
172
+ quoteIdentifier(identifier) {
173
+ return "`".concat(String(identifier).replace(/`/g, "``"), "`");
174
+ }
175
+
176
+ collectionPath(scope_name, collection_name) {
177
+ return "default:".concat(this.quoteIdentifier(this.db_config.bucket), ".").concat(this.quoteIdentifier(scope_name), ".").concat(this.quoteIdentifier(collection_name));
178
+ }
179
+
180
+ isCollectionAlreadyExistsError(error) {
181
+ var _error$cause, _error$cause2, _error$context, _error$context2;
182
+
183
+ var message = (error === null || error === void 0 ? void 0 : error.message) || (error === null || error === void 0 ? void 0 : (_error$cause = error.cause) === null || _error$cause === void 0 ? void 0 : _error$cause.first_error_message) || (error === null || error === void 0 ? void 0 : (_error$context = error.context) === null || _error$context === void 0 ? void 0 : _error$context.first_error_message) || (error === null || error === void 0 ? void 0 : (_error$context2 = error.context) === null || _error$context2 === void 0 ? void 0 : _error$context2.http_response_body) || "";
184
+ return error instanceof couchbase.CollectionExistsError || (error === null || error === void 0 ? void 0 : (_error$cause2 = error.cause) === null || _error$cause2 === void 0 ? void 0 : _error$cause2.first_error_code) === 12003 || (error === null || error === void 0 ? void 0 : (_error$cause2 = error.cause) === null || _error$cause2 === void 0 ? void 0 : _error$cause2.first_error_code) === 12027 || /collection .*already exists|already exists/i.test(message);
185
+ }
186
+
183
187
  upsertCollection() {
184
188
  var _this3 = this;
185
189
 
@@ -207,46 +211,24 @@ module.exports = class Database {
207
211
  try {
208
212
  var _spice2, _spice2$db, _spice2$db$collection;
209
213
 
210
- var that = _this4;
211
-
212
- function loadAllCollections() {
213
- return _loadAllCollections.apply(this, arguments);
214
- }
215
-
216
- function _loadAllCollections() {
217
- _loadAllCollections = _asyncToGenerator(function* () {
218
- var all_scopes = yield axios.get("".concat(that.getCouchbaseHost(), "/pools/default/buckets/").concat(that.db_config.bucket, "/scopes"), {
219
- auth: {
220
- username: that.db_config.username,
221
- password: that.db_config.password
222
- }
223
- });
224
-
225
- var scope = _.find(all_scopes.data.scopes, {
226
- name: scope_name
227
- });
228
-
229
- var collectionSet = new Set();
230
-
231
- _.each(scope.collections, collection => {
232
- collectionSet.add(collection.name);
233
- });
234
-
235
- return collectionSet;
236
- });
237
- return _loadAllCollections.apply(this, arguments);
238
- }
239
-
240
214
  if ((_spice2 = spice) === null || _spice2 === void 0 ? void 0 : (_spice2$db = _spice2.db) === null || _spice2$db === void 0 ? void 0 : (_spice2$db$collection = _spice2$db.collections) === null || _spice2$db$collection === void 0 ? void 0 : _spice2$db$collection.has(collection_name)) {
241
215
  return true;
242
216
  } else {
243
- var _spice3, _spice3$db, _spice3$db$collection;
217
+ var response = yield _this4.cluster.query("SELECT RAW keyspace_id\n FROM system:keyspaces\n WHERE bucket_id = ?\n AND scope_id = ?\n AND keyspace_id = ?\n LIMIT 1", {
218
+ parameters: [_this4.db_config.bucket, scope_name, collection_name],
219
+ scanConsistency: QueryScanConsistency.RequestPlus
220
+ });
221
+
222
+ if (Array.isArray(response === null || response === void 0 ? void 0 : response.rows) && response.rows.length > 0) {
223
+ spice.db.collections.add(collection_name);
224
+ return true;
225
+ }
244
226
 
245
- spice.db.collections = yield loadAllCollections();
246
- return (_spice3 = spice) === null || _spice3 === void 0 ? void 0 : (_spice3$db = _spice3.db) === null || _spice3$db === void 0 ? void 0 : (_spice3$db$collection = _spice3$db.collections) === null || _spice3$db$collection === void 0 ? void 0 : _spice3$db$collection.has(collection_name);
227
+ return false;
247
228
  }
248
229
  } catch (e) {
249
230
  console.log("Exists error happened", e);
231
+ return false;
250
232
  }
251
233
  })();
252
234
  }
@@ -256,22 +238,19 @@ module.exports = class Database {
256
238
 
257
239
  return _asyncToGenerator(function* () {
258
240
  try {
259
- var token = "".concat(_this5.db_config.username, ":").concat(_this5.db_config.password);
260
- var encodedToken = Buffer.from(token).toString("base64");
261
- var headers = {
262
- Authorization: "Basic " + encodedToken,
263
- "Content-Type": "application/x-www-form-urlencoded"
264
- };
265
- var response = yield axios({
266
- method: "post",
267
- url: "".concat(_this5.getCouchbaseHost(), "/pools/default/buckets/").concat(_this5.db_config.bucket, "/scopes/").concat(scope_name, "/collections"),
268
- headers,
269
- data: qs.stringify({
270
- name: connection_name
271
- })
272
- }).data;
241
+ var response = yield _this5.cluster.query("CREATE COLLECTION ".concat(_this5.collectionPath(scope_name, connection_name)), {
242
+ scanConsistency: QueryScanConsistency.RequestPlus
243
+ });
244
+ spice.db.collections.add(connection_name);
273
245
  return response;
274
- } catch (e) {}
246
+ } catch (e) {
247
+ if (_this5.isCollectionAlreadyExistsError(e)) {
248
+ spice.db.collections.add(connection_name);
249
+ return;
250
+ }
251
+
252
+ console.log("CREATE Collection :::", e);
253
+ }
275
254
  })();
276
255
  }
277
256
 
@@ -301,12 +280,12 @@ module.exports = class Database {
301
280
  return;
302
281
  }
303
282
 
304
- var response = yield _this7.query("CREATE PRIMARY INDEX ON default:`".concat(_this7.db_config.bucket, "`.`").concat(scope_name, "`.`").concat(col_name, "`"));
283
+ var response = yield _this7.query("CREATE PRIMARY INDEX ON ".concat(_this7.collectionPath(scope_name, col_name)));
305
284
  return response;
306
285
  } catch (e) {
307
- var _e$cause, _e$cause2;
286
+ var _e$cause3, _e$cause4;
308
287
 
309
- if (e instanceof couchbase.IndexExistsError || (e === null || e === void 0 ? void 0 : (_e$cause = e.cause) === null || _e$cause === void 0 ? void 0 : _e$cause.first_error_code) === 4300 || /index .*already exists|index_exists/i.test((e === null || e === void 0 ? void 0 : e.message) || (e === null || e === void 0 ? void 0 : (_e$cause2 = e.cause) === null || _e$cause2 === void 0 ? void 0 : _e$cause2.first_error_message) || "")) {
288
+ if (e instanceof couchbase.IndexExistsError || (e === null || e === void 0 ? void 0 : (_e$cause3 = e.cause) === null || _e$cause3 === void 0 ? void 0 : _e$cause3.first_error_code) === 4300 || /index .*already exists|index_exists/i.test((e === null || e === void 0 ? void 0 : e.message) || (e === null || e === void 0 ? void 0 : (_e$cause4 = e.cause) === null || _e$cause4 === void 0 ? void 0 : _e$cause4.first_error_message) || "")) {
310
289
  return;
311
290
  }
312
291
 
@@ -899,4 +878,4 @@ module.exports = class Database {
899
878
  })();
900
879
  }
901
880
 
902
- };
881
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.51",
3
+ "version": "2.0.53",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -9,10 +9,8 @@
9
9
  "author": "Chad Fraser <chad@sonover.com> (http://sonover.com)",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "axios": "^0.26.1",
13
12
  "couchbase": "^4.6.0",
14
- "lodash": "^4.17.15",
15
- "qs": "^6.10.3"
13
+ "lodash": "^4.17.15"
16
14
  },
17
15
  "devDependencies": {
18
16
  "@babel/cli": "^7.7.7",
package/src/index.js CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  let _ = require("lodash");
4
4
  let couchbase = require("couchbase");
5
- const { default: axios } = require("axios");
6
- var qs = require("qs");
7
5
  const { ConflictResolutionType } = require("couchbase");
8
6
  const { QueryScanConsistency } = require("couchbase");
9
7
 
@@ -163,22 +161,36 @@ module.exports = class Database {
163
161
  }
164
162
  }
165
163
 
166
- getCouchbaseHost(default_port = 8091) {
167
- return (
168
- (this.db_config.api_base ||
169
- `http://${_.last(
170
- _.first(this.db_config.nodes.split(",")).split("://"),
171
- )}`) +
172
- ":" +
173
- default_port
174
- );
175
- }
176
-
177
164
  //create wait function
178
165
  async wait(ms) {
179
166
  return new Promise((resolve) => setTimeout(resolve, ms));
180
167
  }
181
168
 
169
+ quoteIdentifier(identifier) {
170
+ return `\`${String(identifier).replace(/`/g, "``")}\``;
171
+ }
172
+
173
+ collectionPath(scope_name, collection_name) {
174
+ return `default:${this.quoteIdentifier(this.db_config.bucket)}.${this.quoteIdentifier(
175
+ scope_name,
176
+ )}.${this.quoteIdentifier(collection_name)}`;
177
+ }
178
+
179
+ isCollectionAlreadyExistsError(error) {
180
+ const message =
181
+ error?.message ||
182
+ error?.cause?.first_error_message ||
183
+ error?.context?.first_error_message ||
184
+ error?.context?.http_response_body ||
185
+ "";
186
+ return (
187
+ error instanceof couchbase.CollectionExistsError ||
188
+ error?.cause?.first_error_code === 12003 ||
189
+ error?.cause?.first_error_code === 12027 ||
190
+ /collection .*already exists|already exists/i.test(message)
191
+ );
192
+ }
193
+
182
194
  async upsertCollection() {
183
195
  try {
184
196
  if (!this.checkGlobelConnectionObject(this.collection_name)) {
@@ -199,55 +211,52 @@ module.exports = class Database {
199
211
 
200
212
  async collectionExists(collection_name, scope_name) {
201
213
  try {
202
- let that = this;
203
- async function loadAllCollections() {
204
- let all_scopes = await axios.get(
205
- `${that.getCouchbaseHost()}/pools/default/buckets/${
206
- that.db_config.bucket
207
- }/scopes`,
208
- {
209
- auth: {
210
- username: that.db_config.username,
211
- password: that.db_config.password,
212
- },
213
- },
214
- );
215
- let scope = _.find(all_scopes.data.scopes, { name: scope_name });
216
- let collectionSet = new Set();
217
- _.each(scope.collections, (collection) => {
218
- collectionSet.add(collection.name);
219
- });
220
- return collectionSet;
221
- }
222
214
  if (spice?.db?.collections?.has(collection_name)) {
223
215
  return true;
224
216
  } else {
225
- spice.db.collections = await loadAllCollections();
226
- return spice?.db?.collections?.has(collection_name);
217
+ let response = await this.cluster.query(
218
+ `SELECT RAW keyspace_id
219
+ FROM system:keyspaces
220
+ WHERE bucket_id = ?
221
+ AND scope_id = ?
222
+ AND keyspace_id = ?
223
+ LIMIT 1`,
224
+ {
225
+ parameters: [this.db_config.bucket, scope_name, collection_name],
226
+ scanConsistency: QueryScanConsistency.RequestPlus,
227
+ },
228
+ );
229
+ if (Array.isArray(response?.rows) && response.rows.length > 0) {
230
+ spice.db.collections.add(collection_name);
231
+ return true;
232
+ }
233
+ return false;
227
234
  }
228
235
  } catch (e) {
229
236
  console.log("Exists error happened", e);
237
+ return false;
230
238
  }
231
239
  }
232
240
 
233
241
  async createCollection(connection_name, scope_name) {
234
242
  try {
235
- const token = `${this.db_config.username}:${this.db_config.password}`;
236
- const encodedToken = Buffer.from(token).toString("base64");
237
- const headers = {
238
- Authorization: "Basic " + encodedToken,
239
- "Content-Type": "application/x-www-form-urlencoded",
240
- };
241
- let response = await axios({
242
- method: "post",
243
- url: `${this.getCouchbaseHost()}/pools/default/buckets/${
244
- this.db_config.bucket
245
- }/scopes/${scope_name}/collections`,
246
- headers,
247
- data: qs.stringify({ name: connection_name }),
248
- }).data;
243
+ let response = await this.cluster.query(
244
+ `CREATE COLLECTION ${this.collectionPath(scope_name, connection_name)}`,
245
+ {
246
+ scanConsistency: QueryScanConsistency.RequestPlus,
247
+ },
248
+ );
249
+ spice.db.collections.add(connection_name);
249
250
  return response;
250
- } catch (e) {}
251
+ } catch (e) {
252
+ if (
253
+ this.isCollectionAlreadyExistsError(e)
254
+ ) {
255
+ spice.db.collections.add(connection_name);
256
+ return;
257
+ }
258
+ console.log("CREATE Collection :::", e);
259
+ }
251
260
  }
252
261
 
253
262
  async primaryIndexExists(scope_name, col_name) {
@@ -278,7 +287,7 @@ module.exports = class Database {
278
287
  return;
279
288
  }
280
289
  let response = await this.query(
281
- `CREATE PRIMARY INDEX ON default:\`${this.db_config.bucket}\`.\`${scope_name}\`.\`${col_name}\``,
290
+ `CREATE PRIMARY INDEX ON ${this.collectionPath(scope_name, col_name)}`,
282
291
  );
283
292
  return response;
284
293
  } catch (e) {