update typings

This commit is contained in:
Erik 2023-05-07 03:37:14 +03:00
parent bc3a1d8e4c
commit da1a45e9f0
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68

View File

@ -187,7 +187,7 @@ class MongoDB {
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
* @memberof Database * @memberof Database
*/ */
async updateMany (db: string, filter: MongoQuery, data: object, upsert = false) { async updateMany (db: string, filter: MongoQuery, data: Document, upsert = false) {
if (!this.#db) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
@ -195,7 +195,6 @@ class MongoDB {
throw new TypeError('Expecting collection name for the first argument'); throw new TypeError('Expecting collection name for the first argument');
if (!filter) if (!filter)
throw new Error(`Cannot run update many without a filter, if you mean to update every single document, pass an empty object`); throw new Error(`Cannot run update many without a filter, if you mean to update every single document, pass an empty object`);
if (typeof filter._id === 'string') if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id); filter._id = new ObjectId(filter._id);
@ -214,13 +213,12 @@ class MongoDB {
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
* @memberof Database * @memberof Database
*/ */
async updateOne (db: string, filter: MongoQuery, data: object, upsert = false) { async updateOne (db: string, filter: MongoQuery, data: Document, upsert = false) {
if (!this.#db) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); throw new TypeError('Expecting collection name for the first argument');
if (typeof filter._id === 'string') if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id); filter._id = new ObjectId(filter._id);
@ -239,12 +237,14 @@ class MongoDB {
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified * @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
* @memberof Database * @memberof Database
*/ */
async insertOne (db: string, data: object) { async insertOne (db: string, data: Document) {
if (!this.#db) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); throw new TypeError('Expecting collection name for the first argument');
if (typeof data._id === 'string')
data._id = new ObjectId(data._id);
this.#logger.debug(`Incoming insertOne query for ${db} with parameters ${inspect(data)}`); this.#logger.debug(`Incoming insertOne query for ${db} with parameters ${inspect(data)}`);
const result = await this.#db.collection(db).insertOne(data); const result = await this.#db.collection(db).insertOne(data);
@ -252,12 +252,14 @@ class MongoDB {
} }
async deleteOne (db: string, filter: object) { async deleteOne (db: string, filter: Document) {
if (!this.#db) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); throw new TypeError('Expecting collection name for the first argument');
if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id);
this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`); this.#logger.debug(`Incoming deleteOne query for ${db} with parameters ${inspect(filter)}`);
const result = await this.#db.collection(db).deleteOne(filter); const result = await this.#db.collection(db).deleteOne(filter);
@ -275,12 +277,14 @@ class MongoDB {
* @returns * @returns
* @memberof Database * @memberof Database
*/ */
async push (db: string, filter: object, data: object, upsert = false) { async push (db: string, filter: Document, data: object, upsert = false) {
if (!this.#db) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); throw new TypeError('Expecting collection name for the first argument');
if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id);
this.#logger.debug(`Incoming push query for ${db}, with upsert ${upsert} and with parameters ${inspect(filter)} and data ${inspect(data)}`); this.#logger.debug(`Incoming push query for ${db}, with upsert ${upsert} and with parameters ${inspect(filter)} and data ${inspect(data)}`);
const result = await this.#db.collection(db).updateOne(filter, { $push: data }, { upsert }); const result = await this.#db.collection(db).updateOne(filter, { $push: data }, { upsert });
@ -297,12 +301,14 @@ class MongoDB {
* @returns {object} * @returns {object}
* @memberof Database * @memberof Database
*/ */
random (db: string, filter = {}, amount = 1) { random (db: string, filter: Document = {}, amount = 1) {
if (!this.#db) if (!this.#db)
throw new Error(`MongoDB not connected`); throw new Error(`MongoDB not connected`);
if (typeof db !== 'string') if (typeof db !== 'string')
throw new TypeError('Expecting collection name for the first argument'); throw new TypeError('Expecting collection name for the first argument');
if (typeof filter._id === 'string')
filter._id = new ObjectId(filter._id);
this.#logger.debug(`Incoming random query for ${db} with parameters ${inspect(filter)} and amount ${amount}`); this.#logger.debug(`Incoming random query for ${db} with parameters ${inspect(filter)} and amount ${amount}`);
@ -335,18 +341,6 @@ class MongoDB {
await this.#db.collection(collection).createIndex(indices); await this.#db.collection(collection).createIndex(indices);
} }
// async getKey (key: string, collection = 'memoryStore') {
// const response = await this.findOne(collection, { key });
// if (response)
// return response.value;
// return null;
// }
// async setKey (key: string, value: object, collection = 'memoryStore') {
// await this.updateOne(collection, { key }, { value }, true);
// return value;
// }
} }
export { MongoDB }; export { MongoDB };