forked from sproutcore/sproutcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchdb_server.js
456 lines (378 loc) · 15.8 KB
/
couchdb_server.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// ========================================================================
// SproutCore
// copyright 2006-2008 Sprout Systems, Inc.
// ========================================================================
require('core') ;
require('server') ;
/**
@class
couchdbServer gives the ability to use couchdb as a backend for sproutcore
Working so far:
- listFor: will make a temp view to get all documents of the type ie. "Contacts.Contact"
- createRecords: uses the bulk_docs options to make 1 or more documents on the server.
Todo:
- listFor: to take an optional argument/setting to uses a named view that will already
be on couchdb
- listFor: take an order option (if possible)
- refreshRecords: to use cacheing (when usings a predefined view), to enable less traffic.
- commitRecords:
- destroyRecords:
- requestRecords: clean-up to code that is not used by couchdb
- All: enable use of, limit and offset, so that pagenaion will work
- All: probably merge all common code into 1 function.
@extends SC.RestServer
@author Geoffrey Donaldson
@copyright 2006-2008, Sprout Systems, Inc. and contributors.
@since SproutCore 1.0
*/
SC.CouchdbServer = SC.Server.extend({
request: function(resource, action, ids, params, method) {
// Get Settings and Options
if (!params) params = {} ;
var opts = {} ;
var onSuccess = params.onSuccess; delete params.onSuccess;
var onNotModified = params.onNotModified; delete params.onNotModified ;
var onFailure = params.onFailure ; delete params.onFailure ;
var context = params.requestContext ; delete params.requestContext ;
var accept = params.accept ; delete params.accept ;
var cacheCode = params.cacheCode; delete params.cacheCode ;
var url = params.url; delete params.url;
// If params.body is a string, then add it, else JSONfy it
if (typeof(params.body) == "string"){
opts.postBody = params.body ;
}else if(typeof(params.body) == "object"){
opts.postBody = Object.toJSONString(params.body) ;
} ; delete params.body ;
opts.requestHeaders = params.requestHeaders ; delete params.requestHeaders ;
if (!opts.requestHeaders) opts.requestHeaders = {} ;
opts.requestHeaders['Accept'] = 'application/json, */*' ;
opts.requestHeaders['X-SproutCore-Version'] = SC.VERSION ;
if (accept) opts.requestHeaders['Accept'] = accept ;
if (cacheCode) opts.requestHeaders['Sproutit-Cache'] = cacheCode ;
opts.method = method || 'get' ;
opts.contentType = "application/json" // this is needed to make couchdb accept our request.
// ids are handeled by the calling methods
// convert remainging parameters into query string.
var parameters = this._toQueryString(params) ;
if (parameters && parameters.length > 0) opts.parameters = parameters ;
var server = this ;
var request = null ; //will container the ajax request
// Save callback functions.
opts.onSuccess = function(transport) {
var cacheCode = request.getHeader('Last-Modified') ;
if ((transport.status == '200') && (transport.responseText == '304 Not Modified')) {
if (onNotModified) onNotModified(transport.status, transport, cacheCode,context);
} else {
if (onSuccess) onSuccess(transport.status, transport, cacheCode,context);
}
} ;
opts.onFailure = function(transport) {
var cacheCode = request.getHeader('Last-Modified') ;
if (onFailure) onFailure(transport.status, transport, cacheCode,context);
} ;
console.log('REQUEST: %@ %@'.fmt(opts.method, url)) ;
request = new Ajax.Request(url,opts) ;
},
// I don't think that we need urlFor, as the models will need to specify where to look.
// ..........................................
// LIST
// This is the method called by a collection to get an updated list of
// records.
listFor: function(opts) {
var recordType = opts.recordType ;
var resource = recordType.resourceURL() ; if (!resource) return false ;
var recordName = recordType.toString()
// TODO: check if the user has given a path to a view.
// if so, call that view (with Method: GET)
var url = resource + "/_temp_view"
var content = {}
recordName = recordName.split('.').last() ;
var context = {
recordType: recordName
}
// TODO: CouchDB will have to deal with these a little different i think
params = {} ;
if (opts.conditions) {
var conditions = this._decamelizeData(opts.conditions) ;
for(var key in conditions) {
params[key] = conditions[key] ;
}
}
// Here is the couchdb temp view code.
content.map = "function(doc) { " +
"if (doc.type == \'"+ recordName +"\' ){ "+
"emit(doc._id, doc)"+
"}}" ;
// TODO: check if the user has given a path to a view.
// if so, call that view (with Method: GET)
params.requestContext = context ;
params.url = url ;
params.body = content ;
params.onSuccess = this._listSuccess.bind(this) ;
params.onNotModified = this._listNotModified.bind(this) ;
params.onFailure = this._listFailure.bind(this) ;
this.request(resource, this._listForAction, null, params, this._listForMethod) ;
},
_listForAction: 'list', // We don't acually use this with couchdb
_listForMethod: 'post', // This is post because we are using _temp_views
_listSuccess: function(status, transport, cacheCode, context) {
var json = eval('json='+transport.responseText) ;
if (!json) { console.log('invalid json!'); return; }
// Due to the way that couchdb returns data, we need to make our own list of id's,
// and build the records from the "value" key of each row.
ids = []
records = json.rows.map(function(row) {
ids.push(row.id) ;
console.log("Got Data - "+Object.toJSONString(row.value))
return row.value ;
}) ;
// then, build any records passed back
if (records.length > 0) {
this.refreshRecordsWithData(records,context.recordType,cacheCode,false);
}
// next, convert the list of ids into records.
var recs = (ids) ? ids.map(function(guid) {
return SC.Store.getRecordFor(guid,context.recordType) ;
}) : [] ;
// now invoke callback
if (context.callback) context.callback(recs,json.count,cacheCode) ;
},
// ..........................................
// CREATE
// send the records back to create them. added a special parameter to
// the hash for each record, _guid, which will be used onSuccess.
createRecords: function(records) {
if (!records || records.length == 0) return ;
records = this._recordsByResource(records) ; // sort by resource.
for(var resource in records) {
if (resource == '*') continue ;
var curRecords = records[resource] ;
// TODO: possibly change this to work differently with 1 record.
// but this works with
var create_url = resource + "/_bulk_docs" ;
// collect data for records
var server = this ; var content = {} ;
var objects = []; var recs = [] ;
for (rec in curRecords){
if (!curRecords.hasOwnProperty(rec)) continue ;
if (curRecords[rec].get('attributes')){
atts = curRecords[rec].get('attributes');
atts.type = curRecords[rec]._type._objectClassName.split('.').last() ;
//atts._id = curRecords[rec]._guid ; // we don't want to send an id to start with
delete atts.guid ;
delete atts.idDirty ; // Not sure what this is or where it comes from
recs.push(curRecords[rec]) ;
}else{
atts = {} ;
}
objects.push(atts);
}
content.docs = objects ;// request() will call toJSONString() on this. ;
var context = {
records: recs
} ;
var params = {
requestContext: context,
onSuccess: this._createSuccess.bind(this),
onFailure: this._createFailure.bind(this),
body: content,
url: create_url
};
// issue request
this.request(resource, this._createAction, null, params, this._createMethod) ;
}
},
_createAction: 'create',
_createMethod: 'post',
// This method is called when a create is successful. It first goes through
// and assigns the primaryKey to each record.
_createSuccess: function(status, transport, cacheCode, context) {
var json = eval('json='+transport.responseText) ;
if (!json) { console.log('invalid json!'); return; }
// first go through and assign the primaryKey to each record.
if (json.new_revs) {
// CouchDB will return the documents in the same order you sent them
// so here we walk through the returned id's
for(i=0; i < json.new_revs.length; i++ ) {
data = json.new_revs[i] ;
var rec = context.records[i] ;
if (rec) {
var pk = rec.get('primaryKey') ;
var dataKey = (pk == 'guid') ? 'id' : pk.decamelize().toLowerCase().replace(/\-/g,'_') ;
rec.set(pk,data[dataKey]) ;
rec.set("_id", data.id) ; // Set couchDB specific
rec.set("_rev", data.rev) ;
rec.set('newRecord',false) ;
}
context.records[i] = rec ;
}
// now this method will work so go do it.
this.refreshRecordsWithData(context.records, context.recordType, cacheCode, true) ;
}
if (context.onSuccess) context.onSuccess(transport, cacheCode) ;
},
_refreshAction: 'refresh',
_refreshMethod: 'get',
// ..........................................
// COMMIT
// This is mostly just a copy of createRecords, as the process is the same
// in couchDB
commitRecords: function(records) {
if (!records || records.length == 0) return ;
records = this._recordsByResource(records) ; // sort by resource.
for(var resource in records) {
if (resource == '*') continue ;
var curRecords = records[resource] ;
// TODO: possibly change this to work differently with 1 record.
// but this works with
var create_url = resource + "/_bulk_docs" ;
// collect data for records
var server = this ; var content = {} ;
var objects = []; var recs = [] ;
for (rec in curRecords){
if (!curRecords.hasOwnProperty(rec)) continue ;
if (curRecords[rec].get('attributes')){
atts = curRecords[rec].get('attributes');
atts._id = curRecords[rec].get('guid') ;
recs.push(curRecords[rec]) ;
}else{
atts = {} ;
}
objects.push(atts);
}
content.docs = objects ;// request() will call toJSONString() on this. ;
if (content.docs.length > 0) {
var context = {
records: recs
} ;
var params = {
requestContext: context,
onSuccess: this._commitSuccess.bind(this),
onFailure: this._commitFailure.bind(this),
body: content,
url: create_url
};
// issue request
this.request(resource, this._createAction, null, params, this._createMethod) ;
}
}
},
_commitAction: 'save',
_commitMethod: 'post', // again, this will use couchDB's bulk_docs call, which is post
// This method is called when a refresh is successful. It expects an array
// of hashes, which it will convert to records.
_commitSuccess: function(status, transport, cacheCode, context) {
var json = eval('json='+transport.responseText) ;
if (!json) { console.log('invalid json!'); return; }
// first go through and assign the primaryKey to each record.
if (json.new_revs) {
// CouchDB will return the documents in the same order you sent them
// so here we walk through the returned id's
for(i=0; i < json.new_revs.length; i++ ) {
var data = json.new_revs[i] ;
var rec = context.records[i] ;
if (rec) {
var pk = rec.get('primaryKey') ;
var dataKey = (pk == 'guid') ? 'id' : pk.decamelize().toLowerCase().replace(/\-/g,'_') ;
rec.set(pk,data[dataKey]) ;
rec.set("_id", data.id) ; // Set couchDB specific
rec.set("_rev", data.rev) ;
rec.set('newRecord',false) ;
}
context.records[i] = rec ;
}
// now this method will work so go do it.
this.refreshRecordsWithData(context.records, context.recordType, cacheCode, true) ;
}
if (context.onSuccess) context.onSuccess(transport, cacheCode) ;
},
// ..........................................
// DESTROY
// And once again, this is almost a copy of commit
// ... I wonder if there is a way to make this cleaner
destroyRecords: function(records) {
if (!records || records.length == 0) return ;
records = this._recordsByResource(records) ; // sort by resource.
for(var resource in records) {
if (resource == '*') continue ;
var curRecords = records[resource] ;
// TODO: possibly change this to work differently with 1 record.
// but this works with
var create_url = resource + "/_bulk_docs" ;
// collect data for records
var server = this ;
var objects = []; var content = {} ;
for (rec in curRecords){
if (!curRecords.hasOwnProperty(rec)) continue ;
if (curRecords[rec].get('attributes')){
atts = curRecords[rec].get('attributes');
atts._id = curRecords[rec].get('guid') ;
atts._deleted = true ;
}else{
atts = {} ;
}
objects.push(atts);
}
content.docs = objects ;// request() will call toJSONString() on this. ;
if (content.docs.length > 0) {
var context = {
records: curRecords
} ;
var params = {
requestContext: context,
onSuccess: this._destroySuccess.bind(this),
onFailure: this._destroyFailure.bind(this),
body: content,
url: create_url
};
// issue request
this.request(resource, this._createAction, null, params, this._createMethod) ;
}
}
},
_destroyAction: 'destroy',
_destroyMethod: 'post', // We are using post to couchdb's _bulk_doc page.
_destroySuccess: function(status, transport, cacheCode, context) {
SC.Store.destroyRecords(context.records);
console.log('destroySuccess!') ;
},
refreshRecordsWithData: function(dataAry,recordType,cacheCode,loaded) {
var server = this ;
// first, prepare each data item in the Ary.
dataAry = dataAry.map(function(data) {
// camelize the keys received back.
//data = server._camelizeData(data) ;
console.log('Processing Data: '+Object.toJSONString(data)) ;
// ** Changed **
// convert the '_id' property to 'guid' to keep the id's that couchdb has given
if (data._id) {
data.guid = data._id; delete data._id;
}else if (data.id) {
data.guid = data.id; delete data.id;
}
if (data.rev) {
data._rev = data.rev; delete data.rev ;
}
// find the recordType
if (data.type) {
var recordName = data.type.split(".").last().capitalize() ;
if (server.prefix) {
for (var prefixLoc = 0; prefixLoc < server.prefix.length; prefixLoc++) {
var prefixParts = server.prefix[prefixLoc].split('.');
var namespace = window;
for (var prefixPartsLoc = 0; prefixPartsLoc < prefixParts.length; prefixPartsLoc++) {
var namespace = namespace[prefixParts[prefixPartsLoc]] ;
}
if (namespace != window) data.recordType = namespace[recordName] ;
if (data.recordType) break ;
}
} else data.recordType = window[recordName] ;
if (!data.recordType) console.log('skipping undefined recordType:'+recordName) ;
} else data.recordType = recordType ;
if (!data.recordType) return null; // could not process.
else return data ;
}).compact() ;
// now update.
SC.Store.updateRecords(dataAry,server,recordType,loaded) ;
}
}) ;