Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a way for consumers to add model hooks #228

Closed
ferjm opened this issue Jan 18, 2017 · 2 comments
Closed

Implement a way for consumers to add model hooks #228

ferjm opened this issue Jan 18, 2017 · 2 comments
Assignees
Milestone

Comments

@ferjm
Copy link
Member

ferjm commented Jan 18, 2017

The user story SSB-7 says "As a user I would like to be able to understand the source of data that I am viewing. (LASS, other source, Sensorweb device) so that I can judge the credibility and see the source of the information."

@ferjm ferjm added this to the Sprint 6 milestone Jan 18, 2017
@ferjm
Copy link
Member Author

ferjm commented Jan 20, 2017

Copy&paste of an IRC conversation with my proposal for this:

so I think we can use the Datastream/Thing.properties field to add something like a MozillaSensorWebOwner field inside with the client name as value
but it is going to be tricky to do it in a clean way ... :/
because we have the ST API implementation as an independent module
we are not supposed to access the Clients DB from it
so I am thinking that we should probably expose a way for users of the ST npm module to execute some code inside the sequelize model hooks
http://docs.sequelizejs.com/en/latest/docs/hooks/
this way from sensorweb-server we can pass a callback to execute within the beforeCreate hook for example
within this callback we will get the instance of the entity being created
so we need to check at this point if this entity is Thing and in that case add the MozillaSensorWebOwner within its 'properties' field
To get the value of MozillaSensorWebOwner (the Client's name), we can get instance.clientId
and with the clientId value, we can get the Client's name from the Clients DB

I'd like to dedicate this issue to implement the mechanism to allow consumers to add model hooks.

In the end, the consumer should be able to do something like:

const bodyParser    = require('body-parser');
const express          = require('express');
const SensorThings = require('../dist/sensorthings');
const Clients             = require('myclientsmodel');

var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const config = {
  db: {
    host: 'localhost',
    port: 5432,
    name: 'sensorthingsexample',
    user: 'postgres',
    password: '12345678'
  }
};

const dbHooks = {
  beforeCreate: (instance, options, fn) => { ... },
  beforeDestroy: (instance, options, fn) => { ... },
  beforeUpdate: null,
  beforeSave: (instance, options, fn) => {
    //XXX Mostly pseudocode!
    if (instance.$modelOptions.name.plural !== 'Things' || !instance.clientId) {
      return;
    }
    return Clients.getById(instance.clientId).then(client => {
      instance.properties = Object.assign({}, instance.properties, {
        MozillaSensorWebOwner: client.name
      });
    });
  },
  beforeUpsert: (values, options, fn) => { ... }
};

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

app.listen(8080, () => console.log('Running on localhost:8080'));

@ferjm ferjm changed the title Allow setting the source of the data Implement a way for consumers to add model hooks Jan 20, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 20, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 21, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 24, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 24, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 24, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 25, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 25, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 26, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 28, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 28, 2017
@ferjm ferjm modified the milestones: Sprint 7, Sprint 6 Jan 30, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 30, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 30, 2017
russnicoletti pushed a commit to russnicoletti/sensorthings that referenced this issue Jan 30, 2017
@ferjm
Copy link
Member Author

ferjm commented Jan 31, 2017

Fixed by #235

@ferjm ferjm closed this as completed Jan 31, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants