@rudderjs/orm-drizzle 1.5.0 → 1.5.1

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.
package/README.md CHANGED
@@ -207,6 +207,39 @@ When the Model enables soft deletes (via `_enableSoftDeletes()` on the QueryBuil
207
207
 
208
208
  ---
209
209
 
210
+ ## pgvector similarity search
211
+
212
+ The adapter implements `whereVectorSimilarTo()` against Postgres + the pgvector extension via `db.execute(sql\`...\`)`. SQLite and LibSQL connections throw `VectorStorageUnsupportedError` — vector queries require a Postgres driver (`postgres-js`, `pg`, or `neon-serverless`).
213
+
214
+ ```ts
215
+ import { Model, vector } from '@rudderjs/orm'
216
+
217
+ class Document extends Model {
218
+ static override casts = {
219
+ embedding: vector({ dimensions: 1536 }),
220
+ }
221
+ declare embedding: number[]
222
+ }
223
+
224
+ const hits = await Document
225
+ .where('tenantId', tenantId)
226
+ .whereVectorSimilarTo('embedding', queryEmbedding, { metric: 'cosine', limit: 10 })
227
+ .get()
228
+ ```
229
+
230
+ Setup:
231
+
232
+ ```sql
233
+ CREATE EXTENSION IF NOT EXISTS vector;
234
+ ALTER TABLE documents ADD COLUMN embedding vector(1536);
235
+ ```
236
+
237
+ Or scaffold the migration: `pnpm rudder make:migration add_embedding_to_documents --vector`.
238
+
239
+ See the [Vector search](../orm/README.md#vector-search) section in `@rudderjs/orm` for the full API + limitations.
240
+
241
+ ---
242
+
210
243
  ## Known Limitations
211
244
 
212
245
  ### `with()` is a no-op
@@ -0,0 +1,84 @@
1
+ # @rudderjs/orm-drizzle
2
+
3
+ ## Overview
4
+
5
+ Drizzle ORM adapter implementing the `OrmAdapter` contract from `@rudderjs/orm`. Drizzle is schema-first: tables are defined in TypeScript with `pgTable` / `sqliteTable` and the adapter compiles `@rudderjs/orm` query-builder calls into Drizzle's fluent API. Supports SQLite (`better-sqlite3`, `libsql`) and PostgreSQL (`postgres-js`, `node-postgres`).
6
+
7
+ ## Key Patterns
8
+
9
+ ### Configure (`config/database.ts`)
10
+
11
+ ```ts
12
+ import type { DatabaseConfig } from '@rudderjs/orm-drizzle'
13
+ import { users, posts } from '../drizzle/schema.js'
14
+
15
+ export default {
16
+ default: 'pg',
17
+ connections: {
18
+ pg: {
19
+ driver: 'drizzle',
20
+ dialect: 'postgres-js',
21
+ url: Env.get('DATABASE_URL', 'postgres://localhost/app'),
22
+ },
23
+ },
24
+ tables: { users, posts }, // register every table you'll query against
25
+ } satisfies DatabaseConfig
26
+ ```
27
+
28
+ `DatabaseProvider` is auto-discovered. Boot calls `ModelRegistry.set(new DrizzleAdapter(config))`.
29
+
30
+ ### Register tables outside config
31
+
32
+ For tables only used at runtime (modules, dynamic loaders), register via the global registry:
33
+
34
+ ```ts
35
+ import { DrizzleTableRegistry } from '@rudderjs/orm-drizzle'
36
+ import { auditLogs } from './drizzle/audit.js'
37
+
38
+ DrizzleTableRegistry.register('auditLogs', auditLogs)
39
+ ```
40
+
41
+ Every table referenced by `whereHas` / `withCount` / model `static table` must be registered — otherwise the adapter throws with the missing table name.
42
+
43
+ ### Vector queries
44
+
45
+ ```ts
46
+ await Embedding.query()
47
+ .whereVectorSimilarTo('embedding', queryVec, { limit: 5 })
48
+ .all()
49
+ ```
50
+
51
+ Routes through raw SQL using pgvector's `<=>` operator. Drizzle's fluent API doesn't expose pgvector operators directly, so the adapter shells out to `db.execute()` under the hood.
52
+
53
+ ### Soft deletes
54
+
55
+ ```ts
56
+ export class Post extends Model { static softDeletes = true }
57
+ ```
58
+
59
+ The adapter applies `WHERE deletedAt IS NULL` automatically on every read path. `withTrashed()` / `onlyTrashed()` disable / invert it.
60
+
61
+ ## Common Pitfalls
62
+
63
+ - **`.with('relation')` is a no-op on Drizzle**: Drizzle's relation API requires pre-declared relations on the table object, and the adapter has no portable way to resolve them dynamically. Load relations via separate queries or use the framework's `loadCount`/`loadSum` aggregate helpers — those route through `whereIn`-style batch SQL the adapter does support.
64
+ - **MySQL is not supported**: Drizzle's MySQL dialect doesn't implement `returning()`, which the adapter relies on. Use SQLite or Postgres.
65
+ - **`connect()` is a no-op**: Drizzle connects lazily on first query. `disconnect()` only does work on the `postgres-js` dialect. Tests that depend on a teardown step should call `client.end()` directly on the underlying driver.
66
+ - **Missing `whereHas` table**: `await Comment.whereHas('post', q => q.where('isPublished', true))` throws if the `posts` table isn't in `tables: {}` or in `DrizzleTableRegistry`. Error message names the missing table.
67
+ - **pgvector extension missing**: vector queries surface as `VectorStorageUnsupportedError` — install the extension (`CREATE EXTENSION vector;`) and re-run the migration.
68
+ - **Drizzle `eq(col, null)` never matches**: use `isNull(col)` / `isNotNull(col)` for null comparisons. The adapter does this correctly internally; only matters if you drop down to raw Drizzle SQL.
69
+
70
+ ## Key Imports
71
+
72
+ ```ts
73
+ import {
74
+ drizzle, // factory function — registers DatabaseProvider config
75
+ DrizzleAdapter, // the OrmAdapter implementation (rarely instantiated directly)
76
+ DrizzleTableRegistry, // runtime table registration
77
+ } from '@rudderjs/orm-drizzle'
78
+
79
+ import type {
80
+ DatabaseConfig,
81
+ DrizzleConfig,
82
+ DrizzleDialect, // 'better-sqlite3' | 'libsql' | 'postgres-js' | 'node-postgres'
83
+ } from '@rudderjs/orm-drizzle'
84
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rudderjs/orm-drizzle",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Drizzle ORM adapter for RudderJS. Implements OrmAdapterProvider / OrmAdapter / QueryBuilder<T> using Drizzle's SQL-like fluent API.",
5
5
  "rudderjs": {
6
6
  "provider": "DatabaseProvider",
@@ -15,7 +15,8 @@
15
15
  },
16
16
  "type": "module",
17
17
  "files": [
18
- "dist"
18
+ "dist",
19
+ "boost"
19
20
  ],
20
21
  "main": "./dist/index.js",
21
22
  "types": "./dist/index.d.ts",
@@ -30,11 +31,11 @@
30
31
  "drizzle-orm": "^0.38.0",
31
32
  "@rudderjs/contracts": "^1.6.0",
32
33
  "@rudderjs/core": "^1.1.3",
33
- "@rudderjs/orm": "^1.9.0",
34
+ "@rudderjs/orm": "^1.9.1",
34
35
  "@rudderjs/support": "^1.2.0"
35
36
  },
36
37
  "peerDependencies": {
37
- "@rudderjs/ai": "^1.6.0"
38
+ "@rudderjs/ai": "^1.6.1"
38
39
  },
39
40
  "peerDependenciesMeta": {
40
41
  "@rudderjs/ai": {