UNPKG

3.79 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.6.3
2var actions, criterion, rawPrototype,
3 __slice = [].slice;
4
5criterion = require('criterion');
6
7actions = require('./actions');
8
9rawPrototype = {
10 sql: function() {
11 return this._sql;
12 },
13 params: function() {
14 return this._params;
15 }
16};
17
18module.exports = {
19 raw: function() {
20 var object, params, sql;
21 sql = arguments[0], params = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
22 object = Object.create(rawPrototype);
23 object._sql = sql;
24 object._params = params;
25 return object;
26 },
27 fluent: function(key, value) {
28 var object;
29 object = Object.create(this);
30 object[key] = value;
31 return object;
32 },
33 _escape: function(string) {
34 return string;
35 },
36 _action: actions.select('*'),
37 _joins: [],
38 insert: function(data) {
39 if ('object' !== typeof data) {
40 throw new Error('data argument must be an object');
41 }
42 return this.fluent('_action', actions.insert(data));
43 },
44 insertMany: function(array) {
45 var keysOfFirstRecord, msg;
46 if (!Array.isArray(array)) {
47 throw new Error('array argument must be an array');
48 }
49 if (array.length === 0) {
50 throw new Error('array argument is empty - no records to insert');
51 }
52 msg = 'all records in the argument array must have the same keys.';
53 keysOfFirstRecord = Object.keys(array[0]);
54 array.forEach(function(data) {
55 var keys;
56 keys = Object.keys(data);
57 if (keys.length !== keysOfFirstRecord.length) {
58 throw new Error(msg);
59 }
60 return keysOfFirstRecord.forEach(function(key) {
61 if (data[key] == null) {
62 throw new Error(msg);
63 }
64 });
65 });
66 return this.fluent('_action', actions.insertMany(array));
67 },
68 escape: function(arg) {
69 return this.fluent('_escape', arg);
70 },
71 select: function() {
72 var params, sql;
73 sql = arguments[0], params = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
74 if (sql == null) {
75 sql = '*';
76 }
77 return this.fluent('_action', actions.select(sql, params));
78 },
79 "delete": function() {
80 return this.fluent('_action', actions["delete"]());
81 },
82 update: function(updates) {
83 return this.fluent('_action', actions.update(updates));
84 },
85 join: function() {
86 var criterionArgs, join, object, sql;
87 sql = arguments[0], criterionArgs = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
88 join = {
89 sql: sql
90 };
91 if (criterionArgs.length !== 0) {
92 join.criterion = criterion.apply(null, criterionArgs);
93 }
94 object = Object.create(this);
95 object._joins = this._joins.slice();
96 object._joins.push(join);
97 return object;
98 },
99 "with": function(arg) {
100 if (!(('object' === typeof arg) && Object.keys(arg).length !== 0)) {
101 throw new Error('with must be called with an object that has at least one property');
102 }
103 return this.fluent('_with', arg);
104 },
105 group: function(arg) {
106 return this.fluent('_group', arg);
107 },
108 order: function(arg) {
109 return this.fluent('_order', arg);
110 },
111 limit: function(arg) {
112 return this.fluent('_limit', parseInt(arg, 10));
113 },
114 offset: function(arg) {
115 return this.fluent('_offset', parseInt(arg, 10));
116 },
117 table: function(table) {
118 return this.fluent('_table', table);
119 },
120 where: function() {
121 var args, where;
122 args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
123 where = criterion.apply(null, args);
124 return this.fluent('_where', this._where != null ? this._where.and(where) : where);
125 },
126 sql: function() {
127 if (this._table == null) {
128 throw new Error('sql() requires call to table() before it');
129 }
130 return this._action.sql(this);
131 },
132 params: function() {
133 return this._action.params(this);
134 }
135};