import { and, eq, like, sql } from 'drizzle-orm'; import useDatabase from '~/composables/useDatabase'; import { explorerContentTable } from '~/db/schema'; export default defineEventHandler(async (e) => { const query = getQuery(e); const where = []; if(query && query.path !== undefined) { where.push(eq(explorerContentTable.path, sql.placeholder('path'))); } if(query && query.title !== undefined) { where.push(eq(explorerContentTable.title, sql.placeholder('title'))); } if(query && query.type !== undefined) { where.push(eq(explorerContentTable.type, sql.placeholder('type'))); } if (query && query.search !== undefined) { where.push(like(explorerContentTable.path, sql.placeholder('search'))); } if(where.length > 0) { const db = useDatabase(); const content = db.select({ 'path': explorerContentTable.path, 'owner': explorerContentTable.owner, 'title': explorerContentTable.title, 'type': explorerContentTable.type, 'content': sql`cast(${explorerContentTable.content} as TEXT)`.as('content'), 'navigable': explorerContentTable.navigable, 'private': explorerContentTable.private, }).from(explorerContentTable).where(and(...where)).prepare().all(query); if(content.length > 0) { return content; } } setResponseStatus(e, 404); });