-
Notifications
You must be signed in to change notification settings - Fork 2
Fix database index and table drop errors #9
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,161 @@ | ||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Drizzle-TypeBox Schema Conversion Utilities | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * Converts Drizzle ORM schemas to TypeBox schemas for Elysia validation. | ||||||||||||||||||||||||||||||||||||||||||
| * This eliminates duplicate type definitions - define once in Drizzle, use everywhere. | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * Usage: | ||||||||||||||||||||||||||||||||||||||||||
| * import { UserInsertSchema, UserSelectSchema, spread } from './typebox-schemas' | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * .post('/users', handler, { | ||||||||||||||||||||||||||||||||||||||||||
| * body: UserInsertSchema, | ||||||||||||||||||||||||||||||||||||||||||
| * response: UserSelectSchema | ||||||||||||||||||||||||||||||||||||||||||
| * }) | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * IMPORTANT: To avoid TypeScript infinite type instantiation errors, | ||||||||||||||||||||||||||||||||||||||||||
| * always declare TypeBox schemas as separate variables before using in Elysia. | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * @see https://elysiajs.com/integrations/drizzle | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import { createInsertSchema, createSelectSchema } from "drizzle-typebox"; | ||||||||||||||||||||||||||||||||||||||||||
| import type { TObject } from "@sinclair/typebox"; | ||||||||||||||||||||||||||||||||||||||||||
| import { t } from "elysia"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Import Drizzle schemas | ||||||||||||||||||||||||||||||||||||||||||
| import { users, projects, activityLog } from "./schema/users.schema"; | ||||||||||||||||||||||||||||||||||||||||||
| import { assets } from "./schema/assets.schema"; | ||||||||||||||||||||||||||||||||||||||||||
| import { apiKeys } from "./schema/api-keys.schema"; | ||||||||||||||||||||||||||||||||||||||||||
| import { prompts } from "./schema/prompts.schema"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // UTILITY FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Spread utility - extracts TypeBox properties as plain object | ||||||||||||||||||||||||||||||||||||||||||
| * Use this to pick specific fields from a schema | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * @example | ||||||||||||||||||||||||||||||||||||||||||
| * body: t.Object({ | ||||||||||||||||||||||||||||||||||||||||||
| * ...spread(UserInsertSchema, ['displayName', 'email']), | ||||||||||||||||||||||||||||||||||||||||||
| * customField: t.String() | ||||||||||||||||||||||||||||||||||||||||||
| * }) | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| export function spread<T extends TObject>( | ||||||||||||||||||||||||||||||||||||||||||
| schema: T, | ||||||||||||||||||||||||||||||||||||||||||
| keys: (keyof T["properties"])[], | ||||||||||||||||||||||||||||||||||||||||||
| ): Partial<T["properties"]> { | ||||||||||||||||||||||||||||||||||||||||||
| const result: Partial<T["properties"]> = {}; | ||||||||||||||||||||||||||||||||||||||||||
| for (const key of keys) { | ||||||||||||||||||||||||||||||||||||||||||
| if (key in schema.properties) { | ||||||||||||||||||||||||||||||||||||||||||
| result[key] = schema.properties[key]; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+45
to
+56
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. Silent Failure in
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
| * Spreads all properties from a schema | ||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||
| * @example | ||||||||||||||||||||||||||||||||||||||||||
| * body: t.Object({ | ||||||||||||||||||||||||||||||||||||||||||
| * ...spreads(UserInsertSchema) | ||||||||||||||||||||||||||||||||||||||||||
| * }) | ||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||
| export function spreads<T extends TObject>(schema: T): T["properties"] { | ||||||||||||||||||||||||||||||||||||||||||
| return schema.properties; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // USER SCHEMAS | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for inserting a new user */ | ||||||||||||||||||||||||||||||||||||||||||
| export const UserInsertSchema = createInsertSchema(users, { | ||||||||||||||||||||||||||||||||||||||||||
| // Add email format validation | ||||||||||||||||||||||||||||||||||||||||||
| email: t.Optional(t.String({ format: "email" })), | ||||||||||||||||||||||||||||||||||||||||||
|
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. Bug: TypeBox email format validation is silently skipped due to missing 🔍 Detailed AnalysisTypeBox's 💡 Suggested FixAdd 🤖 Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||||||||||||||||||||||||||||||||||||||||||
| // Ensure wallet addresses are lowercase | ||||||||||||||||||||||||||||||||||||||||||
| walletAddress: t.Optional(t.String({ minLength: 42, maxLength: 42 })), | ||||||||||||||||||||||||||||||||||||||||||
|
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. logic: wallet address validation assumes Ethereum addresses (42 chars), but comment mentions lowercase while validation only checks length. Should the walletAddress validation include a pattern check for hexadecimal format and enforce lowercase, or are non-Ethereum addresses also supported? Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/core/server/db/typebox-schemas.ts
Line: 79:79
Comment:
**logic:** wallet address validation assumes Ethereum addresses (42 chars), but comment mentions lowercase while validation only checks length. Should the walletAddress validation include a pattern check for hexadecimal format and enforce lowercase, or are non-Ethereum addresses also supported?
How can I resolve this? If you propose a fix, please make it concise.
Comment on lines
+78
to
+79
Contributor
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 validation for
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for selecting/returning a user */ | ||||||||||||||||||||||||||||||||||||||||||
| export const UserSelectSchema = createSelectSchema(users); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for user profile updates */ | ||||||||||||||||||||||||||||||||||||||||||
| export const UserProfileUpdateSchema = t.Object({ | ||||||||||||||||||||||||||||||||||||||||||
| displayName: t.String({ minLength: 1, maxLength: 255 }), | ||||||||||||||||||||||||||||||||||||||||||
| email: t.String({ format: "email" }), | ||||||||||||||||||||||||||||||||||||||||||
| discordUsername: t.Optional(t.String({ maxLength: 255 })), | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+85
to
+90
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. style: UserProfileUpdateSchema is manually defined instead of using createInsertSchema with field selection - this creates potential schema drift if the users table changes Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! Prompt To Fix With AIThis is a comment left during a code review.
Path: apps/core/server/db/typebox-schemas.ts
Line: 85:90
Comment:
**style:** UserProfileUpdateSchema is manually defined instead of using createInsertSchema with field selection - this creates potential schema drift if the users table changes
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // PROJECT SCHEMAS | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for inserting a new project */ | ||||||||||||||||||||||||||||||||||||||||||
| export const ProjectInsertSchema = createInsertSchema(projects, { | ||||||||||||||||||||||||||||||||||||||||||
| name: t.String({ minLength: 1, maxLength: 255 }), | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for selecting/returning a project */ | ||||||||||||||||||||||||||||||||||||||||||
| export const ProjectSelectSchema = createSelectSchema(projects); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // ACTIVITY LOG SCHEMAS | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for inserting activity log entries */ | ||||||||||||||||||||||||||||||||||||||||||
| export const ActivityLogInsertSchema = createInsertSchema(activityLog); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for selecting activity log entries */ | ||||||||||||||||||||||||||||||||||||||||||
| export const ActivityLogSelectSchema = createSelectSchema(activityLog); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // ASSET SCHEMAS | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for inserting a new asset */ | ||||||||||||||||||||||||||||||||||||||||||
| export const AssetInsertSchema = createInsertSchema(assets, { | ||||||||||||||||||||||||||||||||||||||||||
| name: t.String({ minLength: 1, maxLength: 255 }), | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for selecting/returning an asset */ | ||||||||||||||||||||||||||||||||||||||||||
| export const AssetSelectSchema = createSelectSchema(assets); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // API KEY SCHEMAS | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for inserting a new API key */ | ||||||||||||||||||||||||||||||||||||||||||
| export const ApiKeyInsertSchema = createInsertSchema(apiKeys, { | ||||||||||||||||||||||||||||||||||||||||||
| name: t.String({ minLength: 1, maxLength: 255 }), | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for selecting API keys */ | ||||||||||||||||||||||||||||||||||||||||||
| export const ApiKeySelectSchema = createSelectSchema(apiKeys); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // PROMPT SCHEMAS | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for inserting prompts */ | ||||||||||||||||||||||||||||||||||||||||||
| export const PromptInsertSchema = createInsertSchema(prompts); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** Schema for selecting prompts */ | ||||||||||||||||||||||||||||||||||||||||||
| export const PromptSelectSchema = createSelectSchema(prompts); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
| // TYPE EXPORTS (inferred from TypeBox schemas) | ||||||||||||||||||||||||||||||||||||||||||
| // ============================================================================ | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import type { Static } from "@sinclair/typebox"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export type UserInsert = Static<typeof UserInsertSchema>; | ||||||||||||||||||||||||||||||||||||||||||
| export type UserSelect = Static<typeof UserSelectSchema>; | ||||||||||||||||||||||||||||||||||||||||||
| export type ProjectInsert = Static<typeof ProjectInsertSchema>; | ||||||||||||||||||||||||||||||||||||||||||
| export type ProjectSelect = Static<typeof ProjectSelectSchema>; | ||||||||||||||||||||||||||||||||||||||||||
| export type AssetInsert = Static<typeof AssetInsertSchema>; | ||||||||||||||||||||||||||||||||||||||||||
| export type AssetSelect = Static<typeof AssetSelectSchema>; | ||||||||||||||||||||||||||||||||||||||||||
| export type ApiKeyInsert = Static<typeof ApiKeyInsertSchema>; | ||||||||||||||||||||||||||||||||||||||||||
| export type ApiKeySelect = Static<typeof ApiKeySelectSchema>; | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+154
to
+161
Contributor
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. There's an inconsistency in the exported types. While schemas for
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ import { userService } from "../services/UserService"; | |
| import { ActivityLogService } from "../services/ActivityLogService"; | ||
| import { ApiKeyService } from "../services/ApiKeyService"; | ||
| import type { AuthUser } from "../types/auth"; | ||
| // Import drizzle-typebox schemas for Elysia validation | ||
| import { UserProfileUpdateSchema } from "../db/typebox-schemas"; | ||
|
|
||
| export const usersRoutes = new Elysia({ prefix: "/api/users" }) | ||
| // Regular authenticated user routes | ||
|
|
@@ -76,11 +78,9 @@ export const usersRoutes = new Elysia({ prefix: "/api/users" }) | |
| return { user: updatedUser }; | ||
| }, | ||
|
Comment on lines
78
to
79
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. Security Risk: Potential Exposure of Sensitive User FieldsThe response return { user: filterUserForClient(updatedUser) };Where |
||
| { | ||
| body: t.Object({ | ||
| displayName: t.String(), | ||
| email: t.String(), | ||
| discordUsername: t.Optional(t.String()), | ||
| }), | ||
| // Using drizzle-typebox schema for validation | ||
| // Defined once in Drizzle, used for both DB and API validation | ||
| body: UserProfileUpdateSchema, | ||
| detail: { | ||
| tags: ["Users"], | ||
| summary: "Update user profile", | ||
|
|
||
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.
The
DROP INDEXstatement on line 6 is redundant becauseDROP TABLE ... CASCADEon line 11 will automatically drop any dependent objects, including indexes. Removing lines 5-7 will make the script more concise and rely on the intended behavior ofCASCADE.