Skip to content

Commit

Permalink
feat(gstore-save): Add save method to global save
Browse files Browse the repository at this point in the history
gstore.save() method has accepts a options object to declare the save method (insert, update,
upsert)

#105
  • Loading branch information
sebelga committed May 8, 2018
1 parent d0282e0 commit 9908d7c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class Gstore {
}

// Convert gstore entities to datastore forma ({key, data})
args[0] = datastoreSerializer.entitiesToDatastore(entities);
args[0] = datastoreSerializer.entitiesToDatastore(entities, options);

if (args[1] && args[1].constructor && args[1].constructor.name === 'Transaction') {
// Save inside a transaction
Expand Down
14 changes: 8 additions & 6 deletions lib/serializers/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const is = require('is');
const arrify = require('arrify');

function toDatastore(entity) {
function toDatastore(entity, options = {}) {
const excludeFromIndexes = [...entity.excludeFromIndexes] || [];

let isArray;
Expand Down Expand Up @@ -57,12 +57,14 @@ function toDatastore(entity) {
datastoreFormat.excludeFromIndexes = excludeFromIndexes;
}

if (options.method) {
datastoreFormat.method = options.method;
}

return datastoreFormat;
}

function fromDatastore(entity, options) {
options = options || {};

function fromDatastore(entity, options = {}) {
switch (options.format) {
case 'ENTITY':
return convertToEntity.call(this);
Expand Down Expand Up @@ -122,7 +124,7 @@ function fromDatastore(entity, options) {
* @param {any} entities Entity(ies) to format
* @returns {array} the formated entity(ies)
*/
function entitiesToDatastore(entities) {
function entitiesToDatastore(entities, options) {
const multiple = is.array(entities);
entities = arrify(entities);

Expand All @@ -131,7 +133,7 @@ function entitiesToDatastore(entities) {
return entities;
}

const result = entities.map(toDatastore);
const result = entities.map(e => toDatastore(e, options));

return multiple ? result : result[0];
}
Expand Down
24 changes: 17 additions & 7 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ describe('gstore-node', () => {
));

it('should convert entity instances to datastore Format', () => {
const model1 = new ModelInstance({ name: 'John' });
const model2 = new ModelInstance({ name: 'Mick' });
const entity1 = new ModelInstance({ name: 'John' });
const entity2 = new ModelInstance({ name: 'Mick' });

return gstore.save([model1, model2]).then(() => {
return gstore.save([entity1, entity2]).then(() => {
const { args } = ds.save.getCall(0);
const firstEntity = args[0][0];
assert.isUndefined(firstEntity.className);
Expand All @@ -201,9 +201,9 @@ describe('gstore-node', () => {
});

it('should work inside a transaction', () => {
const model1 = new ModelInstance({ name: 'John' });
const entity = new ModelInstance({ name: 'John' });

gstore.save(model1, transaction);
gstore.save(entity, transaction);

expect(transaction.save.called).equal(true);
expect(ds.save.called).equal(false);
Expand All @@ -214,9 +214,9 @@ describe('gstore-node', () => {

sinon.stub(ds, 'save').callsFake((entity, cb) => cb());

const model = new ModelInstance({ name: 'John' });
const entity = new ModelInstance({ name: 'John' });

return gstore.save(model, () => {
return gstore.save(entity, () => {
const { args } = ds.save.getCall(0);
const firstEntity = args[0];
assert.isUndefined(firstEntity.className);
Expand Down Expand Up @@ -247,6 +247,16 @@ describe('gstore-node', () => {
done();
});
});

it('should allow to pass a save method ("insert", "update", "upsert")', () => {
const entity = new ModelInstance({ name: 'John' });

return gstore.save(entity, undefined, { method: 'insert' })
.then(() => {
const { args } = ds.save.getCall(0);
expect(args[0].method).equal('insert');
});
});
});

describe('cache', () => {
Expand Down

0 comments on commit 9908d7c

Please sign in to comment.