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 30, 2017
1 parent e271773 commit 54c5b8c
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
16 changes: 16 additions & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ It defaults to `v1.0` and the default endpoint URLs are of the form:

https://<host-url>/v1.0/<api-endpoint>

Additionally, there is an optional `hooks` configuration parameter that let's you define database hooks. This is an example of a database hook that adds a property to a `Things` instance that is being created:
```
const config = {
db: {
hooks: {
'beforeCreate': (instance, options) => {
if (instance.$modelOptions.name.plural === 'Things') {
instance.properties = Object.assign({}, instance.properties, {
customProperty: 'custom property value'
});
}
}
}
}
};
```
For more details about the API you can check this [document](https://github.com/mozilla-sensorweb/sensorthings/blob/master/doc/API.md).

## Example requests
Expand Down
5 changes: 4 additions & 1 deletion src/models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export default config => {
host,
port,
dialect: 'postgres',
logging: false
logging: false,
define: {
hooks: config.hooks
}
});

db = {};
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;
export default app;

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

exports.hookInvoked = name => {
return hooks[name] !== undefined
};
73 changes: 73 additions & 0 deletions test/test_client_db_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import db from '../src/models/db';
import * as CONST from './constants';
import { hookInvoked, resetHooks } from './server';

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

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(() => {
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(() => {
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(() => {
dbHooks.slice(4).forEach(hook => {
hookInvoked(hook).should.be.true();
});
done();
});
});
})
});
});
});

0 comments on commit 54c5b8c

Please sign in to comment.