@budibase/backend-core 2.14.6 → 2.14.7

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/dist/index.js CHANGED
@@ -2926,12 +2926,6 @@ var init_instrumentation = __esm({
2926
2926
  return this.db.exists();
2927
2927
  });
2928
2928
  }
2929
- checkSetup() {
2930
- return import_dd_trace.default.trace("db.checkSetup", (span) => {
2931
- span?.addTags({ db_name: this.name });
2932
- return this.db.checkSetup();
2933
- });
2934
- }
2935
2929
  get(id) {
2936
2930
  return import_dd_trace.default.trace("db.get", (span) => {
2937
2931
  span?.addTags({ db_name: this.name, doc_id: id });
@@ -3040,7 +3034,7 @@ function DatabaseWithConnection(dbName, connection, opts) {
3040
3034
  const db = new DatabaseImpl(dbName, opts, connection);
3041
3035
  return new DDInstrumentedDatabase(db);
3042
3036
  }
3043
- var import_nano, DatabaseImpl;
3037
+ var import_nano, DATABASE_NOT_FOUND, DatabaseImpl;
3044
3038
  var init_DatabaseImpl = __esm({
3045
3039
  "src/db/couch/DatabaseImpl.ts"() {
3046
3040
  "use strict";
@@ -3051,6 +3045,7 @@ var init_DatabaseImpl = __esm({
3051
3045
  init_pouchDB();
3052
3046
  init_newid();
3053
3047
  init_instrumentation();
3048
+ DATABASE_NOT_FOUND = "Database does not exist.";
3054
3049
  DatabaseImpl = class _DatabaseImpl {
3055
3050
  constructor(dbName, opts, connection) {
3056
3051
  this.couchInfo = getCouchInfo();
@@ -3079,7 +3074,10 @@ var init_DatabaseImpl = __esm({
3079
3074
  nano() {
3080
3075
  return this.instanceNano || _DatabaseImpl.nano;
3081
3076
  }
3082
- async checkSetup() {
3077
+ getDb() {
3078
+ return this.nano().db.use(this.name);
3079
+ }
3080
+ async checkAndCreateDb() {
3083
3081
  let shouldCreate = !this.pouchOpts?.skip_setup;
3084
3082
  let exists2 = await this.exists();
3085
3083
  if (!shouldCreate && !exists2) {
@@ -3094,24 +3092,31 @@ var init_DatabaseImpl = __esm({
3094
3092
  }
3095
3093
  }
3096
3094
  }
3097
- return this.nano().db.use(this.name);
3095
+ return this.getDb();
3098
3096
  }
3099
- async updateOutput(fnc) {
3097
+ // this function fetches the DB and handles if DB creation is needed
3098
+ async performCall(call) {
3099
+ const db = this.getDb();
3100
+ const fnc = await call(db);
3100
3101
  try {
3101
3102
  return await fnc();
3102
3103
  } catch (err) {
3103
- if (err.statusCode) {
3104
+ if (err.statusCode === 404 && err.reason === DATABASE_NOT_FOUND) {
3105
+ await this.checkAndCreateDb();
3106
+ return await this.performCall(call);
3107
+ } else if (err.statusCode) {
3104
3108
  err.status = err.statusCode;
3105
3109
  }
3106
3110
  throw err;
3107
3111
  }
3108
3112
  }
3109
3113
  async get(id) {
3110
- const db = await this.checkSetup();
3111
- if (!id) {
3112
- throw new Error("Unable to get doc without a valid _id.");
3113
- }
3114
- return this.updateOutput(() => db.get(id));
3114
+ return this.performCall((db) => {
3115
+ if (!id) {
3116
+ throw new Error("Unable to get doc without a valid _id.");
3117
+ }
3118
+ return () => db.get(id);
3119
+ });
3115
3120
  }
3116
3121
  async getMultiple(ids, opts) {
3117
3122
  ids = [...new Set(ids)];
@@ -3135,20 +3140,21 @@ var init_DatabaseImpl = __esm({
3135
3140
  return rows.map((row) => row.doc);
3136
3141
  }
3137
3142
  async remove(idOrDoc, rev) {
3138
- const db = await this.checkSetup();
3139
- let _id;
3140
- let _rev;
3141
- if (isDocument(idOrDoc)) {
3142
- _id = idOrDoc._id;
3143
- _rev = idOrDoc._rev;
3144
- } else {
3145
- _id = idOrDoc;
3146
- _rev = rev;
3147
- }
3148
- if (!_id || !_rev) {
3149
- throw new Error("Unable to remove doc without a valid _id and _rev.");
3150
- }
3151
- return this.updateOutput(() => db.destroy(_id, _rev));
3143
+ return this.performCall((db) => {
3144
+ let _id;
3145
+ let _rev;
3146
+ if (isDocument(idOrDoc)) {
3147
+ _id = idOrDoc._id;
3148
+ _rev = idOrDoc._rev;
3149
+ } else {
3150
+ _id = idOrDoc;
3151
+ _rev = rev;
3152
+ }
3153
+ if (!_id || !_rev) {
3154
+ throw new Error("Unable to remove doc without a valid _id and _rev.");
3155
+ }
3156
+ return () => db.destroy(_id, _rev);
3157
+ });
3152
3158
  }
3153
3159
  async post(document, opts) {
3154
3160
  if (!document._id) {
@@ -3160,37 +3166,41 @@ var init_DatabaseImpl = __esm({
3160
3166
  if (!document._id) {
3161
3167
  throw new Error("Cannot store document without _id field.");
3162
3168
  }
3163
- const db = await this.checkSetup();
3164
- if (!document.createdAt) {
3165
- document.createdAt = (/* @__PURE__ */ new Date()).toISOString();
3166
- }
3167
- document.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
3168
- if (opts?.force && document._id) {
3169
- try {
3170
- const existing = await this.get(document._id);
3171
- if (existing) {
3172
- document._rev = existing._rev;
3173
- }
3174
- } catch (err) {
3175
- if (err.status !== 404) {
3176
- throw err;
3169
+ return this.performCall(async (db) => {
3170
+ if (!document.createdAt) {
3171
+ document.createdAt = (/* @__PURE__ */ new Date()).toISOString();
3172
+ }
3173
+ document.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
3174
+ if (opts?.force && document._id) {
3175
+ try {
3176
+ const existing = await this.get(document._id);
3177
+ if (existing) {
3178
+ document._rev = existing._rev;
3179
+ }
3180
+ } catch (err) {
3181
+ if (err.status !== 404) {
3182
+ throw err;
3183
+ }
3177
3184
  }
3178
3185
  }
3179
- }
3180
- return this.updateOutput(() => db.insert(document));
3186
+ return () => db.insert(document);
3187
+ });
3181
3188
  }
3182
3189
  async bulkDocs(documents) {
3183
- const db = await this.checkSetup();
3184
- return this.updateOutput(() => db.bulk({ docs: documents }));
3190
+ return this.performCall((db) => {
3191
+ return () => db.bulk({ docs: documents });
3192
+ });
3185
3193
  }
3186
3194
  async allDocs(params2) {
3187
- const db = await this.checkSetup();
3188
- return this.updateOutput(() => db.list(params2));
3195
+ return this.performCall((db) => {
3196
+ return () => db.list(params2);
3197
+ });
3189
3198
  }
3190
3199
  async query(viewName, params2) {
3191
- const db = await this.checkSetup();
3192
- const [database, view] = viewName.split("/");
3193
- return this.updateOutput(() => db.view(database, view, params2));
3200
+ return this.performCall((db) => {
3201
+ const [database, view] = viewName.split("/");
3202
+ return () => db.view(database, view, params2);
3203
+ });
3194
3204
  }
3195
3205
  async destroy() {
3196
3206
  try {
@@ -3204,8 +3214,9 @@ var init_DatabaseImpl = __esm({
3204
3214
  }
3205
3215
  }
3206
3216
  async compact() {
3207
- const db = await this.checkSetup();
3208
- return this.updateOutput(() => db.compact());
3217
+ return this.performCall((db) => {
3218
+ return () => db.compact();
3219
+ });
3209
3220
  }
3210
3221
  // All below functions are in-frequently called, just utilise PouchDB
3211
3222
  // for them as it implements them better than we can