Skip to content

Commit 46ccbbe

Browse files
author
Ben Hanna
committed
Reorganized parsers and transforms.
1 parent 0739ec8 commit 46ccbbe

File tree

5 files changed

+147
-158
lines changed

5 files changed

+147
-158
lines changed

handler.js

Lines changed: 82 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,108 @@
11
const _ = require('lodash');
2+
const qs = require('./parsers/qs');
3+
const transforms = require('./transforms');
24

3-
class Handler {
5+
class ModelHandler {
46
constructor(Model) {
57
this.Model = Model;
68
}
79

8-
async create(req, res, next) {
9-
try {
10-
const row = await this.Model.create(req.body);
11-
res.send(row);
12-
} catch (error) {
13-
next(error);
14-
}
10+
create() {
11+
return [
12+
transforms.raw,
13+
async create(req, res, next) => {
14+
try {
15+
const row = await this.Model.create(req.body);
16+
res.send(res.transform(row));
17+
} catch (error) {
18+
next(error);
19+
}
20+
}
21+
];
1522
}
1623

17-
async get(req, res, next) {
18-
try {
19-
const row = await this.findOne(req.params);
20-
res.send(row);
21-
} catch (error) {
22-
next(error);
23-
}
24+
get() {
25+
return [
26+
transforms.raw,
27+
async (req, res, next) => {
28+
try {
29+
const row = await this.findOne(req.params);
30+
res.send(res.transform(row));
31+
} catch (error) {
32+
next(error);
33+
}
34+
}
35+
];
2436
}
2537

26-
async destroy(req, res, next) {
27-
try {
28-
const row = await this.findOne(req.params);
29-
await row.destroy();
30-
res.sendStatus(204);
31-
} catch (error) {
32-
next(error);
33-
}
38+
query () {
39+
return [
40+
transforms.raw,
41+
async (req, res, next) => {
42+
try {
43+
const { rows, start, end, count } = await this.findAndCountAll(req.query);
44+
45+
res.set('Content-Range', `${start}-${end}/${count}`);
46+
47+
if (count > end) {
48+
res.status(206);
49+
} else {
50+
res.status(200);
51+
}
52+
53+
res.send(res.transform(rows));
54+
} catch (error) {
55+
next(error);
56+
}
57+
}
58+
];
3459
}
3560

36-
async update(req, res, next) {
37-
try {
38-
const row = await this.findOne(req, res, next);
39-
const updated = await row.updateAttributes(req.body);
40-
res.send(updated);
41-
} catch (error) {
42-
next(error);
43-
}
61+
remove() {
62+
return [
63+
async (req, res, next) => {
64+
try {
65+
const row = await this.findOne(req.params);
66+
await row.destroy();
67+
res.sendStatus(204);
68+
} catch (error) {
69+
next(error);
70+
}
71+
}
72+
];
4473
}
4574

46-
async query(req, res, next) {
47-
try {
48-
const { rows, start, end, count } = await this.findAndCountAll(req.query);
49-
50-
res.set('Content-Range', `${start}-${end}/${count}`);
51-
52-
if (count > end) {
53-
res.status(206);
54-
} else {
55-
res.status(200);
75+
update() {
76+
return [
77+
transforms.raw,
78+
async (req, res, next) => {
79+
try {
80+
const row = await this.findOne(req, res, next);
81+
const updated = await row.updateAttributes(req.body);
82+
res.send(updated);
83+
} catch (error) {
84+
next(error);
85+
}
5686
}
57-
58-
res.send(rows);
59-
} catch (error) {
60-
next(error);
61-
}
87+
];
6288
}
6389

64-
async findOne(params, options = { where: {} }) {
65-
options.where = _.merge(this.filter(params), options.where);
90+
async findOne(params, options) {
91+
options = _.merge(qs(params, this.Model), options);
6692

67-
const row = await this.Model.findOne(options);
68-
69-
return row;
93+
return await this.Model.findOne(options);
7094
}
7195

72-
async findAndCountAll(params, options = { where: {} }) {
73-
const { fields, limit = 50, offset = 0, sort, ...filters } = params;
74-
75-
options.where = _.merge(this.filter(filters), options.where);
76-
77-
if (fields) {
78-
options.attributes = fields.split(',');
79-
}
80-
81-
if (sort) {
82-
const keys = sort.split(',');
83-
84-
options.order = keys.map((key) => {
85-
if (key.indexOf('-') === 0) {
86-
return [key.substr(1), 'DESC'];
87-
} else {
88-
return [key, 'ASC'];
89-
}
90-
});
91-
}
96+
async findAndCountAll(params, options) {
97+
options = _.merge(qs(params, this.Model), options);
9298

9399
const { count, rows } = await this.Model.findAndCountAll(options);
94100

95-
const start = offset;
96-
const end = Math.max(count, offset + limit);
101+
const start = options.offset;
102+
const end = Math.min(count, options.offset + options.limit);
97103

98104
return { rows, start, end, count };
99105
}
100-
101-
filter(params, filters = {}) {
102-
const { rawAttributes } = this.Model;
103-
104-
_.forOwn(params, (value, key) => {
105-
if (rawAttributes.hasOwnProperty(key)) {
106-
try {
107-
filters[key] = JSON.parse(value);
108-
} catch (error) {
109-
filters[key] = value.split(',');
110-
}
111-
}
112-
});
113-
114-
return filters;
115-
}
116-
}
106+
}
107+
108+
module.exports = ModelHandler;

parsers/qs.js

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,76 @@
1-
var _ = require('lodash');
1+
const _ = require('lodash');
22

3-
module.exports = {
4-
distinct: distinct,
5-
fields: fields,
6-
filters: filters,
7-
limit: limit,
8-
offset: offset,
9-
sort: sort
10-
}
11-
12-
function distinct(value) {
13-
return value ? true : null;
14-
}
3+
module.exports = parse;
154

16-
function fields(options) {
17-
var fields = null;
5+
function parse(params, { rawAttributes }) {
6+
const options = {
7+
where: {}
8+
};
189

19-
if (options) {
20-
fields = options.split(',');
21-
}
22-
23-
return fields;
24-
}
10+
const keywords = [
11+
'fields',
12+
'group',
13+
'limit',
14+
'offset',
15+
'sort'
16+
];
2517

26-
function filters(options) {
27-
var filters = null;
18+
options.attributes = parseString(params.fields);
19+
options.group = parseString(params.group);
20+
options.limit = parseInteger(params.limit);
21+
options.offset = parseInteger(params.offset);
22+
options.order = parseSort(params.sort);
2823

29-
if (!_.isEmpty(options)) {
30-
filters = {};
31-
_.forOwn(options, function (value, key) {
32-
try {
33-
filters[key] = JSON.parse(value);
34-
} catch (err) {
35-
filters[key] = value.split(',');
24+
_(params)
25+
.omit(keywords)
26+
.forOwn((value, key) => {
27+
if (rawAttributes.hasOwnProperty(key)) {
28+
options.where[key] = parseJson(value);
3629
}
3730
});
31+
32+
return options;
33+
};
34+
35+
function parseString(value) {
36+
if (value) {
37+
value = value.split(',');
3838
}
3939

40-
return filters;
40+
return value;
4141
}
4242

43-
function limit(value) {
44-
value = parseInt(value);
43+
function parseJson(value) {
44+
try {
45+
value = JSON.parse(value);
46+
} catch (error) {
47+
value = parseString(value);
48+
}
4549

46-
if (!value || value < 0) {
47-
value = 0;
48-
}
4950
return value;
5051
}
5152

52-
function offset(value) {
53+
function parseInteger(value) {
5354
value = parseInt(value);
5455

5556
if (!value || value < 0) {
5657
value = 0;
5758
}
59+
5860
return value;
5961
}
6062

61-
function sort(options) {
62-
var properties,
63-
sort = null;
63+
function parseSort(value) {
64+
let sort = null;
6465

65-
if (options) {
66-
properties = options.split(',');
66+
if (value) {
67+
const keys = parseString(value);
6768

68-
sort = _.map(properties, function (x) {
69-
if (x.indexOf('-') === 0) {
70-
return [x.substr(1), 'DESC'];
69+
sort = _.map(keys, (key) => {
70+
if (key.indexOf('-') === 0) {
71+
return [key.substr(1), 'DESC'];
7172
} else {
72-
return [x, 'ASC'];
73+
return [key, 'ASC'];
7374
}
7475
});
7576
}

transforms/distinct.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

transforms/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const _ = require('lodash');
2+
3+
module.exports = {
4+
raw
5+
};
6+
7+
function raw(req, res, next) {
8+
if (!_.isFunction(res.transform)) {
9+
res.transform = transform;
10+
}
11+
12+
next();
13+
14+
function transform(data) {
15+
return data;
16+
}
17+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _ = require('lodash');
1+
const _ = require('lodash');
22

33
module.exports = transform;
44

0 commit comments

Comments
 (0)