gameobject module dev
This commit is contained in:
+23
-1
@@ -3,10 +3,13 @@ import { ObjectId, MongoClient } from 'mongodb';
|
||||
let db;
|
||||
let dbo;
|
||||
|
||||
/**
|
||||
* Manages database operations
|
||||
*/
|
||||
class Db {
|
||||
name = 'db';
|
||||
init(app){
|
||||
this.app = app;
|
||||
|
||||
}
|
||||
|
||||
async start(app){
|
||||
@@ -23,6 +26,12 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts a record in a db collection
|
||||
* @param {string} collection The name of the collection
|
||||
* @param {Object} value The object to insert
|
||||
* @returns Inserted Id
|
||||
*/
|
||||
async create(collection, value){
|
||||
try {
|
||||
delete value._id;
|
||||
@@ -31,6 +40,13 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a record from db collection
|
||||
* @param {string} collection The name of the collection
|
||||
* @param {Object} key Record identifier
|
||||
* @param {Object} projection What data to take from the object
|
||||
* @returns A record
|
||||
*/
|
||||
async get(collection, key, projection){
|
||||
try {
|
||||
let res = await dbo.collection(collection).findOne(key, projection ? {projection} : undefined)
|
||||
@@ -39,6 +55,12 @@ class Db {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a database query
|
||||
* @param {string} collection Collection name
|
||||
* @param {Object} query A mongo db query
|
||||
* @returns Array of records
|
||||
*/
|
||||
async list(collection, query){
|
||||
try {
|
||||
let cursor = dbo.collection(collection).find(query.query, query.project ? {projection: query.project} : undefined);
|
||||
|
||||
Reference in New Issue
Block a user