Skip to content

Commit

Permalink
feat(hooks): Model.delete() "post" hooks callback have now their scop…
Browse files Browse the repository at this point in the history
…e on the entity instance delete

The scope (this) inside a "post" Model.delete() hook is now correctly set to the entity that has
just been deleted. The callback still receives the key deleted in its first argument.
  • Loading branch information
sebelga committed Apr 7, 2018
1 parent 8e5c25b commit 4d9b4dd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
23 changes: 17 additions & 6 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ class Model extends Entity {
* @param {String} hook The name of the hook (save, delete...)
* @param {Array} args The arguments passed to the original method
*/
static __scopeHook(hook, args) {
static __scopeHook(hook, args, hookName, hookType) {
const _this = this;

switch (hook) {
Expand All @@ -893,18 +893,29 @@ class Model extends Entity {
arrify(args[0].__override)[0] :
args[0];

const multiple = is.array(id);
if (is.array(id)) {
return null;
}

id = parseId(id);
let ancestors;
let namespace;
let key;

const ancestors = args.length > 1 ? args[1] : undefined;
const namespace = args.length > 2 ? args[2] : undefined;
const key = args.length > 4 ? args[4] : undefined;
if (hookType === 'post') {
({ key } = args);
if (is.array(key)) {
return null;
}
} else {
([ancestors, namespace,,, key] = args);
}

if (!id && !ancestors && !namespace && !key) {
return undefined;
}

return multiple ? null : _this.__model(null, id, ancestors, namespace, key);
return _this.__model(null, id, ancestors, namespace, key);
}
}

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"main": "index.js",
"scripts": {
"commit": "git-cz",
"coverage-old": "istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec --recursive",
"coverage": "nyc mocha test --recursive",
"coveralls": "nyc mocha test --recursive && nyc report --reporter=text-lcov | coveralls && rm -rf ./coverage",
"local-datastore": "gcloud beta emulators datastore start --data-dir=$PWD/local-datastore",
Expand Down Expand Up @@ -52,7 +51,7 @@
"text"
]
},
"license": "ISC",
"license": "MIT",
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
Expand Down
20 changes: 20 additions & 0 deletions test/integration/model.integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Datastore = require('@google-cloud/datastore');
const chai = require('chai');
const { argv } = require('yargs');
const gstore = require('../../lib')();
const Entity = require('../../lib/entity');

const ds = new Datastore({ projectId: 'gstore-integration-tests' });
gstore.connect(ds);
Expand All @@ -30,6 +31,9 @@ const addKey = (key) => {

describe('Integration Tests (Model)', () => {
beforeEach(function integrationTest() {
gstore.models = {};
gstore.modelSchemas = {};

if (argv.int !== true) {
// Skip e2e tests suite
this.skip();
Expand Down Expand Up @@ -92,4 +96,20 @@ describe('Integration Tests (Model)', () => {
expect(response2.state).equal('requested');
});
});

describe('hooks', () => {
it('post delete hook should set scope on entity instance', (done) => {
const schema = new Schema({ name: { type: 'string' } });
schema.post('delete', function postDelete({ key }) {
expect(key.kind).equal('User');
expect(key.id).equal(123);
expect(this instanceof Entity);
expect(key).equal(this.entityKey);
done();
return Promise.resolve();
});
const Model = gstore.model('User', schema);
Model.delete(123).then(() => {});
});
});
});
17 changes: 10 additions & 7 deletions test/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,14 +852,15 @@ describe('Model', () => {
});
});

it('should set "pre" hook scope to entity being deleted (1)', () => {
it('should set "pre" hook scope to entity being deleted (1)', (done) => {
schema.pre('delete', function preDelete() {
expect(this.className).equal('Entity');
expect(this instanceof Entity);
done();
return Promise.resolve();
});
ModelInstance = Model.compile('Blog', schema, gstore);

return ModelInstance.delete(123);
ModelInstance.delete(123);
});

it('should set "pre" hook scope to entity being deleted (2)', () => {
Expand Down Expand Up @@ -896,10 +897,12 @@ describe('Model', () => {
});
});

it('should pass key deleted to post hooks', () => {
schema.post('delete', (response) => {
expect(response.key.constructor.name).equal('Key');
expect(response.key.id).equal(123);
it('should pass key deleted to post hooks and set the scope to the entity deleted', () => {
schema.post('delete', function postDeleteHook({ key }) {
expect(key.constructor.name).equal('Key');
expect(key.id).equal(123);
expect(this instanceof Entity);
expect(this.entityKey).equal(key);
return Promise.resolve();
});
ModelInstance = Model.compile('Blog', schema, gstore);
Expand Down

0 comments on commit 4d9b4dd

Please sign in to comment.