Skip to content

Commit

Permalink
[Issue #228] consumers to add model hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Russ Nicoletti committed Jan 28, 2017
1 parent e271773 commit c859a05
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 10 deletions.
11 changes: 10 additions & 1 deletion example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ const config = {
port: 5432,
name: 'sensorthingsexample',
user: 'postgres',
password: '12345678'
password: '12345678',
hooks: {
'beforeCreate': (instance, options) => {
if (instance.$modelOptions.name.plural === 'Things') {
instance.properties = Object.assign({}, instance.properties, {
MozillaSensorWebOwner: 'client-name'
});
}
}
}
}
};

Expand Down
10 changes: 10 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,13 @@ export const components = {
featureOfInterestId: 'FeatureOfInterest/id'
};

// Database hooks (sequelize)
export const dbHooks = [
'beforeCreate',
'afterCreate',
'beforeDestroy',
'afterDestroy',
'beforeUpdate',
'afterUpdate'
];

8 changes: 8 additions & 0 deletions src/models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Sequelize from 'sequelize';

import {
belongsToMany,
dbHooks,
entities,
integrityConstrains,
iotId,
Expand Down Expand Up @@ -85,6 +86,13 @@ export default config => {
}
});

dbHooks.forEach(hook => {
const clientHook = config.hooks[hook];
if (clientHook) {
sequelize.addHook(hook, clientHook);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

Expand Down
3 changes: 2 additions & 1 deletion test/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import app from './server';
import { app } from './server';
import db from '../src/models/db';
import should from 'should';
import supertest from 'supertest';
Expand All @@ -16,6 +16,7 @@ import * as ERR from '../src/errors';
*/

module.exports = (endpoint, port, mandatory, optional = []) => {

const server = supertest.agent(app.listen(port));
const ERRNOS = ERR.errnos;
const ERRORS = ERR.errors;
Expand Down
35 changes: 32 additions & 3 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,45 @@ let app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

let hooks = {};
const config = {
db: {
host: 'localhost',
port: 5432,
name: 'sensorthingstest',
user: 'postgres',
pass: '12345678'
pass: '12345678',
hooks: {
beforeCreate: () => {
hooks.beforeCreate = true
},
afterCreate: () => {
hooks.afterCreate = true
},
beforeDestroy: () => {
hooks.beforeDestroy = true
},
afterDestroy: () => {
hooks.afterDestroy = true
},
beforeUpdate: () => {
hooks.beforeUpdate = true
},
afterUpdate: () => {
hooks.afterUpdate = true
}
}
}
};
}

app.use('/', SensorThings(config));

exports = module.exports = app;
exports.app = app;

exports.resetHooks = () => {
hooks = {}
};

exports.hookInvoked = name => {
return hooks[name] !== undefined
};
2 changes: 1 addition & 1 deletion test/test_associations_middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import app from './server';
import { app } from './server';
import db from '../src/models/db';
import should from 'should';
import supertest from 'supertest';
Expand Down
2 changes: 1 addition & 1 deletion test/test_base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import app from './server';
import { app } from './server';
import should from 'should';
import supertest from 'supertest';

Expand Down
63 changes: 63 additions & 0 deletions test/test_client_db_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import db from '../src/models/db';
import * as CONST from './constants';
import { hookInvoked, resetHooks } from './server';

db().then(models => {
describe('Database hooks', () => {
beforeEach(() => {
resetHooks();
return models.sequelize.transaction(transaction => {
return Promise.all(Object.keys(CONST.entities).map(name => {
return models[name].destroy({ transaction, where: {} });
}));
});
});

Object.keys(CONST.entities).forEach(entity => {
it(entity + ' "create" hooks', done => {
models[entity].create(CONST[entity + 'Entity']).then(() => {
CONST.dbHooks.slice(0, 2).forEach(hook => {
hookInvoked(hook).should.be.true();
});
done();
});
})
});

// These tests fail. See:
// https://github.com/mozilla-sensorweb/sensorthings/issues/246
Object.keys(CONST.entities).forEach(entity => {
xit(entity + ' "destroy" hooks', done => {
models[entity].create(CONST[entity + 'Entity']).then(
instance => {
const id = instance.id;
models[entity].destroy({
where: { id } }).then(() => {
CONST.dbHooks.slice(2, 4).forEach(hook => {
hookInvoked(hook).should.be.true();
});
done();
});
});
})
});

Object.keys(CONST.entities).forEach(entity => {
it(entity + ' "update" hooks', done => {
models[entity].create(CONST[entity + 'Entity']).then(
instance => {
models[entity].update({ name: 'newname' }, {
where: { id: instance.id },
individualHooks: true
}).then(() => {
CONST.dbHooks.slice(4).forEach(hook => {
hookInvoked(hook).should.be.true();
});
done();
});
});
})
});
});
});

2 changes: 1 addition & 1 deletion test/test_create_observations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import app from './server';
import { app } from './server';
import db from '../src/models/db';
import supertest from 'supertest';
import should from 'should';
Expand Down
2 changes: 1 addition & 1 deletion test/test_deep_insert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import app from './server';
import { app } from './server';
import db from '../src/models/db';
import should from 'should';
import supertest from 'supertest';
Expand Down
2 changes: 1 addition & 1 deletion test/test_query_language.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

'use strict';

import app from './server';
import { app } from './server';
import db from '../src/models/db';
import should from 'should';
import supertest from 'supertest';
Expand Down

0 comments on commit c859a05

Please sign in to comment.