objects IDs counter refactoring

This commit is contained in:
2026-02-07 11:46:52 +02:00
parent b72d386b73
commit 06d485e63a
6 changed files with 14 additions and 19 deletions
+8 -13
View File
@@ -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(){