diff --git a/.babelrc b/.babelrc new file mode 100644 index 00000000..9d8d5165 --- /dev/null +++ b/.babelrc @@ -0,0 +1 @@ +{ "presets": ["es2015"] } diff --git a/.eslintrc b/.eslintrc index 82efae60..ac449856 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,11 @@ { + "parserOptions": { + "ecmaVersion": 6 + }, "env": { "node": true, - "browser": false + "browser": false, + "es6": true }, "ecmaFeatures": { "arrowFunctions": true, diff --git a/.gitignore b/.gitignore index bb1b458d..0c864dda 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ docs/man npm-debug.log .idea .coveralls.yml +build diff --git a/Makefile b/Makefile index f3a65001..c1fa875c 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ JS_FILES = $(shell find . -type f -name "*.js" \ check: @$(JSHINT) $(JS_FILES) -test: check +test: $(TESTER) $(OPTS) $(TESTS) test-verbose: $(TESTER) $(OPTS) --reporter spec $(TESTS) @@ -51,6 +51,7 @@ man: $(MAN_DOCS) html: $(HTML_DOCS) build: man + babel -d build/ $(shell find ./lib/ -type f -name "*.js") *.js web: html cp ./docs/html/* ../docs diff --git a/circle.yml b/circle.yml index 6131e254..66819b2c 100644 --- a/circle.yml +++ b/circle.yml @@ -9,5 +9,7 @@ general: test: override: + - nvm use 0.10.26 && npm run test-ci-babel + - nvm use 4.4.7 && npm run test-ci-babel - nvm use 6.3.0 && npm run test-ci - npm i coveralls && export COVERALLS_GIT_COMMIT=`git rev-parse HEAD` && cat ./coverage/lcov.info | coveralls diff --git a/index.js b/index.js index dd99ae38..69db037b 100644 --- a/index.js +++ b/index.js @@ -1,28 +1,38 @@ -var fs = require('fs'); -var path = require('path'); +'use strict'; -var Schema = exports.Schema = require('./lib/schema').Schema; -exports.AbstractClass = require('./lib/model.js'); +const { Schema } = require('./lib/schema'); +const AbstractClass = require('./lib/model.js'); -var baseSQL = './lib/sql'; +module.exports = { -exports.__defineGetter__('BaseSQL', function () { - return require(baseSQL); -}); + Schema, -exports.loadSchema = function(filename, settings, compound) { - return require('./legacy-compound-schema-loader')(filename, settings, compound); -}; + AbstractClass, -exports.init = function (compound) { - return require('./legacy-compound-init')(compound); -}; + // deprecated api + loadSchema: function(filename, settings, compound) { + return require('./legacy-compound-schema-loader')(Schema, filename, settings, compound); + }, + + init: function init(compound) { + return require('./legacy-compound-init')(compound, Schema, AbstractClass); + }, + + get BaseSQL() { + return require('./lib/sql'); + }, -exports.__defineGetter__('version', function () { - return JSON.parse(fs.readFileSync(__dirname + '/package.json')).version; -}); + get version() { + return require( + process.versions.node >= '6' + ? './package.json' + : '../package.json' + ).version; + }, + + get test() { + return require('./test/common_test'); + } + +}; -var commonTest = './test/common_test'; -exports.__defineGetter__('test', function () { - return require(commonTest); -}); diff --git a/legacy-compound-init.js b/legacy-compound-init.js index 6996e9a4..6992997f 100644 --- a/legacy-compound-init.js +++ b/legacy-compound-init.js @@ -1,16 +1,19 @@ 'use strict'; -module.exports = function(compound) { +var loadSchema = require('./legacy-compound-schema-loader'); + +module.exports = function init(compound, Schema, AbstractClass) { if (global.railway) { global.railway.orm = exports; } else { compound.orm = { - Schema: exports.Schema, - AbstractClass: exports.AbstractClass + Schema, + AbstractClass }; if (compound.app.enabled('noeval schema')) { - compound.orm.schema = exports.loadSchema( + compound.orm.schema = loadSchema( + Schema, compound.root + '/db/schema', compound.app.get('database'), compound diff --git a/legacy-compound-schema-loader.js b/legacy-compound-schema-loader.js index bdf95232..4acfa8d5 100644 --- a/legacy-compound-schema-loader.js +++ b/legacy-compound-schema-loader.js @@ -1,5 +1,5 @@ -module.exports = function(filename, settings, compound) { +module.exports = function(Schema, filename, settings, compound) { var schema = []; var definitions = require(filename); Object.keys(definitions).forEach(function(k) { diff --git a/lib/model.js b/lib/model.js index 6846dafe..5b8d719b 100644 --- a/lib/model.js +++ b/lib/model.js @@ -9,8 +9,7 @@ module.exports = AbstractClass; var setImmediate = global.setImmediate || process.nextTick; var util = require('util'); var assert = require('assert'); -var validations = require('./validations.js'); -var ValidationError = validations.ValidationError; +const { ValidationError } = require('./validations.js'); var List = require('./list.js'); var when = require('when'); require('./hooks.js'); @@ -456,6 +455,12 @@ AbstractClass.fetch = function fetch(id) { }); }; +AbstractClass.expand = function(object) { + return Object.assign({}, object, { + pets: [ {} ] + }); +}; + /** * Find all instances of Model, matched by query * make sure you have marked as `index: true` fields for filter or sort diff --git a/main.js b/main.js new file mode 100644 index 00000000..a25817f1 --- /dev/null +++ b/main.js @@ -0,0 +1,11 @@ +var isNodeSix = process.versions.node >= '6'; + +if (!isNodeSix) { + global.Promise = require('when').Promise; +} + +module.exports = isNodeSix + ? require('./index') + : require('./build/index'); + + diff --git a/package.json b/package.json index 1cfe605d..410f7a57 100644 --- a/package.json +++ b/package.json @@ -49,12 +49,14 @@ "type": "git", "url": "https://github.com/1602/jugglingdb" }, - "main": "index.js", + "main": "main.js", "scripts": { - "test": "make test", + "test": "mocha --bail --reporter spec --check-leaks test/", + "test-babel": "mocha --compilers js:babel-register --bail --reporter spec --check-leaks test/", "prepublish": "make build", "test-coverage": "istanbul cover node_modules/.bin/_mocha -- --reporter landing --no-exit --check-leaks test/", - "test-ci": "eslint lib/ && istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" + "test-ci-babel": "node_modules/mocha/bin/_mocha --compilers js:babel-register --reporter spec --check-leaks test/" + "test-ci-native": "eslint lib/ && istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --compilers js:babel-register --check-leaks test/" }, "man": [ "./docs/man/jugglingdb.3", @@ -69,6 +71,9 @@ "node >= 0.6" ], "devDependencies": { + "babel-cli": "^6.11.4", + "babel-preset-es2015": "^6.9.0", + "babel-register": "^6.11.5", "eslint": "^3.1.1", "expect": "^1.20.2", "istanbul": "^0.4.4",