Skip to content

Commit

Permalink
Merge d5a63a8 into ad35d9b
Browse files Browse the repository at this point in the history
  • Loading branch information
segayuu committed Aug 30, 2019
2 parents ad35d9b + d5a63a8 commit bfa934a
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 76 deletions.
7 changes: 1 addition & 6 deletions lib/document.js
Expand Up @@ -11,12 +11,7 @@ class Document {
*/
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];
}
Object.assign(this, data);
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions lib/error.js
Expand Up @@ -11,17 +11,13 @@ class WarehouseError extends Error {
constructor(msg, code) {
super(msg);

if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
Error.captureStackTrace(this);

this.name = 'WarehouseError';
this.code = code;
}
}

WarehouseError.prototype.name = 'WarehouseError';
WarehouseError.ID_EXIST = 'ID_EXIST';
WarehouseError.ID_NOT_EXIST = 'ID_NOT_EXIST';
WarehouseError.ID_UNDEFINED = 'ID_UNDEFINED';
Expand Down
14 changes: 2 additions & 12 deletions lib/error/population.js
Expand Up @@ -2,18 +2,8 @@

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

class PopulationError extends WarehouseError {
class PopulationError extends WarehouseError {}

/**
* PopulationError constructor
*
* @param {string} msg
*/
constructor(msg) {
super(msg);

this.name = 'PopulationError';
}
}
PopulationError.prototype.name = 'PopulationError';

module.exports = PopulationError;
15 changes: 2 additions & 13 deletions lib/error/validation.js
Expand Up @@ -2,19 +2,8 @@

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

/**
* ValidationError constructor
*
* @class
* @param {String} msg
* @extends WarehouseError
*/
class ValidationError extends WarehouseError {
constructor(msg) {
super(msg);
class ValidationError extends WarehouseError {}

this.name = 'ValidationError';
}
}
ValidationError.prototype.name = 'ValidationError';

module.exports = ValidationError;
16 changes: 2 additions & 14 deletions lib/model.js
Expand Up @@ -67,22 +67,10 @@ class Model extends EventEmitter {
_Query.prototype._schema = schema;

// Apply static methods
const statics = schema.statics;
const staticKeys = Object.keys(statics);

for (let i = 0, len = staticKeys.length; i < len; i++) {
const key = staticKeys[i];
this[key] = statics[key];
}
Object.assign(this, schema.statics);

// 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];
}
Object.assign(_Document.prototype, schema.methods);
}
}

Expand Down
27 changes: 2 additions & 25 deletions lib/types/buffer.js
Expand Up @@ -63,7 +63,7 @@ SchemaTypeBuffer.prototype.validate = function(value_, data) {
*/
SchemaTypeBuffer.prototype.compare = (a, b) => {
if (Buffer.isBuffer(a)) {
return Buffer.isBuffer(b) ? bufferCompare(a, b) : 1;
return Buffer.isBuffer(b) ? a.compare(b) : 1;
}

return Buffer.isBuffer(b) ? -1 : 0;
Expand Down Expand Up @@ -101,33 +101,10 @@ SchemaTypeBuffer.prototype.value = function(value, data) {
*/
SchemaTypeBuffer.prototype.match = (value, query, data) => {
if (Buffer.isBuffer(value) && Buffer.isBuffer(query)) {
return bufferEqual(value, query);
return value.equals(query);
}

return value === query;
};

function bufferCompare(a, b) {
if (typeof a.compare === 'function') return a.compare(b);

if (a > b) {
return 1;
} else if (a < b) {
return -1;
}

return 0;
}

function bufferEqual(a, b) {
if (typeof a.equals === 'function') return a.equals(b);
if (a.length !== b.length) return false;

for (let i = 0, len = a.length; i < len; i++) {
if (a[i] !== b[i]) return false;
}

return true;
}

module.exports = SchemaTypeBuffer;

0 comments on commit bfa934a

Please sign in to comment.