Skip to content

Commit

Permalink
Merge pull request #109 from sebelga/feature/validate-entity-global-save
Browse files Browse the repository at this point in the history
feat(global-save): Add option to validate in gstore.save()
  • Loading branch information
sebelga committed May 7, 2018
2 parents 2edffec + 39ccb9c commit d0282e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ declare namespace GstoreNode {
*/
save(
entity: Entity | Entity[],
transaction?: DatastoreTransaction
transaction?: DatastoreTransaction,
options?: { validate: boolean }
): Promise<any>;
}

Expand Down
26 changes: 22 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const is = require('is');
const extend = require('extend');
const arrify = require('arrify');
const hooks = require('promised-hooks');
const NsqlCache = require('nsql-cache');
const dsAdapter = require('nsql-cache-datastore');
Expand Down Expand Up @@ -106,17 +107,34 @@ class Gstore {
return names;
}

save(entities, transaction) {
if (!entities) {
save(_entities, transaction, options = {}) {
if (!_entities) {
throw new Error('No entities passed');
}

const args = Array.prototype.slice.apply(arguments);
const args = Array.prototype.slice.apply(arguments, [0, 2]);

let entities = _entities;

/**
* Validate entities before saving
*/
if (options.validate) {
entities = arrify(entities);
const l = entities.length;
let error;
for (let i = 0; i < l; i += 1) {
({ error } = entities[i].validate());
if (error) {
return Promise.reject(error);
}
}
}

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

if (args.length > 1 && !is.fn(args[1])) {
if (args[1] && args[1].constructor && args[1].constructor.name === 'Transaction') {
// Save inside a transaction
return transaction.save(args[0]);
}
Expand Down
19 changes: 19 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe('gstore-node', () => {

describe('save() alias', () => {
beforeEach(() => {
gstore.connect(ds);
sinon.stub(ds, 'save').resolves();
});

Expand Down Expand Up @@ -228,6 +229,24 @@ describe('gstore-node', () => {

expect(func).to.throw('No entities passed');
});

it('should validate entity before saving', (done) => {
schema = new Schema({ name: { type: String } });
const Model = gstore.model('TestValidate', schema);
const entity1 = new Model({ name: 'abc' });
const entity2 = new Model({ name: 123 });
const entity3 = new Model({ name: 'def' });
sinon.spy(entity1, 'validate');
sinon.spy(entity3, 'validate');

gstore.save([entity1, entity2, entity3], undefined, { validate: true })
.catch((e) => {
expect(e.code).equal('ERR_VALIDATION');
expect(entity1.validate.called).equal(true);
expect(entity3.validate.called).equal(false); // fail fast, exit validation
done();
});
});
});

describe('cache', () => {
Expand Down
2 changes: 2 additions & 0 deletions test/mocks/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ function Transaction() {
this.runQuery = () => Promise.resolve();
}

Transaction.prototype.name = 'Transaction';

module.exports = Transaction;

0 comments on commit d0282e0

Please sign in to comment.