Skip to content

Commit b2ee77b

Browse files
authored
feat: vectorize
1 parent f9c4cea commit b2ee77b

File tree

10 files changed

+68
-5
lines changed

10 files changed

+68
-5
lines changed

playground/nuxt.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export default defineNuxtConfig({
1414
database: true,
1515
kv: true,
1616
blob: true,
17-
cache: true
17+
cache: true,
18+
vectorize: true
1819
// projectUrl: ({ branch }) => branch === 'main' ? 'https://playground.nuxt.dev' : `https://${encodeHost(branch).replace(/\//g, '-')}.playground-to39.pages.dev`
1920
},
2021
ui: {

src/features.ts

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface HubConfig {
2626
cache?: boolean
2727
database?: boolean
2828
kv?: boolean
29+
vectorize?: boolean
2930

3031
remoteManifest?: {
3132
version: string
@@ -101,6 +102,12 @@ export function setupKV(_nuxt: Nuxt) {
101102
addServerImportsDir(resolve('./runtime/kv/server/utils'))
102103
}
103104

105+
export function setupVectorize(_nuxt: Nuxt) {
106+
// Add Server scanning
107+
addServerScanDir(resolve('./runtime/vectorize/server'))
108+
addServerImportsDir(resolve('./runtime/vectorize/server/utils'))
109+
}
110+
104111
export function setupOpenAPI(nuxt: Nuxt) {
105112
// Fallback to custom placeholder when openAPI is disabled
106113
nuxt.options.alias['#hub/openapi'] = nuxt.options.nitro?.experimental?.openAPI === true

src/module.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { findWorkspaceDir } from 'pkg-types'
77
import { parseArgs } from 'citty'
88
import { version } from '../package.json'
99
import { generateWrangler } from './utils/wrangler'
10-
import { setupAi, setupCache, setupAnalytics, setupBlob, setupOpenAPI, setupDatabase, setupKV, setupBase, setupRemote } from './features'
10+
import { setupAi, setupCache, setupAnalytics, setupBlob, setupOpenAPI, setupDatabase, setupKV, setupVectorize, setupBase, setupRemote } from './features'
1111
import type { ModuleOptions } from './types/module'
1212
import { addBuildHooks } from './utils/build'
1313

@@ -50,6 +50,7 @@ export default defineNuxtModule<ModuleOptions>({
5050
cache: false,
5151
database: false,
5252
kv: false,
53+
vectorize: false,
5354
// Other options
5455
version,
5556
env: process.env.NUXT_HUB_ENV || 'production',
@@ -83,6 +84,7 @@ export default defineNuxtModule<ModuleOptions>({
8384
hub.cache && setupCache(nuxt)
8485
hub.database && setupDatabase(nuxt)
8586
hub.kv && setupKV(nuxt)
87+
hub.vectorize && setupVectorize(nuxt)
8688

8789
// nuxt prepare, stop here
8890
if (nuxt.options._prepare) {
@@ -134,7 +136,7 @@ export default defineNuxtModule<ModuleOptions>({
134136
await writeFile(gitignorePath, `${gitignore ? gitignore + '\n' : gitignore}.data`, 'utf-8')
135137
}
136138

137-
const needWrangler = Boolean(hub.ai || hub.analytics || hub.blob || hub.database || hub.kv)
139+
const needWrangler = Boolean(hub.ai || hub.analytics || hub.blob || hub.database || hub.kv || hub.vectorize)
138140
if (needWrangler) {
139141
// Generate the wrangler.toml file
140142
const wranglerPath = join(hubDir, './wrangler.toml')

src/runtime/utils/features.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const featureMessages = {
2222
kv: [
2323
'NuxtHub KV is not enabled, set `hub.kv = true` in your `nuxt.config.ts`',
2424
'Read more at `https://hub.nuxt.com/docs/storage/kv`'
25+
].join('\n'),
26+
vectorize: [
27+
'NuxtHub Vectorize is not enabled, set `hub.vectorize = true` in your `nuxt.config.ts`'
2528
].join('\n')
2629
}
2730

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createError } from 'h3'
2+
import type { VectorizeIndex } from '@nuxthub/core'
3+
import { requireNuxtHubFeature } from '../../../utils/features'
4+
5+
let _vectorize: VectorizeIndex
6+
7+
/**
8+
* Access the Vectorize database.
9+
*
10+
* @example ```ts
11+
* const vectorize = hubVectorize()
12+
* let vectorsToInsert = [
13+
* {id: "123", values: [32.4, 6.5, 11.2, 10.3, 87.9]},
14+
* {id: "456", values: [2.5, 7.8, 9.1, 76.9, 8.5]},
15+
* ]
16+
* let inserted = await vectorize.insert(vectorsToInsert)
17+
* ```
18+
*
19+
* @see https://developers.cloudflare.com/vectorize/reference/client-api/
20+
*/
21+
export function hubVectorize(): VectorizeIndex {
22+
requireNuxtHubFeature('vectorize')
23+
24+
if (_vectorize) {
25+
return _vectorize
26+
}
27+
// @ts-expect-error globalThis.__env__ is not defined
28+
const binding = process.env.VECTORIZE || globalThis.__env__?.VECTORIZE || globalThis.VECTORIZE
29+
if (binding) {
30+
_vectorize = binding as VectorizeIndex
31+
return _vectorize
32+
}
33+
throw createError('Missing Cloudflare DB binding (Vectorize)')
34+
}

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './blob'
22
export * from './database'
33
export * from './kv'
44
export * from './module'
5+
export * from './vectorize'

src/types/module.ts

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export interface ModuleOptions {
4949
* @default process.env.NUXT_HUB_REMOTE or --remote option when running `nuxt dev`
5050
* @see https://hub.nuxt.com/docs/getting-started/remote-storage
5151
*/
52+
/**
53+
* Set `true` to enable the database for the project.
54+
*
55+
* @default false
56+
*/
57+
vectorize?: boolean
5258
remote?: boolean | 'production' | 'preview'
5359
/**
5460
* The URL of the NuxtHub Admin

src/types/vectorize.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type { VectorizeIndex } from '@cloudflare/workers-types/experimental'

src/utils/build.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ export function addBuildHooks(nuxt: Nuxt, hub: HubConfig) {
101101
blob: hub.blob,
102102
cache: hub.cache,
103103
database: hub.database,
104-
kv: hub.kv
104+
kv: hub.kv,
105+
vectorize: hub.vectorize
105106
}
106107
await writeFile(join(nitro.options.output.publicDir, 'hub.config.json'), JSON.stringify(hubConfig, null, 2), 'utf-8')
107108
})

src/utils/wrangler.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { stringifyTOML } from 'confbox'
22

3-
export function generateWrangler(hub: { kv: boolean, database: boolean, blob: boolean, analytics: boolean, ai: boolean, accountId?: string }) {
3+
export function generateWrangler(hub: { kv: boolean, database: boolean, blob: boolean, analytics: boolean, ai: boolean, accountId?: string, vectorize: boolean }) {
44
const wrangler: { [key: string]: any } = {}
55

66
if (hub.accountId) {
@@ -42,5 +42,12 @@ export function generateWrangler(hub: { kv: boolean, database: boolean, blob: bo
4242
}]
4343
}
4444

45+
if (hub.vectorize) {
46+
wrangler['vectorize'] = [{
47+
binding: 'VECTORIZE',
48+
index_name: 'default',
49+
}]
50+
}
51+
4552
return stringifyTOML(wrangler)
4653
}

0 commit comments

Comments
 (0)