Skip to content

Commit

Permalink
Convert method definitions (#52)
Browse files Browse the repository at this point in the history
* Revert "[BREAKING CHANGE] Replace constructor function to class declaration (#30)"

This reverts commit 34399c5.

* Run lebab

* Restore README.md before revert

* Perform class conversion not handled by lebab

* Remove unnecessary differences with master branch
  • Loading branch information
segayuu authored Sep 7, 2019
1 parent 78acb47 commit 109151a
Show file tree
Hide file tree
Showing 19 changed files with 2,929 additions and 2,893 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ Post.insert({

``` bash
$ npm test
```
```
188 changes: 94 additions & 94 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,106 +37,106 @@ class Database {

_Model.prototype._database = this;
}
}

/**
* Creates a new model.
*
* @param {string} name
* @param {Schema|object} [schema]
* @return {Model}
*/
Database.prototype.model = function(name, schema) {
if (this._models[name]) {
return this._models[name];
/**
* Creates a new model.
*
* @param {string} name
* @param {Schema|object} [schema]
* @return {Model}
*/
model(name, schema) {
if (this._models[name]) {
return this._models[name];
}

const model = this._models[name] = new this.Model(name, schema);
return model;
}

const model = this._models[name] = new this.Model(name, schema);
return model;
};

/**
* Loads database.
*
* @param {function} [callback]
* @return {Promise}
*/
Database.prototype.load = function(callback) {
const path = this.options.path;

if (!path) throw new WarehouseError('options.path is required');

return new Promise((resolve, reject) => {
const src = fs.createReadStream(path, {encoding: 'utf8'});
let oldVersion = 0;

const stream = JSONStream.parse([true, true], (value, keys) => {
switch (keys.shift()) {
case 'meta':
if (keys.shift() === 'version') {
oldVersion = value;
}

break;

case 'models':
this.model(keys.shift())._import(value);
break;
}
});
/**
* Loads database.
*
* @param {function} [callback]
* @return {Promise}
*/
load(callback) {
const path = this.options.path;

if (!path) throw new WarehouseError('options.path is required');

return new Promise((resolve, reject) => {
const src = fs.createReadStream(path, {encoding: 'utf8'});
let oldVersion = 0;

src
.pipe(stream)
.on('error', reject)
.on('end', () => {
resolve(oldVersion);
const stream = JSONStream.parse([true, true], (value, keys) => {
switch (keys.shift()) {
case 'meta':
if (keys.shift() === 'version') {
oldVersion = value;
}

break;

case 'models':
this.model(keys.shift())._import(value);
break;
}
});
}).then(oldVersion => {
const newVersion = this.options.version;

if (newVersion > oldVersion) {
return this.options.onUpgrade(oldVersion, newVersion);
} else if (newVersion < oldVersion) {
return this.options.onDowngrade(oldVersion, newVersion);
}
}).asCallback(callback);
};

/**
* Saves database.
*
* @param {function} [callback]
* @return {Promise}
*/
Database.prototype.save = function(callback) {
const { path } = this.options;

if (!path) throw new WarehouseError('options.path is required');

const rs = new JsonReadable(this);

const ws = fs.createWriteStream(path);

return new Promise((resolve, reject) => {
rs.once('error', reject).pipe(ws).once('error', reject).once('finish', resolve);
}).asCallback(callback);
};

Database.prototype.toJSON = function() {
const models = Object.keys(this._models)
.reduce((obj, key) => {
const value = this._models[key];
if (value != null) obj[key] = value;
return obj;
}, {});

return {
meta: {
version: this.options.version,
warehouse: pkg.version
}, models
};
};
src
.pipe(stream)
.on('error', reject)
.on('end', () => {
resolve(oldVersion);
});
}).then(oldVersion => {
const newVersion = this.options.version;

if (newVersion > oldVersion) {
return this.options.onUpgrade(oldVersion, newVersion);
} else if (newVersion < oldVersion) {
return this.options.onDowngrade(oldVersion, newVersion);
}
}).asCallback(callback);
}

/**
* Saves database.
*
* @param {function} [callback]
* @return {Promise}
*/
save(callback) {
const { path } = this.options;

if (!path) throw new WarehouseError('options.path is required');

const rs = new JsonReadable(this);

const ws = fs.createWriteStream(path);

return new Promise((resolve, reject) => {
rs.once('error', reject).pipe(ws).once('error', reject).once('finish', resolve);
}).asCallback(callback);
}

toJSON() {
const models = Object.keys(this._models)
.reduce((obj, key) => {
const value = this._models[key];
if (value != null) obj[key] = value;
return obj;
}, {});

return {
meta: {
version: this.options.version,
warehouse: pkg.version
}, models
};
}
}

Database.Schema = Database.prototype.Schema = Schema;
Database.SchemaType = Database.prototype.SchemaType = SchemaType;
Expand Down
148 changes: 74 additions & 74 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,91 @@ class Document {
Object.assign(this, data);
}
}
}

/**
* Saves the document.
*
* @param {function} [callback]
* @return {Promise}
*/
Document.prototype.save = function(callback) {
return this._model.save(this, callback);
};
/**
* Saves the document.
*
* @param {function} [callback]
* @return {Promise}
*/
save(callback) {
return this._model.save(this, callback);
}

/**
* Updates the document.
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
*/
update(data, callback) {
return this._model.updateById(this._id, data, callback);
}

/**
* Replaces the document.
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
*/
replace(data, callback) {
return this._model.replaceById(this._id, data, callback);
}

/**
* Updates the document.
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
*/
Document.prototype.update = function(data, callback) {
return this._model.updateById(this._id, data, callback);
};
/**
* Removes the document.
*
* @param {function} [callback]
* @return {Promise}
*/
remove(callback) {
return this._model.removeById(this._id, callback);
}

/**
* Replaces the document.
*
* @param {object} data
* @param {function} [callback]
* @return {Promise}
*/
Document.prototype.replace = function(data, callback) {
return this._model.replaceById(this._id, data, callback);
};
/**
* Returns a plain JavaScript object.
*
* @return {object}
*/
toObject() {
const keys = Object.keys(this);
const obj = {};

/**
* Removes the document.
*
* @param {function} [callback]
* @return {Promise}
*/
Document.prototype.remove = function(callback) {
return this._model.removeById(this._id, callback);
};
for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
// Don't deep clone getters in order to avoid "Maximum call stack size
// exceeded" error
obj[key] = isGetter(this, key) ? this[key] : cloneDeep(this[key]);
}

/**
* Returns a plain JavaScript object.
*
* @return {object}
*/
Document.prototype.toObject = function() {
const keys = Object.keys(this);
const obj = {};
return obj;
}

for (let i = 0, len = keys.length; i < len; i++) {
const key = keys[i];
// Don't deep clone getters in order to avoid "Maximum call stack size
// exceeded" error
obj[key] = isGetter(this, key) ? this[key] : cloneDeep(this[key]);
/**
* Returns a string representing the document.
*
* @return {String}
*/
toString() {
return JSON.stringify(this);
}

return obj;
};
/**
* Populates document references.
*
* @param {String|Object} expr
* @return {Document}
*/
populate(expr) {
const stack = this._schema._parsePopulate(expr);
return this._model._populate(this, stack);
}
}

function isGetter(obj, key) {
return Object.getOwnPropertyDescriptor(obj, key).get;
}

/**
* Returns a string representing the document.
*
* @return {String}
*/
Document.prototype.toString = function() {
return JSON.stringify(this);
};

/**
* Populates document references.
*
* @param {String|Object} expr
* @return {Document}
*/
Document.prototype.populate = function(expr) {
const stack = this._schema._parsePopulate(expr);
return this._model._populate(this, stack);
};

module.exports = Document;
Loading

0 comments on commit 109151a

Please sign in to comment.