From c393c41184a4afe58d113681be64ae659d3c5469 Mon Sep 17 00:00:00 2001 From: Yasamato Date: Mon, 9 Dec 2024 12:15:17 +0100 Subject: [PATCH] Correct usage of dbClient (#116) --- lib/db/itemScreenshots.ts | 33 +++++++++++++++++++++++---------- lib/db/views.ts | 4 +++- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/db/itemScreenshots.ts b/lib/db/itemScreenshots.ts index a6a443e1..1b413ef0 100644 --- a/lib/db/itemScreenshots.ts +++ b/lib/db/itemScreenshots.ts @@ -32,7 +32,8 @@ export async function addItemScreenshot(img: Uint8Array, itemId: string) { imgStream.push(img) imgStream.push(null) - const db = (await dbClient).db('index') + const client = await dbClient().connect() + const db = client.db('index') const bucket = new GridFSBucket(db, { bucketName: 'itemScreenshots', }) @@ -56,39 +57,51 @@ export async function addItemScreenshot(img: Uint8Array, itemId: string) { stream.on('finish', resolve) imgStream.on('error', reject) }) + client.close() } export async function getItemScreenshotBuffer(itemId: string) { - const db = (await dbClient).db('index') + const client = await dbClient().connect() + const db = client.db('index') const bucket = new GridFSBucket(db, { bucketName: 'itemScreenshots', }) - const stream = bucket.openDownloadStreamByName(itemId) - return await streamToBuffer(stream) + const data = await streamToBuffer(bucket.openDownloadStreamByName(itemId)) + client.close() + return data } export async function screenshotExists(itemId: string) { - const db = (await dbClient).db('index') + const client = await dbClient().connect() + const db = client.db('index') const bucket = new GridFSBucket(db, { bucketName: 'itemScreenshots', }) const cursor = await bucket.find({ filename: itemId }) - return await cursor.hasNext() + const data = await cursor.hasNext() + client.close() + return data } export async function clearAllScreenshots() { - const db = (await dbClient).db('index') + const client = await dbClient().connect() + const db = client.db('index') const bucket = new GridFSBucket(db, { bucketName: 'itemScreenshots', }) - return await bucket.drop() + const data = await bucket.drop() + client.close() + return data } export async function listScreenshotsOfItem(itemId: string) { - const db = (await dbClient).db('index') + const client = await dbClient().connect() + const db = client.db('index') const bucket = new GridFSBucket(db, { bucketName: 'itemScreenshots', }) - return bucket.find({ filename: itemId }) + const data = bucket.find({ filename: itemId }) + client.close() + return data } diff --git a/lib/db/views.ts b/lib/db/views.ts index 0a183c15..ce2af4b6 100644 --- a/lib/db/views.ts +++ b/lib/db/views.ts @@ -8,7 +8,8 @@ export async function getViews() { } export async function getLastViews(type: Types, n: number) { - const db = (await dbClient).db('index') + const client = await dbClient().connect() + const db = client.db('index') const data = cleanId( await db .collection('views') @@ -17,6 +18,7 @@ export async function getLastViews(type: Types, n: number) { .limit(n) .toArray() ) + client.close() console.log('Found', data.length, 'entries in views table for', type) // count what has been popular recently