From fef5ecf5e0734b7d16c9ab9ae1adfb67ea423a71 Mon Sep 17 00:00:00 2001 From: segayuu Date: Mon, 8 Oct 2018 13:53:31 +0900 Subject: [PATCH 1/3] Replace constructor function to class declaration --- lib/database.js | 46 +++++++------- lib/document.js | 26 ++++---- lib/error.js | 38 ++++++------ lib/error/population.js | 25 ++++---- lib/error/validation.js | 12 ++-- lib/model.js | 117 ++++++++++++++++++------------------ lib/mutex.js | 8 ++- lib/query.js | 20 ++++--- lib/schema.js | 68 +++++++++++---------- lib/schematype.js | 130 ++++++++++++++++++++-------------------- lib/types/array.js | 36 +++++------ lib/types/boolean.js | 14 +---- lib/types/buffer.js | 29 +++++---- lib/types/cuid.js | 13 +--- lib/types/date.js | 13 +--- lib/types/enum.js | 30 +++++----- lib/types/integer.js | 14 +---- lib/types/number.js | 14 +---- lib/types/object.js | 26 ++++---- lib/types/string.js | 14 +---- lib/types/virtual.js | 15 +---- lib/util.js | 1 - test/scripts/model.js | 7 +-- 23 files changed, 313 insertions(+), 403 deletions(-) diff --git a/lib/database.js b/lib/database.js index 87ab5e1a..68f3ae18 100644 --- a/lib/database.js +++ b/lib/database.js @@ -6,37 +6,37 @@ const fs = require('graceful-fs'); const Model = require('./model'); const Schema = require('./schema'); const SchemaType = require('./schematype'); -const util = require('./util'); const WarehouseError = require('./error'); const pkg = require('../package.json'); const JsonReadable = require('json-stream-stringify'); -/** - * Database constructor. - * - * @class - * @param {object} [options] - * @param {number} [options.version=0] Database version - * @param {string} [options.path] Database path - * @param {function} [options.onUpgrade] Triggered when the database is upgraded - * @param {function} [options.onDowngrade] Triggered when the database is downgraded - */ -function Database(options) { - this.options = Object.assign({ - version: 0, - onUpgrade() {}, +class Database { - onDowngrade() {} - }, options); + /** + * Database constructor. + * + * @param {object} [options] + * @param {number} [options.version=0] Database version + * @param {string} [options.path] Database path + * @param {function} [options.onUpgrade] Triggered when the database is upgraded + * @param {function} [options.onDowngrade] Triggered when the database is downgraded + */ + constructor(options) { + this.options = Object.assign({ + version: 0, + onUpgrade() {}, - this._models = {}; + onDowngrade() {} + }, options); - const _Model = this.Model = function(name, schema) { - Model.call(this, name, schema); - }; + this._models = {}; - util.inherits(_Model, Model); - _Model.prototype._database = this; + class _Model extends Model {} + + this.Model = _Model; + + _Model.prototype._database = this; + } } /** diff --git a/lib/document.js b/lib/document.js index 82dfa518..42a9b1fe 100644 --- a/lib/document.js +++ b/lib/document.js @@ -2,19 +2,21 @@ const cloneDeep = require('lodash/cloneDeep'); -/** - * Document constructor. - * - * @class - * @param {object} data - */ -function Document(data) { - if (data) { - const keys = Object.keys(data); +class Document { + + /** + * Document constructor. + * + * @param {object} data + */ + constructor(data) { + if (data) { + const keys = Object.keys(data); - for (let i = 0, len = keys.length; i < len; i++) { - const key = keys[i]; - this[key] = data[key]; + for (let i = 0, len = keys.length; i < len; i++) { + const key = keys[i]; + this[key] = data[key]; + } } } } diff --git a/lib/error.js b/lib/error.js index 0f5aabb7..7ef0706b 100644 --- a/lib/error.js +++ b/lib/error.js @@ -1,31 +1,27 @@ 'use strict'; -const util = require('./util'); +class WarehouseError extends Error { -/** - * WarehouseError constructor - * - * @class - * @param {String} msg - * @param {String} code - * @extends Error - */ -function WarehouseError(msg, code) { - Error.call(this); + /** + * WarehouseError constructor + * + * @param {string} msg + * @param {string} code + */ + constructor(msg, code) { + super(msg); - if (Error.captureStackTrace) { - Error.captureStackTrace(this); - } else { - this.stack = new Error().stack; - } + if (Error.captureStackTrace) { + Error.captureStackTrace(this); + } else { + this.stack = new Error().stack; + } - this.name = 'WarehouseError'; - this.message = msg; - this.code = code; + this.name = 'WarehouseError'; + this.code = code; + } } -util.inherits(WarehouseError, Error); - WarehouseError.ID_EXIST = 'ID_EXIST'; WarehouseError.ID_NOT_EXIST = 'ID_NOT_EXIST'; WarehouseError.ID_UNDEFINED = 'ID_UNDEFINED'; diff --git a/lib/error/population.js b/lib/error/population.js index 447bf760..4febccc2 100644 --- a/lib/error/population.js +++ b/lib/error/population.js @@ -1,22 +1,19 @@ 'use strict'; -const util = require('../util'); const WarehouseError = require('../error'); -/** - * PopulationError constructor - * - * @class - * @param {String} msg - * @extends WarehouseError - */ -function PopulationError(msg) { - WarehouseError.call(this); +class PopulationError extends WarehouseError { - this.name = 'PopulationError'; - this.message = msg; -} + /** + * PopulationError constructor + * + * @param {string} msg + */ + constructor(msg) { + super(msg); -util.inherits(PopulationError, WarehouseError); + this.name = 'PopulationError'; + } +} module.exports = PopulationError; diff --git a/lib/error/validation.js b/lib/error/validation.js index e5c34e07..ab3bdeb9 100644 --- a/lib/error/validation.js +++ b/lib/error/validation.js @@ -1,6 +1,5 @@ 'use strict'; -const util = require('../util'); const WarehouseError = require('../error'); /** @@ -10,13 +9,12 @@ const WarehouseError = require('../error'); * @param {String} msg * @extends WarehouseError */ -function ValidationError(msg) { - WarehouseError.call(this); +class ValidationError extends WarehouseError { + constructor(msg) { + super(msg); - this.name = 'ValidationError'; - this.message = msg; + this.name = 'ValidationError'; + } } -util.inherits(ValidationError, WarehouseError); - module.exports = ValidationError; diff --git a/lib/model.js b/lib/model.js index aa47602d..03d65413 100644 --- a/lib/model.js +++ b/lib/model.js @@ -3,7 +3,7 @@ const { EventEmitter } = require('events'); const cloneDeep = require('lodash/cloneDeep'); const Promise = require('bluebird'); -const { parseArgs, getProp, setGetter, shuffle, inherits } = require('./util'); +const { parseArgs, getProp, setGetter, shuffle } = require('./util'); const Document = require('./document'); const Query = require('./query'); const Schema = require('./schema'); @@ -12,79 +12,80 @@ const WarehouseError = require('./error'); const PopulationError = require('./error/population'); const Mutex = require('./mutex'); -/** - * Model constructor. - * - * @class - * @param {string} name Model name - * @param {Schema|object} [schema] Schema - * @extends EventEmitter - */ -function Model(name, schema_) { - EventEmitter.call(this); +class Model extends EventEmitter { - let schema; + /** + * Model constructor. + * + * @param {string} name Model name + * @param {Schema|object} [schema] Schema + */ + constructor(name, schema_) { + super(); - // Define schema - if (schema_ instanceof Schema) { - schema = schema_; - } else if (typeof schema_ === 'object') { - schema = new Schema(schema_); - } else { - schema = new Schema(); - } + let schema; - // Set `_id` path for schema - if (!schema.path('_id')) { - schema.path('_id', {type: Types.CUID, required: true}); - } + // Define schema + if (schema_ instanceof Schema) { + schema = schema_; + } else if (typeof schema_ === 'object') { + schema = new Schema(schema_); + } else { + schema = new Schema(); + } - this.name = name; - this.data = {}; - this._mutex = new Mutex(); - this.schema = schema; - this.length = 0; + // Set `_id` path for schema + if (!schema.path('_id')) { + schema.path('_id', {type: Types.CUID, required: true}); + } - const _Document = this.Document = function(data) { - Document.call(this, data); + this.name = name; + this.data = {}; + this._mutex = new Mutex(); + this.schema = schema; + this.length = 0; - // Apply getters - schema._applyGetters(this); - }; + class _Document extends Document { + constructor(data) { + super(data); - inherits(_Document, Document); - _Document.prototype._model = this; - _Document.prototype._schema = schema; + // Apply getters + schema._applyGetters(this); + } + } - const _Query = this.Query = function(data) { - Query.call(this, data); - }; + this.Document = _Document; - inherits(_Query, Query); - _Query.prototype._model = this; - _Query.prototype._schema = schema; + _Document.prototype._model = this; + _Document.prototype._schema = schema; - // Apply static methods - const statics = schema.statics; - const staticKeys = Object.keys(statics); + class _Query extends Query {} - for (let i = 0, len = staticKeys.length; i < len; i++) { - const key = staticKeys[i]; - this[key] = statics[key]; - } + this.Query = _Query; + + _Query.prototype._model = this; + _Query.prototype._schema = schema; - // Apply instance methods - const methods = schema.methods; - const methodKeys = Object.keys(methods); + // Apply static methods + const statics = schema.statics; + const staticKeys = Object.keys(statics); - for (let i = 0, len = methodKeys.length; i < len; i++) { - const key = methodKeys[i]; - _Document.prototype[key] = methods[key]; + for (let i = 0, len = staticKeys.length; i < len; i++) { + const key = staticKeys[i]; + this[key] = statics[key]; + } + + // Apply instance methods + const methods = schema.methods; + const methodKeys = Object.keys(methods); + + for (let i = 0, len = methodKeys.length; i < len; i++) { + const key = methodKeys[i]; + _Document.prototype[key] = methods[key]; + } } } -inherits(Model, EventEmitter); - /** * Creates a new document. * diff --git a/lib/mutex.js b/lib/mutex.js index b5ba60a9..e10b634f 100644 --- a/lib/mutex.js +++ b/lib/mutex.js @@ -1,8 +1,10 @@ 'use strict'; -function Mutex() { - this._locked = false; - this._queue = []; +class Mutex { + constructor() { + this._locked = false; + this._queue = []; + } } Mutex.prototype.lock = function(fn) { diff --git a/lib/query.js b/lib/query.js index 0d5d77bb..48c3085f 100644 --- a/lib/query.js +++ b/lib/query.js @@ -3,15 +3,17 @@ const Promise = require('bluebird'); const { parseArgs, shuffle } = require('./util'); -/** - * Query constructor. - * - * @class - * @param {Array} data - */ -function Query(data) { - this.data = data; - this.length = data.length; +class Query { + + /** + * Query constructor. + * + * @param {Array} data + */ + constructor(data) { + this.data = data; + this.length = data.length; + } } /** diff --git a/lib/schema.js b/lib/schema.js index 178fab77..b156b1e4 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -7,10 +7,10 @@ const util = require('./util'); const PopulationError = require('./error/population'); const isPlainObject = require('is-plain-object'); -const getProp = util.getProp; -const setProp = util.setProp; -const delProp = util.delProp; -const isArray = Array.isArray; +const { getProp } = util; +const { setProp } = util; +const { delProp } = util; +const { isArray } = Array; const builtinTypes = { String: true, @@ -22,37 +22,39 @@ const builtinTypes = { Buffer: true }; -/** - * Schema constructor. - * - * @class - * @param {Object} schema - */ -function Schema(schema) { - this.paths = {}; - this.statics = {}; - this.methods = {}; - - this.hooks = { - pre: { - save: [], - remove: [] - }, - post: { - save: [], - remove: [] - } - }; +class Schema { + + /** + * Schema constructor. + * + * @param {Object} schema + */ + constructor(schema) { + this.paths = {}; + this.statics = {}; + this.methods = {}; + + this.hooks = { + pre: { + save: [], + remove: [] + }, + post: { + save: [], + remove: [] + } + }; - this.stacks = { - getter: [], - setter: [], - import: [], - export: [] - }; + this.stacks = { + getter: [], + setter: [], + import: [], + export: [] + }; - if (schema) { - this.add(schema); + if (schema) { + this.add(schema); + } } } diff --git a/lib/schematype.js b/lib/schematype.js index 862e861e..407d5201 100644 --- a/lib/schematype.js +++ b/lib/schematype.js @@ -1,76 +1,76 @@ 'use strict'; -const util = require('./util'); +const { setProp } = require('./util'); const ValidationError = require('./error/validation'); -const setProp = util.setProp; +class SchemaType { -/** - * SchemaType constructor. - * - * This is the basic schema type. All schema types should inherit from this - * class. For example: - * - * ``` js - * var SchemaTypeCustom = function(name, options){ - * SchemaType.call(this, name, options); - * }; - * - * require('util').inherits(SchemaTypeCustom, SchemaType); - * ``` - * - * **Query operators** - * - * To add a query operator, defines a method whose name is started with `q$`. - * For example: - * - * ``` js - * SchemaTypeCustom.q$foo = function(value, query, data){ - * // ... - * }; - * ``` - * - * The `value` parameter is the value of specified field; the `query` parameter - * is the value passed to the query operator; the `data` parameter is the - * complete data. - * - * The return value must be a boolean indicating whether the data passed. - * - * **Update operators** - * - * To add a update operator, defines a method whose name is started with `u$`. - * For example: - * - * ``` js - * SchemaTypeCustom.u$foo = function(value, update, data){ - * // ... - * }; - * - * The `value` parameter is the value of specified field; the `update` parameter - * is the value passed to the update operator; the `data` parameter is the - * complete data. - * - * The return value will replace the original data. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @param {*} [options.default] - */ -function SchemaType(name, options) { - this.name = name || ''; + /** + * SchemaType constructor. + * + * This is the basic schema type. All schema types should inherit from this + * class. For example: + * + ``` js + class SchemaTypeCustom extends SchemaType { + constructor(name, options) { + super(name, options); + } + } + ``` + * + * **Query operators** + * + * To add a query operator, defines a method whose name is started with `q$`. + * For example: + * + * ``` js + * SchemaTypeCustom.q$foo = function(value, query, data){ + * // ... + * }; + * ``` + * + * The `value` parameter is the value of specified field; the `query` parameter + * is the value passed to the query operator; the `data` parameter is the + * complete data. + * + * The return value must be a boolean indicating whether the data passed. + * + * **Update operators** + * + * To add a update operator, defines a method whose name is started with `u$`. + * For example: + * + * ``` js + * SchemaTypeCustom.u$foo = function(value, update, data){ + * // ... + * }; + * + * The `value` parameter is the value of specified field; the `update` parameter + * is the value passed to the update operator; the `data` parameter is the + * complete data. + * + * The return value will replace the original data. + * + * @param {string} name + * @param {object} [options] + * @param {boolean} [options.required=false] + * @param {*} [options.default] + */ + constructor(name = '', options) { + this.name = name; - this.options = Object.assign({ - required: false - }, options); + this.options = Object.assign({ + required: false + }, options); - const default_ = this.options.default; + const default_ = this.options.default; - if (typeof default_ === 'function') { - this.default = default_; - } else { - this.default = () => default_; + if (typeof default_ === 'function') { + this.default = default_; + } else { + this.default = () => default_; + } } } diff --git a/lib/types/array.js b/lib/types/array.js index 62ab44bc..7519d8e6 100644 --- a/lib/types/array.js +++ b/lib/types/array.js @@ -1,32 +1,32 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const ValidationError = require('../error/validation'); -const isArray = Array.isArray; +const { isArray } = Array; /** * Array schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @param {Array|Function} [options.default=[]] - * @param {SchemaType} [options.child] - * @extends {SchemaType} */ -function SchemaTypeArray(name, options) { - SchemaType.call(this, name, Object.assign({ - default: [] - }, options)); - - this.child = this.options.child || new SchemaType(name); +class SchemaTypeArray extends SchemaType { + + /** + * + * @param {String} name + * @param {Object} [options] + * @param {Boolean} [options.required=false] + * @param {Array|Function} [options.default=[]] + * @param {SchemaType} [options.child] + */ + constructor(name, options) { + super(name, Object.assign({ + default: [] + }, options)); + + this.child = this.options.child || new SchemaType(name); + } } -util.inherits(SchemaTypeArray, SchemaType); - /** * Casts an array and its child elements. * diff --git a/lib/types/boolean.js b/lib/types/boolean.js index 44897335..e5b708d7 100644 --- a/lib/types/boolean.js +++ b/lib/types/boolean.js @@ -1,24 +1,12 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const ValidationError = require('../error/validation'); /** * Boolean schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @param {Boolean|Function} [options.default] - * @extends {SchemaType} */ -function SchemaTypeBoolean(name, options) { - SchemaType.call(this, name, options); -} - -util.inherits(SchemaTypeBoolean, SchemaType); +class SchemaTypeBoolean extends SchemaType {} /** * Casts a boolean. diff --git a/lib/types/buffer.js b/lib/types/buffer.js index 2c4b6382..0d3b80b9 100644 --- a/lib/types/buffer.js +++ b/lib/types/buffer.js @@ -1,28 +1,27 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const ValidationError = require('../error/validation'); /** * Boolean schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @param {Boolean|Function} [options.default] - * @param {String} [options.encoding=hex] - * @extends {SchemaType} */ -function SchemaTypeBuffer(name, options) { - SchemaType.call(this, name, Object.assign({ - encoding: 'hex' - }, options)); +class SchemaTypeBuffer extends SchemaType { + + /** + * @param {string} name + * @param {object} [options] + * @param {boolean} [options.required=false] + * @param {boolean|Function} [options.default] + * @param {string} [options.encoding=hex] + */ + constructor(name, options) { + super(name, Object.assign({ + encoding: 'hex' + }, options)); + } } -util.inherits(SchemaTypeBuffer, SchemaType); - /** * Casts data. * diff --git a/lib/types/cuid.js b/lib/types/cuid.js index 3c5eebc5..8d899748 100644 --- a/lib/types/cuid.js +++ b/lib/types/cuid.js @@ -1,24 +1,13 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const cuid = require('cuid'); const ValidationError = require('../error/validation'); /** * [CUID](https://github.com/ericelliott/cuid) schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @extends {SchemaType} */ -function SchemaTypeCUID(name, options) { - SchemaType.call(this, name, options); -} - -util.inherits(SchemaTypeCUID, SchemaType); +class SchemaTypeCUID extends SchemaType {} /** * Casts data. Returns a new CUID only if value is null and the field is diff --git a/lib/types/date.js b/lib/types/date.js index 0e85ca14..226f66ab 100644 --- a/lib/types/date.js +++ b/lib/types/date.js @@ -6,19 +6,8 @@ const ValidationError = require('../error/validation'); /** * Date schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @param {Date|Number|Function} [options.default] - * @extends {SchemaType} */ -function SchemaTypeDate(name, options) { - SchemaType.call(this, name, options); -} - -util.inherits(SchemaTypeDate, SchemaType); +class SchemaTypeDate extends SchemaType {} /** * Casts data. diff --git a/lib/types/enum.js b/lib/types/enum.js index 5117159e..e2469989 100644 --- a/lib/types/enum.js +++ b/lib/types/enum.js @@ -1,27 +1,27 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const ValidationError = require('../error/validation'); /** * Enum schema type. - * - * @class - * @param {String} name - * @param {Object} options - * @param {Boolean} [options.required=false] - * @param {Array} options.elements - * @param {*} [options.default] - * @extends {SchemaType} */ -function SchemaTypeEnum(name, options) { - SchemaType.call(this, name, Object.assign({ - elements: [] - }, options)); -} +class SchemaTypeEnum extends SchemaType { -util.inherits(SchemaTypeEnum, SchemaType); + /** + * + * @param {string} name + * @param {object} options + * @param {Boolean} [options.required=false] + * @param {Array} options.elements + * @param {*} [options.default] + */ + constructor(name, options) { + super(name, Object.assign({ + elements: [] + }, options)); + } +} /** * Validates data. The value must be one of elements set in the options. diff --git a/lib/types/integer.js b/lib/types/integer.js index 4b6c849d..304a7497 100644 --- a/lib/types/integer.js +++ b/lib/types/integer.js @@ -1,24 +1,12 @@ 'use strict'; const SchemaTypeNumber = require('./number'); -const util = require('../util'); const ValidationError = require('../error/validation'); /** * Integer schema type. - * - * @class - * @param {String} name - * @param {Object} options - * @param {Boolean} [options.required=false] - * @param {Number|Function} [options.default] - * @extends {SchemaTypeNumber} */ -function SchemaTypeInteger(name, options) { - SchemaTypeNumber.call(this, name, options); -} - -util.inherits(SchemaTypeInteger, SchemaTypeNumber); +class SchemaTypeInteger extends SchemaTypeNumber {} /** * Casts a integer. diff --git a/lib/types/number.js b/lib/types/number.js index 21d9ad68..47627ac1 100644 --- a/lib/types/number.js +++ b/lib/types/number.js @@ -1,24 +1,12 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const ValidationError = require('../error/validation'); /** * Number schema type. - * - * @class - * @param {String} name - * @param {Object} options - * @param {Boolean} [options.required=false] - * @param {Number|Function} [options.default] - * @extends {SchemaType} */ -function SchemaTypeNumber(name, options) { - SchemaType.call(this, name, options); -} - -util.inherits(SchemaTypeNumber, SchemaType); +class SchemaTypeNumber extends SchemaType {} /** * Casts a number. diff --git a/lib/types/object.js b/lib/types/object.js index 0d8ac05f..ff2eb391 100644 --- a/lib/types/object.js +++ b/lib/types/object.js @@ -1,24 +1,22 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); /** * Object schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @param {Object|Function} [options.default={}] - * @extends {SchemaType} */ -function SchemaTypeObject(name, options) { - SchemaType.call(this, name, Object.assign({ - default: {} - }, options)); -} +class SchemaTypeObject extends SchemaType { -util.inherits(SchemaTypeObject, SchemaType); + /** + * + * @param {String} name + * @param {Object} [options] + * @param {Boolean} [options.required=false] + * @param {Object|Function} [options.default={}] + */ + constructor(name, options) { + super(name, Object.assign({ default: {} }, options)); + } +} module.exports = SchemaTypeObject; diff --git a/lib/types/string.js b/lib/types/string.js index fd258e56..820eab63 100644 --- a/lib/types/string.js +++ b/lib/types/string.js @@ -1,24 +1,12 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const ValidationError = require('../error/validation'); /** * String schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @param {Boolean} [options.required=false] - * @param {String|Function} [options.default] - * @extends {SchemaType} */ -function SchemaTypeString(name, options) { - SchemaType.call(this, name, options); -} - -util.inherits(SchemaTypeString, SchemaType); +class SchemaTypeString extends SchemaType {} /** * Casts a string. diff --git a/lib/types/virtual.js b/lib/types/virtual.js index bb42aee7..417b4f4d 100644 --- a/lib/types/virtual.js +++ b/lib/types/virtual.js @@ -1,23 +1,12 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); - -const setGetter = util.setGetter; +const { setGetter } = require('../util'); /** * Virtual schema type. - * - * @class - * @param {String} name - * @param {Object} [options] - * @extends {SchemaType} */ -function SchemaTypeVirtual(name, options) { - SchemaType.call(this, name, options); -} - -util.inherits(SchemaTypeVirtual, SchemaType); +class SchemaTypeVirtual extends SchemaType {} /** * Add a getter. diff --git a/lib/util.js b/lib/util.js index 767ab201..d2ba128f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,7 +2,6 @@ const util = require('util'); -exports.inherits = util.inherits; exports.isDate = util.isDate; exports.shuffle = array => { diff --git a/test/scripts/model.js b/test/scripts/model.js index b4c5195f..0d8166bf 100644 --- a/test/scripts/model.js +++ b/test/scripts/model.js @@ -4,7 +4,6 @@ const should = require('chai').should(); const sortBy = require('lodash/sortBy'); const Promise = require('bluebird'); const sinon = require('sinon'); -const util = require('util'); const cuid = require('cuid'); describe('Model', () => { @@ -1250,11 +1249,7 @@ describe('Model', () => { }); it('_export() - should not save undefined value', () => { - const CacheType = function(...args) { - SchemaType.apply(this, args); - }; - - util.inherits(CacheType, SchemaType); + class CacheType extends SchemaType {} CacheType.prototype.value = () => {}; From 86dc315bfeb2dcc8e6fb1c9d2b806192bb19c285 Mon Sep 17 00:00:00 2001 From: segayuu Date: Fri, 19 Oct 2018 11:56:15 +0900 Subject: [PATCH 2/3] Update README.md - Writing a text page assuming that major version upgrade was done. --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 7bf1671f..6786ffcc 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,33 @@ A JSON database with Models, Schemas, and a flexible querying interface. It powe $ npm install warehouse ``` +## 3.0 BREAKING CHANGE + +In `warehouse@3`, the constructor has been changed from function declaration to `class` declaration or definition by `class` expression. +Derived classes of classes defined by `class` declarations and `class` expressions must also be defined in `class` declaration, `class` expression. +Anyone who created their own SchemaType will need to change. + +``` js +const SchemaType = require('warehouse/schematype'); + +class MySchemaType extends SchemaType { + constructor(name, options = {}) { + super(name, Object.assign({ foo: 'foo' }, options)); + } +} +``` + +It changes to a class declaration or a class expression, but it does not need to deal with other than the definition of the constructor. + +``` js +// It work! + +MySchemaType.prototype.cast = function (value, data) { + let result = SchemaType.prototype.cast.call(this, value, data); + return result ? result : ''; +} +``` + ## Usage ``` js From d75d4e994b579dac0975cf37491ff813872c58a9 Mon Sep 17 00:00:00 2001 From: segayuu Date: Sun, 23 Jun 2019 16:18:34 +0900 Subject: [PATCH 3/3] Fix Lint: no-unused-vars --- lib/types/date.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/types/date.js b/lib/types/date.js index bf5610fa..9f38d59e 100644 --- a/lib/types/date.js +++ b/lib/types/date.js @@ -1,7 +1,6 @@ 'use strict'; const SchemaType = require('../schematype'); -const util = require('../util'); const ValidationError = require('../error/validation'); /**