Skip to content

Commit

Permalink
Correct usage of dbClient (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasamato authored Dec 9, 2024
1 parent f2b4066 commit c393c41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
33 changes: 23 additions & 10 deletions lib/db/itemScreenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
Expand All @@ -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
}
4 changes: 3 additions & 1 deletion lib/db/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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
Expand Down

0 comments on commit c393c41

Please sign in to comment.