Comparing version 0.0.7 to 0.0.8
/*jslint plusplus: true, devel: true, nomen: true, node: true, indent: 4, maxerr: 50 */ | ||
/*global require, exports, module */ | ||
module.exports = require("./lib/entree"); | ||
module.exports = require("./lib/entree"); |
@@ -5,10 +5,14 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, indent: 4, maxerr: 50 */ | ||
var Manager = require("./manager"); | ||
var Manager = require("./manager"), | ||
_ = require("lodash"); | ||
function createManger(opts, callback) { | ||
function createManager(opts, callback) { | ||
var man; | ||
if (!callback) { | ||
if (!callback && opts) { | ||
callback = opts; | ||
} | ||
if (!opts) { | ||
opts = {}; | ||
} | ||
if (opts.manager) { | ||
@@ -24,2 +28,57 @@ man = new (require(opts.manager))(opts); | ||
exports.createManager = createManger; | ||
function isInitialized() { | ||
return exports.manager instanceof Manager; | ||
} | ||
function init(opts, replace, done) { | ||
var k; | ||
if (_.isFunction(replace)) { | ||
done = replace; | ||
replace = false; | ||
} else if (_.isFunction(opts)) { | ||
done = opts; | ||
replace = undefined; | ||
opts = undefined; | ||
} | ||
if (_.isBoolean(opts)) { | ||
replace = opts; | ||
opts = undefined; | ||
} | ||
if (exports.manager) { | ||
if (!replace) { | ||
if (done) { | ||
done(); | ||
} | ||
return; | ||
} | ||
if (exports.dispose) { | ||
exports.dispose(); | ||
} | ||
for (k in exports) { | ||
delete exports[k]; | ||
} | ||
} | ||
createManager(opts, function (err, manager) { | ||
if (!err) { | ||
_.extend(exports, manager); | ||
_.mixin(exports, manager); | ||
exports.manager = manager; | ||
assign(); | ||
} | ||
if (done) { | ||
done(err); | ||
} | ||
}); | ||
} | ||
function assign() { | ||
exports.createManager = createManager; | ||
exports.isInitialized = isInitialized; | ||
exports.init = init; | ||
} | ||
assign(); |
@@ -20,37 +20,37 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, sloppy: true, indent: 4, maxerr: 50 */ | ||
this.config = opts.config; | ||
} else { | ||
var confArgs = [""], | ||
confName; | ||
return; | ||
} | ||
if (!opts.config) { | ||
confName = "./providers/file-system"; | ||
confArgs.push(path.join(process.cwd(), "data")); | ||
confArgs.push({ | ||
name: "config" | ||
}); | ||
} else { | ||
var confArgs = [""], | ||
confName; | ||
if (opts.config) { | ||
if (opts.config.modelDocument) { | ||
this.dataModelDoc = opts.config.modelDocument; | ||
} | ||
if (opts.config.provider) { | ||
confName = opts.config.provider; | ||
if (!_.isString(confName)) { | ||
throw new Error(Strings.MISSING_CONF_PROV); | ||
} | ||
if (opts.config.dataModel) { | ||
if (typeof opts.config.dataModel === "string") { | ||
this.dataModelDoc = opts.config.dataModel; | ||
} else { | ||
this.model = opts.config.dataModel; | ||
this.dataModelDoc = null; | ||
} | ||
} | ||
if (opts.config.connStr) { | ||
confArgs.push(opts.config.connStr); | ||
} | ||
if (opts.config.opts) { | ||
confArgs.push(opts.config.opts); | ||
} | ||
confArgs.push(opts.config.options); | ||
confArgs.push(opts.config.schema); | ||
} | ||
} | ||
var ConfProv = require(confName); | ||
this.config = new (Function.prototype.bind.apply(ConfProv, confArgs))(); | ||
this.providers.push(this.config); | ||
if (!confName) { | ||
confName = "./providers/file-system"; | ||
confArgs.push({ | ||
connStr: path.join(process.cwd(), "data") | ||
}); | ||
confArgs.push({ | ||
__collName: "config" | ||
}); | ||
} | ||
if (opts.model) { | ||
this.model = opts.model; | ||
} | ||
var ConfProv = require(confName); | ||
this.config = new (Function.prototype.bind.apply(ConfProv, confArgs))(); | ||
this.providers.push(this.config); | ||
} | ||
@@ -62,9 +62,14 @@ | ||
function initProviders() { | ||
if (!that.model.environments && !that.model.environments[that.env]) { | ||
return callback(new Error(util.format(Strings.MISSING_CONF_FOR_ENV, that.env))); | ||
var provs; | ||
if (that.model.environments && that.model.environments[that.env]) { | ||
provs = that.model.environments[that.env].providers; | ||
} | ||
var provs = that.model.environments[that.env].providers; | ||
if (!provs) { | ||
return callback(new Error(util.format(Strings.NO_PROVIDERS, that.env))); | ||
if (that.model.providers) { | ||
provs = that.model.providers; | ||
} else { | ||
return callback(new Error(util.format(Strings.NO_PROVIDERS, that.env))); | ||
} | ||
} | ||
@@ -84,3 +89,10 @@ | ||
function iterator2(coll, done2) { | ||
var prov = new Provider(conf.connStr, coll); | ||
var schema, prov; | ||
if (coll.schema && that.model.schema && that.model.schema[coll.schma]) { | ||
schema = that.model.schema[coll.schma]; | ||
schema.__collName = coll.name; | ||
} else { | ||
schema = { __collName: coll.name }; | ||
} | ||
prov = new Provider(conf.options, schema); | ||
prov.init(function (err) { | ||
@@ -87,0 +99,0 @@ if (!err) { |
@@ -101,5 +101,10 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, sloppy: true, es5: true, indent: 4, maxerr: 50 */ | ||
function Provider(connStr, options) { | ||
this.connectionString = connStr; | ||
this.options = options || {}; | ||
function Provider(options, schema) { | ||
if (!options || !options.connStr || !_.isString(options.connStr) || !schema || !schema.__collName || !_.isString(schema.__collName)) { | ||
throw new Error(Strings.MISSING_PROV_ARGS); | ||
} | ||
this.options = options; | ||
this.schema = schema; | ||
this._stack = []; | ||
@@ -192,3 +197,3 @@ } | ||
Provider.prototype._getIdKey = function () { | ||
return this.options.identifier || "_id"; | ||
return this.schema.identifier || "_id"; | ||
}; | ||
@@ -195,0 +200,0 @@ |
@@ -163,19 +163,12 @@ /*jslint node: true, plusplus: true, devel: true, nomen: true, vars: true, es5: true, indent: 4, maxerr: 50 */ | ||
function EverliveProvider(connStr, options) { | ||
var path = connStr; | ||
function EverliveProvider(opts, schema) { | ||
Provider.call(this, opts, schema); | ||
if (!connStr || !_.isString(connStr)) { | ||
throw new Error(Strings.MISSING_CONN_STR); | ||
} | ||
var path = url.resolve(opts.connStr, schema.__collName); | ||
if (options) { | ||
if (options.name) { | ||
path = url.resolve(path, options.name); | ||
} | ||
this._reqOpts = url.parse(path); | ||
this._reqOpts.headers = { "Content-Type": "application/json" }; | ||
this._reqOpts = url.parse(path); | ||
this._reqOpts.headers = { "Content-Type": "application/json" }; | ||
if (options.authorization) { | ||
this._reqOpts.headers.Authorization = options.authorization; | ||
} | ||
if (opts.authorization) { | ||
this._reqOpts.headers.Authorization = opts.authorization; | ||
} | ||
@@ -188,4 +181,2 @@ | ||
} | ||
Provider.call(this, connStr, options); | ||
} | ||
@@ -192,0 +183,0 @@ |
@@ -149,20 +149,15 @@ /*jslint node: true, plusplus: true, devel: true, nomen: true, vars: true, indent: 4, maxerr: 50 */ | ||
function FsProvider(connStr, options) { | ||
var orgCwd; | ||
function FsProvider(opts, schema) { | ||
Provider.call(this, opts, schema); | ||
connStr = path.resolve(process.cwd(), connStr); | ||
if (options && options.name) { | ||
this.dir = path.join(connStr, options.name); | ||
} else { | ||
this.dir = connStr; | ||
} | ||
var orgCwd, | ||
connStr = path.resolve(process.cwd(), opts.connStr); | ||
this.dir = path.join(connStr, schema.__collName); | ||
if (!fs.existsSync(this.dir)) { | ||
orgCwd = process.cwd(); | ||
process.chdir(connStr); | ||
fs.mkdirSync(options.name); | ||
fs.mkdirSync(schema.__collName); | ||
process.chdir(orgCwd); | ||
} | ||
Provider.call(this, connStr, options); | ||
} | ||
@@ -169,0 +164,0 @@ |
@@ -15,4 +15,4 @@ /*jslint node: true, plusplus: true, devel: true, nomen: true, vars: true, es5: true, indent: 4, maxerr: 50 */ | ||
function getCollection(prov, callback) { | ||
var connStr = prov.connectionString, | ||
collName = prov.options.name, | ||
var connStr = prov.options.connStr, | ||
collName = prov.schema.__collName, | ||
mdb = _conns[connStr]; | ||
@@ -39,6 +39,6 @@ | ||
function removeReference(prov) { | ||
var db = _conns[prov.connectionString]; | ||
var db = _conns[prov.options.connStr]; | ||
delete db.__prefs[prov._uuid]; | ||
if (!_.keys(db.__prefs).length) { | ||
delete _conns[prov.connectionString]; | ||
delete _conns[prov.options.connStr]; | ||
db.close(); | ||
@@ -250,14 +250,10 @@ } | ||
function MongoProvider(connStr, options) { | ||
if (!connStr || !_.isString(connStr)) { | ||
throw new Error(Strings.MISSING_CONN_STR); | ||
} | ||
function MongoProvider(opts, schema) { | ||
Provider.call(this, opts, schema); | ||
this._uuid = uuid.v1(); | ||
if (!options.safeMode) { | ||
options.safeMode = 1; | ||
if (!this.options.safeMode) { | ||
this.options.safeMode = 1; | ||
} | ||
Provider.call(this, connStr, options); | ||
} | ||
@@ -264,0 +260,0 @@ |
@@ -22,4 +22,5 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, indent: 4, maxerr: 50 */ | ||
"REQUIRED_CALLBACK" : "Callback function is required.", | ||
"MISSING_PROV_ARGS" : "Missing provider arguments. The minimum required arguments are: Provider({ connStr: \"someConnection\" }, { __collName: \"collectionName\" });" | ||
}; | ||
module.exports = strings; |
{ | ||
"name" : "entree", | ||
"description" : "Data provider model abstraction.", | ||
"version" : "0.0.7", | ||
"version" : "0.0.8", | ||
"main" : "index", | ||
@@ -6,0 +6,0 @@ "scripts" : { |
@@ -123,3 +123,3 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, es5: true, indent: 4, maxerr: 50 */ | ||
exports.getTestCase = function (Provider, connStr, options, messages, init) { | ||
exports.getTestCase = function (Provider, options, schema, messages, init) { | ||
if (!_.isFunction(Provider)) { | ||
@@ -142,3 +142,3 @@ Provider = require(Provider); | ||
provider = new Provider(connStr, options); | ||
provider = new Provider(options, schema); | ||
@@ -157,3 +157,3 @@ context = provider.createContext({ | ||
test.equal(provider.connectionString, connStr); | ||
test.equal(provider.options.connStr, options.connStr); | ||
test.equal(provider.options, options); | ||
@@ -160,0 +160,0 @@ |
@@ -6,15 +6,19 @@ { | ||
{ | ||
"provider": "../lib/providers/file-system", | ||
"connStr": "./data", | ||
"provider": "file-system", | ||
"options": { | ||
"connStr": "./data" | ||
}, | ||
"collections": [ | ||
{ "name": "blogs", "identifier": "_id" }, | ||
{ "name": "posts", "identifier": "_id" }, | ||
{ "name": "users", "identifier": "_id" } | ||
{ "name": "blogs", "shecma": "blog" }, | ||
{ "name": "posts", "shecma": "post" }, | ||
{ "name": "users", "shecma": "user" } | ||
] | ||
}, | ||
{ | ||
"provider": "../lib/providers/mongodb", | ||
"connStr": "mongodb://localhost/entreeTest", | ||
"provider": "mongodb", | ||
"options": { | ||
"connStr": "mongodb://localhost/entreeTest" | ||
}, | ||
"collections": [ | ||
{ "name": "comments", "identifier": "_id" } | ||
{ "name": "comments" } | ||
] | ||
@@ -24,3 +28,11 @@ } | ||
} | ||
}, | ||
"schema": { | ||
"blog": { | ||
"identifier": "_id" | ||
}, | ||
"post": { | ||
"identifier": "_id" | ||
} | ||
} | ||
} |
@@ -6,14 +6,16 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, indent: 4, maxerr: 50 */ | ||
var url = require("url"), | ||
http = require("http"), | ||
http = require("http"), | ||
testCase = require("./common-provider"), | ||
provider = "../lib/providers/everlive", | ||
connStr = "http://api.everlive.com/v1/uZEGyZYKiSq5CTSq/", | ||
options = { | ||
name: "blogs", | ||
identifier: "_id"//, | ||
options = { | ||
connStr: "http://api.everlive.com/v1/uZEGyZYKiSq5CTSq/"//, | ||
//authorization: "MasterKey PqmmvlWWBF5svReW7p3mkYG9X61nus1w" | ||
}, | ||
schema = { | ||
__collName: "blogs", | ||
identifier: "_id" | ||
}; | ||
function init(callback) { | ||
var opts = url.parse(url.resolve(connStr, options.name)), | ||
var opts = url.parse(url.resolve(options.connStr, schema.__collName)), | ||
req; | ||
@@ -52,2 +54,2 @@ | ||
module.exports = testCase.getTestCase(provider, connStr, options, { item_doesnt_exist: "{\"message\":\"Item not found.\",\"errorCode\":801}" }, init); | ||
module.exports = testCase.getTestCase(provider, options, schema, { item_doesnt_exist: "{\"message\":\"Item not found.\",\"errorCode\":801}" }, init); |
/*jslint node: true, plusplus: true, devel: true, nomen: true, vars: true, indent: 4, maxerr: 50 */ | ||
"use strict"; | ||
debugger; | ||
var testCase = require("nodeunit").testCase, | ||
entree = require("../lib/entree"), | ||
path = require("path"), | ||
fsPath = path.resolve(process.cwd(), "./data"), | ||
manager; | ||
module.exports = testCase({ | ||
"Fixture Setup": function (test) { | ||
"Create Manger": function (test) { | ||
test.expect(11); | ||
var fsPath = path.resolve(process.cwd(), "./data"); | ||
entree.createManager(function (err, man) { | ||
@@ -24,10 +24,57 @@ manager = man; | ||
test.ok(manager.users); | ||
test.equal(manager.blogs.connectionString, fsPath); | ||
test.equal(manager.posts.connectionString, fsPath); | ||
test.equal(manager.comments.connectionString, "mongodb://localhost/entreeTest"); | ||
test.equal(manager.users.connectionString, fsPath); | ||
test.equal(manager.blogs.dir, fsPath + "/blogs"); | ||
test.equal(manager.posts.dir, fsPath + "/posts"); | ||
test.equal(manager.comments.options.connStr, "mongodb://localhost/entreeTest"); | ||
test.equal(manager.users.dir, fsPath + "/users"); | ||
test.done(); | ||
}); | ||
}, | ||
"Fixture Tear Down": function (test) { | ||
"Init Entree Without Options": function (test) { | ||
test.expect(11); | ||
entree.init(function (err) { | ||
test.ok(!err); | ||
test.equal(entree.providers.length, 5); | ||
test.ok(entree.config); | ||
test.ok(entree.blogs); | ||
test.ok(entree.posts); | ||
test.ok(entree.comments); | ||
test.ok(entree.users); | ||
test.equal(entree.blogs.dir, fsPath + "/blogs"); | ||
test.equal(entree.posts.dir, fsPath + "/posts"); | ||
test.equal(entree.comments.options.connStr, "mongodb://localhost/entreeTest"); | ||
test.equal(entree.users.dir, fsPath + "/users"); | ||
test.done(); | ||
}); | ||
}, | ||
"Replace Entree With Options": function (test) { | ||
test.expect(5); | ||
var opts = { | ||
model: { | ||
providers: [ | ||
{ | ||
provider: "file-system", | ||
options: { | ||
connStr: "./data" | ||
}, | ||
collections: [ | ||
{ name: "foo" } | ||
] | ||
} | ||
] | ||
} | ||
}; | ||
entree.init(opts, true, function (err) { | ||
test.ok(!err); | ||
test.equal(entree.providers.length, 2); | ||
test.ok(entree.config); | ||
test.ok(entree.foo); | ||
test.equal(entree.foo.dir, fsPath + "/foo"); | ||
entree.dispose(); | ||
test.done(); | ||
}); | ||
}, | ||
"Dispose Manger": function (test) { | ||
test.expect(0); | ||
@@ -34,0 +81,0 @@ manager.dispose(function (err) { |
@@ -28,7 +28,7 @@ /*jslint node: true, plusplus: true, devel: true, nomen: true, vars: true, es5: true, indent: 4, maxerr: 50 */ | ||
var connStr = __dirname + "/data", | ||
options = { name: "blogs" }; | ||
var options = { connStr: __dirname + "/data" }, | ||
schema = { __collName: "blogs" }; | ||
function createProvider() { | ||
blogs = new Provider(connStr, options); | ||
blogs = new Provider(options, schema); | ||
@@ -47,4 +47,4 @@ context = blogs.createContext({ | ||
test.equal(blogs.connectionString, connStr); | ||
test.equal(blogs.options, options); | ||
test.equal(blogs.options.connStr, options.connStr); | ||
test.equal(blogs.schema, schema); | ||
@@ -54,7 +54,7 @@ test.done(); | ||
fs.exists(connStr, function (exists) { | ||
fs.exists(options.connStr, function (exists) { | ||
if (exists) { | ||
createProvider(); | ||
} else { | ||
fs.mkdir(connStr, function (err) { | ||
fs.mkdir(options.connStr, function (err) { | ||
if (err) { | ||
@@ -61,0 +61,0 @@ throw err; |
@@ -7,10 +7,12 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, indent: 4, maxerr: 50 */ | ||
provider = "../lib/providers/file-system", | ||
connStr = __dirname + "/data", | ||
options = { name: "blogs", identifier: "_id", option: 1, foo: "bar" }, | ||
options = { | ||
connStr: __dirname + "/data" | ||
}, | ||
schema = { __collName: "blogs", identifier: "_id", option: 1, foo: "bar" }, | ||
wrench = require("wrench"), | ||
fs = require("fs"), | ||
path = connStr + "/blogs"; | ||
path = options.connStr + "/blogs"; | ||
if (!fs.existsSync(connStr)) { | ||
fs.mkdirSync(connStr); | ||
if (!fs.existsSync(options.connStr)) { | ||
fs.mkdirSync(options.connStr); | ||
} else if (fs.existsSync(path)) { | ||
@@ -20,2 +22,2 @@ wrench.rmdirSyncRecursive(path); | ||
module.exports = testCase.getTestCase(provider, connStr, options); | ||
module.exports = testCase.getTestCase(provider, options, schema); |
@@ -24,6 +24,6 @@ /*jslint node: true, plusplus: true, devel: true, nomen: true, vars: true, indent: 4, maxerr: 50 */ | ||
test.ok(manager.users); | ||
test.equal(manager.blogs.connectionString, fsPath); | ||
test.equal(manager.posts.connectionString, fsPath); | ||
test.equal(manager.comments.connectionString, "mongodb://localhost/entreeTest"); | ||
test.equal(manager.users.connectionString, fsPath); | ||
test.equal(manager.blogs.dir, fsPath + "/blogs"); | ||
test.equal(manager.posts.dir, fsPath + "/posts"); | ||
test.equal(manager.comments.options.connStr, "mongodb://localhost/entreeTest"); | ||
test.equal(manager.users.dir, fsPath + "/users"); | ||
test.done(); | ||
@@ -30,0 +30,0 @@ }); |
@@ -7,5 +7,7 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, indent: 4, maxerr: 50 */ | ||
provider = "../lib/providers/mongodb", | ||
connStr = "mongodb://localhost/entreeTest", | ||
options = { | ||
name: "blogs", | ||
connStr: "mongodb://localhost/entreeTest" | ||
}, | ||
schema = { | ||
__collName: "blogs", | ||
identifier: "_id" | ||
@@ -20,7 +22,7 @@ }, | ||
MongoClient.connect(connStr, function (err, db) { | ||
MongoClient.connect(options.connStr, function (err, db) { | ||
if (err) { | ||
throw err; | ||
} | ||
db.collection(options.name).drop(function (err) { | ||
db.collection(schema.__collName).drop(function (err) { | ||
db.close(); | ||
@@ -35,2 +37,2 @@ if (err && err.message !== "ns not found") { | ||
module.exports = testCase.getTestCase(provider, connStr, options, messages, init); | ||
module.exports = testCase.getTestCase(provider, options, schema, messages, init); |
@@ -12,4 +12,4 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, sloppy: true, indent: 4, maxerr: 50 */ | ||
function PostProvider(connStr, options) { | ||
Provider.call(this, connStr, options); | ||
function PostProvider(options, schema) { | ||
Provider.call(this, options, schema); | ||
this.store = []; | ||
@@ -29,3 +29,3 @@ this.sync = false; | ||
id = uuid.v1(); | ||
item[that.options.identifier] = id; | ||
item[that.schema.identifier] = id; | ||
} | ||
@@ -61,3 +61,3 @@ | ||
id = uuid.v1(); | ||
item[this.options.identifier] = id; | ||
item[this.schema.identifier] = id; | ||
} | ||
@@ -64,0 +64,0 @@ |
@@ -7,5 +7,7 @@ /*jslint plusplus: true, devel: true, nomen: true, vars: true, node: true, indent: 4, maxerr: 50 */ | ||
provider = "./mocks/post-provider", | ||
connStr = "test connection string", | ||
options = { identifier: "_id", option: 1, foo: "bar" }; | ||
options = { | ||
connStr: "test connection string" | ||
}, | ||
schema = { __collName: "test", identifier: "_id", option: 1, foo: "bar" }; | ||
module.exports = testCase.getTestCase(provider, connStr, options); | ||
module.exports = testCase.getTestCase(provider, options, schema); |
101361
27
3123