Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create index for path base search #1401

Merged
merged 9 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ export default defineNuxtModule<ModuleOptions>({
route: `/api/${options.base}/query`,
handler: resolveRuntimeModule('./server/api/query')
},
{
method: 'get',
route: `/api/${options.base}/find_one/:qid/**:slug`,
farnabaz marked this conversation as resolved.
Show resolved Hide resolved
handler: resolveRuntimeModule('./server/api/findOne')
},
{
method: 'get',
route: `/api/${options.base}/cache`,
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/composables/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export const createQueryFetch = <T = ParsedContent>(path?: string) => (query: Qu

const params = query.params()

const apiPath = withContentBase(process.dev ? '/query' : `/query/${hash(params)}`)
let apiPath
if (params.where?.length === 1 && Object.keys(params.where[0]).length === 1 && typeof params.where[0]._path === 'string') {
apiPath = withContentBase(`/find_one/${hash(params)}/${params.where[0]._path}`)
} else {
apiPath = withContentBase(process.dev ? '/query' : `/query/${hash(params)}`)
}

// Prefetch the query
if (!process.dev && process.server) {
Expand All @@ -35,7 +40,7 @@ export const createQueryFetch = <T = ParsedContent>(path?: string) => (query: Qu
})
}

return $fetch<T | T[]>(apiPath as any, {
return $fetch(apiPath as any, {
method: 'GET',
responseType: 'json',
params: {
Expand Down
11 changes: 9 additions & 2 deletions src/runtime/server/api/cache.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { defineEventHandler } from 'h3'
import { serverQueryContent } from '../storage'
import { cacheStorage, serverQueryContent } from '../storage'

// This route is used to cache all the parsed content
export default defineEventHandler(async (event) => {
const now = Date.now()
// Fetch all content
await serverQueryContent(event).find()
const data = await serverQueryContent(event).find()

// Generate Index
const index = data.reduce((acc, item) => {
acc[item._path!] = item._id
return acc
}, {} as Record<string, string>)
await cacheStorage.setItem('index.json', index)

return {
generatedAt: now,
Expand Down
34 changes: 34 additions & 0 deletions src/runtime/server/api/findOne.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createError, defineEventHandler } from 'h3'
import { withLeadingSlash } from 'ufo'
import { cacheStorage, getContent, serverQueryContent } from '../storage'

// This route is used to cache all the parsed content
export default defineEventHandler(async (event) => {
let index = await cacheStorage.getItem('index.json') as Record<string, string>
if (!index) {
// Fetch all content
const data = await serverQueryContent(event).find()

index = data.reduce((acc, item) => {
acc[item._path!] = item._id
return acc
}, {} as Record<string, string>)

await cacheStorage.setItem('index.json', index)
}

const slug = withLeadingSlash(event.context.params.slug)

if (!index[slug]) {
throw createError({
statusMessage: 'Document not found!',
statusCode: 404,
data: {
description: 'Could not find document for the given path.',
path: slug
}
})
}

return getContent(event, index[slug])
})