objects IDs counter refactoring
This commit is contained in:
+8
-13
@@ -26,7 +26,7 @@ class Db {
|
||||
try {
|
||||
dbo = db.db(app.config.db.name);
|
||||
this.instance = dbo;
|
||||
for (let c of ['users', 'user_sessions', 'history', 'log', 'assets', 'scenarios', 'games']){
|
||||
for (let c of ['users', 'user_sessions', 'history', 'log', 'assets', 'scenarios', 'games', 'config']){
|
||||
try {
|
||||
await dbo.createCollection(c);
|
||||
}catch(err){}
|
||||
@@ -250,18 +250,13 @@ class Db {
|
||||
* Gets last asset Id from database, намира последния пореден идентификатор на обект в базата от данни
|
||||
* @returns {Number} Last Asset Id, последен (най-голям) идентификатор
|
||||
*/
|
||||
async getLastId(collection){
|
||||
let ag = await this.aggregate(collection, [
|
||||
{
|
||||
$group:{
|
||||
_id: null,
|
||||
max: {
|
||||
$max: "$id",
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
return ag.max || 0;
|
||||
async getId(collection){
|
||||
let newId = (await dbo.collection('config').findOneAndUpdate(
|
||||
{ 'counter': { $exists: true } },
|
||||
{ $inc: { 'counter.value': 1 } },
|
||||
{ upsert: true, returnDocument: 'after' }
|
||||
)).counter.value;
|
||||
return newId;
|
||||
}
|
||||
|
||||
async stop(){
|
||||
|
||||
Reference in New Issue
Block a user