spice-couchbase 2.0.51 → 2.0.52

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,14 @@ 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
+
183
180
  upsertCollection() {
184
181
  var _this3 = this;
185
182
 
@@ -207,46 +204,24 @@ module.exports = class Database {
207
204
  try {
208
205
  var _spice2, _spice2$db, _spice2$db$collection;
209
206
 
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
207
  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
208
  return true;
242
209
  } else {
243
- var _spice3, _spice3$db, _spice3$db$collection;
210
+ var response = yield _this4.cluster.query("SELECT RAW name\n FROM system:keyspaces\n WHERE bucket_id = ?\n AND scope_id = ?\n AND name = ?\n LIMIT 1", {
211
+ parameters: [_this4.db_config.bucket, scope_name, collection_name],
212
+ scanConsistency: QueryScanConsistency.RequestPlus
213
+ });
214
+
215
+ if (Array.isArray(response === null || response === void 0 ? void 0 : response.rows) && response.rows.length > 0) {
216
+ spice.db.collections.add(collection_name);
217
+ return true;
218
+ }
244
219
 
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);
220
+ return false;
247
221
  }
248
222
  } catch (e) {
249
223
  console.log("Exists error happened", e);
224
+ return false;
250
225
  }
251
226
  })();
252
227
  }
@@ -256,22 +231,21 @@ module.exports = class Database {
256
231
 
257
232
  return _asyncToGenerator(function* () {
258
233
  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;
234
+ var response = yield _this5.cluster.query("CREATE COLLECTION ".concat(_this5.collectionPath(scope_name, connection_name)), {
235
+ scanConsistency: QueryScanConsistency.RequestPlus
236
+ });
237
+ spice.db.collections.add(connection_name);
273
238
  return response;
274
- } catch (e) {}
239
+ } catch (e) {
240
+ var _e$cause, _e$cause2;
241
+
242
+ if (e instanceof couchbase.CollectionExistsError || (e === null || e === void 0 ? void 0 : (_e$cause = e.cause) === null || _e$cause === void 0 ? void 0 : _e$cause.first_error_code) === 12003 || /collection .*already exists|already 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) || "")) {
243
+ spice.db.collections.add(connection_name);
244
+ return;
245
+ }
246
+
247
+ console.log("CREATE Collection :::", e);
248
+ }
275
249
  })();
276
250
  }
277
251
 
@@ -301,12 +275,12 @@ module.exports = class Database {
301
275
  return;
302
276
  }
303
277
 
304
- var response = yield _this7.query("CREATE PRIMARY INDEX ON default:`".concat(_this7.db_config.bucket, "`.`").concat(scope_name, "`.`").concat(col_name, "`"));
278
+ var response = yield _this7.query("CREATE PRIMARY INDEX ON ".concat(_this7.collectionPath(scope_name, col_name)));
305
279
  return response;
306
280
  } catch (e) {
307
- var _e$cause, _e$cause2;
281
+ var _e$cause3, _e$cause4;
308
282
 
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) || "")) {
283
+ 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
284
  return;
311
285
  }
312
286
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spice-couchbase",
3
- "version": "2.0.51",
3
+ "version": "2.0.52",
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,21 @@ 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
+
182
179
  async upsertCollection() {
183
180
  try {
184
181
  if (!this.checkGlobelConnectionObject(this.collection_name)) {
@@ -199,55 +196,56 @@ module.exports = class Database {
199
196
 
200
197
  async collectionExists(collection_name, scope_name) {
201
198
  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
199
  if (spice?.db?.collections?.has(collection_name)) {
223
200
  return true;
224
201
  } else {
225
- spice.db.collections = await loadAllCollections();
226
- return spice?.db?.collections?.has(collection_name);
202
+ let response = await this.cluster.query(
203
+ `SELECT RAW name
204
+ FROM system:keyspaces
205
+ WHERE bucket_id = ?
206
+ AND scope_id = ?
207
+ AND name = ?
208
+ LIMIT 1`,
209
+ {
210
+ parameters: [this.db_config.bucket, scope_name, collection_name],
211
+ scanConsistency: QueryScanConsistency.RequestPlus,
212
+ },
213
+ );
214
+ if (Array.isArray(response?.rows) && response.rows.length > 0) {
215
+ spice.db.collections.add(collection_name);
216
+ return true;
217
+ }
218
+ return false;
227
219
  }
228
220
  } catch (e) {
229
221
  console.log("Exists error happened", e);
222
+ return false;
230
223
  }
231
224
  }
232
225
 
233
226
  async createCollection(connection_name, scope_name) {
234
227
  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;
228
+ let response = await this.cluster.query(
229
+ `CREATE COLLECTION ${this.collectionPath(scope_name, connection_name)}`,
230
+ {
231
+ scanConsistency: QueryScanConsistency.RequestPlus,
232
+ },
233
+ );
234
+ spice.db.collections.add(connection_name);
249
235
  return response;
250
- } catch (e) {}
236
+ } catch (e) {
237
+ if (
238
+ e instanceof couchbase.CollectionExistsError ||
239
+ e?.cause?.first_error_code === 12003 ||
240
+ /collection .*already exists|already exists/i.test(
241
+ e?.message || e?.cause?.first_error_message || "",
242
+ )
243
+ ) {
244
+ spice.db.collections.add(connection_name);
245
+ return;
246
+ }
247
+ console.log("CREATE Collection :::", e);
248
+ }
251
249
  }
252
250
 
253
251
  async primaryIndexExists(scope_name, col_name) {
@@ -278,7 +276,7 @@ module.exports = class Database {
278
276
  return;
279
277
  }
280
278
  let response = await this.query(
281
- `CREATE PRIMARY INDEX ON default:\`${this.db_config.bucket}\`.\`${scope_name}\`.\`${col_name}\``,
279
+ `CREATE PRIMARY INDEX ON ${this.collectionPath(scope_name, col_name)}`,
282
280
  );
283
281
  return response;
284
282
  } catch (e) {