✨ feat: unify API options structure and enhance lookup handling across collections
This commit is contained in:
+9
-35
@@ -149,63 +149,37 @@ type EntryTypeSwitch<T extends string> = T extends "medialib"
|
||||
|
||||
export async function getDBEntries<T extends CollectionNameT>(
|
||||
collectionName: T,
|
||||
filter?: MongoFilter,
|
||||
sort: string = "sort",
|
||||
limit?: number,
|
||||
offset?: number,
|
||||
projection?: string,
|
||||
params?: Record<string, string>,
|
||||
lookup?: string
|
||||
options?: ApiOptions
|
||||
): Promise<EntryTypeSwitch<T>[]> {
|
||||
const c = await api<EntryTypeSwitch<T>[]>(collectionName, {
|
||||
filter,
|
||||
sort,
|
||||
limit,
|
||||
offset,
|
||||
projection,
|
||||
params,
|
||||
lookup,
|
||||
})
|
||||
const c = await api<EntryTypeSwitch<T>[]>(collectionName, options)
|
||||
return c.data
|
||||
}
|
||||
|
||||
export async function getCachedEntries<T extends CollectionNameT>(
|
||||
collectionName: T,
|
||||
filter?: MongoFilter,
|
||||
sort: string = "sort",
|
||||
limit?: number,
|
||||
offset?: number,
|
||||
projection?: string,
|
||||
params?: Record<string, string>,
|
||||
lookup?: string
|
||||
options?: ApiOptions
|
||||
): Promise<EntryTypeSwitch<T>[]> {
|
||||
const filterStr = obj2str({ collectionName, filter, sort, limit, offset, projection, params, lookup })
|
||||
const filterStr = obj2str({ collectionName, options })
|
||||
if (cache[filterStr] && cache[filterStr].expire >= Date.now()) {
|
||||
return cache[filterStr].data as EntryTypeSwitch<T>[]
|
||||
}
|
||||
const entries = await getDBEntries<T>(collectionName, filter, sort, limit, offset, projection, params, lookup)
|
||||
const entries = await getDBEntries<T>(collectionName, options)
|
||||
cache[filterStr] = { expire: Date.now() + CACHE_TTL, data: entries }
|
||||
return entries
|
||||
}
|
||||
|
||||
export async function getDBEntry<T extends CollectionNameT>(
|
||||
collectionName: T,
|
||||
filter: MongoFilter,
|
||||
projection?: string,
|
||||
params?: Record<string, string>,
|
||||
lookup?: string
|
||||
options?: ApiOptions
|
||||
): Promise<EntryTypeSwitch<T> | undefined> {
|
||||
return (await getDBEntries<T>(collectionName, filter, "_id", 1, undefined, projection, params, lookup))?.[0]
|
||||
return (await getDBEntries<T>(collectionName, { ...options, limit: 1 }))?.[0]
|
||||
}
|
||||
|
||||
export async function getCachedEntry<T extends CollectionNameT>(
|
||||
collectionName: T,
|
||||
filter: MongoFilter,
|
||||
projection?: string,
|
||||
params?: Record<string, string>,
|
||||
lookup?: string
|
||||
options?: ApiOptions
|
||||
): Promise<EntryTypeSwitch<T> | undefined> {
|
||||
return (await getCachedEntries<T>(collectionName, filter, "_id", 1, undefined, projection, params, lookup))?.[0]
|
||||
return (await getCachedEntries<T>(collectionName, { ...options, limit: 1 }))?.[0]
|
||||
}
|
||||
|
||||
export async function postDBEntry<T extends CollectionNameT>(
|
||||
|
||||
@@ -242,7 +242,7 @@ function cloneEntry<T>(entry: T): T {
|
||||
}
|
||||
|
||||
function applyAggregate(entry: Record<string, unknown>, options?: ApiOptions): Record<string, unknown> {
|
||||
const rawAggregate = options?.params?.aggregate
|
||||
const rawAggregate = options?.aggregate || options?.params?.aggregate
|
||||
if (!rawAggregate) return entry
|
||||
|
||||
const aggregates = Array.isArray(rawAggregate)
|
||||
|
||||
Reference in New Issue
Block a user