@budibase/backend-core 2.29.23 → 2.29.25

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.
@@ -56,24 +56,24 @@ class CouchDBError extends Error implements DBError {
56
56
  constructor(
57
57
  message: string,
58
58
  info: {
59
- status: number | undefined
60
- statusCode: number | undefined
59
+ status?: number
60
+ statusCode?: number
61
61
  name: string
62
- errid: string
63
- description: string
64
- reason: string
65
- error: string
62
+ errid?: string
63
+ description?: string
64
+ reason?: string
65
+ error?: string
66
66
  }
67
67
  ) {
68
68
  super(message)
69
69
  const statusCode = info.status || info.statusCode || 500
70
70
  this.status = statusCode
71
71
  this.statusCode = statusCode
72
- this.reason = info.reason
72
+ this.reason = info.reason || "Unknown"
73
73
  this.name = info.name
74
- this.errid = info.errid
75
- this.description = info.description
76
- this.error = info.error
74
+ this.errid = info.errid || "Unknown"
75
+ this.description = info.description || "Unknown"
76
+ this.error = info.error || "Not found"
77
77
  }
78
78
  }
79
79
 
@@ -246,6 +246,35 @@ export class DatabaseImpl implements Database {
246
246
  })
247
247
  }
248
248
 
249
+ async bulkRemove(documents: Document[], opts?: { silenceErrors?: boolean }) {
250
+ const response: Nano.DocumentBulkResponse[] = await this.performCall(db => {
251
+ return () =>
252
+ db.bulk({
253
+ docs: documents.map(doc => ({
254
+ ...doc,
255
+ _deleted: true,
256
+ })),
257
+ })
258
+ })
259
+ if (opts?.silenceErrors) {
260
+ return
261
+ }
262
+ let errorFound = false
263
+ let errorMessage: string = "Unable to bulk remove documents: "
264
+ for (let res of response) {
265
+ if (res.error) {
266
+ errorFound = true
267
+ errorMessage += res.error
268
+ }
269
+ }
270
+ if (errorFound) {
271
+ throw new CouchDBError(errorMessage, {
272
+ name: this.name,
273
+ status: 400,
274
+ })
275
+ }
276
+ }
277
+
249
278
  async post(document: AnyDocument, opts?: DatabasePutOpts) {
250
279
  if (!document._id) {
251
280
  document._id = newid()
@@ -71,6 +71,16 @@ export class DDInstrumentedDatabase implements Database {
71
71
  })
72
72
  }
73
73
 
74
+ bulkRemove(
75
+ documents: Document[],
76
+ opts?: { silenceErrors?: boolean }
77
+ ): Promise<void> {
78
+ return tracer.trace("db.bulkRemove", span => {
79
+ span?.addTags({ db_name: this.name, num_docs: documents.length })
80
+ return this.db.bulkRemove(documents, opts)
81
+ })
82
+ }
83
+
74
84
  put(
75
85
  document: AnyDocument,
76
86
  opts?: DatabasePutOpts | undefined
@@ -113,15 +113,12 @@ export async function addUser(
113
113
  export async function removeUser(user: User) {
114
114
  const db = getPlatformDB()
115
115
  const keys = [user._id!, user.email]
116
- const userDocs = await db.allDocs({
116
+ const userDocs = await db.allDocs<User>({
117
117
  keys,
118
118
  include_docs: true,
119
119
  })
120
- const toDelete = userDocs.rows.map((row: any) => {
121
- return {
122
- ...row.doc,
123
- _deleted: true,
124
- }
125
- })
126
- await db.bulkDocs(toDelete)
120
+ await db.bulkRemove(
121
+ userDocs.rows.map(row => row.doc!),
122
+ { silenceErrors: true }
123
+ )
127
124
  }