Merge branch 'feature-43-backend-and-dataset-setup'

This commit is contained in:
Rufus Pollock
2012-02-17 09:17:04 +00:00
3 changed files with 142 additions and 126 deletions

View File

@@ -9,13 +9,11 @@ this.recline = this.recline || {};
this.recline.Model = this.recline.Model || {}; this.recline.Model = this.recline.Model || {};
(function($, my) { (function($, my) {
my.backends = {};
// ## Backbone.sync // ## Backbone.sync
// //
// Override Backbone.sync to hand off to sync function in relevant backend // Override Backbone.sync to hand off to sync function in relevant backend
Backbone.sync = function(method, model, options) { Backbone.sync = function(method, model, options) {
return my.backends[model.backendConfig.type].sync(method, model, options); return model.backend.sync(method, model, options);
} }
// ## wrapInTimeout // ## wrapInTimeout
@@ -45,49 +43,59 @@ this.recline.Model = this.recline.Model || {};
// ## BackendMemory - uses in-memory data // ## BackendMemory - uses in-memory data
// //
// To use you should: // This is very artificial and is really only designed for testing
// purposes.
// //
// A. provide metadata as model data to the Dataset // To use it you should provide in your constructor data:
// //
// B. Set backendConfig on your dataset with attributes: // * metadata (including headers array)
// * documents: list of hashes, each hash being one doc. A doc *must* have an id attribute which is unique.
// //
// - type: 'memory' // Example:
// - data: hash with 2 keys:
//
// * headers: list of header names/labels
// * rows: list of hashes, each hash being one row. A row *must* have an id attribute which is unique.
//
// Example of data:
// //
// <pre> // <pre>
// { // // Backend setup
// headers: ['x', 'y', 'z'] // var backend = Backend();
// , rows: [ // backend.addDataset({
// {id: 0, x: 1, y: 2, z: 3} // metadata: {
// , {id: 1, x: 2, y: 4, z: 6} // id: 'my-id',
// title: 'My Title',
// headers: ['x', 'y', 'z'],
// },
// documents: [
// {id: 0, x: 1, y: 2, z: 3},
// {id: 1, x: 2, y: 4, z: 6}
// ] // ]
// }; // });
// // later ...
// var dataset = Dataset({id: 'my-id'});
// dataset.fetch();
// etc ...
// </pre> // </pre>
my.BackendMemory = Backbone.Model.extend({ my.BackendMemory = Backbone.Model.extend({
initialize: function() {
this.datasets = {};
},
addDataset: function(data) {
this.datasets[data.metadata.id] = $.extend(true, {}, data);
},
sync: function(method, model, options) { sync: function(method, model, options) {
var self = this; var self = this;
if (method === "read") { if (method === "read") {
var dfd = $.Deferred(); var dfd = $.Deferred();
if (model.__type__ == 'Dataset') { if (model.__type__ == 'Dataset') {
var dataset = model; var rawDataset = this.datasets[model.id];
dataset.set({ model.set(rawDataset.metadata);
headers: dataset.backendConfig.data.headers model.docCount = rawDataset.documents.length;
}); dfd.resolve(model);
dataset.docCount = dataset.backendConfig.data.rows.length;
dfd.resolve(dataset);
} }
return dfd.promise(); return dfd.promise();
} else if (method === 'update') { } else if (method === 'update') {
var dfd = $.Deferred(); var dfd = $.Deferred();
if (model.__type__ == 'Document') { if (model.__type__ == 'Document') {
_.each(model.backendConfig.data.rows, function(row, idx) { _.each(self.datasets[model.dataset.id].documents, function(doc, idx) {
if(row.id === model.id) { if(doc.id === model.id) {
model.backendConfig.data.rows[idx] = model.toJSON(); self.datasets[model.dataset.id].documents[idx] = model.toJSON();
} }
}); });
dfd.resolve(model); dfd.resolve(model);
@@ -96,9 +104,11 @@ this.recline.Model = this.recline.Model || {};
} else if (method === 'delete') { } else if (method === 'delete') {
var dfd = $.Deferred(); var dfd = $.Deferred();
if (model.__type__ == 'Document') { if (model.__type__ == 'Document') {
model.backendConfig.data.rows = _.reject(model.backendConfig.data.rows, function(row) { var rawDataset = self.datasets[model.dataset.id];
return (row.id === model.id); var newdocs = _.reject(rawDataset.documents, function(doc) {
return (doc.id === model.id);
}); });
rawDataset.documents = newdocs;
dfd.resolve(model); dfd.resolve(model);
} }
return dfd.promise(); return dfd.promise();
@@ -110,11 +120,11 @@ this.recline.Model = this.recline.Model || {};
var numRows = queryObj.size; var numRows = queryObj.size;
var start = queryObj.offset; var start = queryObj.offset;
var dfd = $.Deferred(); var dfd = $.Deferred();
results = model.backendConfig.data.rows; results = this.datasets[model.id].documents;
// not complete sorting! // not complete sorting!
_.each(queryObj.sort, function(item) { _.each(queryObj.sort, function(item) {
results = _.sortBy(results, function(row) { results = _.sortBy(results, function(doc) {
var _out = row[item[0]]; var _out = doc[item[0]];
return (item[1] == 'asc') ? _out : -1*_out; return (item[1] == 'asc') ? _out : -1*_out;
}); });
}); });
@@ -129,20 +139,12 @@ this.recline.Model = this.recline.Model || {};
// //
// Connecting to [Webstores](http://github.com/okfn/webstore) // Connecting to [Webstores](http://github.com/okfn/webstore)
// //
// To use this backend set backendConfig on your Dataset as: // To use this backend ensure your Dataset has a webstore_url in its attributes.
//
// <pre>
// {
// 'type': 'webstore',
// 'url': url to relevant Webstore table
// }
// </pre>
my.BackendWebstore = Backbone.Model.extend({ my.BackendWebstore = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, model, options) {
if (method === "read") { if (method === "read") {
if (model.__type__ == 'Dataset') { if (model.__type__ == 'Dataset') {
var dataset = model; var base = model.get('webstore_url');
var base = dataset.backendConfig.url;
var schemaUrl = base + '/schema.json'; var schemaUrl = base + '/schema.json';
var jqxhr = $.ajax({ var jqxhr = $.ajax({
url: schemaUrl, url: schemaUrl,
@@ -154,11 +156,11 @@ this.recline.Model = this.recline.Model || {};
headers = _.map(schema.data, function(item) { headers = _.map(schema.data, function(item) {
return item.name; return item.name;
}); });
dataset.set({ model.set({
headers: headers headers: headers
}); });
dataset.docCount = schema.count; model.docCount = schema.count;
dfd.resolve(dataset, jqxhr); dfd.resolve(model, jqxhr);
}) })
.fail(function(arguments) { .fail(function(arguments) {
dfd.reject(arguments); dfd.reject(arguments);
@@ -168,7 +170,7 @@ this.recline.Model = this.recline.Model || {};
} }
}, },
query: function(model, queryObj) { query: function(model, queryObj) {
var base = model.backendConfig.url; var base = model.get('webstore_url');
var data = { var data = {
_limit: queryObj.size _limit: queryObj.size
, _offset: queryObj.offset , _offset: queryObj.offset
@@ -193,33 +195,30 @@ this.recline.Model = this.recline.Model || {};
// //
// For connecting to [DataProxy-s](http://github.com/okfn/dataproxy). // For connecting to [DataProxy-s](http://github.com/okfn/dataproxy).
// //
// Set a Dataset to use this backend:
//
// dataset.backendConfig = {
// // required
// url: {url-of-data-to-proxy},
// format: csv | xls,
// }
//
// When initializing the DataProxy backend you can set the following attributes: // When initializing the DataProxy backend you can set the following attributes:
// //
// * dataproxy: {url-to-proxy} (optional). Defaults to http://jsonpdataproxy.appspot.com // * dataproxy: {url-to-proxy} (optional). Defaults to http://jsonpdataproxy.appspot.com
// //
// Datasets using using this backend should set the following attributes:
//
// * url: (required) url-of-data-to-proxy
// * format: (optional) csv | xls (defaults to csv if not specified)
//
// Note that this is a **read-only** backend. // Note that this is a **read-only** backend.
my.BackendDataProxy = Backbone.Model.extend({ my.BackendDataProxy = Backbone.Model.extend({
defaults: { defaults: {
dataproxy: 'http://jsonpdataproxy.appspot.com' dataproxy_url: 'http://jsonpdataproxy.appspot.com'
}, },
sync: function(method, model, options) { sync: function(method, model, options) {
var self = this;
if (method === "read") { if (method === "read") {
if (model.__type__ == 'Dataset') { if (model.__type__ == 'Dataset') {
var dataset = model; var base = self.get('dataproxy_url');
var base = my.backends['dataproxy'].get('dataproxy');
// TODO: should we cache for extra efficiency // TODO: should we cache for extra efficiency
var data = { var data = {
url: dataset.backendConfig.url url: model.get('url')
, 'max-results': 1 , 'max-results': 1
, type: dataset.backendConfig.format , type: model.get('format') || 'csv'
}; };
var jqxhr = $.ajax({ var jqxhr = $.ajax({
url: base url: base
@@ -228,10 +227,10 @@ this.recline.Model = this.recline.Model || {};
}); });
var dfd = $.Deferred(); var dfd = $.Deferred();
wrapInTimeout(jqxhr).done(function(results) { wrapInTimeout(jqxhr).done(function(results) {
dataset.set({ model.set({
headers: results.fields headers: results.fields
}); });
dfd.resolve(dataset, jqxhr); dfd.resolve(model, jqxhr);
}) })
.fail(function(arguments) { .fail(function(arguments) {
dfd.reject(arguments); dfd.reject(arguments);
@@ -243,11 +242,11 @@ this.recline.Model = this.recline.Model || {};
} }
}, },
query: function(dataset, queryObj) { query: function(dataset, queryObj) {
var base = my.backends['dataproxy'].get('dataproxy'); var base = this.get('dataproxy_url');
var data = { var data = {
url: dataset.backendConfig.url url: dataset.get('url')
, 'max-results': queryObj.size , 'max-results': queryObj.size
, type: dataset.backendConfig.format , type: dataset.get('format')
}; };
var jqxhr = $.ajax({ var jqxhr = $.ajax({
url: base url: base
@@ -256,10 +255,10 @@ this.recline.Model = this.recline.Model || {};
}); });
var dfd = $.Deferred(); var dfd = $.Deferred();
jqxhr.done(function(results) { jqxhr.done(function(results) {
var _out = _.map(results.data, function(row) { var _out = _.map(results.data, function(doc) {
var tmp = {}; var tmp = {};
_.each(results.fields, function(key, idx) { _.each(results.fields, function(key, idx) {
tmp[key] = row[idx]; tmp[key] = doc[idx];
}); });
return tmp; return tmp;
}); });
@@ -273,15 +272,27 @@ this.recline.Model = this.recline.Model || {};
// ## Google spreadsheet backend // ## Google spreadsheet backend
// //
// Connect to Google Docs spreadsheet. For write operations // Connect to Google Docs spreadsheet.
//
// Dataset must have a url attribute pointing to the Gdocs
// spreadsheet's JSON feed e.g.
//
// <pre>
// var dataset = new recline.Model.Dataset({
// url: 'https://spreadsheets.google.com/feeds/list/0Aon3JiuouxLUdDQwZE1JdV94cUd6NWtuZ0IyWTBjLWc/od6/public/values?alt=json'
// },
// 'gdocs'
// );
// </pre>
my.BackendGDoc = Backbone.Model.extend({ my.BackendGDoc = Backbone.Model.extend({
sync: function(method, model, options) { sync: function(method, model, options) {
var self = this;
if (method === "read") { if (method === "read") {
var dfd = $.Deferred(); var dfd = $.Deferred();
var dataset = model; var dataset = model;
$.getJSON(model.backendConfig.url, function(d) { $.getJSON(model.get('url'), function(d) {
result = my.backends['gdocs'].gdocsToJavascript(d); result = self.gdocsToJavascript(d);
model.set({'headers': result.header}); model.set({'headers': result.header});
// cache data onto dataset (we have loaded whole gdoc it seems!) // cache data onto dataset (we have loaded whole gdoc it seems!)
model._dataCache = result.data; model._dataCache = result.data;

View File

@@ -11,11 +11,13 @@ this.recline.Model = this.recline.Model || {};
// * docCount: total number of documents in this dataset (obtained on a fetch for this Dataset) // * docCount: total number of documents in this dataset (obtained on a fetch for this Dataset)
my.Dataset = Backbone.Model.extend({ my.Dataset = Backbone.Model.extend({
__type__: 'Dataset', __type__: 'Dataset',
initialize: function(options) { initialize: function(model, backend) {
console.log(options); this.backend = backend;
if (backend && backend.constructor == String) {
this.backend = my.backends[backend];
}
this.currentDocuments = new my.DocumentList(); this.currentDocuments = new my.DocumentList();
this.docCount = null; this.docCount = null;
this.backend = null;
this.defaultQuery = { this.defaultQuery = {
size: 100 size: 100
, offset: 0 , offset: 0
@@ -37,15 +39,14 @@ this.recline.Model = this.recline.Model || {};
// This also illustrates the limitations of separating the Dataset and the Backend // This also illustrates the limitations of separating the Dataset and the Backend
query: function(queryObj) { query: function(queryObj) {
var self = this; var self = this;
var backend = my.backends[this.backendConfig.type];
this.queryState = queryObj || this.defaultQuery; this.queryState = queryObj || this.defaultQuery;
this.queryState = _.extend({size: 100, offset: 0}, this.queryState); this.queryState = _.extend({size: 100, offset: 0}, this.queryState);
var dfd = $.Deferred(); var dfd = $.Deferred();
backend.query(this, this.queryState).done(function(rows) { this.backend.query(this, this.queryState).done(function(rows) {
var docs = _.map(rows, function(row) { var docs = _.map(rows, function(row) {
var _doc = new my.Document(row); var _doc = new my.Document(row);
_doc.backendConfig = self.backendConfig; _doc.backend = self.backend;
_doc.backend = backend; _doc.dataset = self;
return _doc; return _doc;
}); });
self.currentDocuments.reset(docs); self.currentDocuments.reset(docs);
@@ -76,5 +77,11 @@ this.recline.Model = this.recline.Model || {};
__type__: 'DocumentList', __type__: 'DocumentList',
model: my.Document model: my.Document
}); });
// ## Backend registry
//
// Backends will register themselves by id into this registry
my.backends = {};
}(jQuery, this.recline.Model)); }(jQuery, this.recline.Model));

View File

@@ -1,16 +1,16 @@
(function ($) { (function ($) {
module("Dataset"); module("Dataset");
test('new Dataset', function () { test('Memory Backend', function () {
var datasetId = 'test-dataset'; var datasetId = 'test-dataset';
var metadata = { var inData = {
metadata: {
title: 'My Test Dataset' title: 'My Test Dataset'
, name: '1-my-test-dataset' , name: '1-my-test-dataset'
, id: datasetId , id: datasetId
}; , headers: ['x', 'y', 'z']
var indata = { },
headers: ['x', 'y', 'z'] documents: [
, rows: [
{id: 0, x: 1, y: 2, z: 3} {id: 0, x: 1, y: 2, z: 3}
, {id: 1, x: 2, y: 4, z: 6} , {id: 1, x: 2, y: 4, z: 6}
, {id: 2, x: 3, y: 6, z: 9} , {id: 2, x: 3, y: 6, z: 9}
@@ -19,23 +19,23 @@ test('new Dataset', function () {
, {id: 5, x: 6, y: 12, z: 18} , {id: 5, x: 6, y: 12, z: 18}
] ]
}; };
var dataset = new recline.Model.Dataset(metadata); var backend = new recline.Model.BackendMemory();
dataset.backendConfig = { backend.addDataset(inData);
type: 'memory' var dataset = new recline.Model.Dataset({id: datasetId}, backend);
// deep copy so we do not touch original data ... // ### Start testing
, data: $.extend(true, {}, indata)
};
expect(10); expect(10);
dataset.fetch().then(function(dataset) { // convenience for tests
equal(dataset.get('name'), metadata.name); var data = backend.datasets[datasetId];
deepEqual(dataset.get('headers'), indata.headers); dataset.fetch().then(function(datasetAgain) {
equal(dataset.get('name'), data.metadata.name);
deepEqual(dataset.get('headers'), data.metadata.headers);
equal(dataset.docCount, 6); equal(dataset.docCount, 6);
var queryObj = { var queryObj = {
size: 4 size: 4
, offset: 2 , offset: 2
}; };
dataset.query(queryObj).then(function(documentList) { dataset.query(queryObj).then(function(documentList) {
deepEqual(indata.rows[2], documentList.models[0].toJSON()); deepEqual(data.documents[2], documentList.models[0].toJSON());
}); });
var queryObj = { var queryObj = {
sort: [ sort: [
@@ -47,21 +47,21 @@ test('new Dataset', function () {
equal(doc0.x, 6); equal(doc0.x, 6);
}); });
dataset.query().then(function(docList) { dataset.query().then(function(docList) {
equal(docList.length, Math.min(100, indata.rows.length)); equal(docList.length, Math.min(100, data.documents.length));
var doc1 = docList.models[0]; var doc1 = docList.models[0];
deepEqual(doc1.toJSON(), indata.rows[0]); deepEqual(doc1.toJSON(), data.documents[0]);
// Test UPDATE // Test UPDATE
var newVal = 10; var newVal = 10;
doc1.set({x: newVal}); doc1.set({x: newVal});
doc1.save().then(function() { doc1.save().then(function() {
equal(dataset.backendConfig.data.rows[0].x, newVal); equal(data.documents[0].x, newVal);
}) })
// Test Delete // Test Delete
doc1.destroy().then(function() { doc1.destroy().then(function() {
equal(dataset.backendConfig.data.rows.length, 5); equal(data.documents.length, 5);
equal(dataset.backendConfig.data.rows[0].x, indata.rows[1].x); equal(data.documents[0].x, inData.documents[1].x);
}); });
}); });
}); });
@@ -144,12 +144,12 @@ webstoreData = {
}; };
test('Webstore Backend', function() { test('Webstore Backend', function() {
var dataset = new recline.Model.Dataset(); var dataset = new recline.Model.Dataset({
dataset.backendConfig = { id: 'my-id',
type: 'webstore', webstore_url: 'http://webstore.test.ckan.org/rufuspollock/demo/data'
url: 'http://webstore.test.ckan.org/rufuspollock/demo/data' },
}; 'webstore'
);
var stub = sinon.stub($, 'ajax', function(options) { var stub = sinon.stub($, 'ajax', function(options) {
if (options.url.indexOf('schema.json') != -1) { if (options.url.indexOf('schema.json') != -1) {
return { return {
@@ -175,10 +175,10 @@ test('Webstore Backend', function() {
dataset.fetch().done(function(dataset) { dataset.fetch().done(function(dataset) {
deepEqual(['__id__', 'date', 'geometry', 'amount'], dataset.get('headers')); deepEqual(['__id__', 'date', 'geometry', 'amount'], dataset.get('headers'));
equal(3, dataset.docCount) equal(3, dataset.docCount)
// dataset.query().done(function(docList) { dataset.query().done(function(docList) {
// equal(3, docList.length) equal(3, docList.length)
// equal("2009-01-01", docList.models[0].get('date')); equal("2009-01-01", docList.models[0].get('date'));
// }); });
}); });
$.ajax.restore(); $.ajax.restore();
}); });
@@ -250,11 +250,11 @@ var dataProxyData = {
test('DataProxy Backend', function() { test('DataProxy Backend', function() {
// needed only if not stubbing // needed only if not stubbing
// stop(); // stop();
var dataset = new recline.Model.Dataset(); var dataset = new recline.Model.Dataset({
dataset.backendConfig = {
type: 'dataproxy',
url: 'http://webstore.thedatahub.org/rufuspollock/gold_prices/data.csv' url: 'http://webstore.thedatahub.org/rufuspollock/gold_prices/data.csv'
}; },
'dataproxy'
);
var stub = sinon.stub($, 'ajax', function(options) { var stub = sinon.stub($, 'ajax', function(options) {
var partialUrl = 'jsonpdataproxy.appspot.com'; var partialUrl = 'jsonpdataproxy.appspot.com';
@@ -452,13 +452,11 @@ var sample_gdocs_spreadsheet_data = {
} }
test("GDoc Backend", function() { test("GDoc Backend", function() {
var dataset = new recline.Model.Dataset(); var dataset = new recline.Model.Dataset({
dataset.backendConfig = {
type: 'gdocs',
url: 'https://spreadsheets.google.com/feeds/list/0Aon3JiuouxLUdDQwZE1JdV94cUd6NWtuZ0IyWTBjLWc/od6/public/values?alt=json' url: 'https://spreadsheets.google.com/feeds/list/0Aon3JiuouxLUdDQwZE1JdV94cUd6NWtuZ0IyWTBjLWc/od6/public/values?alt=json'
}; },
'gdocs'
console.log('got gdoc dataset', dataset); );
var stub = sinon.stub($, 'getJSON', function(options, cb) { var stub = sinon.stub($, 'getJSON', function(options, cb) {
console.log('options are', options, cb); console.log('options are', options, cb);