-
Notifications
You must be signed in to change notification settings - Fork 0
Add databaseTypes.ts file generation #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { AuthProvider, DatabaseHost } from '../../types'; | ||
|
|
||
| type GenerateTypesProps = { | ||
| databaseHost: DatabaseHost; | ||
| authProvider: AuthProvider; | ||
| }; | ||
|
|
||
| export const generateDatabaseTypes = ({ | ||
| databaseHost, | ||
| authProvider | ||
| }: GenerateTypesProps) => { | ||
| let dbImport = ''; | ||
| let dbTypeLine = ''; | ||
| if (databaseHost === 'neon') { | ||
| dbImport = `import { NeonHttpDatabase } from 'drizzle-orm/neon-http';`; | ||
| dbTypeLine = 'export type DatabaseType = NeonHttpDatabase<SchemaType>;'; | ||
| } else if (databaseHost === 'planetscale') { | ||
| dbImport = `import { PlanetScaleDatabase } from 'drizzle-orm/planetscale-serverless';`; | ||
| dbTypeLine = | ||
| 'export type DatabaseType = PlanetScaleDatabase<SchemaType>;'; | ||
| } else if (databaseHost === 'turso') { | ||
| dbImport = `import { LibSQLDatabase } from 'drizzle-orm/libsql';`; | ||
| dbTypeLine = 'export type DatabaseType = LibSQLDatabase<SchemaType>;'; | ||
| } | ||
|
|
||
| const schemaImport = | ||
| authProvider === 'absoluteAuth' | ||
| ? `import { users, SchemaType } from '../../db/schema';` | ||
| : `import { countHistory, SchemaType } from '../../db/schema';`; | ||
| const extraTypes = | ||
| authProvider === 'absoluteAuth' | ||
| ? `export type User = typeof users.$inferSelect; | ||
| export type NewUser = typeof users.$inferInsert;` | ||
| : `export type CountHistory = typeof countHistory.$inferSelect; | ||
| export type NewCountHistory = typeof countHistory.$inferInsert;`; | ||
|
|
||
| return `${schemaImport} | ||
| ${dbImport} | ||
|
|
||
| ${dbTypeLine ? `${dbTypeLine}\n\n` : '\n'}${extraTypes} | ||
| `; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,9 +159,9 @@ export const generateImportsBlock = ({ | |
| ...(databaseEngine === 'sqlite' && !isRemoteHost | ||
| ? [] | ||
| : [`import { getEnv } from '@absolutejs/absolute'`]), | ||
| authProvider === 'absoluteAuth' | ||
| ? `import { schema, User } from '../../db/schema'` | ||
| : `import { schema } from '../../db/schema'` | ||
| ...(authProvider === 'absoluteAuth' | ||
| ? [`import { schema } from '../../db/schema'`, `import { User } from '../types/databaseTypes'`] | ||
| : [`import { schema } from '../../db/schema'`]) | ||
|
Comment on lines
+162
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chainVerify the import path and User type availability. The User type import has been moved from
Run the following script to verify the import path and type availability: 🏁 Script executed: #!/bin/bash
# Description: Verify the User type is exported from the new databaseTypes file and check path consistency
# Find where generateDatabaseTypes creates the types file
echo "=== Checking generateDatabaseTypes implementation ==="
ast-grep --pattern 'export $_ generateDatabaseTypes($$$) {
$$$
}'
# Search for User type exports in database type generation
echo -e "\n=== Searching for User type exports ==="
rg -nP --type=ts 'export\s+(type|interface)\s+User\b' -C 3
# Check where the imports are being written relative to backend directory
echo -e "\n=== Checking typesDirectory usage in scaffolding ==="
rg -nP --type=ts 'typesDirectory' -C 2
# Verify the generated imports structure
echo -e "\n=== Checking other usages of schema imports ==="
rg -nP --type=ts "from ['\"].*db/schema['\"]" -C 1Length of output: 5506 Fix relative import path for User type. The User type is correctly exported from the generated Update line 163 in 🤖 Prompt for AI Agents
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The path is correct as the import is in src/backend/server.ts and the types file is in src/types/databaseTypes.ts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bnziv, you're absolutely right! I apologize for the confusion. From
The import path as written in the code is accurate. Thank you for the clarification! |
||
| ); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add handling for unhandled databaseHost values.
The function only handles
'neon','planetscale', and'turso'asdatabaseHostvalues, but according to the type definition,databaseHostcan also beundefinedor'none'. When an unhandled value is provided,dbImportanddbTypeLineremain empty strings, resulting in a generated file without aDatabaseTypeexport. This will cause TypeScript compilation errors if downstream code imports or referencesDatabaseType.Consider one of the following approaches:
databaseHostvaluesApply this diff to add validation:
export const generateDatabaseTypes = ({ databaseHost, authProvider }: GenerateTypesProps) => { + // Skip type generation for non-hosted databases + if (!databaseHost || databaseHost === 'none') { + return null; + } + let dbImport = ''; let dbTypeLine = ''; if (databaseHost === 'neon') { dbImport = `import { NeonHttpDatabase } from 'drizzle-orm/neon-http';`; dbTypeLine = 'export type DatabaseType = NeonHttpDatabase<SchemaType>;'; } else if (databaseHost === 'planetscale') { dbImport = `import { PlanetScaleDatabase } from 'drizzle-orm/planetscale-serverless';`; dbTypeLine = 'export type DatabaseType = PlanetScaleDatabase<SchemaType>;'; } else if (databaseHost === 'turso') { dbImport = `import { LibSQLDatabase } from 'drizzle-orm/libsql';`; dbTypeLine = 'export type DatabaseType = LibSQLDatabase<SchemaType>;'; + } else { + throw new Error(`Unsupported databaseHost: ${databaseHost}`); }Then update the caller in
scaffoldDatabase.tsto handle the null return: