Skip to content

Commit eef5fbf

Browse files
illyaVvengrov
authored andcommitted
fix (Data Service): (#97)
- remove unsupported method bulkCreate - fix method bulkDelete - add tests for method bulkDelete
1 parent b3404ad commit eef5fbf

File tree

3 files changed

+73
-49
lines changed

3 files changed

+73
-49
lines changed

src/backendless.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,6 @@ declare module __Backendless {
431431
deleteRelationSync(parentObject:Object, columnName:string, childObjectIdArray:Array<string>):string;
432432
deleteRelationSync(parentObject:Object, columnName:string, whereClause:string):string;
433433

434-
bulkCreate(objectsArray:Array<Object>):Promise<string>;
435-
bulkCreateSync(objectsArray:Array<Object>):string;
436-
437434
bulkUpdate(templateObject:Object, whereClause:string):Promise<string>;
438435
bulkUpdateSync(templateObject:Object, whereClause:string):string;
439436

src/backendless.js

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,34 +1546,6 @@
15461546
return url;
15471547
},
15481548

1549-
/**
1550-
* Create of several objects
1551-
*
1552-
* @param {object[]} objectsArray - array of objects
1553-
* @returns {Promise}
1554-
*/
1555-
bulkCreate: promisified('_bulkCreate'),
1556-
1557-
/**
1558-
* Create of several objects (sync)
1559-
*
1560-
* @param {object[]} objectsArray - array of objects
1561-
* @returns {*}
1562-
*/
1563-
bulkCreateSync: synchronized('_bulkCreate'),
1564-
1565-
_bulkCreate: function(objectsArray, async) {
1566-
this._validateBulkCreateArg(objectsArray);
1567-
1568-
return Backendless._ajax({
1569-
method : 'POST',
1570-
url : this.bulkRestUrl,
1571-
data : JSON.stringify(objectsArray),
1572-
isAsync : !!async,
1573-
asyncHandler: async
1574-
});
1575-
},
1576-
15771549
/**
15781550
* Update of several objects by template
15791551
*
@@ -1633,34 +1605,18 @@
16331605
objects = objectsArray.map(function(obj) {
16341606
return Utils.isString(obj) ? obj : obj.objectId;
16351607
});
1608+
1609+
whereClause = 'objectId in (\'' + objects.join('\', \'') + '\')';
16361610
}
16371611

16381612
return Backendless._ajax({
16391613
method : 'DELETE',
16401614
url : this.bulkRestUrl + '?' + Utils.toQueryParams({ where: whereClause }),
1641-
data : objects && JSON.stringify(objects),
16421615
isAsync : !!async,
16431616
asyncHandler: async
16441617
});
16451618
},
16461619

1647-
_validateBulkCreateArg: function(objectsArray) {
1648-
var MSG_ERROR = (
1649-
'Invalid value for the "objectsArray" argument. ' +
1650-
'The argument must contain only array of objects.'
1651-
);
1652-
1653-
if (!Utils.isArray(objectsArray)) {
1654-
throw new Error(MSG_ERROR);
1655-
}
1656-
1657-
for (var i = 0; i < objectsArray.length; i++) {
1658-
if (!Utils.isObject(objectsArray[i])) {
1659-
throw new Error(MSG_ERROR);
1660-
}
1661-
}
1662-
},
1663-
16641620
_validateBulkUpdateArgs: function(templateObject, whereClause) {
16651621
if (!templateObject || !Utils.isObject(templateObject)) {
16661622
throw new Error('Invalid templateObject argument. The first argument must contain object');

test/e2e/specs/persistence.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,77 @@ describe('Persistence', function() {
344344
})
345345
})
346346

347+
/**********************
348+
* BULK OPERATIONS *
349+
**********************/
350+
351+
describe('Bulk operations', function() {
352+
const TestTableName = 'MovieAnimal'
353+
const testData = [
354+
{
355+
name: 'Beethoven',
356+
kind: 'dog'
357+
},
358+
{
359+
name: 'Rex',
360+
kind: 'dog'
361+
},
362+
{
363+
name: 'Lessie',
364+
kind: 'dog'
365+
},
366+
{
367+
name: 'Garfield',
368+
kind: 'cat'
369+
}
370+
]
371+
372+
let testDataItems
373+
let TestTable
374+
375+
beforeEach(() => {
376+
TestTable = Persistence.of(TestTableName)
377+
testDataItems = []
378+
379+
let saveAction = Promise.resolve()
380+
381+
testData.forEach(data => {
382+
saveAction = saveAction
383+
.then(() => TestTable.save(data)
384+
.then(item => testDataItems.push(item))
385+
)
386+
})
387+
388+
return saveAction
389+
}
390+
)
391+
392+
it('Bulk delete by where clause', function() {
393+
return TestTable.bulkDelete('kind=\'dog\'')
394+
.then(result => {
395+
expect(result).to.be.equal(3)
396+
})
397+
})
398+
399+
it('Bulk delete by array of ids', function() {
400+
const ids = testDataItems
401+
.filter(item => item.kind === 'dog')
402+
.map(item => item.objectId)
403+
404+
return TestTable.bulkDelete(ids)
405+
.then(result => {
406+
expect(result).to.be.equal(ids.length)
407+
})
408+
})
409+
410+
it('Bulk delete by array of objects', function() {
411+
return TestTable.bulkDelete(testDataItems)
412+
.then(result => {
413+
expect(result).to.be.equal(testDataItems.length)
414+
})
415+
})
416+
})
417+
347418
/****************
348419
* RELATIONS *
349420
****************/

0 commit comments

Comments
 (0)