1 | define(["dojo", "dojox", "dojox/data/JsonRestStore"], function(dojo, dojox) { |
---|
2 | |
---|
3 | // A CouchDBRestStore is an extension of JsonRestStore to handle CouchDB's idiosyncrasies, special features, |
---|
4 | // and deviations from standard HTTP Rest. |
---|
5 | // NOTE: CouchDB is not designed to be run on a public facing network. There is no access control |
---|
6 | // on database documents, and you should NOT rely on client side control to implement security. |
---|
7 | |
---|
8 | |
---|
9 | dojo.declare("dojox.data.CouchDBRestStore", |
---|
10 | dojox.data.JsonRestStore, |
---|
11 | { |
---|
12 | save: function(kwArgs){ |
---|
13 | var actions = this.inherited(arguments); // do the default save and then update for version numbers |
---|
14 | var prefix = this.service.servicePath; |
---|
15 | for(var i = 0; i < actions.length; i++){ |
---|
16 | // need to update the item's version number after it has been committed |
---|
17 | (function(item,dfd){ |
---|
18 | dfd.addCallback(function(result){ |
---|
19 | if(result){ |
---|
20 | item.__id = prefix + result.id; // update the object with the results of the post |
---|
21 | item._rev = result.rev; |
---|
22 | } |
---|
23 | return result; |
---|
24 | }); |
---|
25 | })(actions[i].content,actions[i].deferred); |
---|
26 | } |
---|
27 | }, |
---|
28 | fetch: function(args){ |
---|
29 | // summary: |
---|
30 | // This only differs from JsonRestStore in that it, will put the query string the query part of the URL and it handles start and count |
---|
31 | args.query = args.query || '_all_docs?'; |
---|
32 | if(args.start){ |
---|
33 | args.query = (args.query ? (args.query + '&') : '') + 'startkey=' + args.start; |
---|
34 | delete args.start; |
---|
35 | } |
---|
36 | if(args.count){ |
---|
37 | args.query = (args.query ? (args.query + '&') : '') + 'limit=' + args.count; |
---|
38 | delete args.count; |
---|
39 | } |
---|
40 | return this.inherited(arguments); |
---|
41 | }, |
---|
42 | _processResults: function(results){ |
---|
43 | var rows = results.rows; |
---|
44 | if(rows){ |
---|
45 | var prefix = this.service.servicePath; |
---|
46 | var self = this; |
---|
47 | for(var i = 0; i < rows.length;i++){ |
---|
48 | var realItem = rows[i].value; |
---|
49 | realItem.__id= prefix + rows[i].id; |
---|
50 | realItem._id= rows[i].id; |
---|
51 | realItem._loadObject= dojox.rpc.JsonRest._loader; |
---|
52 | rows[i] = realItem; |
---|
53 | } |
---|
54 | return {totalCount:results.total_rows, items:results.rows}; |
---|
55 | }else{ |
---|
56 | return {items:results}; |
---|
57 | } |
---|
58 | |
---|
59 | } |
---|
60 | } |
---|
61 | ); |
---|
62 | |
---|
63 | // create a set of stores |
---|
64 | dojox.data.CouchDBRestStore.getStores = function(couchServerUrl){ |
---|
65 | var dfd = dojo.xhrGet({ |
---|
66 | url: couchServerUrl+"_all_dbs", |
---|
67 | handleAs: "json", |
---|
68 | sync: true |
---|
69 | }); |
---|
70 | var stores = {}; |
---|
71 | dfd.addBoth(function(dbs){ |
---|
72 | for(var i = 0; i < dbs.length; i++){ |
---|
73 | stores[dbs[i]] = new dojox.data.CouchDBRestStore({target:couchServerUrl + dbs[i],idAttribute:"_id"}); |
---|
74 | } |
---|
75 | return stores; |
---|
76 | }); |
---|
77 | return stores; |
---|
78 | }; |
---|
79 | |
---|
80 | return dojox.data.CouchDBRestStore; |
---|
81 | |
---|
82 | }); |
---|