Skip to content

Commit

Permalink
[BREAKING CHANGE] Replace constructor function to class declaration (#30
Browse files Browse the repository at this point in the history
)

* Replace constructor function to class declaration

* Update README.md
 - Writing a text page assuming that major version upgrade was done.

* Fix Lint: no-unused-vars
  • Loading branch information
segayuu committed Jul 25, 2019
1 parent 28883c2 commit 34399c5
Show file tree
Hide file tree
Showing 24 changed files with 340 additions and 407 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 23 additions & 23 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down
26 changes: 14 additions & 12 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}
}
Expand Down
38 changes: 17 additions & 21 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
25 changes: 11 additions & 14 deletions lib/error/population.js
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 5 additions & 7 deletions lib/error/validation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const util = require('../util');
const WarehouseError = require('../error');

/**
Expand All @@ -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;
117 changes: 59 additions & 58 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.
*
Expand Down
8 changes: 5 additions & 3 deletions lib/mutex.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Loading

0 comments on commit 34399c5

Please sign in to comment.