-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
131 lines (103 loc) · 3.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
/**
* Module dependencies.
*/
const isEmpty = require('lodash.isempty');
const castArray = require('lodash.castarray');
const compact = require('lodash.compact');
const util = require('util');
/**
* Export `unique`.
*/
module.exports = options => {
options = Object.assign({
identifiers: ['id']
}, options);
if (isEmpty(options.fields) || isEmpty(options.identifiers)) {
throw new Error('Fields and identifiers options must be defined.');
}
return Model => {
return class extends Model {
/**
* Before insert.
*/
$beforeInsert(context) {
const parent = super.$beforeInsert(context);
return this.queryResolver(parent, false, {}, context);
}
/**
* Before update.
*/
$beforeUpdate(queryOptions, context) {
const parent = super.$beforeUpdate(queryOptions, context);
if (isEmpty(queryOptions.old)) {
throw new Error('Unique validation at update only works with queries started with $query.');
}
return this.queryResolver(parent, true, queryOptions, context);
}
/**
* Query resolver.
*/
queryResolver(parent, update = false, queryOptions = {}, context) {
return Promise.resolve(parent)
.then(() => Promise.all(this.getQuery(update, queryOptions, context)))
.then(rows => {
const errors = this.parseErrors(rows);
if (!isEmpty(errors)) {
throw Model.createValidationError({
data: errors,
message: 'Unique Validation Failed',
type: 'ModelValidation'
});
}
});
}
/**
* Get select query.
*/
getQuery(update, queryOptions, context) {
return options.fields.reduce((queries, field, index) => {
const knex = context.transaction || Model.knex();
const collection = knex(this.constructor.tableName);
const fields = castArray(field);
if (isEmpty(compact(fields.map(fieldName => this[fieldName])))) {
return queries;
}
const query = fields
.reduce((subset, fieldName) => {
const oldFieldValue = queryOptions.old && queryOptions.old[fieldName];
return subset.where(
fieldName,
this[fieldName] || oldFieldValue || null
);
}, collection.select())
.limit(1);
if (update) {
options.identifiers.forEach(identifier =>
query.andWhereNot(identifier, queryOptions.old[identifier])
);
}
queries[index] = query;
return queries;
}, []);
}
/**
* Parse errors.
*/
parseErrors(rows) {
return rows.reduce((errors, error, index) => {
if (!isEmpty(error)) {
const fields = castArray(options.fields[index]);
fields.forEach(field => {
errors[[field]] = [{
keyword: 'unique',
message: util.format('%s already in use.', options.fields[index])
}];
});
}
return errors;
}, {});
}
};
};
};