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(){
|
||||
|
||||
@@ -30,7 +30,7 @@ class GameObjectsManager{
|
||||
* @param {GameObject} data Asset 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);
|
||||
if (ctx.files?.file){
|
||||
await this.addFile(data, ctx.files.file)
|
||||
|
||||
@@ -19,7 +19,7 @@ class GamesManager{
|
||||
* @param {Game} data the game description, дефиниция на играта
|
||||
*/
|
||||
this.create = async function(ctx, data){
|
||||
data.id = (await db.getLastId(collection)) + 1;
|
||||
data.id = await db.getId(collection);
|
||||
await db.create(collection, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ class ScenariosManager{
|
||||
* @param {Scenario} data the scenario, данни за сценария
|
||||
*/
|
||||
this.create = async function(ctx, data){
|
||||
data.id = (await db.getLastId(collection)) + 1;
|
||||
data.id = await db.getId(collection);
|
||||
await db.create(collection, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
+2
-2
@@ -12,7 +12,7 @@ class MockDb {
|
||||
this.data = {};
|
||||
this.lastId = 0;
|
||||
}
|
||||
async getLastId() { return this.lastId; }
|
||||
async getId() { return this.lastId; }
|
||||
async create(collection, obj) {
|
||||
this.lastId++;
|
||||
obj.id = this.lastId;
|
||||
@@ -43,7 +43,7 @@ class GamesManager {
|
||||
init(app) {
|
||||
const db = app.db;
|
||||
this.create = async (ctx, data) => {
|
||||
data.id = (await db.getLastId()) + 1;
|
||||
data.id = await db.getId('games');
|
||||
await db.create('games', data);
|
||||
return data;
|
||||
};
|
||||
|
||||
@@ -45,7 +45,7 @@ function createMockDb() {
|
||||
let store = {};
|
||||
let lastId = 0;
|
||||
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 }; }),
|
||||
get: vi.fn(async (coll, query) => store[query.id]),
|
||||
update: vi.fn(async (coll, query, obj) => { store[query.id] = { ...store[query.id], ...obj }; }),
|
||||
|
||||
Reference in New Issue
Block a user