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 { try {
dbo = db.db(app.config.db.name); dbo = db.db(app.config.db.name);
this.instance = dbo; 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 { try {
await dbo.createCollection(c); await dbo.createCollection(c);
}catch(err){} }catch(err){}
@@ -250,18 +250,13 @@ class Db {
* Gets last asset Id from database, намира последния пореден идентификатор на обект в базата от данни * Gets last asset Id from database, намира последния пореден идентификатор на обект в базата от данни
* @returns {Number} Last Asset Id, последен (най-голям) идентификатор * @returns {Number} Last Asset Id, последен (най-голям) идентификатор
*/ */
async getLastId(collection){ async getId(collection){
let ag = await this.aggregate(collection, [ let newId = (await dbo.collection('config').findOneAndUpdate(
{ { 'counter': { $exists: true } },
$group:{ { $inc: { 'counter.value': 1 } },
_id: null, { upsert: true, returnDocument: 'after' }
max: { )).counter.value;
$max: "$id", return newId;
},
},
},
]);
return ag.max || 0;
} }
async stop(){ async stop(){
+1 -1
View File
@@ -30,7 +30,7 @@ class GameObjectsManager{
* @param {GameObject} data Asset data, данни за игровия обект * @param {GameObject} data Asset data, данни за игровия обект
*/ */
this.create = async function(ctx, data){ this.create = async function(ctx, data){
data.id = (await db.getLastId(collection)) + 1; data.id = await db.getId(collection);
await db.create(collection, data); await db.create(collection, data);
if (ctx.files?.file){ if (ctx.files?.file){
await this.addFile(data, ctx.files.file) await this.addFile(data, ctx.files.file)
+1 -1
View File
@@ -19,7 +19,7 @@ class GamesManager{
* @param {Game} data the game description, дефиниция на играта * @param {Game} data the game description, дефиниция на играта
*/ */
this.create = async function(ctx, data){ this.create = async function(ctx, data){
data.id = (await db.getLastId(collection)) + 1; data.id = await db.getId(collection);
await db.create(collection, data); await db.create(collection, data);
return data; return data;
} }
+1 -1
View File
@@ -20,7 +20,7 @@ class ScenariosManager{
* @param {Scenario} data the scenario, данни за сценария * @param {Scenario} data the scenario, данни за сценария
*/ */
this.create = async function(ctx, data){ this.create = async function(ctx, data){
data.id = (await db.getLastId(collection)) + 1; data.id = await db.getId(collection);
await db.create(collection, data); await db.create(collection, data);
return data; return data;
} }
+2 -2
View File
@@ -12,7 +12,7 @@ class MockDb {
this.data = {}; this.data = {};
this.lastId = 0; this.lastId = 0;
} }
async getLastId() { return this.lastId; } async getId() { return this.lastId; }
async create(collection, obj) { async create(collection, obj) {
this.lastId++; this.lastId++;
obj.id = this.lastId; obj.id = this.lastId;
@@ -43,7 +43,7 @@ class GamesManager {
init(app) { init(app) {
const db = app.db; const db = app.db;
this.create = async (ctx, data) => { this.create = async (ctx, data) => {
data.id = (await db.getLastId()) + 1; data.id = await db.getId('games');
await db.create('games', data); await db.create('games', data);
return data; return data;
}; };
+1 -1
View File
@@ -45,7 +45,7 @@ function createMockDb() {
let store = {}; let store = {};
let lastId = 0; let lastId = 0;
return { return {
getLastId: vi.fn(async () => lastId), getId: vi.fn(async () => lastId),
create: vi.fn(async (coll, obj) => { lastId++; obj.id = lastId; store[obj.id] = { ...obj }; }), create: vi.fn(async (coll, obj) => { lastId++; obj.id = lastId; store[obj.id] = { ...obj }; }),
get: vi.fn(async (coll, query) => store[query.id]), get: vi.fn(async (coll, query) => store[query.id]),
update: vi.fn(async (coll, query, obj) => { store[query.id] = { ...store[query.id], ...obj }; }), update: vi.fn(async (coll, query, obj) => { store[query.id] = { ...store[query.id], ...obj }; }),