Skip to content

Commit

Permalink
chore: clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
r1tsuu committed Dec 13, 2024
1 parent 3b61e96 commit 9ed9bf2
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/drizzle/src/utilities/createSchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ const renameTypescriptVariables = (source: string, variablesMap: Map<string, str
return updatedCode
}

/**
* This code is responsible for:
* * Sanitizing imports from 'drizzle-orm/*' to '@payloadcms/package-name/drizzle/*'
* * Sanitizing generated variables for tables to match what payload.db.drizzle actually has
* * Workaround for point field with default value, Drizzle does not generate it properly for some reason.
* * Generating the final output with all imports, schema type achieved through all `exports` and declaration for package.
*/
const formatSchemaFile = (packageName: string, content: string) => {
let formatted = ''

Expand Down Expand Up @@ -138,6 +145,12 @@ declare module "${packageName}/types" {
)
}

/**
* * Returns method that uses Drizzle CLI pull command to generate output schema.
* * Builds dynamically needed for CLI config https://orm.drizzle.team/docs/drizzle-config-file
* * Uses temporary directory for calling the CLI.
* * Saves the final output, either to defaultOutputFile or current working directory.
*/
export const createSchemaGenerator = ({
dbCredentials,
defaultOutputFile,
Expand Down Expand Up @@ -188,16 +201,9 @@ ${Object.entries(dbCredentials)

await writeFile(drizzleConfigPath, drizzleConfig, 'utf-8')

try {
execSync(`node ${drizzleBinPath} pull --config=${drizzleConfigPath}`, {
stdio: 'inherit',
})
} catch (e) {
this.payload.logger.error(e)
throw e
}

this.payload.logger.info('Post processing...')
execSync(`node ${drizzleBinPath} pull --config=${drizzleConfigPath}`, {
stdio: 'inherit',
})

const [schema] = await Promise.all([
readFile(path.resolve(tempDir, 'schema.ts'), 'utf-8'),
Expand Down

0 comments on commit 9ed9bf2

Please sign in to comment.