Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Get model loading working

  • Loading branch information...
commit e7d25a287c2cc69a8ab890c61ced870985860506 1 parent 4d11e74
mde authored
View
26 lib/adapters/base_adapter.js
@@ -0,0 +1,26 @@
+var BaseAdapter
+ , EventEmitter = require('events').EventEmitter
+ , adapter = require('./index')
+ , utils = require('utilities');
+
+BaseAdapter = function () {
+};
+
+BaseAdapter.prototype = new EventEmitter();
+utils.mixin(BaseAdapter.prototype, new (function () {
+
+ this.getAppConfig = function (dbConfig) {
+ var info;
+ for (var p in dbConfig) {
+ // Return the first alias key recognized whose
+ // canonical name is the same
+ info = adapter.getAdapterInfo(p);
+ if (info && info.name == this.name) {
+ return dbConfig[p];
+ }
+ }
+ };
+
+})());
+
+module.exports.BaseAdapter = BaseAdapter;
View
33 lib/adapters/index.js
@@ -1,24 +1,27 @@
var adapters
- , path = require('path');
+ , path = require('path')
+ , _aliases
+ , _paths;
+
+_aliases = {
+ postgres: 'postgres'
+, pg: 'postgres'
+, postgresql: 'postgres'
+};
+
+_paths = {
+ postgres: 'sql/postgres'
+};
adapters = new (function () {
this.getAdapterInfo = function (adapter) {
- var ret;
- switch (adapter) {
- case 'postgres':
- case 'postgresql':
- case 'pg':
- ret = {
- name: 'Adapter'
- , filePath: path.join('sql', 'postgres')
- };
- break;
- default:
- ret = null;
- }
- return ret;
+ var canonical = _aliases[adapter];
+ return {
+ name: canonical
+ , filePath: _paths[canonical]
+ };
};
})();
View
4 lib/adapters/sql/base.js
@@ -1,12 +1,12 @@
var Adapter
+ , BaseAdapter = require('../base_adapter').BaseAdapter
, datatypes = require('../../datatypes')
- , EventEmitter = require('events').EventEmitter
, utils = require('utilities');
Adapter = function () {
};
-Adapter.prototype = new EventEmitter();
+Adapter.prototype = new BaseAdapter();
utils.mixin(Adapter.prototype, new (function () {
this._tableizeModelName = function (name) {
View
26 lib/adapters/sql/postgres.js
@@ -1,5 +1,6 @@
var pg = require('pg')
+ , generator = require('../../../lib/generators/sql')
, utils = require('utilities')
, model = require('../../../lib')
, Query = require('../../../lib/query/query').Query
@@ -7,21 +8,24 @@ var pg = require('pg')
, Adapter
, _baseConfig;
-
_baseConfig = {
user: process.env.USER
, database: process.env.USER
, password: null
, port: 5432
, host: null
+, autoConnect: true
};
Adapter = function (options) {
- var opts = options || {};
+ var opts = options || {}
+ , config;
+
+ this.name = 'postgres';
this.config = _baseConfig;
this.client = null;
- utils.mixin(this.config, opts);
+ utils.mixin(this.config, this.getAppConfig(opts));
this.init.apply(this, arguments);
};
@@ -145,10 +149,9 @@ utils.mixin(Adapter.prototype, new (function () {
this.init = function () {
this.client = new pg.Client(this.config);
-
- this.client.on('error', function(error) {
- console.log(error);
- });
+ if (this.config.autoConnect) {
+ this.connect();
+ }
};
this.connect = function () {
@@ -200,6 +203,10 @@ utils.mixin(Adapter.prototype, new (function () {
inst.saved = true;
res.push(inst);
});
+ // `load` method
+ if (query.opts.limit == 1) {
+ res = res[0];
+ }
callback(null, res);
}
});
@@ -264,6 +271,11 @@ utils.mixin(Adapter.prototype, new (function () {
};
+ this.createTable = function (names, callback) {
+ var sql = generator.createTable(names);
+ this.exec(sql, callback);
+ };
+
})());
module.exports.Adapter = Adapter;
View
6 lib/generators/sql.js
@@ -48,8 +48,10 @@ generator = new (function () {
this.createTable = function (modelNames) {
var sql = ''
, reg = model.descriptionRegistry
- , props;
- modelNames.forEach(function (name) {
+ , props
+ , names = Array.isArray(modelNames) ?
+ modelNames : [modelNames];
+ names.forEach(function (name) {
props = reg[name].properties;
sql += _createTable(name, props);
});
View
1  lib/index.js
@@ -52,6 +52,7 @@ utils.mixin(model, new (function () {
this.validators = require('./validators.js');
this.formatters = require('./formatters.js');
this.adapters = {};
+ this.loadedAdapters = {};
this.descriptionRegistry = {};
this.useTimestamps = false;
this.forceCamel = true;
View
1  test/adapters/sql/postgres.js
@@ -26,6 +26,7 @@ tests = {
adapter = new Adapter({
database: 'model_test'
+ , autoConnect: false
});
sql = generator.createTable(['Zooby']);
adapter.once('connect', function () {
Please sign in to comment.
Something went wrong with that request. Please try again.