Skip to content

Commit

Permalink
feat(drizzle): add public runtime mode
Browse files Browse the repository at this point in the history
  • Loading branch information
productdevbook committed Mar 10, 2024
1 parent 2517bb1 commit 930e848
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 60 deletions.
23 changes: 18 additions & 5 deletions packages/nuxt/src/runtime/core/utils/moduleRuntimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ export function generateModuleRuntimeConfig<T>(
const name = generateProjectName(projectName, moduleName)
if (publicRuntime) {
runtimeConfig.public[projectName] ??= {}
runtimeConfig.public[projectName] = defu(runtimeConfig.public[projectName] as any, {
runtimeConfig.public[projectName] = defu({
[moduleName]: {
...Object.entries(config).map(([key, value]) => {
...Object.entries(config).map(([key, _]) => {
return {
[key]: value === undefined
? process.env[`NUXT_${snakeCase(`${projectName}_${moduleName}_${key}` as string).toUpperCase()}`] ?? defaultConfig[key] ?? ''
: value,
[key]: import.meta.env[`NUXT_${snakeCase(`${projectName}_${moduleName}_${key}` as string).toUpperCase()}`],
}
}).reduce((acc, cur) => {
return {
Expand All @@ -40,6 +38,21 @@ export function generateModuleRuntimeConfig<T>(
}
}, {}),
},
}, {
...runtimeConfig.public[projectName] as any,
}, {
[moduleName]: {
...Object.entries(config).map(([key, _]) => {
return {
[key]: defaultConfig[key] ?? '',
}
}).reduce((acc, cur) => {
return {
...acc,
...cur,
}
}),
},
}) as T

const { keyEnvValue } = runtimeConfigToEnv(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export async function setupPostgres(
user: 'postgres', // Username of database user
password: 'postgres', // Password of database user
ssl: false, // Use SSL
drop: false, // Drop database before migration
seed: false, // Seed database after migration
migrate: false, // Migrate database
}, false)

// Config generation
Expand Down
141 changes: 89 additions & 52 deletions packages/nuxt/src/runtime/modules/drizzle/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { join } from 'node:path'
import { execSync } from 'node:child_process'
import { addServerImportsDir, createResolver, useLogger } from '@nuxt/kit'
import { addServerImportsDir, createResolver } from '@nuxt/kit'
import { camelCase } from 'scule'
import { basename } from 'pathe'
import defu from 'defu'
import consola from 'consola'
import { definePergelModule } from '../../core/definePergel'
import { useNitroImports } from '../../core/utils/useImports'
import { globsBuilderWatch } from '../../core/utils/globs'
import { createFolderModule } from '../../core/utils/createFolderModule'
import type { DrizzleConfig, ResolvedDrizzleConfig } from './types'
import { generateModuleRuntimeConfig, generateModuleRuntimeConfigEnv } from '../../core/utils/moduleRuntimeConfig'
import type { DrizzleConfig, DrizzleRuntimeConfig, ResolvedDrizzleConfig } from './types'
import { setupPostgres } from './drivers/postgres'
import { copyMigrationFolder } from './core'

const _logger = useLogger('pergel:drizzle')
const logger = consola.create({}).withTag('drizzle')

export default definePergelModule<DrizzleConfig, ResolvedDrizzleConfig>({
meta: {
Expand Down Expand Up @@ -76,17 +79,32 @@ export default definePergelModule<DrizzleConfig, ResolvedDrizzleConfig>({
driver: driver ?? 'pg',
} as any,
studio: true,
watch: {
push: true,
seed: true,
drop: true,
},
devtoolsStatus: true,
}
},
async setup({ nuxt, options }) {
const resolver = createResolver(import.meta.url)

const envData = generateModuleRuntimeConfig<DrizzleRuntimeConfig>(nuxt, options, {
drop: true, // Drop database before migration
seed: true, // Seed database after migration
migrate: true, // Migrate database
mode: 'dev', // Development mode || 'production'
}, true)

generateModuleRuntimeConfigEnv(nuxt, options, {
drop: true, // Drop database before migration
push: true, // Push database after migration
seed: true, // Seed database after migration
mode: 'dev', // Development mode || 'production'
default: {
drop: true, // Drop database before migration
push: true, // Push database after migration
seed: true, // Seed database after migration
mode: 'dev', // Development mode || 'production'
},
})

// Driver setup
switch (options._driver.name) {
case 'postgresjs':
Expand Down Expand Up @@ -145,76 +163,95 @@ export default definePergelModule<DrizzleConfig, ResolvedDrizzleConfig>({

copyMigrationFolder(nuxt)

if (nuxt.options.dev) {
// Watch for changes
nuxt.hook('builder:watch', async (event, path) => {
const match = globsBuilderWatch(
nuxt,
path,
'.ts',
'schema',
)
if (!match)
return

const { projectName, moduleName } = match

if (projectName) {
const activeProject = (nuxt._pergel.rootOptions.projects as any)[projectName][moduleName] as DrizzleConfig

if (!activeProject)
nuxt.hook('builder:watch', async (event, path) => {
const match = globsBuilderWatch(
nuxt,
path,
'.ts',
'schema',
)
if (!match)
return

if (match) {
if (activeProject.watch?.drop) {
execSync(
const { projectName, moduleName } = match

if (projectName) {
const activeProject = (nuxt._pergel.rootOptions.projects as any)[projectName][moduleName] as DrizzleConfig

if (!activeProject)
return

const config = defu({
drop: activeProject.watch?.drop ?? envData.runtimeConfig?.drop,
push: activeProject.watch?.push ?? envData.runtimeConfig?.push,
seed: activeProject.watch?.seed ?? envData.runtimeConfig?.seed,
mode: envData.runtimeConfig?.mode ?? 'dev',
}, {
drop: true,
push: true,
seed: true,
ssl: true,
mode: 'dev',
})

if (config.mode !== 'dev') {
consola.info('Drizzle, skipping drop, push and seed')
return
}

if (match && config.mode === 'dev') {
if (config.drop) {
execSync(
`pergel module -s=dev:drop -p=${projectName} -m=${moduleName}`,
{
stdio: 'inherit',
cwd: nuxt.options.rootDir,
},
)
_logger.info(`Drop ${projectName} schema`)
}
)
logger.info(`Drop ${projectName} schema`)
}

if (activeProject.watch?.push) {
execSync(
if (config.push) {
execSync(
`pergel module -s=push -p=${projectName} -m=${moduleName}`,
{
stdio: 'inherit',
cwd: nuxt.options.rootDir,
},
)
_logger.info(`Pushed ${projectName} schema`)
}
)
logger.info(`Pushed ${projectName} schema`)
}

if (activeProject.watch?.seed) {
execSync(
if (config.seed) {
execSync(
`pergel module -s=dev:seed -p=${projectName} -m=${moduleName}`,
{
stdio: 'inherit',
cwd: nuxt.options.rootDir,
},
)
_logger.info(`Seeded ${projectName} schema`)
)
logger.info(`Seeded ${projectName} schema`)
}
}
}
}
})
})
}

// Auto import
switch (options._driver.name) {
case 'postgresjs':
// TODO: bug https://github.com/unjs/mlly/issues/209
// useNitroImports(nuxt, {
// presets: [
// {
// from: 'postgres',
// imports: [
// 'PostgresError',
// ],
// },
// ],
// })
useNitroImports(nuxt, {
presets: [
{
from: 'postgres',
imports: [
'PostgresError',
] satisfies Array<keyof typeof import('postgres')>,
},
],
})
break
}

Expand Down
8 changes: 8 additions & 0 deletions packages/nuxt/src/runtime/modules/drizzle/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,11 @@ export interface PostgresJSModuleRuntimeConfig {
url?: string
options?: Options<any>
}

export interface DrizzleRuntimeConfig {
drop: boolean
push: boolean
seed: boolean
migrate: boolean
mode: string
}

0 comments on commit 930e848

Please sign in to comment.