backendless 6.3.2 → 6.3.3

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.
@@ -219,6 +219,35 @@ class UnitOfWork {
219
219
  return this.addOperations(OperationType.FIND, tableName, payload)
220
220
  }
221
221
 
222
+ /**
223
+ * upsert(object: object): OpResult;
224
+ * upsert(tableName: string, object: object): OpResult;
225
+ * **/
226
+ upsert(...args) {
227
+ let tableName
228
+ let changes
229
+
230
+ if (args.length === 1) {
231
+ tableName = Utils.getClassName(args[0])
232
+ changes = args[0]
233
+ } else if (args.length === 2) {
234
+ tableName = args[0]
235
+ changes = args[1]
236
+ } else {
237
+ throw new Error('Invalid arguments')
238
+ }
239
+
240
+ if (!tableName || typeof tableName !== 'string') {
241
+ throw new Error('Invalid arguments')
242
+ }
243
+
244
+ if (!changes || typeof changes !== 'object' || Array.isArray(changes)) {
245
+ throw new Error('Invalid arguments')
246
+ }
247
+
248
+ return this.addOperations(OperationType.UPSERT, tableName, changes)
249
+ }
250
+
222
251
  /**
223
252
  * create(object: object): OpResult;
224
253
  * create(tableName: string, object: object): OpResult;
@@ -342,6 +371,27 @@ class UnitOfWork {
342
371
  return this.addOperations(OperationType.DELETE, tableName, object)
343
372
  }
344
373
 
374
+ /**
375
+ * bulkUpsert(tableName: string, objects: object[]): OpResult;
376
+ * bulkUpsert(objects: object[]): OpResult;
377
+ * **/
378
+ bulkUpsert(tableName, objects) {
379
+ if (Array.isArray(tableName)) {
380
+ objects = tableName
381
+ tableName = Utils.getClassName(objects[0])
382
+ }
383
+
384
+ if (!objects || !Array.isArray(objects)) {
385
+ throw new Error('Objects must be an array of objects.')
386
+ }
387
+
388
+ if (!tableName || typeof tableName !== 'string') {
389
+ throw new Error('Table Name must be a string.')
390
+ }
391
+
392
+ return this.addOperations(OperationType.UPSERT_BULK, tableName, objects)
393
+ }
394
+
345
395
  /**
346
396
  * bulkCreate(tableName: string, objects: object[]): OpResult;
347
397
  * bulkCreate(objects: object[]): OpResult;
@@ -600,7 +650,7 @@ export default function UnitOfWorkService(app) {
600
650
 
601
651
  opResult.setOpResultId(op.opResultId)
602
652
  })
603
-
653
+
604
654
  return uow
605
655
  }
606
656
 
@@ -41,6 +41,8 @@ export const OperationJSONAdapter = {
41
41
 
42
42
  UPDATE: (uow, { table, payload }) => uow.update.call(uow, table, resolveOpResultValueReference(uow, payload)),
43
43
 
44
+ UPSERT: (uow, { table, payload }) => uow.upsert.call(uow, table, resolveOpResultValueReference(uow, payload)),
45
+
44
46
  UPDATE_BULK: (uow, { table, payload }) => {
45
47
  const args = baseBulkArgs(uow, { table, payload })
46
48
 
@@ -59,6 +61,10 @@ export const OperationJSONAdapter = {
59
61
  return uow.bulkCreate.call(uow, table, resolveOpResultValueReference(uow, payload))
60
62
  },
61
63
 
64
+ UPSERT_BULK: (uow, { table, payload }) => {
65
+ return uow.bulkUpsert.call(uow, table, resolveOpResultValueReference(uow, payload))
66
+ },
67
+
62
68
  SET_RELATION: (uow, { table, payload }) => updateRelations(uow, 'setRelation', { table, payload }),
63
69
 
64
70
  DELETE_RELATION: (uow, { table, payload }) => updateRelations(uow, 'deleteRelation', { table, payload }),
package/src/urls.js CHANGED
@@ -109,6 +109,10 @@ export default class Urls {
109
109
  return `${this.data()}/${tableName}`
110
110
  }
111
111
 
112
+ dataTableUpsert(tableName) {
113
+ return `${this.data()}/${tableName}/upsert`
114
+ }
115
+
112
116
  dataTableDeepSave(tableName) {
113
117
  return `${this.data()}/${tableName}/deep-save`
114
118
  }
@@ -141,6 +145,10 @@ export default class Urls {
141
145
  return `${this.data()}/bulk/${tableName}`
142
146
  }
143
147
 
148
+ dataBulkTableUpsert(tableName) {
149
+ return `${this.data()}/bulkupsert/${tableName}`
150
+ }
151
+
144
152
  dataBulkTableDelete(tableName) {
145
153
  return `${this.dataBulkTable(tableName)}/delete`
146
154
  }