var safeRequire = require('../utils').safeRequire; /** * Module dependencies */ var mongodb = safeRequire('mongodb'); var ObjectID = mongodb.ObjectID; exports.initialize = function initializeSchema(schema, callback) { if (!mongodb) return; var s = schema.settings; if (schema.settings.rs) { s.rs = schema.settings.rs; if (schema.settings.url) { var uris = schema.settings.url.split(','); s.hosts = [] s.ports = [] uris.forEach(function(uri) { var url = require('url').parse(uri); s.hosts.push(url.hostname || 'localhost'); s.ports.push(parseInt(url.port || '27017', 10)); if (!s.database) s.database = url.pathname.replace(/^\//, ''); if (!s.username) s.username = url.auth && url.auth.split(':')[0]; if (!s.password) s.password = url.auth && url.auth.split(':')[1]; }); } s.database = s.database || 'test'; } else { if (schema.settings.url) { var url = require('url').parse(schema.settings.url); s.host = url.hostname; s.port = url.port; s.database = url.pathname.replace(/^\//, ''); s.username = url.auth && url.auth.split(':')[0]; s.password = url.auth && url.auth.split(':')[1]; } s.host = s.host || 'localhost'; s.port = parseInt(s.port || '27017', 10); s.database = s.database || 'test'; } schema.adapter = new MongoDB(s, schema, callback); }; function MongoDB(s, schema, callback) { this._models = {}; this.collections = {}; var server; if (s.rs) { set = []; for(i=0, n=s.hosts.length; i