Rename schema/adapter to be dataSource/connector
This commit is contained in:
parent
4bedf2200c
commit
382f99b9c6
30
lib/mysql.js
30
lib/mysql.js
|
@ -6,10 +6,10 @@ var mysql = require('mysql');
|
|||
var jdb = require('loopback-data');
|
||||
var EnumFactory = require('./enumFactory').EnumFactory;
|
||||
|
||||
exports.initialize = function initializeSchema(schema, callback) {
|
||||
exports.initialize = function initializeDataSource(dataSource, callback) {
|
||||
if (!mysql) return;
|
||||
|
||||
var s = schema.settings;
|
||||
var s = dataSource.settings;
|
||||
|
||||
if (s.collation) {
|
||||
s.charset = s.collation.substr(0,s.collation.indexOf('_')); // Charset should be first 'chunk' of collation.
|
||||
|
@ -21,7 +21,7 @@ exports.initialize = function initializeSchema(schema, callback) {
|
|||
s.supportBigNumbers = (s.supportBigNumbers || false);
|
||||
s.timezone = (s.timezone || 'local');
|
||||
|
||||
schema.client = mysql.createConnection({
|
||||
dataSource.client = mysql.createConnection({
|
||||
host: s.host || 'localhost',
|
||||
port: s.port || 3306,
|
||||
user: s.username,
|
||||
|
@ -33,23 +33,23 @@ exports.initialize = function initializeSchema(schema, callback) {
|
|||
supportBigNumbers: s.supportBigNumbers
|
||||
});
|
||||
|
||||
schema.client.on('error', function (err) {
|
||||
schema.emit('error', err);
|
||||
dataSource.client.on('error', function (err) {
|
||||
dataSource.emit('error', err);
|
||||
});
|
||||
|
||||
schema.adapter = new MySQL(schema.client);
|
||||
schema.adapter.schema = schema;
|
||||
dataSource.connector = new MySQL(dataSource.client);
|
||||
dataSource.connector.dataSource = dataSource;
|
||||
|
||||
schema.client.query('USE `' + s.database + '`', function (err) {
|
||||
dataSource.client.query('USE `' + s.database + '`', function (err) {
|
||||
if (err) {
|
||||
if (err.message.match(/(^|: )unknown database/i)) {
|
||||
var dbName = s.database;
|
||||
var charset = s.charset;
|
||||
var collation = s.collation;
|
||||
var q = 'CREATE DATABASE ' + dbName + ' CHARACTER SET ' + charset + ' COLLATE ' + collation;
|
||||
schema.client.query(q, function (error) {
|
||||
dataSource.client.query(q, function (error) {
|
||||
if (!error) {
|
||||
schema.client.query('USE ' + s.database, callback);
|
||||
dataSource.client.query('USE ' + s.database, callback);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
|
@ -59,9 +59,9 @@ exports.initialize = function initializeSchema(schema, callback) {
|
|||
});
|
||||
|
||||
// MySQL specific column types
|
||||
schema.constructor.registerType(function Point() {});
|
||||
dataSource.constructor.registerType(function Point() {});
|
||||
|
||||
schema.EnumFactory = EnumFactory; // factory for Enums. Note that currently Enums can not be registered.
|
||||
dataSource.EnumFactory = EnumFactory; // factory for Enums. Note that currently Enums can not be registered.
|
||||
|
||||
|
||||
};
|
||||
|
@ -69,7 +69,7 @@ exports.initialize = function initializeSchema(schema, callback) {
|
|||
exports.MySQL = MySQL;
|
||||
|
||||
/**
|
||||
* MySQL adapter
|
||||
* MySQL connector
|
||||
*/
|
||||
function MySQL(client) {
|
||||
this.name = 'mysql';
|
||||
|
@ -80,8 +80,8 @@ function MySQL(client) {
|
|||
require('util').inherits(MySQL, jdb.BaseSQL);
|
||||
|
||||
MySQL.prototype.query = function (sql, callback) {
|
||||
if (!this.schema.connected) {
|
||||
return this.schema.on('connected', function () {
|
||||
if (!this.dataSource.connected) {
|
||||
return this.dataSource.on('connected', function () {
|
||||
this.query(sql, callback);
|
||||
}.bind(this));
|
||||
}
|
||||
|
|
|
@ -7,13 +7,13 @@ DBUSER = 'strongloop'
|
|||
DBPASS = 'password'
|
||||
DBENGINE = 'mysql'
|
||||
|
||||
schema = new Schema __dirname + '/..', database: '', username: DBUSER, password: DBPASS
|
||||
schema.log = (q) -> console.log q
|
||||
dataSource = new Schema __dirname + '/..', database: '', username: DBUSER, password: DBPASS
|
||||
dataSource.log = (q) -> console.log q
|
||||
|
||||
query = (sql, cb) ->
|
||||
schema.adapter.query sql, cb
|
||||
dataSource.adapter.query sql, cb
|
||||
|
||||
User = schema.define 'User',
|
||||
User = dataSource.define 'User',
|
||||
email: { type: String, null: false, index: true }
|
||||
name: String
|
||||
bio: Text
|
||||
|
@ -26,7 +26,7 @@ User = schema.define 'User',
|
|||
columns: 'email, createdByAdmin'
|
||||
|
||||
withBlankDatabase = (cb) ->
|
||||
db = schema.settings.database = DBNAME
|
||||
db = dataSource.settings.database = DBNAME
|
||||
query 'DROP DATABASE IF EXISTS ' + db, (err) ->
|
||||
query 'CREATE DATABASE ' + db, (err) ->
|
||||
query 'USE '+ db, cb
|
||||
|
@ -57,7 +57,7 @@ it = (name, testCases) ->
|
|||
|
||||
it 'should run migration', (test) ->
|
||||
withBlankDatabase (err) ->
|
||||
schema.automigrate ->
|
||||
dataSource.automigrate ->
|
||||
getFields 'User', (err, fields) ->
|
||||
test.deepEqual fields,
|
||||
id:
|
||||
|
@ -176,7 +176,7 @@ it 'should autoupgrade', (test) ->
|
|||
User.defineProperty 'name', type: String, limit: 50
|
||||
User.defineProperty 'newProperty', type: Number
|
||||
User.defineProperty 'pendingPeriod', false
|
||||
schema.autoupdate (err) ->
|
||||
dataSource.autoupdate (err) ->
|
||||
getFields 'User', (err, fields) ->
|
||||
# change nullable for email
|
||||
test.equal fields.email.Null, 'YES', 'Email is not null'
|
||||
|
@ -194,18 +194,18 @@ it 'should autoupgrade', (test) ->
|
|||
test.ok yep
|
||||
test.done()
|
||||
|
||||
it 'should check actuality of schema', (test) ->
|
||||
it 'should check actuality of dataSource', (test) ->
|
||||
# drop column
|
||||
User.schema.isActual (err, ok) ->
|
||||
test.ok ok, 'schema is actual'
|
||||
User.dataSource.isActual (err, ok) ->
|
||||
test.ok ok, 'dataSource is actual'
|
||||
User.defineProperty 'email', false
|
||||
User.schema.isActual (err, ok) ->
|
||||
test.ok not ok, 'schema is not actual'
|
||||
User.dataSource.isActual (err, ok) ->
|
||||
test.ok not ok, 'dataSource is not actual'
|
||||
test.done()
|
||||
|
||||
it 'should add single-column index', (test) ->
|
||||
User.defineProperty 'email', type: String, index: { kind: 'FULLTEXT', type: 'HASH'}
|
||||
User.schema.autoupdate (err) ->
|
||||
User.dataSource.autoupdate (err) ->
|
||||
return console.log(err) if err
|
||||
getIndexes 'User', (err, ixs) ->
|
||||
test.ok ixs.email && ixs.email.Column_name == 'email'
|
||||
|
@ -215,9 +215,9 @@ it 'should add single-column index', (test) ->
|
|||
|
||||
it 'should change type of single-column index', (test) ->
|
||||
User.defineProperty 'email', type: String, index: { type: 'BTREE' }
|
||||
User.schema.isActual (err, ok) ->
|
||||
test.ok ok, 'schema is actual'
|
||||
User.schema.autoupdate (err) ->
|
||||
User.dataSource.isActual (err, ok) ->
|
||||
test.ok ok, 'dataSource is actual'
|
||||
User.dataSource.autoupdate (err) ->
|
||||
return console.log(err) if err
|
||||
getIndexes 'User', (err, ixs) ->
|
||||
test.ok ixs.email && ixs.email.Column_name == 'email'
|
||||
|
@ -226,17 +226,17 @@ it 'should change type of single-column index', (test) ->
|
|||
|
||||
it 'should remove single-column index', (test) ->
|
||||
User.defineProperty 'email', type: String, index: false
|
||||
User.schema.autoupdate (err) ->
|
||||
User.dataSource.autoupdate (err) ->
|
||||
return console.log(err) if err
|
||||
getIndexes 'User', (err, ixs) ->
|
||||
test.ok !ixs.email
|
||||
test.done()
|
||||
|
||||
it 'should update multi-column index when order of columns changed', (test) ->
|
||||
User.schema.adapter._models.User.settings.indexes.index1.columns = 'createdByAdmin, email'
|
||||
User.schema.isActual (err, ok) ->
|
||||
test.ok not ok, 'schema is not actual'
|
||||
User.schema.autoupdate (err) ->
|
||||
User.dataSource.adapter._models.User.settings.indexes.index1.columns = 'createdByAdmin, email'
|
||||
User.dataSource.isActual (err, ok) ->
|
||||
test.ok not ok, 'dataSource is not actual'
|
||||
User.dataSource.autoupdate (err) ->
|
||||
return console.log(err) if err
|
||||
getIndexes 'User', (err, ixs) ->
|
||||
test.equals ixs.index1.Column_name, 'createdByAdmin'
|
||||
|
@ -245,12 +245,12 @@ it 'should update multi-column index when order of columns changed', (test) ->
|
|||
|
||||
it 'test', (test) ->
|
||||
User.defineProperty 'email', type: String, index: true
|
||||
User.schema.autoupdate (err) ->
|
||||
User.schema.autoupdate (err) ->
|
||||
User.schema.autoupdate (err) ->
|
||||
User.dataSource.autoupdate (err) ->
|
||||
User.dataSource.autoupdate (err) ->
|
||||
User.dataSource.autoupdate (err) ->
|
||||
test.done()
|
||||
|
||||
it 'should disconnect when done', (test) ->
|
||||
schema.disconnect()
|
||||
dataSource.disconnect()
|
||||
test.done()
|
||||
|
||||
|
|
|
@ -279,14 +279,14 @@ describe('migrations', function() {
|
|||
});
|
||||
});
|
||||
|
||||
it('should check actuality of schema', function(done) {
|
||||
it('should check actuality of dataSource', function(done) {
|
||||
// 'drop column'
|
||||
UserData.schema.isActual(function(err, ok) {
|
||||
assert.ok(ok, 'schema is not actual (should be)');
|
||||
UserData.dataSource.isActual(function(err, ok) {
|
||||
assert.ok(ok, 'dataSource is not actual (should be)');
|
||||
UserData.defineProperty('essay', {type: Schema.Text});
|
||||
// UserData.defineProperty('email', false); Can't undefine currently.
|
||||
UserData.schema.isActual(function(err, ok) {
|
||||
assert.ok(!ok, 'schema is actual (shouldn\t be)');
|
||||
UserData.dataSource.isActual(function(err, ok) {
|
||||
assert.ok(!ok, 'dataSource is actual (shouldn\t be)');
|
||||
done()
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue