Skip to content

Commit

Permalink
CLosed #10: Builds adaptable database layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson Gariety committed Feb 16, 2014
1 parent 12a3b31 commit 506b470
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 80 deletions.
1 change: 1 addition & 0 deletions examples/mustache-example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var Sara = require('../../lib/sara')

// Our app
var TodoList = module.exports = new Sara()
.storage(require('../../lib/adapters/mongodb'))
.layout('./templates/layout.html')
.initialize(function () {
var Todo = require('./models/todo')
Expand Down
9 changes: 4 additions & 5 deletions examples/mustache-example/views/todo.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
var TodoList = require('../app')
, Mustache = require('mustache')
, TodoController = require('../controllers/todo')
, _ = require('../../../lib/sara').Utils
, Todo = require('../models/todo')

var TodoView = module.exports = new TodoList.View('Todo', {
template: TodoList.template('todo', '../templates/todo.html')
, render: function (document) {
var Mustache = require('mustache')
, TodoController = require('../controllers/todo')
, _ = require('../../../lib/sara').Utils
, Todo = require('../models/todo')

Todo.all().on('add remove changeAny', render.bind(this))
render.bind(this)()

Expand Down
29 changes: 29 additions & 0 deletions lib/adapters/localstorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*!
*
* A localStorage database adapter for Sara
*/

var _ = require('../sara/utils')

module.exports = function adapterLocalStorage(Constructor, callback) {
callback({
find: function (query, callback) {
if (callback) {
var bootstrap = document.querySelectorAll('[type="text/json"]')
callback(null, _.retrocycle(JSON.parse(bootstrap[0].innerHTML)))
}
}

, insert: function (props, callback) {
if (callback) callback(null, [])
}

, remove: function (query, callback) {
if (callback) callback(null, [])
}

, update: function (query, update, options, callback) {
if (callback) callback(null, [])
}
})
}
45 changes: 45 additions & 0 deletions lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
*
* MONGODB SARA ADAPTER
*
*/

var MongoClient = require('mongodb').MongoClient

module.exports = function (Constructor, callback) {
MongoClient.connect('mongodb://127.0.0.1:27017/' + Constructor.name, function (err, db) {
if (err) throw err
var collection = db.collection(Constructor.name)

callback({
find: function (query, callback) {
if (callback) {
collection.find(query, function (err, docs) {
if (err) callback(err, null)
else {
docs.toArray(function (err, docs) {
callback(err, docs)
})
}
})
}
}

, insert: function (props, callback) {
if (callback) collection.insert(props, function (err, docs) {
callback(err, docs)
})
}

, remove: function (query, callback) {
if (callback) collection.remove(query, {}, callback)
}

, update: function (query, update, options, callback) {
if (callback) collection.update(query, update, options, function (err) {
if (err) throw err
})
}
})
})
}
9 changes: 9 additions & 0 deletions lib/adapters/nedb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*!
* SARA-NEDB ADAPTER
*/

var NeDB = require('nedb')

module.exports = function (Constructor) {
return new NeDB({ filename: Constructor.app.root + '/' + Constructor.name.toLowerCase() + '.db', autoload: true })
}
4 changes: 2 additions & 2 deletions lib/sara.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ _.class(Sara)
*
* FIXME: Adapters should be app-specific, not process-specific. This should really be a method.
*/
.add('adapter', function (adapter) {
_(this).extend(adapter)
.method('storage', function (adapter) {
this.adapter = adapter
return this
})

Expand Down
4 changes: 4 additions & 0 deletions lib/sara/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ module.exports = function Client(app) {
new app.Event(e)
}

app.socket.onclose = function () {

}

this.tryInitRouter = function () {
if (_.every(app.dbstatus)) {
app.paths[window.location.pathname]()
Expand Down
37 changes: 0 additions & 37 deletions lib/sara/datastore.js

This file was deleted.

81 changes: 46 additions & 35 deletions lib/sara/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

var _ = require('./utils')
, Collection = require('./collection')
, Datastore = require('./datastore')
, adapterNeDB = require('../adapters/nedb')
, adapterLocalStorage = require('../adapters/localstorage')
, stream = require('stream')
, Readable = stream.Readable
, Writeable = stream.Writeable
Expand Down Expand Up @@ -37,12 +38,38 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize
if (initialize) initialize();
})

// Our storage
var db = new Datastore(Constructor)
Constructor.app = app
app.resources[name] = Constructor
app.dbstatus[name] = false

// Start the DB
var adapterServer = app.adapter || adapterNeDB
, db

if (IS_SERVER) adapterServer(Constructor, callback)
else adapterLocalStorage(Constructor, callback)

function callback(datastore) {
db = datastore

// Load the db
setTimeout(function () {
db.find({}, function (err, docs) {
if (err) throw err
if (docs.length) {
docs.forEach(function (doc) {
new Constructor(doc).push()
})
}

app.dbstatus[name] = true
// FIXME: awful hack on next line as well
if (app.local && IS_CLIENT) app.local.tryInitRouter()
})
})
}

// JSON endpoints
app.get('/' + name.toLowerCase() + '.json', function () {
return Constructor.toJSON()
})
Expand All @@ -67,23 +94,6 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize

})

// A zero timeout ensures the Contructor has finished being created before the db call is made
// FIXME: worst hack in all of Sara
setTimeout(function () {
// Load the db
db.find({}, function (err, docs) {
if (err) throw err

if (docs.length) {
docs.forEach(function (doc) {
new Constructor(doc).push()
})
}
this.constructor.app.dbstatus[name] = true
if (this.constructor.app.local && IS_CLIENT) this.constructor.app.local.tryInitRouter()
}.bind(this))
}.bind(this))

// Include _.method and _.add
return _.class(Constructor)

Expand Down Expand Up @@ -173,20 +183,19 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize
}

// Write to disk
db.find({ id: this.id() }
, function (err, docs) {
if (err) throw err

if (!docs.length) {
db.insert(this.data(), function () {
if (err) throw err
})
} else {
db.update({ id: this.id() }, this.data(), function () {
if (err) throw err
})
}
}.bind(this))
db.find({ id: this.id() }, function (err, docs) {
if (err) throw err

if (!docs.length) {
db.insert(this.data(), function (err, docs) {
if (err) throw err
})
} else {
db.update({ id: this.id() }, this.data(), {}, function () {
if (err) throw err
})
}
}.bind(this))
})

/**
Expand All @@ -197,7 +206,9 @@ var Model = module.exports = (function ModelConstructor(name, schema, initialize
}).method(function set(name, value, forward) {
if (forward !== false) forward = true
this[name] = value
db.update({ _id: this._id }, this.data())
db.update({ _id: this._id }, this.data(), {}, function () {

})
this.constructor.all().trigger('changeAny')

if (forward) {
Expand Down
2 changes: 2 additions & 0 deletions lib/sara/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ var Server = module.exports = (function Server(app) {
app.bundle.ignore('jsdom')
app.bundle.ignore('nedb')
app.bundle.ignore('fs')
app.bundle.ignore('nedb')
app.bundle.ignore('mongodb')
app.bundle.add(path.join(__dirname, '../sara')) // Compile Sara
app.bundle.add(require.main.filename) // Compile the app
app.clientId = 0
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"domready": "~0.2.13",
"contextify": "~0.1.6",
"require-like": "~0.1.2",
"through": "~2.3.4"
"through": "~2.3.4",
"mongodb": "~1.3.23"
},
"devDependencies": {
"gulp": "3.2.4",
Expand Down

0 comments on commit 506b470

Please sign in to comment.