@neon/skills 1.2.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,172 @@
1
+ # Managing Lakebase Search with Drizzle
2
+
3
+ When the user wants Lakebase Search managed through Drizzle, treat the SQL in [Vector Search](vector-search.md), [Full-Text Search](full-text-search.md), and [Hybrid Search](hybrid-search.md) as the source of truth and apply it as below. Use Drizzle for all schema and migration management unless the user says otherwise.
4
+
5
+ Requires `drizzle-orm` 0.36+ and `drizzle-kit` 0.27+: the schema below returns its indexes as an array from the `pgTable` extra-config callback. Those versions also include generated-column support for the `tsvector` column, the custom-method `.using(...).op(...)` index API for `lakebase_ann`, the `vector` column type, and the `cosineDistance` helper.
6
+
7
+ Contents:
8
+
9
+ - [Config](#config): `drizzle.config.ts` and the migration connection
10
+ - [Extensions](#extensions): custom migration required to create extensions (Drizzle can't)
11
+ - [Schema](#schema): columns, generated `tsvector`, and the ANN index
12
+ - [BM25 Index](#bm25-index): created after the corpus is seeded
13
+ - [Query](#query): vector, BM25, and hybrid reads
14
+ - [Tune Per Query](#tune-per-query): per-query GUCs
15
+
16
+ Rules:
17
+
18
+ - Express everything Drizzle can in `schema.ts`: the columns, the generated `tsvector`, and the `lakebase_ann` index. Only `CREATE EXTENSION` and the post-seed `lakebase_bm25` index need custom migrations.
19
+ - Use `drizzle-kit generate` then `migrate`. Never run `drizzle-kit push` (it reconciles the database to `schema.ts`, so it drops the post-seed `lakebase_bm25` index and any other object not declared there)
20
+ - Run every migration over the direct (unpooled) connection.
21
+ - The extension must exist before the `vector` column and the `lakebase_ann` index that depend on it.
22
+
23
+ ## Config
24
+
25
+ `drizzle-kit generate` and `migrate` read `drizzle.config.ts`. Point `dbCredentials.url` at the **direct (unpooled)** connection string:
26
+
27
+ ```typescript
28
+ // drizzle.config.ts
29
+ import { defineConfig } from "drizzle-kit";
30
+
31
+ export default defineConfig({
32
+ schema: "./src/schema.ts",
33
+ out: "./drizzle",
34
+ dialect: "postgresql",
35
+ // Direct (unpooled) URL. Neon exposes it as DATABASE_URL_UNPOOLED.
36
+ dbCredentials: { url: process.env.DATABASE_URL_UNPOOLED },
37
+ });
38
+ ```
39
+
40
+ ## Extensions
41
+
42
+ Drizzle cannot express `CREATE EXTENSION`, and the `vector` column and `lakebase_ann` index below depend on `lakebase_vector`, so generate a custom migration for the extensions **first** so it runs before the schema migration:
43
+
44
+ ```bash
45
+ npx drizzle-kit generate --custom --name=lakebase_extensions
46
+ ```
47
+
48
+ ```sql
49
+ -- drizzle/0000_lakebase_extensions.sql
50
+ CREATE EXTENSION IF NOT EXISTS lakebase_vector CASCADE;
51
+ CREATE EXTENSION IF NOT EXISTS lakebase_text;
52
+ ```
53
+
54
+ ## Schema
55
+
56
+ The columns, the generated `tsvector`, and the `lakebase_ann` index all go in `schema.ts`. `tsvector` has no built-in Drizzle type, so define it using the `customType`:
57
+
58
+ ```typescript
59
+ // src/schema.ts
60
+ import { pgTable, bigint, text, vector, index, customType } from "drizzle-orm/pg-core";
61
+ import { sql } from "drizzle-orm";
62
+
63
+ const tsvector = customType<{ data: string }>({
64
+ dataType() {
65
+ return "tsvector";
66
+ },
67
+ });
68
+
69
+ export const documents = pgTable(
70
+ "documents",
71
+ {
72
+ id: bigint("id", { mode: "number" }).generatedByDefaultAsIdentity().primaryKey(),
73
+ title: text("title").notNull(),
74
+ body: text("body").notNull(),
75
+ embedding: vector("embedding", { dimensions: 1536 }),
76
+ bodyTsv: tsvector("body_tsv").generatedAlwaysAs(
77
+ sql`to_tsvector('english', "body")`,
78
+ ),
79
+ },
80
+ (table) => [
81
+ index("documents_embedding_ann").using(
82
+ "lakebase_ann",
83
+ table.embedding.op("vector_cosine_ops"),
84
+ ),
85
+ ],
86
+ );
87
+ ```
88
+
89
+ Set the dimension to match your embedding model. Postgres maintains `body_tsv`, so never write it from the app. Generate and apply the migration after the extensions migration above:
90
+
91
+ ```bash
92
+ npx drizzle-kit generate --name=lakebase_search
93
+ npx drizzle-kit migrate
94
+ ```
95
+
96
+ ## BM25 Index
97
+
98
+ Keep the `lakebase_bm25` index out of `schema.ts`. It must be built only after the initial corpus is loaded, so its build-time statistics are meaningful (see [Full-text search](full-text-search.md)) — a schema migration would build it against an empty table. Add it in a later custom migration that runs after seeding:
99
+
100
+ ```bash
101
+ npx drizzle-kit generate --custom --name=bm25_index
102
+ ```
103
+
104
+ ```sql
105
+ -- drizzle/NNNN_bm25_index.sql, applied after the corpus is seeded
106
+ CREATE INDEX documents_body_bm25 ON documents USING lakebase_bm25 (body_tsv);
107
+ ```
108
+
109
+ ## Query
110
+
111
+ Use the query builder with Drizzle's `cosineDistance` helper for vector search. It emits the `<=>` operator, so keep the index on `vector_cosine_ops`:
112
+
113
+ ```typescript
114
+ import { cosineDistance } from "drizzle-orm";
115
+ import { documents } from "./schema";
116
+
117
+ // queryEmbedding: number[] from the same model used for stored documents
118
+ const distance = cosineDistance(documents.embedding, queryEmbedding);
119
+
120
+ const rows = await db
121
+ .select({ id: documents.id, title: documents.title, distance })
122
+ .from(documents)
123
+ .orderBy(distance)
124
+ .limit(k);
125
+ ```
126
+
127
+ BM25 has no Drizzle helper: `<@>` and `to_bm25query` require raw SQL. Reference the generated column by its `body_tsv` name. Bind user input as parameters through the `sql` template:
128
+
129
+ ```typescript
130
+ import { sql } from "drizzle-orm";
131
+
132
+ const rows = await db.execute(sql`
133
+ SELECT id, title,
134
+ body_tsv <@> to_bm25query(
135
+ to_tsvector('english', ${queryText}),
136
+ 'documents_body_bm25'::regclass
137
+ ) AS score
138
+ FROM documents
139
+ ORDER BY score
140
+ LIMIT ${k}
141
+ `);
142
+ ```
143
+
144
+ Run the [hybrid search](hybrid-search.md) RRF query the same way: raw SQL through `db.execute`.
145
+
146
+ ## Tune Per Query
147
+
148
+ Per-query GUCs (`lakebase_ann.probes`, `lakebase_ann.epsilon`, `lakebase_bm25.default_limit`, `lakebase_bm25.prefilter`) must be set with `SET LOCAL` inside a transaction so they apply to the same pooled connection as the query:
149
+
150
+ ```typescript
151
+ import { cosineDistance, sql } from "drizzle-orm";
152
+ import { documents } from "./schema";
153
+
154
+ const distance = cosineDistance(documents.embedding, queryEmbedding);
155
+
156
+ const rows = await db.transaction(async (tx) => {
157
+ // SET LOCAL scopes the GUC to this transaction's connection; do not hoist it out.
158
+ // Keep probes at 'auto' unless an IVF `lists` layout exists: a numeric value must
159
+ // match the `lists` shape or it errors ("need 0 probes ..."). See vector-search.md.
160
+ await tx.execute(sql`SET LOCAL lakebase_ann.probes = 'auto'`);
161
+ return tx
162
+ .select({ id: documents.id, title: documents.title, distance })
163
+ .from(documents)
164
+ .orderBy(distance)
165
+ .limit(k);
166
+ });
167
+ ```
168
+
169
+ Sources:
170
+
171
+ - [Get started with Lakebase Search](https://neon.com/docs/ai/lakebase-search-get-started)
172
+ - [Schema migration with Lakebase Postgres and Drizzle ORM](https://neon.com/docs/guides/drizzle-migrations)
@@ -19,7 +19,7 @@ metadata:
19
19
  If the `neon` skill is not installed, fetch it from https://neon.com/docs/ai/skills/neon/SKILL.md or install it with:
20
20
 
21
21
  ```bash
22
- npx skills add neondatabase/agent-skills --skill neon
22
+ neon skills -s neon -y
23
23
  ```
24
24
 
25
25
  # Lakebase Postgres Branching
@@ -127,7 +127,7 @@ Schema-only branching is in Beta. If users report unexpected behavior, errors, o
127
127
  1. Ask them to share feedback in the Neon Console:
128
128
  - https://console.neon.tech/app/projects?modal=feedback
129
129
  2. Recommend opening a support conversation in the Neon Discord:
130
- - https://discord.gg/92vNTzKDGp
130
+ - https://neon.com/discord
131
131
 
132
132
  ## Reset from Parent
133
133
 
@@ -282,7 +282,7 @@ Common CI/CD use cases for Neon branches:
282
282
  - `neon branches create --name compliance-dev --parent main --schema-only --project-id <your-project-id> --expires-at 2026-12-15T18:02:16Z`
283
283
  5. Mention Beta support path:
284
284
  - https://console.neon.tech/app/projects?modal=feedback
285
- - https://discord.gg/92vNTzKDGp
285
+ - https://neon.com/discord
286
286
 
287
287
  ## Further Reading
288
288
 
@@ -19,7 +19,7 @@ metadata:
19
19
  If the `neon` skill is not installed, fetch it from https://neon.com/docs/ai/skills/neon/SKILL.md or install it with:
20
20
 
21
21
  ```bash
22
- npx skills add neondatabase/agent-skills --skill neon
22
+ neon skills -s neon -y
23
23
  ```
24
24
 
25
25
  # Postgres Egress Optimizer