Replace the existing types service with one backed by a denormalized model
Fixes #14
This commit is contained in:
parent
777540fc31
commit
dc196e9b34
|
@ -0,0 +1,10 @@
|
|||
'use strict';
|
||||
|
||||
export default (sequelize, DataTypes) => {
|
||||
const Type = sequelize.define('types', {
|
||||
type: DataTypes.STRING,
|
||||
}, {});
|
||||
Type.associate = function(models) {
|
||||
};
|
||||
return Type;
|
||||
};
|
|
@ -3,28 +3,26 @@
|
|||
*/
|
||||
|
||||
import { Params, HooksObject } from '@feathersjs/feathers';
|
||||
import service from 'feathers-sequelize';
|
||||
|
||||
import authorize from '../hooks/authorize';
|
||||
import applyGrant from '../hooks/apply-grant';
|
||||
import db from '../models';
|
||||
import Event from '../models/event';
|
||||
import Type from '../models/type';
|
||||
|
||||
const typesHooks : HooksObject = {
|
||||
before: {},
|
||||
before: {
|
||||
all: [
|
||||
authorize(),
|
||||
applyGrant(),
|
||||
],
|
||||
},
|
||||
after: {},
|
||||
error: {},
|
||||
};
|
||||
|
||||
export class TypesService {
|
||||
async find(params : Params) : Promise<any> {
|
||||
return db.sequelize.query('SELECT DISTINCT(type) FROM events', { type: db.sequelize.QueryTypes.SELECT }).then((types) => {
|
||||
if (types.length > 0) {
|
||||
return types.map(t => t.type);
|
||||
}
|
||||
return [];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default (app) => {
|
||||
app.use('/types', new TypesService);
|
||||
const Model : any = Type(db.sequelize, db.sequelize.Sequelize);
|
||||
app.use('/types', service({ Model: Model }));
|
||||
app.service('types').hooks(typesHooks);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
import url from 'url';
|
||||
import request from 'request-promise';
|
||||
|
||||
import app from '../src/app';
|
||||
import types from '../src/service/types';
|
||||
|
||||
// Offsetting a bit to ensure that we can watch and run at the same time
|
||||
const port = (app.get('port') || 3030) + 10;
|
||||
const getUrl = pathname => url.format({
|
||||
hostname: app.get('host') || 'localhost',
|
||||
protocol: 'http',
|
||||
port,
|
||||
pathname
|
||||
});
|
||||
|
||||
describe('Acceptance tests for /types', () => {
|
||||
beforeEach((done) => {
|
||||
this.server = app.listen(port);
|
||||
this.server.once('listening', () => done());
|
||||
});
|
||||
afterEach(done => this.server.close(done));
|
||||
|
||||
describe('with unauthenticated requests', () => {
|
||||
it('responds to GET /types', () => {
|
||||
return request(getUrl('/types'), {
|
||||
json: true,
|
||||
resolveWithFullResponse: true,
|
||||
}).then(response =>
|
||||
expect(response.statusCode).toEqual(401)
|
||||
).catch(err =>
|
||||
expect(err.statusCode).toEqual(401)
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with authenticated requests', () => {
|
||||
it('responds to GET /types with an Array of types', () => {
|
||||
return request(getUrl('/types'), {
|
||||
json: true,
|
||||
resolveWithFullResponse: true,
|
||||
qs: {
|
||||
testing_access_token: true,
|
||||
},
|
||||
}).then((response) => {
|
||||
expect(response.statusCode).toEqual(200);
|
||||
expect(response.body).toBeInstanceOf(Array);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue