-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathtemplates.js
130 lines (94 loc) · 4.94 KB
/
templates.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
'use strict';
var templateChecks = require('../../utils/templateChecks');
var orRegExp = /^(rollback|abort|replace|fail|ignore)$/i;
module.exports = function(dialect) {
dialect.templates.set('select', {
pattern: '{with} {withRecursive} select {limit} {distinct} {fields} ' +
'from {from} {table} {query} {select} {expression} {alias} ' +
'{join} {condition} {group} {having} {sort} {offset}',
defaults: {
fields: {}
},
validate: function(type, params) {
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']);
templateChecks.propType(type, params, 'with', 'object');
templateChecks.propType(type, params, 'withRecursive', 'object');
templateChecks.propType(type, params, 'distinct', 'boolean');
templateChecks.propType(type, params, 'fields', ['array', 'object']);
templateChecks.propType(type, params, 'from', ['string', 'array', 'object']);
templateChecks.atLeastOneOfProps(type, params, ['table', 'query', 'select', 'expression']);
templateChecks.onlyOneOfProps(type, params, ['table', 'query', 'select', 'expression']);
templateChecks.propType(type, params, 'table', 'string');
templateChecks.propType(type, params, 'query', 'object');
templateChecks.propType(type, params, 'select', 'object');
templateChecks.propType(type, params, 'expression', ['string', 'object']);
templateChecks.propType(type, params, 'alias', ['string', 'object']);
templateChecks.propType(type, params, 'join', ['array', 'object']);
templateChecks.propType(type, params, 'condition', ['array', 'object']);
templateChecks.propType(type, params, 'having', ['array', 'object']);
templateChecks.propType(type, params, 'group', ['string', 'array']);
templateChecks.propType(type, params, 'sort', ['string', 'array', 'object']);
templateChecks.propType(type, params, 'offset', ['number', 'string']);
templateChecks.propType(type, params, 'limit', ['number', 'string']);
}
});
dialect.templates.set('insert', {
pattern: '{with} {withRecursive} insert {or} into {table} {values} ' +
'{condition}',
validate: function(type, params) {
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']);
templateChecks.propType(type, params, 'with', 'object');
templateChecks.propType(type, params, 'withRecursive', 'object');
templateChecks.propType(type, params, 'or', 'string');
templateChecks.propMatch(type, params, 'or', orRegExp);
templateChecks.requiredProp(type, params, 'table');
templateChecks.propType(type, params, 'table', 'string');
templateChecks.requiredProp(type, params, 'values');
templateChecks.propType(type, params, 'values', ['array', 'object']);
templateChecks.propType(type, params, 'condition', ['array', 'object']);
}
});
dialect.templates.set('insertValues', {
pattern: '({fields}) {returning} values {values}',
validate: function(type, params) {
templateChecks.requiredProp('values', params, 'fields');
templateChecks.propType('values', params, 'fields', 'array');
templateChecks.minPropLength('values', params, 'fields', 1);
templateChecks.propType(type, params, 'returning', ['array', 'object']);
templateChecks.requiredProp('values', params, 'values');
templateChecks.propType('values', params, 'values', 'array');
templateChecks.minPropLength('values', params, 'values', 1);
}
});
dialect.templates.set('update', {
pattern: '{with} {withRecursive} update {or} {table} {alias} {modifier} {returning} ' +
'{condition} ',
validate: function(type, params) {
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']);
templateChecks.propType(type, params, 'with', 'object');
templateChecks.propType(type, params, 'withRecursive', 'object');
templateChecks.propType(type, params, 'or', 'string');
templateChecks.propMatch(type, params, 'or', orRegExp);
templateChecks.requiredProp(type, params, 'table');
templateChecks.propType(type, params, 'table', 'string');
templateChecks.propType(type, params, 'returning', ['array', 'object']);
templateChecks.propType(type, params, 'alias', 'string');
templateChecks.requiredProp(type, params, 'modifier');
templateChecks.propType(type, params, 'modifier', 'object');
templateChecks.propType(type, params, 'condition', ['array', 'object']);
}
});
dialect.templates.set('remove', {
pattern: '{with} {withRecursive} delete from {table} {returning} {alias} {condition} ',
validate: function(type, params) {
templateChecks.onlyOneOfProps(type, params, ['with', 'withRecursive']);
templateChecks.propType(type, params, 'with', 'object');
templateChecks.propType(type, params, 'withRecursive', 'object');
templateChecks.requiredProp(type, params, 'table');
templateChecks.propType(type, params, 'table', 'string');
templateChecks.propType(type, params, 'returning', ['array', 'object']);
templateChecks.propType(type, params, 'alias', 'string');
templateChecks.propType(type, params, 'condition', ['array', 'object']);
}
});
};