1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//! Drizzle ORM plugin.
//!
//! Detects Drizzle projects and marks migration/schema files as entry points.
//! Parses drizzle.config to extract the `schema` field (making schema file
//! exports framework-consumed entry points) and the `out` field (custom
//! migration output directory). Also extracts referenced dependencies from
//! import statements.
use std::path::Path;
use super::config_parser;
use super::{Plugin, PluginResult};
pub struct DrizzlePlugin;
const ENABLERS: &[&str] = &["drizzle-orm"];
const ENTRY_PATTERNS: &[&str] = &["drizzle/**/*.{ts,js}"];
const CONFIG_PATTERNS: &[&str] = &["drizzle.config.{ts,js,mjs}"];
const ALWAYS_USED: &[&str] = &["drizzle.config.{ts,js,mjs}"];
const TOOLING_DEPENDENCIES: &[&str] = &["drizzle-orm", "drizzle-kit"];
impl Plugin for DrizzlePlugin {
fn name(&self) -> &'static str {
"drizzle"
}
fn enablers(&self) -> &'static [&'static str] {
ENABLERS
}
fn entry_patterns(&self) -> &'static [&'static str] {
ENTRY_PATTERNS
}
fn config_patterns(&self) -> &'static [&'static str] {
CONFIG_PATTERNS
}
fn always_used(&self) -> &'static [&'static str] {
ALWAYS_USED
}
fn tooling_dependencies(&self) -> &'static [&'static str] {
TOOLING_DEPENDENCIES
}
fn resolve_config(&self, config_path: &Path, source: &str, _root: &Path) -> PluginResult {
let mut result = PluginResult::default();
// Extract import sources as referenced dependencies
let imports = config_parser::extract_imports(source, config_path);
for imp in &imports {
let dep = crate::resolve::extract_package_name(imp);
result.referenced_dependencies.push(dep);
}
// Extract `schema` field → entry patterns for schema files.
// Drizzle schema files export tables, relations, and enums that are
// consumed by the Drizzle runtime (via `drizzle()` init) and by
// drizzle-kit (for migrations). These exports are never directly
// imported in user code, so without this they appear as false positives.
//
// The `schema` field accepts:
// - A single path: `schema: "./src/db/schema.ts"`
// - A glob pattern: `schema: "./src/db/*.ts"`
// - An array: `schema: ["./src/db/users.ts", "./src/db/posts.ts"]`
// - A directory: `schema: "./src/db/schema"` (Drizzle scans recursively)
let schema_paths =
config_parser::extract_config_string_or_array(source, config_path, &["schema"]);
for path in &schema_paths {
result
.entry_patterns
.extend(schema_path_to_entry_patterns(path));
}
// Extract `out` field → custom migration output directory.
// Default is `drizzle/` (covered by static ENTRY_PATTERNS), but users
// can configure a different directory.
if let Some(out_dir) = config_parser::extract_config_string(source, config_path, &["out"]) {
let out = out_dir.trim_start_matches("./").trim_end_matches('/');
if out != "drizzle" {
result.entry_patterns.push(format!("{out}/**/*.{{ts,js}}"));
}
}
result
}
}
/// Convert a schema path from drizzle.config into entry patterns.
///
/// Returns one or more glob patterns:
/// - Glob patterns (`src/db/*.ts`) → used as-is
/// - Directory paths (`src/db/schema`) → `dir/**/*.{ts,...}`
/// - Index/barrel files (`src/db/schema/index.ts`) → the file itself PLUS
/// `dir/**/*.{ts,...}` for siblings, because Drizzle follows imports from
/// the barrel to discover all schema files
/// - Other files (`src/db/schema.ts`) → just the file
fn schema_path_to_entry_patterns(path: &str) -> Vec<String> {
let path = path.trim_start_matches("./");
// If it contains glob characters, use as-is
if path.contains('*') || path.contains('?') || path.contains('{') || path.contains('[') {
return vec![path.to_string()];
}
// If it has a recognized JS/TS extension, it's a file
if let Some(ext) = Path::new(path).extension().and_then(|e| e.to_str())
&& matches!(
ext,
"ts" | "tsx" | "js" | "jsx" | "mts" | "mjs" | "cts" | "cjs"
)
{
let mut patterns = vec![path.to_string()];
// If this is an index/barrel file, also add the parent directory.
// Drizzle follows imports from the barrel to discover all schema files,
// so siblings (relations.ts, users.ts, etc.) should also be entry points.
if let Some(stem) = Path::new(path).file_stem().and_then(|s| s.to_str())
&& stem == "index"
&& let Some(parent) = Path::new(path).parent()
&& parent != Path::new("")
{
let dir = parent.to_string_lossy();
patterns.push(format!("{dir}/**/*.{{ts,tsx,js,jsx,mts,mjs,cts,cjs}}"));
}
return patterns;
}
// Otherwise, treat as a directory — Drizzle scans recursively
vec![format!("{path}/**/*.{{ts,tsx,js,jsx,mts,mjs,cts,cjs}}")]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn schema_path_file() {
assert_eq!(
schema_path_to_entry_patterns("./src/db/schema.ts"),
vec!["src/db/schema.ts"]
);
}
#[test]
fn schema_path_index_file_adds_directory() {
assert_eq!(
schema_path_to_entry_patterns("./src/db/schema/index.ts"),
vec![
"src/db/schema/index.ts".to_string(),
"src/db/schema/**/*.{ts,tsx,js,jsx,mts,mjs,cts,cjs}".to_string(),
]
);
}
#[test]
fn schema_path_directory() {
assert_eq!(
schema_path_to_entry_patterns("./src/db/schema"),
vec!["src/db/schema/**/*.{ts,tsx,js,jsx,mts,mjs,cts,cjs}"]
);
}
#[test]
fn schema_path_glob() {
assert_eq!(
schema_path_to_entry_patterns("./src/db/*.ts"),
vec!["src/db/*.ts"]
);
}
#[test]
fn schema_path_no_prefix() {
assert_eq!(
schema_path_to_entry_patterns("src/db/schema.ts"),
vec!["src/db/schema.ts"]
);
}
#[test]
fn resolve_config_extracts_schema_string() {
let source = r#"
import { defineConfig } from "drizzle-kit";
export default defineConfig({
schema: "./src/db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
});
"#;
let plugin = DrizzlePlugin;
let result = plugin.resolve_config(
Path::new("drizzle.config.ts"),
source,
Path::new("/project"),
);
assert!(
result
.entry_patterns
.contains(&"src/db/schema.ts".to_string())
);
assert!(
result
.referenced_dependencies
.contains(&"drizzle-kit".to_string())
);
}
#[test]
fn resolve_config_extracts_schema_array() {
let source = r#"
export default {
schema: ["./src/db/users.ts", "./src/db/posts.ts"],
out: "./drizzle",
dialect: "postgresql",
};
"#;
let plugin = DrizzlePlugin;
let result = plugin.resolve_config(
Path::new("drizzle.config.ts"),
source,
Path::new("/project"),
);
assert!(
result
.entry_patterns
.contains(&"src/db/users.ts".to_string())
);
assert!(
result
.entry_patterns
.contains(&"src/db/posts.ts".to_string())
);
}
#[test]
fn resolve_config_extracts_schema_directory() {
let source = r#"
export default {
schema: "./src/db",
dialect: "postgresql",
};
"#;
let plugin = DrizzlePlugin;
let result = plugin.resolve_config(
Path::new("drizzle.config.ts"),
source,
Path::new("/project"),
);
assert!(
result
.entry_patterns
.contains(&"src/db/**/*.{ts,tsx,js,jsx,mts,mjs,cts,cjs}".to_string())
);
}
#[test]
fn resolve_config_extracts_schema_glob() {
let source = r#"
export default {
schema: "./src/db/*.ts",
dialect: "postgresql",
};
"#;
let plugin = DrizzlePlugin;
let result = plugin.resolve_config(
Path::new("drizzle.config.ts"),
source,
Path::new("/project"),
);
assert!(result.entry_patterns.contains(&"src/db/*.ts".to_string()));
}
#[test]
fn resolve_config_custom_out_dir() {
let source = r#"
export default {
schema: "./src/db/schema.ts",
out: "./migrations",
dialect: "postgresql",
};
"#;
let plugin = DrizzlePlugin;
let result = plugin.resolve_config(
Path::new("drizzle.config.ts"),
source,
Path::new("/project"),
);
assert!(
result
.entry_patterns
.contains(&"src/db/schema.ts".to_string())
);
assert!(
result
.entry_patterns
.contains(&"migrations/**/*.{ts,js}".to_string())
);
}
#[test]
fn resolve_config_default_out_dir_not_duplicated() {
let source = r#"
export default {
schema: "./src/db/schema.ts",
out: "./drizzle",
dialect: "postgresql",
};
"#;
let plugin = DrizzlePlugin;
let result = plugin.resolve_config(
Path::new("drizzle.config.ts"),
source,
Path::new("/project"),
);
// The default "drizzle/" out dir is already covered by static ENTRY_PATTERNS,
// so resolve_config should NOT add a duplicate.
assert!(
!result
.entry_patterns
.iter()
.any(|p| p.starts_with("drizzle/"))
);
}
#[test]
fn resolve_config_module_exports() {
let source = r#"
module.exports = {
schema: "./src/db/schema",
out: "./migrations",
dialect: "mysql",
};
"#;
let plugin = DrizzlePlugin;
let result = plugin.resolve_config(
Path::new("drizzle.config.js"),
source,
Path::new("/project"),
);
assert!(
result
.entry_patterns
.contains(&"src/db/schema/**/*.{ts,tsx,js,jsx,mts,mjs,cts,cjs}".to_string())
);
assert!(
result
.entry_patterns
.contains(&"migrations/**/*.{ts,js}".to_string())
);
}
}