nitro-drizzle 0.3.1 → 0.3.2

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.
@@ -1,3 +1,4 @@
1
+ import { ConnectionOptions } from "mysql2";
1
2
  import { Schema } from "./index.mjs";
2
3
  /**
3
4
  * MySQL datasource driver using mysql2.
@@ -18,7 +19,10 @@ declare const _default: <TSchema extends Schema>(config: Options, schema: TSchem
18
19
  * Configuration options for the MySQL driver.
19
20
  */
20
21
  export type Options = {
21
- /** Database connection URL. */
22
+ /**
23
+ * Database connection URL.
24
+ * @deprecated Use `uri` instead.
25
+ */
22
26
  url: string;
23
- };
27
+ } | ConnectionOptions;
24
28
  export { _default as default };
@@ -11,7 +11,8 @@ import { SELECT_1 } from "./internal/sql.mjs";
11
11
  * @returns A Datasource instance
12
12
  */
13
13
  var mysql_default = defineConnector(async (config, schema) => {
14
- const connection = await createConnection(config.url);
14
+ config = "url" in config ? { uri: config.url } : config;
15
+ const connection = await createConnection(config);
15
16
  const database = drizzle(connection, {
16
17
  schema,
17
18
  mode: "default"
@@ -1 +1 @@
1
- {"version":3,"file":"mysql.mjs","names":[],"sources":["../../src/drivers/mysql.ts"],"sourcesContent":["import { drizzle } from \"drizzle-orm/mysql2\";\nimport { createConnection } from \"mysql2/promise\";\nimport { defineConnector, type Schema } from \".\";\nimport { SELECT_1 } from \"./internal/sql\";\n\n/**\n * MySQL datasource driver using mysql2.\n * @template TSchema - The schema type\n * @param config - Driver configuration options\n * @param schema - The Drizzle schema\n * @returns A Datasource instance\n */\nexport default defineConnector(async <TSchema extends Schema>(config: Options, schema: TSchema) => {\n const connection = await createConnection(config.url);\n const database = drizzle(connection, { schema, mode: \"default\" });\n return {\n database,\n schema,\n waitReady: async () => {\n await database.execute(SELECT_1);\n },\n close: async () => {\n await database.$client.end();\n },\n };\n});\n\n/**\n * Configuration options for the MySQL driver.\n */\nexport type Options = {\n /** Database connection URL. */\n url: string;\n};\n"],"mappings":";;;;;;;;;;;;AAYA,IAAA,gBAAe,gBAAgB,OAA+B,QAAiB,WAAoB;CACjG,MAAM,aAAa,MAAM,iBAAiB,OAAO,GAAG;CACpD,MAAM,WAAW,QAAQ,YAAY;EAAE;EAAQ,MAAM;CAAU,CAAC;CAChE,OAAO;EACL;EACA;EACA,WAAW,YAAY;GACrB,MAAM,SAAS,QAAQ,QAAQ;EACjC;EACA,OAAO,YAAY;GACjB,MAAM,SAAS,QAAQ,IAAI;EAC7B;CACF;AACF,CAAC"}
1
+ {"version":3,"file":"mysql.mjs","names":[],"sources":["../../src/drivers/mysql.ts"],"sourcesContent":["import { drizzle } from \"drizzle-orm/mysql2\";\nimport { createConnection } from \"mysql2/promise\";\nimport type { ConnectionOptions } from \"mysql2\";\nimport { defineConnector, type Schema } from \".\";\nimport { SELECT_1 } from \"./internal/sql\";\n\n/**\n * MySQL datasource driver using mysql2.\n * @template TSchema - The schema type\n * @param config - Driver configuration options\n * @param schema - The Drizzle schema\n * @returns A Datasource instance\n */\nexport default defineConnector(async <TSchema extends Schema>(config: Options, schema: TSchema) => {\n config = \"url\" in config ? { uri: config.url } : config;\n const connection = await createConnection(config);\n const database = drizzle(connection, { schema, mode: \"default\" });\n return {\n database,\n schema,\n waitReady: async () => {\n await database.execute(SELECT_1);\n },\n close: async () => {\n await database.$client.end();\n },\n };\n});\n\n/**\n * Configuration options for the MySQL driver.\n */\nexport type Options =\n | {\n /**\n * Database connection URL.\n * @deprecated Use `uri` instead.\n */\n url: string;\n }\n | ConnectionOptions;\n"],"mappings":";;;;;;;;;;;;AAaA,IAAA,gBAAe,gBAAgB,OAA+B,QAAiB,WAAoB;CACjG,SAAS,SAAS,SAAS,EAAE,KAAK,OAAO,IAAI,IAAI;CACjD,MAAM,aAAa,MAAM,iBAAiB,MAAM;CAChD,MAAM,WAAW,QAAQ,YAAY;EAAE;EAAQ,MAAM;CAAU,CAAC;CAChE,OAAO;EACL;EACA;EACA,WAAW,YAAY;GACrB,MAAM,SAAS,QAAQ,QAAQ;EACjC;EACA,OAAO,YAAY;GACjB,MAAM,SAAS,QAAQ,IAAI;EAC7B;CACF;AACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitro-drizzle",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Nitro module to integrate Drizzle ORM with ease.",
5
5
  "homepage": "https://github.com/myshkouski/nitro-drizzle#readme",
6
6
  "bugs": {
@@ -13,7 +13,8 @@
13
13
  "url": "git+https://github.com/myshkouski/nitro-drizzle.git"
14
14
  },
15
15
  "files": [
16
- "dist"
16
+ "dist",
17
+ "skills"
17
18
  ],
18
19
  "type": "module",
19
20
  "imports": {
@@ -52,7 +53,7 @@
52
53
  "nitropack": "^2.14.0",
53
54
  "ohash": "^2.0.12",
54
55
  "pathe": "^2.0.3",
55
- "rollup": "^4.69.0",
56
+ "rollup": "^4.69.2",
56
57
  "strip-indent": "^4.1.1",
57
58
  "tinyglobby": "^0.2.17",
58
59
  "unstorage": "^1.17.6"
@@ -68,15 +69,16 @@
68
69
  "lefthook": "^2.2.4",
69
70
  "listhen": "^1.10.1",
70
71
  "oxfmt": "^0.81.0",
71
- "oxlint": "^1.95.0",
72
+ "oxlint": "^1.96.0",
72
73
  "oxlint-tsgolint": "^7.0.2009",
73
74
  "publint": "^0.3.25",
75
+ "skills": "^1.9.1",
74
76
  "testcontainers": "^12.2.2",
75
77
  "tsdown": "^0.23.7",
76
78
  "typescript": "^7.0.2",
77
79
  "unplugin-unused": "^0.6.0",
78
80
  "vitest": "^5.1.2",
79
- "wrangler": "^4.169.1"
81
+ "wrangler": "^4.170.0"
80
82
  },
81
83
  "peerDependencies": {
82
84
  "@cloudflare/workers-types": "^5.20260826.1",
@@ -106,6 +108,7 @@
106
108
  }
107
109
  },
108
110
  "scripts": {
111
+ "postinstall": "skills install --frozen-lockfile",
109
112
  "build:dev": "tsdown --watch",
110
113
  "fmt": "oxfmt",
111
114
  "fmt:check": "oxfmt --check",
@@ -0,0 +1,188 @@
1
+ ---
2
+ name: nitro-drizzle
3
+ description: Install, configure, and use nitro-drizzle in a Nitro application. Use this skill whenever the user needs to add Drizzle ORM to a Nitro project, configure datasources, define schemas, run migrations, or use useDatasource/useDialect/migrate at runtime. Trigger on mentions of nitro-drizzle, Drizzle + Nitro, drizzle datasources, drizzle migrations in Nitro, defining database schemas for a Nitro app, or configuring better-sqlite3/pglite/mysql2/D1 drivers with Nitro — even if the user doesn't explicitly mention 'nitro-drizzle.'
4
+ ---
5
+
6
+ # nitro-drizzle
7
+
8
+ Integrate [Drizzle ORM](https://orm.drizzle.team/) into a [Nitro](https://nitro.build/) app with multiple datasources, drivers, type-safe schemas, automatic migrations, and Nitro task integration.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install nitro-drizzle drizzle-orm drizzle-kit
14
+ ```
15
+
16
+ Install the driver for your database (choose one or more):
17
+
18
+ ```bash
19
+ npm install better-sqlite3 # SQLite
20
+ npm install @electric-sql/pglite # PostgreSQL (pglite)
21
+ npm install mysql2 # MySQL
22
+ npm install @cloudflare/workers-types # Cloudflare D1
23
+ ```
24
+
25
+ Drivers are optional peer dependencies — install only what you need.
26
+
27
+ ## Configure the module
28
+
29
+ Add `nitro-drizzle` to `nitro.config.ts`:
30
+
31
+ ```ts
32
+ import { defineNitroConfig } from "nitropack/config";
33
+
34
+ export default defineNitroConfig({
35
+ modules: ["nitro-drizzle"],
36
+ drizzle: {
37
+ datasources: {
38
+ content: { drivers: { sqlite: true, d1: true, pglite: false } },
39
+ },
40
+ },
41
+ });
42
+ ```
43
+
44
+ Prefer object syntax (`{ sqlite: true, d1: false }`) to enable/disable drivers. The underscore prefix format (`["sqlite", "_pglite"]`) exists only for environment variable configuration — env vars can only hold strings, so `_pglite` means "disable pglite". Do not use it in `nitro.config.ts`.
45
+
46
+ ### Module options
47
+
48
+ | Option | Type | Default | Description |
49
+ | --------------- | ------------------------------------ | ------------------------------------------------------------- | ----------------------------------- |
50
+ | `baseDir` | `string` | `"./drizzle"` | Directory to search for datasources |
51
+ | `configPattern` | `ConfigPattern` | `["drizzle.config.{js,ts}", "drizzle-*.config.{js,ts}"]` | Patterns to match Drizzle configs |
52
+ | `datasources` | `DatasourceOptions` | `{}` | Per-datasource driver config |
53
+ | `migrations` | `false \| Partial<MigrationOptions>` | `{ storageBase: "drizzle:migrations", migrateOnInit: false }` | Migration storage config |
54
+
55
+ ## Define Drizzle config and schema
56
+
57
+ Create a Drizzle config and schema under `baseDir` (default `server/drizzle/<datasource>/`).
58
+
59
+ ### Config file (e.g. `server/drizzle/content/drizzle-sqlite.config.ts`)
60
+
61
+ Pass `import.meta.url` as the second argument for `drizzle-kit` compatibility:
62
+
63
+ ```ts
64
+ import { defineConfig } from "nitro-drizzle/config";
65
+
66
+ export default defineConfig(
67
+ {
68
+ strict: true,
69
+ dialect: "sqlite",
70
+ out: "./sqlite/migrations",
71
+ schema: "./sqlite/schema.ts",
72
+ migrations: { table: "drizzle_migrations" },
73
+ },
74
+ import.meta.url,
75
+ );
76
+ ```
77
+
78
+ ### Schema file (e.g. `server/drizzle/content/sqlite/schema.ts`)
79
+
80
+ ```ts
81
+ import { sqliteTable, integer, text } from "drizzle-orm/sqlite-core";
82
+
83
+ export const posts = sqliteTable("posts", {
84
+ id: integer("id").primaryKey({ autoIncrement: true }),
85
+ title: text("title").notNull(),
86
+ content: text("content").notNull(),
87
+ });
88
+ ```
89
+
90
+ ### Generate migrations
91
+
92
+ ```bash
93
+ npx drizzle-kit generate --config server/drizzle/content/drizzle-sqlite.config.ts
94
+ ```
95
+
96
+ ## Use datasources in API routes
97
+
98
+ ### `useDatasource(name, options?)`
99
+
100
+ Get a Drizzle datasource by name (cached for reuse):
101
+
102
+ ```ts
103
+ import { useDatasource } from "nitro-drizzle/runtime";
104
+
105
+ export default defineEventHandler(async (event) => {
106
+ await event.context.drizzle.waitReady();
107
+ const { database, waitReady } = await useDatasource("content");
108
+ await waitReady();
109
+ const posts = await database.select().from(schema.posts).limit(10);
110
+ return { posts };
111
+ });
112
+ ```
113
+
114
+ Options: `{ autoClose?: boolean }` (default `true`) — whether to auto-close the datasource when Nitro shuts down.
115
+
116
+ ### `useDialect(name, handlers)`
117
+
118
+ Type-safe dialect-specific handlers — the correct handler is resolved automatically based on the configured driver:
119
+
120
+ ```ts
121
+ import { useDialect } from "nitro-drizzle/runtime";
122
+
123
+ const result = await useDialect("content", {
124
+ sqlite: (datasource) => datasource.database.select().from(schema.posts).all(),
125
+ postgresql: (datasource) => datasource.database.select().from(schema.posts).all(),
126
+ });
127
+ ```
128
+
129
+ ### `defineConfig(config, filename)`
130
+
131
+ Define a DrizzleKit-compatible config with automatic path resolution. Pass `import.meta.url` as `filename`:
132
+
133
+ ```ts
134
+ import { defineConfig } from "nitro-drizzle/config";
135
+
136
+ export default defineConfig(
137
+ { dialect: "sqlite", out: "./migrations", schema: ["./schema.ts"] },
138
+ import.meta.url,
139
+ );
140
+ ```
141
+
142
+ ## Migrations
143
+
144
+ ### Run migrations as a Nitro task
145
+
146
+ Enable Nitro tasks in `nitro.config.ts` (requires `experimental.tasks` or Nitro with task support), then:
147
+
148
+ ```bash
149
+ npx nitro task drizzle:migrate
150
+ ```
151
+
152
+ ### Run migrations programmatically
153
+
154
+ ```ts
155
+ import { migrate } from "nitro-drizzle/migrations";
156
+
157
+ await migrate("content");
158
+ ```
159
+
160
+ ### Auto-migrate on init
161
+
162
+ ```ts
163
+ export default defineNitroConfig({
164
+ modules: ["nitro-drizzle"],
165
+ drizzle: {
166
+ migrations: { migrateOnInit: true },
167
+ },
168
+ });
169
+ ```
170
+
171
+ ## Project structure example
172
+
173
+ ```
174
+ my-nitro-app/
175
+ ├── nitro.config.ts
176
+ └── server/
177
+ └── drizzle/
178
+ ├── content/
179
+ │ ├── drizzle-sqlite.config.ts
180
+ │ └── sqlite/
181
+ │ ├── migrations/*.sql
182
+ │ └── schema.ts
183
+ └── users/
184
+ ├── drizzle-postgresql.config.ts
185
+ └── postgresql/
186
+ ├── migrations/*.sql
187
+ └── schema.ts
188
+ ```