-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Adamant-im/dev
v5.5.0
- Loading branch information
Showing
18 changed files
with
1,871 additions
and
685 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,78 @@ | ||
module.exports = (db) => { | ||
return class { | ||
constructor(data = {}, isSave) { | ||
class Model { | ||
constructor(data = {}, shouldSave) { | ||
this.db = db; | ||
|
||
Object.assign(this, data); | ||
if (isSave) { | ||
|
||
if (shouldSave) { | ||
this.save(); | ||
} | ||
} | ||
static get db() { | ||
return db; | ||
} | ||
static find(a) { // return Array | ||
return new Promise((resolve, reject) => { | ||
this.db.find(a).toArray((err, data) => { | ||
resolve(data.map((d)=>new this(d))); | ||
}); | ||
}); | ||
static async find(req) { | ||
const data = await db.find(req).toArray(); | ||
|
||
return data.map((d) => new this(d)); | ||
} | ||
static aggregate(a) { // return Array | ||
return new Promise((resolve, reject) => { | ||
this.db.aggregate(a).toArray((err, data) => { | ||
resolve(data.map((d)=>new this(d))); | ||
}); | ||
}); | ||
static async aggregate(req) { | ||
const data = await db.aggregate(req).toArray(); | ||
|
||
return data.map((d) => new this(d)); | ||
} | ||
static findOne(a) { | ||
return new Promise((resolve, reject) => { | ||
db.findOne(a).then((doc, b) => { | ||
if (!doc) { | ||
resolve(doc); | ||
} else { | ||
resolve(new this(doc)); | ||
} | ||
}); | ||
}); | ||
static async findOne(req) { | ||
const doc = await db.findOne(req); | ||
|
||
return doc ? new this(doc) : doc; | ||
} | ||
static deleteOne(a) { | ||
return new Promise((resolve, reject) => { | ||
delete a.db; | ||
db.deleteOne(a).then((res) => { | ||
resolve(res.deletedCount); | ||
}); | ||
}); | ||
static async deleteOne(req) { | ||
delete req.db; | ||
|
||
const { deletedCount } = await db.deleteOne(req); | ||
|
||
return deletedCount; | ||
} | ||
static count(a) { | ||
return new Promise((resolve, reject) => { | ||
db.count(a).then((count) => { | ||
resolve(count); | ||
}); | ||
}); | ||
static async count(req) { | ||
const count = await db.count(req); | ||
|
||
return count; | ||
} | ||
_data() { | ||
const data = {}; | ||
for (const field in this) { | ||
if (!['db', '_id'].includes(field)) { | ||
data[field] = this[field]; | ||
|
||
for (const fieldName in this) { | ||
if (Object.prototype.hasOwnProperty.call(this, fieldName)) { | ||
if (!['db', '_id'].includes(fieldName)) { | ||
data[fieldName] = this[fieldName]; | ||
} | ||
} | ||
} | ||
|
||
return data; | ||
} | ||
async update(obj, isSave) { | ||
async update(obj, shouldSave) { | ||
Object.assign(this, obj); | ||
if (isSave) { | ||
|
||
if (shouldSave) { | ||
await this.save(); | ||
} | ||
} | ||
save() { | ||
return new Promise((resolve, reject) => { | ||
if (!this._id) { | ||
db.insertOne(this._data(), (err, res) => { | ||
this._id = res.insertedId; | ||
resolve(this._id); | ||
}); | ||
} else { | ||
db.updateOne({ _id: this._id }, { | ||
$set: this._data(), | ||
}, { upsert: true }).then(() => { | ||
resolve(this._id); | ||
}); | ||
} | ||
}); | ||
async save() { | ||
if (!this._id) { | ||
const res = await db.insertOne(this._data()); | ||
this._id = res.insertedId; | ||
return this._id; | ||
} else { | ||
await db.updateOne({ _id: this._id }, { | ||
$set: this._data(), | ||
}, { upsert: true }); | ||
|
||
return this._id; | ||
} | ||
} | ||
}; | ||
|
||
return Model; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.