Replace the existing types service with one backed by a denormalized model

Fixes #14
This commit is contained in:
R. Tyler Croy 2018-10-23 12:46:14 -07:00
parent 777540fc31
commit dc196e9b34
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
3 changed files with 72 additions and 14 deletions

10
src/models/type.ts Normal file
View File

@ -0,0 +1,10 @@
'use strict';
export default (sequelize, DataTypes) => {
const Type = sequelize.define('types', {
type: DataTypes.STRING,
}, {});
Type.associate = function(models) {
};
return Type;
};

View File

@ -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);
}

50
test/types.test.ts Normal file
View File

@ -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);
});
});
});
});