Skip to content

Commit

Permalink
Make search an edge route
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Jan 16, 2024
1 parent 97c41d2 commit 7d6baab
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 22 deletions.
14 changes: 14 additions & 0 deletions apps/web/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (
config,
{buildId, dev, isServer, defaultLoaders, nextRuntime, webpack}
) => {
if (config.name === 'edge-server') {
config.resolve.conditionNames = ['worker', 'import', 'require']
config.module.rules.unshift({
test: /\.wasm$/,
loader: 'next-middleware-wasm-loader',
type: 'javascript/auto'
})
}
return config
},
reactStrictMode: false,
swcMinify: true,
typescript: {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function GET(request: Request) {
if (!searchTerm) return Response.json([])
const matches = await cms
.in(cms.workspaces.main.pages)
.syncInterval(0)
.disableSync()
.find(
Entry()
.select({
Expand Down
2 changes: 1 addition & 1 deletion src/backend/Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class Handler implements Resolver {
}

async periodicSync(syncInterval = 5) {
if (syncInterval === 0) return
if (syncInterval === Infinity) return
const now = Date.now()
if (now - this.lastSync < syncInterval * 1000) return
this.lastSync = now
Expand Down
17 changes: 8 additions & 9 deletions src/cli/Generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Database} from 'alinea/backend'
import {Store} from 'alinea/backend/Store'
import {exportStore} from 'alinea/cli/util/ExportStore.server'
import {CMS} from 'alinea/core/CMS'
import {Config} from 'alinea/core/Config'
import {join} from 'alinea/core/util/Paths'
import {BuildResult} from 'esbuild'
import fs from 'node:fs'
import {createRequire} from 'node:module'
Expand Down Expand Up @@ -103,14 +105,17 @@ export async function* generate(options: GenerateOptions): AsyncGenerator<
let nextBuild: Promise<{value: BuildResult; done?: boolean}> = builds.next()
let afterGenerateCalled = false

const [store, exportStore] = await createDb()
function writeStore(data: Uint8Array) {
return exportStore(data, join(context.outDir, 'store.js'))
}
const [store, storeData] = await createDb()
await copyStaticFiles(context)
while (true) {
const {done} = await nextBuild
nextBuild = builds.next()
try {
const cms = await loadCMS(context.outDir)
cms.exportStore(context.outDir, new Uint8Array())
await writeStore(new Uint8Array())
const fileData = new LocalData({
config: cms.config,
fs: fs.promises,
Expand All @@ -125,12 +130,6 @@ export async function* generate(options: GenerateOptions): AsyncGenerator<
nextBuild
)) {
yield {cms, db, localData: fileData}
// For debug reasons write out db
if (process.env.NODE_ENV === 'development')
fs.writeFileSync(
path.join(context.outDir, 'content.sqlite'),
exportStore()
)
if (onAfterGenerate && !afterGenerateCalled) {
afterGenerateCalled = true
onAfterGenerate()
Expand All @@ -139,7 +138,7 @@ export async function* generate(options: GenerateOptions): AsyncGenerator<
if (done) {
await Promise.all([
generatePackage(context, cms.config),
cms.exportStore(context.outDir, exportStore())
writeStore(storeData())
])
break
}
Expand Down
1 change: 0 additions & 1 deletion src/core/CMS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export abstract class CMS extends GraphRealm implements CMSApi {
}

abstract resolver(): Promise<Resolver>
abstract exportStore(cwd: string, store: Uint8Array): Promise<void>
abstract readStore(): Promise<Store>

#attach(config: Config) {
Expand Down
15 changes: 15 additions & 0 deletions src/core/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface GraphRealmApi {
find<S extends Projection | Type>(select: S): Promise<Selection.Infer<S>>
/** The time in seconds to poll for updates to content */
syncInterval(interval: number): GraphRealmApi
/** Disable polling for updates to content */
disableSync(): GraphRealmApi
/** The amount of results found */
count(cursor: Cursor.Find<any>): Promise<number>
}
Expand All @@ -49,6 +51,19 @@ export class GraphRealm implements GraphRealmApi {
this.targets = Schema.targets(config.schema)
}

disableSync() {
return new GraphRealm(
this.config,
params => {
return this.resolve({
...params,
syncInterval: Infinity
})
},
this.origin
)
}

syncInterval(interval: number) {
return new GraphRealm(
this.config,
Expand Down
6 changes: 0 additions & 6 deletions src/core/driver/DefaultDriver.server.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import {Database} from 'alinea/backend'
import {Store, createStore} from 'alinea/backend/Store'
import {EntryResolver} from 'alinea/backend/resolver/EntryResolver'
import {exportStore} from 'alinea/cli/util/ExportStore'
import {base64} from 'alinea/core/util/Encoding'
import PLazy from 'p-lazy'
import {CMS, CMSApi} from '../CMS.js'
import {Client} from '../Client.js'
import {Config} from '../Config.js'
import {Resolver} from '../Resolver.js'
import {Realm} from '../pages/Realm.js'
import {join} from '../util/Paths.js'

export class DefaultDriver extends CMS {
db = PLazy.from(this.createDb.bind(this))

exportStore(outDir: string, data: Uint8Array): Promise<void> {
return exportStore(data, join(outDir, 'store.js'))
}

async readStore(): Promise<Store> {
// @ts-ignore
const {storeData} = await import('@alinea/generated/store.js')
Expand Down
4 changes: 0 additions & 4 deletions src/core/driver/DefaultDriver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import {Config} from '../Config.js'
import {Resolver} from '../Resolver.js'

export class DefaultDriver extends CMS {
exportStore(outDir: string, data: Uint8Array): Promise<void> {
throw new Error('Not implemented')
}

async readStore(): Promise<Store> {
throw new Error('Not implemented')
}
Expand Down

0 comments on commit 7d6baab

Please sign in to comment.