-
Notifications
You must be signed in to change notification settings - Fork 8
/
select.js
269 lines (253 loc) · 8.75 KB
/
select.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
importPackage(com.google.appengine.api.datastore);
/**
* The main `select()` function. Use `select("kind")` to start the chain.
*
* @param {String} the string kind of the datastore entity we're interacting with
*/
var select = function(kind) {
return select.fn.init(kind);
};
select.fn = {
init: function(kind) {
this.datastore = DatastoreServiceFactory.getDatastoreService();
this.kind = kind;
this.query = false;
this.options = false;
this.fetchOptions = FetchOptions.Builder.withDefaults();
this.filterOperators = {
'<' : 'LESS_THAN',
'<=': 'LESS_THAN_OR_EQUAL',
'=' : 'EQUAL',
'>' : 'GREATER_THAN',
'>=': 'GREATER_THAN_OR_EQUAL',
'!=': 'NOT_EQUAL'
};
this.sortDirections = {
'ASC' : 'ASCENDING',
'DESC': 'DESCENDING'
};
return this;
},
/**
* Add a new record to the database.
*
* select('users').
* add({ name: 'Bill Adama' });
*
* @param {Object} record An object containing values for a new record in the collection.
*/
add: function(record) {
var entity = new Entity(this.kind);
for(var key in record) {
this.setProperty(entity, key, record[key]);
}
this.datastore.put(entity);
},
/**
* Modify attributes.
*
* select('users').
* find(1).
* attr({ name: 'William Adama' });
*
* @param {Object} record An object containing values for a new record in the collection.
* @returns {Object} The current `select` object
*/
attr: function(record) {
var result = this.getResult();
for(var i=0; i<result.length; i++) {
var entity = result[i];
for(var key in record) {
this.setProperty(entity, key, record[key]);
}
// XXX should do bulk update
this.datastore.put(entity);
}
},
/**
* Limit the next database find query.
*
* @param {Number} limit Limits the next find() by this amount
* @returns {Object} The current `select` object
*/
limit: function(limit) {
if(limit) this.fetchOptions = this.fetchOptions.limit(limit);
return this;
},
/**
* Offset the next database find query.
*
* @param {Number} offset Offset the next find() by this amount
* @returns {Object} The current `select` object
*/
offset: function(offset) {
if(offset) this.fetchOptions = this.fetchOptions.offset(offset);
return this;
},
/**
* Sorts the results of the next find.
*
* select('users').
* find().
* sort('name', 'ASC').
* each(function() { console.log(this); });
*
* @param {String} propertyName - the property to which to sort on
* @param {String} direction - either "ASC" or "DESC"
* @returns {Object} The current `select` object
*/
sort: function(propertyName, direction) {
if(this.query) {
direction = this.sortDirections[direction||"ASC"] || direction;
this.query.addSort(propertyName, Query.SortDirection[direction]);
}
return this;
},
/**
* Find records.
*
* Find based on ID:
* select('users').
* find(1).
* each(function() { console.log(this); });
*
* Find based on attributes:
* select('users').
* find({ type: 'admin' }).
* each(function() { console.log(this); });
*
* @returns {Object} The current `select` object
*/
find: function(options, fn) {
this.options = options;
if(typeof this.options === "object" || !this.options) {
// start a query, cuz we know it's not a single value
this.query = new Query(this.kind);
}
return this;
},
/**
* Causes the previous `find()` to run, and iterates over the result.
* In the passed in callback, `this` will refer to each value.
*
* select('users').
* find().
* sort('name').
* each(function(id) { console.log(this); });
*
* @param {Function} fn A callback that will be run for each value
* @returns {Object} The current `select` object
*/
each: function(fn) {
var result = this.getResult();
for(var i=0; i<result.length; i++) {
var ent = result[i];
fn.call(this.toJS(ent), ent.getKey().getId());
}
},
/**
* Delete the current set of values specificed by `.find()`
*
* select("users").find({ type: "trial" }).del();
*
*/
del: function() {
var result = this.getResult();
for(var i=0; i<result.length; i++) {
var ent = result[i];
// XXX should do bulk delete
this.datastore["delete"](ent.getKey());
}
},
getResult: function() {
var result = [];
if(typeof this.options === "number" || typeof this.options === "string") {
var key = KeyFactory.createKey(this.kind, this.options);
var entity = this.datastore.get(key);
result.push(entity);
} else if (this.query) {
for(var x in this.options) {
var operator = this.filterOperators["="];
this.query.addFilter(x, Query.FilterOperator[operator], this.options[x]);
}
var preparedQuery = this.datastore.prepare(this.query);
result = preparedQuery.asList(this.fetchOptions).toArray();
}
return result;
},
/**
* Transforms an entity into a nice
* JavaScript object ready to be stringified
* so we don't have to call getProperty() all the time.
* this should be more generic. only supports values
* that are directly convertable into strings
* otherwise JSON won't show them.
*
* This should be private :(
*
* Also should convert all these types: http://code.google.com/appengine/docs/java/datastore/entities.html#Properties_and_Value_Types
*/
toJS: function(entity) {
var properties = entity.getProperties(),
entries = properties.entrySet().iterator();
var ret = {};
while(entries.hasNext()) {
var entry = entries.next(),
key = entry.getKey(),
value = entry.getValue();
if(value instanceof com.google.appengine.api.blobstore.BlobKey) {
// get metadata
var blobInfo = new BlobInfoFactory().loadBlobInfo(value),
contentType = blobInfo.getContentType();
// based on the mime type we need to figure out which image to show
if(!contentType.startsWith("image")) { // default to plain text
value = "<a target='_blank' href='/serve/"+value.getKeyString()+"'>"+blobInfo.getFilename()+"</a>";
} else {
value = "<a target='_blank' href='/serve/"+value.getKeyString()+"'><img src='/serve/"+value.getKeyString()+"' /></a>";
}
} else if(value instanceof Text) {
value = value.getValue();
}
if(value instanceof java.util.List) {
// this is how we convert Java Lists into JavaScript native arrays
ret[key] = org.mozilla.javascript.NativeArray(value.toArray());
} else {
// putting an empty string in front of it
// casts it to a JavaScript string even if it's
// more of a complicated type
ret[key] = ""+value;
}
// always try to parse this string to see if it's valid JSON
try {
if(typeof ret[key] === "string")
ret[key] = JSON.parse(value);
} catch(e) {
// not valid JSON - don't do anything
}
}
return ret;
},
setProperty: function(entity, key, value) {
// google's datastore doesn't like native arrays.
// it needs a Collection for properties with
// multiple values
if(this.isArray(value)) {
value = java.util.Arrays.asList(value);
} else if(this.isObject(value)) {
value = JSON.stringify(value);
}
/*
if(value instanceof java.lang.String || typeof value === "string") {
value = new Text(value);
}
*/
entity.setProperty(key, value);
},
isArray: function( obj ) {
return toString.call(obj) === "[object Array]";
},
isObject: function( obj ) {
return toString.call(obj) === "[object Object]";
}
};
exports = select;