Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/guide/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ Use `db/seed.ts` for a programmatic seed or `db/seed.sql` for SQL. JavaScript an

Use `db/seed.ts` when you want to generate seed data in code. Export a default function or a named `seed` function.

On Node.js, seed files and schema modules use the aliases in your project's `tsconfig.json`, such as `@schema` and `@auth-schema`. This includes dynamic `import()` calls and seeds selected with `--file`. Dependencies keep their own package imports, including linked workspace packages.

Running the CLI directly under Bun uses Bun's own resolver. Use `npx void db seed` if a seed outside your project needs the project's aliases or a workspace package uses a conflicting alias.

If your tsconfig extends `.void/tsconfig.json`, run `void prepare` before seeding a fresh checkout.

```ts
// db/seed.ts
import { defineSeed } from 'void/seed';
Expand Down
2 changes: 2 additions & 0 deletions packages/void/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@
"drizzle-valibot": "^0.4.2",
"drizzle-zod": "^0.8.3",
"email-validator": "^2.0.4",
"esbuild": "^0.28.2",
"get-tsconfig": "^4.14.3",
"gray-matter": "^4.0.3",
"hono": "catalog:",
"ignore": "catalog:",
Expand Down
202 changes: 202 additions & 0 deletions packages/void/src/cli/db-seed-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import { existsSync, readFileSync, realpathSync } from 'node:fs';
import nodeModule, { type ResolveHookSync } from 'node:module';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { buildSync, transformSync } from 'esbuild';
import { createPathsMatcher, getTsconfig } from 'get-tsconfig';
import { createJiti } from 'jiti';
import { basename, dirname, extname, isAbsolute, join, toSlash } from 'pathslash';

export type SeedModuleImporter = (path: string) => Promise<Record<string, unknown>>;

function seedPaths(root: string) {
const path = join(root, 'tsconfig.json');
if (!existsSync(path)) {
return null;
}
try {
const project = getTsconfig(path);
const options = project?.config.compilerOptions;
if (!project || !options) {
return null;
}
const paths = Object.fromEntries(
Object.entries(options.paths ?? {})
.map(
([name, targets]) =>
[name, targets.filter((target) => !/\.d\.[cm]?ts$/.test(target))] as const,
)
.filter(([, targets]) => targets.length > 0),
);
if (!Object.keys(paths).length && options.baseUrl === undefined) {
return null;
}
return createPathsMatcher({ ...project, config: { compilerOptions: { ...options, paths } } });
} catch (error) {
if (
!existsSync(join(root, '.void/tsconfig.json')) &&
error instanceof Error &&
/File ['"].*[/\\]\.void[/\\]tsconfig\.json['"] not found/.test(error.message)
) {
throw new Error('db: Missing .void/tsconfig.json. Run `void prepare` before seeding.', {
cause: error,
});
}
throw error;
}
}

function seedScope(root: string, seed: string) {
const directories = new Map<string, boolean>([
[toSlash(realpathSync(root)), true],
[dirname(realpathSync(seed)), true],
]);
function ownsDirectory(directory: string): boolean {
const known = directories.get(directory);
if (known !== undefined) {
return known;
}
const parent = dirname(directory);
const owned =
parent !== directory &&
basename(directory) !== 'node_modules' &&
!existsSync(join(directory, 'package.json')) &&
ownsDirectory(parent);
directories.set(directory, owned);
return owned;
}
return (url: string) => url.startsWith('file:') && ownsDirectory(dirname(fileURLToPath(url)));
}

export function createSeedModuleLoader(
root: string,
seed: string,
): {
importModule: SeedModuleImporter;
close: () => void;
} {
const match = seedPaths(root);
const jiti = createJiti(import.meta.url, {
fsCache: false,
moduleCache: false,
interopDefault: false,
tsconfigPaths: false,
});
if (!match || typeof nodeModule.registerHooks !== 'function') {
return { importModule: (path) => jiti.import<Record<string, unknown>>(path), close: () => {} };
}
const owns = seedScope(root, seed);
const modules = new Set<string>();

const resolveImport: ResolveHookSync = (specifier, context, nextResolve) => {
const parent = context.parentURL!;
if (specifier.startsWith('./') || specifier.startsWith('../')) {
try {
return nextResolve(specifier, context);
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (
code === 'MODULE_NOT_FOUND' ||
code === 'ERR_MODULE_NOT_FOUND' ||
code === 'ERR_UNSUPPORTED_DIR_IMPORT'
) {
const target = new URL(specifier, parent);
const resolved = jiti.esmResolve(fileURLToPath(target), { try: true });
if (resolved) {
return { shortCircuit: true, url: resolved + target.search + target.hash };
}
}
throw error;
}
}
if (
owns(parent) &&
!isAbsolute(specifier) &&
!specifier.startsWith('.') &&
!/^[\w+.-]+:/.test(specifier)
) {
for (const target of match(specifier)) {
const resolved = jiti.esmResolve(target, { try: true });
if (resolved) {
return { shortCircuit: true, url: resolved };
}
}
}
return nextResolve(specifier, context);
};
const hooks = nodeModule.registerHooks({
resolve(specifier, context, nextResolve) {
const parent = context.parentURL;
if (!parent || !modules.has(parent) || nodeModule.isBuiltin(specifier)) {
return nextResolve(specifier, context);
}
const local = owns(parent) || /\.[cm]?ts$/.test(fileURLToPath(parent));
const result = local
? resolveImport(specifier, context, nextResolve)
: nextResolve(specifier, context);
if (result.url.startsWith('file:')) {
modules.add(result.url);
if (
local &&
extname(fileURLToPath(result.url)) === '.json' &&
!context.importAttributes?.type
) {
return { ...result, importAttributes: { ...context.importAttributes, type: 'json' } };
}
}
return result;
},
load(url, context, nextLoad) {
if (!modules.has(url)) {
return nextLoad(url, context);
}
const sourcefile = fileURLToPath(url);
if (sourcefile.endsWith('.js') && context.format === 'commonjs' && owns(url)) {
const source = readFileSync(sourcefile, 'utf8');
const { metafile } = buildSync({
stdin: { sourcefile, contents: source },
write: false,
metafile: true,
platform: 'node',
target: 'node24',
});
if (Object.values(metafile.inputs)[0]?.format === 'esm') {
return { format: 'module', source, shortCircuit: true };
}
}
if (!/\.[cm]?ts$/.test(sourcefile)) {
return nextLoad(url, context);
}
const commonjs = sourcefile.endsWith('.cts');
const { code } = transformSync(readFileSync(sourcefile, 'utf8'), {
sourcefile,
loader: 'ts',
target: 'node24',
platform: 'node',
format: commonjs ? 'cjs' : 'esm',
sourcemap: 'inline',
...(commonjs
? {}
: {
define: {
require: 'import.meta.require',
__filename: 'import.meta.filename',
__dirname: 'import.meta.dirname',
},
banner:
"import.meta.require = globalThis.process.getBuiltinModule('node:module').createRequire(import.meta.url);",
}),
});
return { format: commonjs ? 'commonjs' : 'module', source: code, shortCircuit: true };
},
});
return {
async importModule(path) {
const url = pathToFileURL(realpathSync(path)).href;
modules.add(url);
const mod = await import(url);
const cjs = mod['module.exports'];
return cjs && typeof cjs === 'object' ? cjs : mod;
},
close: () => hooks.deregister(),
};
}
101 changes: 53 additions & 48 deletions packages/void/src/cli/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import { createRequire } from 'node:module';
import { pathToFileURL } from 'node:url';
import { confirm, isCancel, outro, log, text } from '@voidzero-dev/vite-plus-prompts';
import { createJiti } from 'jiti';
import { basename, dirname, extname, join, relative, resolve } from 'pathslash';
import {
readConfig,
Expand Down Expand Up @@ -52,6 +51,7 @@ import {
quoteSqlIdentifier,
quoteSqlString,
} from './db-export-sequences.ts';
import type { SeedModuleImporter } from './db-seed-loader.ts';
import { formatTimestamp } from './gen.ts';
import { printSuccess, dim, formatCount, formatUrl, getErrorMessage, pc } from './output.ts';
import { readDeploymentPlatform, readProjectConfig } from './project.ts';
Expand Down Expand Up @@ -776,45 +776,53 @@ async function runSeed(
const seed = resolveSeedTarget(root, file);
const sql = seed.kind === 'sql' ? readFileSync(seed.path, 'utf-8') : undefined;
await confirmReset(root, dialect, yes);
const seedFn = sql === undefined ? await loadSeedFunction(seed.path) : undefined;
const loader =
sql === undefined
? (await import('./db-seed-loader.ts')).createSeedModuleLoader(root, seed.path)
: undefined;
try {
const seedFn = loader ? await loadSeedFunction(seed.path, loader.importModule) : undefined;

// Reset first (drop + re-run migrations). The drop is confirmed above, so do not ask again.
const migrationCount = await resetDatabase(root, dialect, true);
log.step(`Reset database. Applied ${formatCount(migrationCount, 'migration')}.`);
// Reset first (drop + re-run migrations). The drop is confirmed above, so do not ask again.
const migrationCount = await resetDatabase(root, dialect, true);
log.step(`Reset database. Applied ${formatCount(migrationCount, 'migration')}.`);

if (sql !== undefined) {
if (dialect === 'postgresql') {
await withPgClient(root, async (client) => {
await client.query(sql);
});
printSuccess(`Seeded from ${seed.displayPath}.`);
if (sql !== undefined) {
if (dialect === 'postgresql') {
await withPgClient(root, async (client) => {
await client.query(sql);
});
printSuccess(`Seeded from ${seed.displayPath}.`);

return;
}
return;
}

if (dialect === 'mysql') {
await withMySqlClient(root, async (client) => {
await client.query(sql);
});
printSuccess(`Seeded from ${seed.displayPath}.`);
if (dialect === 'mysql') {
await withMySqlClient(root, async (client) => {
await client.query(sql);
});
printSuccess(`Seeded from ${seed.displayPath}.`);

return;
}
return;
}

const { openSqliteDatabase } = await import('../migrations/runner.ts');
const { databasePath: dbPath } = getLocalD1Target(root);
const db = await openSqliteDatabase(dbPath);
try {
db.exec(sql);
} finally {
db.close();
const { openSqliteDatabase } = await import('../migrations/runner.ts');
const { databasePath: dbPath } = getLocalD1Target(root);
const db = await openSqliteDatabase(dbPath);
try {
db.exec(sql);
} finally {
db.close();
}
} else {
await runSeedModule(root, seedFn!, dialect, loader!.importModule);
}
} else {
await runSeedModule(root, seedFn!, dialect);
}

log.info(dim("Restart your dev server if it's running."));
printSuccess(`Seeded from ${seed.displayPath}.`);
log.info(dim("Restart your dev server if it's running."));
printSuccess(`Seeded from ${seed.displayPath}.`);
} finally {
loader?.close();
}
}

/**
Expand Down Expand Up @@ -2426,8 +2434,9 @@ async function runSeedModule(
root: string,
seed: SeedFn<Record<string, unknown>>,
dialect: DatabaseDialect,
importModule: SeedModuleImporter,
): Promise<void> {
const schema = await loadSeedSchema(root);
const schema = await loadSeedSchema(root, importModule);
const casing = getDatabaseCasing(readConfig(root));

if (dialect === 'postgresql') {
Expand Down Expand Up @@ -2470,8 +2479,11 @@ async function runSeedModule(
}
}

async function loadSeedFunction(seedPath: string): Promise<SeedFn<Record<string, unknown>>> {
const mod = await importLocalModule(seedPath);
async function loadSeedFunction(
seedPath: string,
importModule: SeedModuleImporter,
): Promise<SeedFn<Record<string, unknown>>> {
const mod = await importModule(seedPath);
const seed =
typeof mod.default === 'function'
? mod.default
Expand All @@ -2486,7 +2498,10 @@ async function loadSeedFunction(seedPath: string): Promise<SeedFn<Record<string,
return seed as SeedFn<Record<string, unknown>>;
}

async function loadSeedSchema(root: string): Promise<Record<string, unknown>> {
async function loadSeedSchema(
root: string,
importModule: SeedModuleImporter,
): Promise<Record<string, unknown>> {
const { discoverSchema } = await import('../drizzle/discover.ts');
const paths = readProjectPaths(root);
const location = discoverSchema(root, paths);
Expand All @@ -2495,14 +2510,14 @@ async function loadSeedSchema(root: string): Promise<Record<string, unknown>> {
}

if (location.type === 'file') {
const mod = await importLocalModule(location.path);
const mod = await importModule(location.path);
return Object.fromEntries(
Object.entries(mod).filter(([name]) => name !== 'default' && name !== '__esModule'),
);
}

const files = collectSchemaModuleFiles(location.path);
const parts = await Promise.all(files.map((file) => importLocalModule(file)));
const parts = await Promise.all(files.map((file) => importModule(file)));
return Object.assign(
{},
...parts.map((mod) =>
Expand Down Expand Up @@ -2530,16 +2545,6 @@ function collectSchemaModuleFiles(dir: string): Array<string> {
return files.sort();
}

async function importLocalModule(modulePath: string): Promise<Record<string, unknown>> {
// Native import() cannot resolve the extensionless re-exports that `void gen model` writes.
const jiti = createJiti(import.meta.url, {
fsCache: false,
moduleCache: false,
interopDefault: false,
});
return jiti.import<Record<string, unknown>>(modulePath);
}

/** The schema and casing that `void/db` gives drizzle, or undefined when neither is set. */
function getDrizzleSchemaOptions(
schema: Record<string, unknown>,
Expand Down
Loading