|
| 1 | +import { carefullyWriteFile, getPackageJSON, spawnAsync } from './utils.js' |
| 2 | +import BaseCommand from '../base-command.js' |
| 3 | +import path from 'path' |
| 4 | +import fs from 'fs/promises' |
| 5 | +import inquirer from 'inquirer' |
| 6 | +import { NETLIFY_NEON_PACKAGE_NAME } from './constants.js' |
| 7 | + |
| 8 | +export const initDrizzle = async (command: BaseCommand) => { |
| 9 | + const workingDirectory = command.project.root ?? command.project.baseDirectory |
| 10 | + if (!workingDirectory) { |
| 11 | + throw new Error('Failed to initialize Drizzle. Project root or base directory not found.') |
| 12 | + } |
| 13 | + const opts = command.opts<{ |
| 14 | + overwrite?: true | undefined |
| 15 | + }>() |
| 16 | + |
| 17 | + const drizzleConfigFilePath = path.resolve(workingDirectory, 'drizzle.config.ts') |
| 18 | + const schemaFilePath = path.resolve(workingDirectory, 'db/schema.ts') |
| 19 | + const dbIndexFilePath = path.resolve(workingDirectory, 'db/index.ts') |
| 20 | + if (opts.overwrite) { |
| 21 | + await fs.writeFile(drizzleConfigFilePath, drizzleConfig) |
| 22 | + await fs.mkdir(path.resolve(workingDirectory, 'db'), { recursive: true }) |
| 23 | + await fs.writeFile(schemaFilePath, drizzleSchema) |
| 24 | + await fs.writeFile(dbIndexFilePath, dbIndex) |
| 25 | + } else { |
| 26 | + await carefullyWriteFile(drizzleConfigFilePath, drizzleConfig, workingDirectory) |
| 27 | + await fs.mkdir(path.resolve(workingDirectory, 'db'), { recursive: true }) |
| 28 | + await carefullyWriteFile(schemaFilePath, drizzleSchema, workingDirectory) |
| 29 | + await carefullyWriteFile(dbIndexFilePath, dbIndex, workingDirectory) |
| 30 | + } |
| 31 | + |
| 32 | + const packageJsonPath = path.resolve(command.workingDir, 'package.json') |
| 33 | + const packageJson = getPackageJSON(command.workingDir) |
| 34 | + |
| 35 | + packageJson.scripts = { |
| 36 | + ...(packageJson.scripts ?? {}), |
| 37 | + ...packageJsonScripts, |
| 38 | + } |
| 39 | + if (opts.overwrite) { |
| 40 | + await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)) |
| 41 | + } |
| 42 | + |
| 43 | + type Answers = { |
| 44 | + updatePackageJson: boolean |
| 45 | + } |
| 46 | + |
| 47 | + if (!opts.overwrite) { |
| 48 | + const answers = await inquirer.prompt<Answers>([ |
| 49 | + { |
| 50 | + type: 'confirm', |
| 51 | + name: 'updatePackageJson', |
| 52 | + message: `Add drizzle db commands to package.json?`, |
| 53 | + }, |
| 54 | + ]) |
| 55 | + if (answers.updatePackageJson) { |
| 56 | + await fs.writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (!Object.keys(packageJson.devDependencies ?? {}).includes('drizzle-kit')) { |
| 61 | + await spawnAsync(command.project.packageManager?.installCommand ?? 'npm install', ['drizzle-kit@latest', '-D'], { |
| 62 | + stdio: 'inherit', |
| 63 | + shell: true, |
| 64 | + }) |
| 65 | + } |
| 66 | + |
| 67 | + if (!Object.keys(packageJson.dependencies ?? {}).includes('drizzle-orm')) { |
| 68 | + await spawnAsync(command.project.packageManager?.installCommand ?? 'npm install', ['drizzle-orm@latest'], { |
| 69 | + stdio: 'inherit', |
| 70 | + shell: true, |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const drizzleConfig = `import { defineConfig } from 'drizzle-kit'; |
| 76 | +
|
| 77 | +export default defineConfig({ |
| 78 | + dialect: 'postgresql', |
| 79 | + dbCredentials: { |
| 80 | + url: process.env.NETLIFY_DATABASE_URL! |
| 81 | + }, |
| 82 | + schema: './db/schema.ts', |
| 83 | + /** |
| 84 | + * Never edit the migrations directly, only use drizzle. |
| 85 | + * There are scripts in the package.json "db:generate" and "db:migrate" to handle this. |
| 86 | + */ |
| 87 | + out: './migrations' |
| 88 | +});` |
| 89 | + |
| 90 | +const drizzleSchema = `import { integer, pgTable, varchar, text } from 'drizzle-orm/pg-core'; |
| 91 | +
|
| 92 | +export const posts = pgTable('posts', { |
| 93 | + id: integer().primaryKey().generatedAlwaysAsIdentity(), |
| 94 | + title: varchar({ length: 255 }).notNull(), |
| 95 | + content: text().notNull().default('') |
| 96 | +});` |
| 97 | + |
| 98 | +const dbIndex = `import { neon } from '${NETLIFY_NEON_PACKAGE_NAME}'; |
| 99 | +import { drizzle } from 'drizzle-orm/neon-http'; |
| 100 | +
|
| 101 | +import * as schema from './schema'; |
| 102 | +
|
| 103 | +export const db = drizzle({ |
| 104 | + schema, |
| 105 | + client: neon() |
| 106 | +});` |
| 107 | + |
| 108 | +const packageJsonScripts = { |
| 109 | + 'db:generate': 'drizzle-kit generate', |
| 110 | + 'db:migrate': 'netlify dev:exec drizzle-kit migrate', |
| 111 | + 'db:studio': 'netlify dev:exec drizzle-kit studio', |
| 112 | +} |
0 commit comments