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 25, 2017
1 parent adf2fd8 commit efe2f7c
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 3 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
22 changes: 22 additions & 0 deletions src/models/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ const IDLE = 0
const INITIALIZING = 1;
const READY = 2;

const hooks = [
'beforeValidate',
'afterValidate',
'beforeCreate',
'afterCreate',
'beforeDestroy',
'afterDestroy',
'beforeRestore',
'afterRestore',
'beforeUpdate',
'afterUpdate'
];

const Deferred = function Deferred () {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
Expand Down Expand Up @@ -82,6 +95,15 @@ export default config => {
if ('associate' in db[modelName]) {
db[modelName].associate(db);
}

if (!config.hooks) {
return;
}

hooks.forEach(hook => {
const clientHook = config.hooks[hook];
clientHook && db[modelName].hook(hook, clientHook);
});
});

db.sequelize = sequelize;
Expand Down
56 changes: 54 additions & 2 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,61 @@ const config = {
port: 5432,
name: 'sensorthingstest',
user: 'postgres',
pass: '12345678'
pass: '12345678',
hooks: {
'beforeValidate': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
beforeValidate: 'beforeValidate'
});
},
'afterValidate': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
afterValidate: 'afterValidate'
});
},
'beforeCreate': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
beforeCreate: 'beforeCreate'
});
},
'afterCreate': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
afterCreate: 'afterCreate'
});
},
'beforeDestroy': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
beforeDestroy: 'beforeDestroy'
});
},
'afterDestroy': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
afterDestroy: 'afterDestroy'
});
},
'beforeRestore': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
beforeRestore: 'beforeRestore'
});
},
'afterRestore': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
afterRestore: 'afterRestore'
});
},
'beforeUpdate': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
beforeUpdate: 'beforeUpdate'
});
},
'afterUpdate': (instance, options) => {
instance.properties = Object.assign({}, instance.properties, {
afterUpdate: 'afterUpdate'
});
}
}
}
};
}

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

Expand Down
70 changes: 70 additions & 0 deletions test/test_client_db_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import app from './server';
import db from '../src/models/db';
import {
datastreams,
DatastreamsEntity,
entities,
featuresOfInterest,
FeaturesOfInterestEntity,
historicalLocations,
HistoricalLocationsEntity,
locations,
LocationsEntity,
observations,
ObservationsEntity,
observedProperties,
ObservedPropertiesEntity,
sensors,
SensorsEntity,
things,
ThingsEntity
} from './constants';
import supertest from 'supertest';

const server = supertest.agent(app.listen(8890));

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

[
[ datastreams, DatastreamsEntity ],
[ featuresOfInterest, FeaturesOfInterestEntity ],
[ historicalLocations, HistoricalLocationsEntity ],
[ locations, LocationsEntity ],
[ observations, ObservationsEntity ],
[ observedProperties, ObservedPropertiesEntity ],
[ sensors, SensorsEntity ],
[ things, ThingsEntity ]
].forEach(testObj => {
it(testObj[0] + ' db \"create\" hooks', done => {
models[testObj[0]].create(testObj[1]).then(instance => {
const expectedMap = new Map();
[
['beforeValidate', 'beforeValidate'],
['afterValidate', 'afterValidate'],
['beforeCreate', 'beforeCreate'],
['afterCreate', 'afterCreate']
].map(expectedObj => {
expectedMap.set(expectedObj[0], expectedObj[1]);
});
const instanceProperties = Object.entries(instance.properties);
const propIndex = (instance.$modelOptions.name.plural === 'Things') ? 3 : 0;
for (var i = 0; i < instanceProperties.length; i++) {
const expectedHook = expectedMap.get(instanceProperties[i + propIndex][0]);
const instanceHook = instanceProperties[i + propIndex][1];
expectedHook.should.be.equal(instanceHook);
}
});
done();
})
});
});
});

0 comments on commit efe2f7c

Please sign in to comment.