-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
349 lines (207 loc) · 6.93 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
UniversQL
Copyright 2016 Brandon Carl
MIT Licensed
Static methods:
• addAdapter
• getDefaultAdapter
• removeAdapter
• setDefaultAdapter
Instance methods:
• addAdapter
• compile
• getAdapter
• getDefaultAdapter
• removeAdapter
• run
• setAdapter
• setDefaultAdapter
• translate
**/
"use strict";
//
// Dependencies
//
var Parser = require('universql-parser');
/**
The UniversQL class: parses a query string and attaches to this.query.
@param {String} queryString The query string to parse.
**/
var UniversQL = module.exports = function(queryString) {
this.queryString = queryString;
this.query = Parser.parse(queryString);
this.compiled = {};
// !!! THIS SHOULD BE REMOVED ONCE MULTIPLE TABLES ARE SUPPORTED
if (Array.isArray(this.query.tables) || !this.query.table)
this.query.table = this.query.tables[0];
};
/**
Adapters allow conversion between a UniversQL JSON-object, and their
specific language. An Adapter should contain `name`, `translate` and
optionally `run`.
The prototypes share a common adapter list. Furthermore, we make #addAdapter
both an Instance and Static method (to facilitate additions).
**/
UniversQL.prototype.adapters = {};
/**
Adds an adapter: sets as default if no current default.
@param {Adapter} adapter The adapter to be added.
@returns {UniversQL} The instance (for chaining).
**/
UniversQL.prototype.addAdapter = UniversQL.addAdapter = function(adapter) {
if (!adapter || !adapter.compile || !adapter.name)
throw new Error("Your adapter is malformed (missing name or translate function)");
UniversQL.prototype.adapters[adapter.name] = adapter;
// Set as default if no default is in place
if (!this.getDefaultAdapter()) this.setDefaultAdapter(adapter.name);
return this;
};
/**
Removes an adapter.
@param {Adapter} adapter The adapter to be removed.
@returns {UniversQL} The adapter that was removed.
**/
UniversQL.prototype.removeAdapter = UniversQL.removeAdapter = function(adapter) {
// Throw error if currently default
if (adapter === UniversQL.prototype.defaultAdapter)
throw new Error(adapter + " is set as the default adapter. Please unset it first.");
adapter = UniversQL.prototype.adapters[adapter];
delete UniversQL.prototype.adapters[adapter];
return adapter;
};
/**
Gets the default adapter.
@returns {Adapter} The default adapter.
**/
UniversQL.prototype.getDefaultAdapter = UniversQL.getDefaultAdapter = function() {
return UniversQL.prototype.defaultAdapter;
};
/**
Sets the default adapter.
@param {Adapter|String} The default adapter (or name of installed adapter).
@returns {UniversQL} The instance (for chaining).
**/
UniversQL.prototype.setDefaultAdapter = UniversQL.setDefaultAdapter = function(adapter) {
if ("string" === typeof adapter) {
adapter = UniversQL.prototype.adapters[adapter];
if (!adapter) throw new Error("Adapter " + adapter + " hasn't been installed");
}
UniversQL.prototype.defaultAdapter = adapter;
return this;
};
/**
Gets the adapter.
@param {String} [name] Optional name of adapter to get.
@returns {Adapter} The adapter.
**/
UniversQL.prototype.getAdapter = function(name) {
if (name) return this.adapters[name];
return this.adapter || this.defaultAdapter;
};
/**
Sets the adapter.
@param {Adapter|String} The adapter (or name of installed adapter).
@returns {UniversQL} The instance (for chaining).
**/
UniversQL.prototype.setAdapter = function(adapter) {
if ("string" === typeof adapter) {
adapter = this.adapters[adapter];
if (!adapter) throw new Error("Adapter " + adapter + " hasn't been installed");
}
this.adapter = adapter;
return this;
};
/**
Compiles the query into another query language. Stores into compiled.
@param {String} [name] Name of the adapter to use (falls back to default).
@param {Boolean} [recompile=false] If true, forces re-translation.
@returns {*} Translated form of query.
**/
UniversQL.prototype.compile = function(name, recompile) {
var adapter = this.adapters[name] || this.getAdapter();
// Return if already translated
if (this.compiled[adapter.name] && !recompile)
return this.compiled[adapter.name];
if (!adapter) throw new Error("No working adapter has been specified");
this.compiled[adapter.name] = adapter.compile(this.query, templater(this.templateRE));
return this.compiled[adapter.name];
};
/**
Translate query using context (and compiles if necessary).
@param {String} [name] Name of the adapter to use (falls back to default).
@param {Object} [context={}] Context for templating.
@returns {*} Query with templates compiled.
**/
UniversQL.prototype.translate = function(name, context) {
// Allow polymorphism
if (name && "object" === typeof name) {
context = name;
name = null;
}
return this.compile(name)(context || {});
};
/**
Runs query (translates, templatizes and compiles if necessary).
@param {String} [name] Name of the adapter to use (falls back to default).
@param {Array} data The data on which to run the query.
@param {Object} [context={}] Context for templating.
@returns {*} Query with templates compiled.
**/
UniversQL.prototype.run = function(name, data, context, next) {
var adapter;
// Allow polymorphism
if ("string" !== typeof name) {
next = context;
context = data;
data = name;
name = null;
}
if ("function" === typeof context) {
next = context;
context = {};
}
// Defaults
context = context || {};
this.getAdapter(name).run(this.translate(name, context), data, next);
};
/**
Basic templating, inspired by doT.js. Only includes basic interpolation.
**/
UniversQL.prototype.templateRE = /\{\{([\s\S]+?)\}\}/g;
/**
Creates a template function (that renders when called with context).
@param {String} str The template to precompile.
@param {RegExp} pattern The template pattern to replace.
@returns {Function} Template function: fn(context)
**/
function createTemplate(template, pattern) {
var str = "context = context || {}; var out ='" + template.replace(pattern, function(match, code) {
code = code.trim();
if (/\./.test(code)) code = code.split(".").join('"]["')
return "'+(context[\"" + code + "\"])+'";
});
return new Function("context", str + "';return out;");
}
/**
Runs data through template engine, then calls fn with results.
@param {*} data The data to preprocess.
@param {Function} fn The subsequent function to call.
**/
function templater(regex) {
return function(data, fn) {
var str = JSON.stringify(data),
tpl,
out;
// Set function default
fn = fn || function(x) { return x; };
if (regex.test(str)) {
tpl = createTemplate(str, regex);
return function(context) {
return fn(JSON.parse(tpl(context || {})));
};
} else {
out = fn(data);
return function() { return out; };
}
};
}