Skip to content

Commit

Permalink
Improved mongodb api code
Browse files Browse the repository at this point in the history
  • Loading branch information
silversonicaxel committed Sep 5, 2024
1 parent 707d53e commit 5c8aa7e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/api/country/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import type { CountryCode } from 'src/types/country'

export const getCountryCodes = async (): Promise<CountryCode> => {
const mongoClient = await clientPromise
const data = await mongoClient

const countryCodes = await mongoClient
.db('ua-db')
.collection('ua-isos')
.find({}, { projection: { _id: 0 } })
.collation({ locale: 'en' })
.toArray()
.findOne({}, { projection: { _id: 0 } })

return data[0]
return countryCodes === null ? {} : countryCodes
}
12 changes: 6 additions & 6 deletions src/api/place/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,37 @@ export const getPlacesList = async (query: ApiQuery = {}): Promise<Place[]> => {
const { findFilter, findOptions } = getFindParams(query)

const mongoClient = await clientPromise
const data = await mongoClient
const places = await mongoClient
.db('ua-db')
.collection('ua-places')
.find(findFilter, findOptions)
.collation({ locale: 'en' })
.sort({ name: 1 })
.toArray()

return JSON.parse(JSON.stringify(data))
return JSON.parse(JSON.stringify(places))
}

export const getPlace = async (id: string): Promise<Place> => {
const mongoClient = await clientPromise
const data = await mongoClient
const place = await mongoClient
.db('ua-db')
.collection('ua-places')
.findOne({ id })

return JSON.parse(JSON.stringify(data))
return JSON.parse(JSON.stringify(place))
}

export const getTotalPlaces = async (query: ApiQuery = {}): Promise<number> => {
const { findFilter } = getFindParams(query)

const mongoClient = await clientPromise
const data = await mongoClient
const places = await mongoClient
.db('ua-db')
.collection('ua-places')
.find(findFilter)
.collation({ locale: 'en' })
.toArray()

return data.length
return places.length
}
4 changes: 2 additions & 2 deletions src/api/quote/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import type { Quote } from 'src/types/quote'

export const getRandomQuote = async (): Promise<Quote> => {
const mongoClient = await clientPromise
const data = await mongoClient
const quotes = await mongoClient
.db('ua-db')
.collection('ua-quotes')
.aggregate([{ $sample: { size: 1 } }])
.toArray()

return JSON.parse(JSON.stringify(data[0]))
return JSON.parse(JSON.stringify(quotes[0]))
}

0 comments on commit 5c8aa7e

Please sign in to comment.