diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..7cd53fd --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +**/node_modules/ diff --git a/.travis.yml b/.travis.yml index 91df308..623c6da 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "6" - "8" - "10" # Xenial build environment comes with MySQL 5.7 diff --git a/deps/juggler-v3/package.json b/deps/juggler-v3/package.json new file mode 100644 index 0000000..b8b1147 --- /dev/null +++ b/deps/juggler-v3/package.json @@ -0,0 +1,8 @@ +{ + "name": "juggler-v3", + "version": "3.0.0", + "dependencies": { + "loopback-datasource-juggler":"3.x", + "should": "^8.4.0" + } +} diff --git a/deps/juggler-v3/test.js b/deps/juggler-v3/test.js new file mode 100644 index 0000000..bfc9965 --- /dev/null +++ b/deps/juggler-v3/test.js @@ -0,0 +1,31 @@ +// Copyright IBM Corp. 2019. All Rights Reserved. +// Node module: loopback-connector-mysql +// This file is licensed under the Artistic License 2.0. +// License text available at https://opensource.org/licenses/Artistic-2.0 + +'use strict'; + +const should = require('should'); +const juggler = require('loopback-datasource-juggler'); +const name = require('./package.json').name; + +require('../../test/init'); + +describe(name, function() { + before(function() { + return global.resetDataSourceClass(juggler.DataSource); + }); + + after(function() { + return global.resetDataSourceClass(); + }); + + require('loopback-datasource-juggler/test/common.batch.js'); + require('loopback-datasource-juggler/test/include.test.js'); + + // === Operation hooks ==== // + + const suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); + const customConfig = Object.assign({}, global.config); + suite(global.getDataSource(customConfig, juggler.DataSource), should, {replaceOrCreateReportsNewInstance: false}); +}); diff --git a/deps/juggler-v4/package.json b/deps/juggler-v4/package.json new file mode 100644 index 0000000..e73724e --- /dev/null +++ b/deps/juggler-v4/package.json @@ -0,0 +1,8 @@ +{ + "name": "juggler-v4", + "version": "4.0.0", + "dependencies": { + "loopback-datasource-juggler":"4.x", + "should": "^13.2.3" + } +} diff --git a/deps/juggler-v4/test.js b/deps/juggler-v4/test.js new file mode 100644 index 0000000..c72b689 --- /dev/null +++ b/deps/juggler-v4/test.js @@ -0,0 +1,32 @@ +// Copyright IBM Corp. 2019. All Rights Reserved. +// Node module: loopback-connector-mysql +// This file is licensed under the Artistic License 2.0. +// License text available at https://opensource.org/licenses/Artistic-2.0 + +'use strict'; + +const semver = require('semver'); +const should = require('should'); +const juggler = require('loopback-datasource-juggler'); +const name = require('./package.json').name; + +require('../../test/init'); + +describe(name, function() { + before(function() { + return global.resetDataSourceClass(juggler.DataSource); + }); + + after(function() { + return global.resetDataSourceClass(); + }); + + require('loopback-datasource-juggler/test/common.batch.js'); + require('loopback-datasource-juggler/test/include.test.js'); + + // === Operation hooks ==== // + + const suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); + const customConfig = Object.assign({}, global.config); + suite(global.getDataSource(customConfig, juggler.DataSource), should, {replaceOrCreateReportsNewInstance: false}); +}); diff --git a/lib/mysql.js b/lib/mysql.js index b4420b0..8376a9a 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -444,6 +444,12 @@ MySQL.prototype.fromColumnValue = function(prop, val) { lat: val.y, }; break; + case 'ObjectID': + val = new prop.type(val); + break; + case 'Buffer': + val = prop.type(val); + break; case 'List': case 'Array': case 'Object': @@ -453,10 +459,6 @@ MySQL.prototype.fromColumnValue = function(prop, val) { } break; default: - if (!Array.isArray(prop.type) && !prop.type.modelName) { - // Do not convert array and model types - val = prop.type(val); - } break; } } diff --git a/package.json b/package.json index 1a07815..2d2a680 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,13 @@ "version": "5.4.1", "description": "MySQL connector for loopback-datasource-juggler", "engines": { - "node": ">=6" + "node": ">=8" }, "main": "index.js", "scripts": { "pretest": "node pretest.js", "lint": "eslint .", - "test": "mocha --timeout 10000 --require test/init.js test/*.js", + "test": "mocha --timeout 10000 test/*.test.js node_modules/juggler-v3/test.js node_modules/juggler-v4/test.js", "posttest": "npm run lint" }, "dependencies": { @@ -24,7 +24,9 @@ "bluebird": "~2.9.10", "eslint": "^4.3.0", "eslint-config-loopback": "^8.0.0", - "loopback-datasource-juggler": "^3.0.0", + "juggler-v3": "file:./deps/juggler-v3", + "juggler-v4": "file:./deps/juggler-v4", + "loopback-datasource-juggler": "^3.0.0 || ^4.0.0", "mocha": "^2.1.0", "rc": "^1.0.0", "should": "^8.0.2", diff --git a/test/datatypes.test.js b/test/datatypes.test.js index 76f37a7..155e671 100644 --- a/test/datatypes.test.js +++ b/test/datatypes.test.js @@ -7,6 +7,7 @@ require('./init.js'); var assert = require('assert'); var _ = require('lodash'); +const GeoPoint = require('loopback-datasource-juggler').GeoPoint; var db, BlobModel, EnumModel, ANIMAL_ENUM, City, Account; @@ -206,7 +207,8 @@ describe('MySQL specific datatypes', function() { var xcor, ycor; City.create(city1, function(err, res) { if (err) return done(err); - res.loc.should.deepEqual(city1.loc); + const loc_in_geo_type = new GeoPoint(city1.loc); + res.loc.should.deepEqual(loc_in_geo_type); res.name.should.equal(city1.name); var sqlStmt = 'select ST_X(loc),ST_Y(loc) from City where id=1'; db.connector.execute(sqlStmt, function(err, res) { diff --git a/test/imported.test.js b/test/imported.test.js deleted file mode 100644 index 3437d88..0000000 --- a/test/imported.test.js +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright IBM Corp. 2013,2016. All Rights Reserved. -// Node module: loopback-connector-mysql -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -'use strict'; -describe('mysql imported features', function() { - before(function() { - require('./init.js'); - }); - - require('loopback-datasource-juggler/test/common.batch.js'); - require('loopback-datasource-juggler/test/include.test.js'); -}); diff --git a/test/init.js b/test/init.js index ca2f057..31cdb39 100644 --- a/test/init.js +++ b/test/init.js @@ -6,8 +6,8 @@ 'use strict'; module.exports = require('should'); - -var DataSource = require('loopback-datasource-juggler').DataSource; +const juggler = require('loopback-datasource-juggler'); +let DataSource = juggler.DataSource; var config = require('rc')('loopback', {test: {mysql: {}}}).test.mysql; global.getConfig = function(options) { @@ -28,11 +28,23 @@ global.getConfig = function(options) { return dbConf; }; -global.getDataSource = global.getSchema = function(options) { - var db = new DataSource(require('../'), global.getConfig(options)); +let db; +global.getDataSource = global.getSchema = function(options, customClass) { + const ctor = customClass || DataSource; + db = new ctor(require('../'), global.getConfig(options)); + db.log = function(a) { + console.log(a); + }; return db; }; +global.resetDataSourceClass = function(ctor) { + DataSource = ctor || juggler.DataSource; + const promise = db ? db.disconnect() : Promise.resolve(); + db = undefined; + return promise; +}; + global.connectorCapabilities = { ilike: false, nilike: false, diff --git a/test/migration.test.js b/test/migration.test.js index c5d7eca..77e079e 100644 --- a/test/migration.test.js +++ b/test/migration.test.js @@ -639,7 +639,9 @@ var getFields = function(model, cb) { res.forEach(function(field) { fields[field.Field] = field; }); - cb(err, fields); + // The returned data are in arrays of type `RowDataPacket`, + // which are not objects. + cb(err, JSON.parse(JSON.stringify(fields))); } }); }; diff --git a/test/mysql.test.js b/test/mysql.test.js index ac8e6f5..73d0848 100644 --- a/test/mysql.test.js +++ b/test/mysql.test.js @@ -7,6 +7,7 @@ var async = require('async'); var should = require('./init.js'); var sinon = require('sinon'); +const List = require('loopback-datasource-juggler/lib/list'); var Post, PostWithStringId, PostWithUniqueTitle, PostWithNumId, Student, db; @@ -88,7 +89,7 @@ describe('mysql', function() { p.content.should.be.equal(post.content); p.title.should.be.equal('a'); - p.comments.should.eql(['1', '2']); + p.comments.should.eql(new List(['1', '2'])); p.history.should.eql({a: 1, b: 'b'}); done(); diff --git a/test/persistence-hooks.test.js b/test/persistence-hooks.test.js deleted file mode 100644 index 27310f0..0000000 --- a/test/persistence-hooks.test.js +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright IBM Corp. 2015,2017. All Rights Reserved. -// Node module: loopback-connector-mysql -// This file is licensed under the MIT License. -// License text available at https://opensource.org/licenses/MIT - -'use strict'; -var should = require('./init'); -var suite = require('loopback-datasource-juggler/test/persistence-hooks.suite.js'); - -suite(global.getDataSource(), should, { - replaceOrCreateReportsNewInstance: false, -});