Repository navigation
sql: add support for column name transforms (snake_case to camelCase) #36451
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -159,13 +159,44 @@ declare module "bun" { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor(message: string, options: { code: string; errno: number; byteOffset?: number | undefined }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| const camel: ColumnTransform; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pascal: ColumnTransform; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const kebab: ColumnTransform; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const snake: ColumnTransform; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function toCamel(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function toPascal(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function toKebab(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function toSnake(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function fromCamel(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function fromPascal(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function fromKebab(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function fromSnake(str: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| type AwaitPromisesArray<T extends Array<PromiseLike<any>>> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| [K in keyof T]: Awaited<T[K]>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ContextCallbackResult<T> = T extends Array<PromiseLike<any>> ? AwaitPromisesArray<T> : Awaited<T>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ContextCallback<T, SQL> = (sql: SQL) => Bun.MaybePromise<T>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface ColumnTransform { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (column: string): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| to?: (column: string) => string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from?: (column: string) => string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface TransformOptions { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| column?: ColumnTransform | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| to?: (column: string) => string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from?: (column: string) => string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| value?: ((value: any) => any) | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| to?: (value: any) => any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| from?: (value: any) => any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| row?: ((row: any) => any) | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+188
to
+198
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. 🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
♻️ Proposed type addition interface TransformOptions {
column?: ColumnTransform | {
to?: (column: string) => string;
from?: (column: string) => string;
} | undefined;
+ /** Shorthand for `column`, applied when `column` is not set. */
+ to?: ((column: string) => string) | undefined;
+ from?: ((column: string) => string) | undefined;
value?: ((value: any) => any) | {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface SQLiteOptions extends BunSQLite.DatabaseOptions { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| adapter?: "sqlite"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -195,6 +226,11 @@ declare module "bun" { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| * Receives the closing `Error`, or `null`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| onclose?: ((err: Error | null) => void) | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Column name and value transforms (e.g. `postgres.camel` for snake_case -> camelCase). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| transform?: TransformOptions | ColumnTransform | ((column: string) => string) | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+229
to
+233
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. 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
Also applies to: 428-432 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface PostgresOrMySQLOptions { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -389,6 +425,11 @@ declare module "bun" { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| * @default false | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| allowPublicKeyRetrieval?: boolean | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Column name and value transforms (e.g. `postgres.camel` for snake_case -> camelCase). | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| transform?: TransformOptions | ColumnTransform | ((column: string) => string) | undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,26 @@ const { Query, SQLQueryFlags } = require("internal/sql/query"); | |
| const { PostgresAdapter } = require("internal/sql/postgres"); | ||
| const { MySQLAdapter } = require("internal/sql/mysql"); | ||
| const { SQLiteAdapter } = require("internal/sql/sqlite"); | ||
| const { SQLHelper, parseOptions } = require("internal/sql/shared"); | ||
| const { | ||
| SQLHelper, | ||
| parseOptions, | ||
| camel, | ||
| pascal, | ||
| kebab, | ||
| snake, | ||
| toCamel, | ||
| toPascal, | ||
| toKebab, | ||
| toSnake, | ||
| toCamelTransform, | ||
| fromCamelTransform, | ||
| toPascalTransform, | ||
| fromPascalTransform, | ||
| toKebabTransform, | ||
| fromKebabTransform, | ||
| toSnakeTransform, | ||
| fromSnakeTransform, | ||
| } = require("internal/sql/shared"); | ||
|
Comment on lines
+10
to
+29
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. 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
Only the 🤖 Prompt for AI Agents |
||
|
|
||
| const { SQLError, PostgresError, SQLiteError, MySQLError } = require("internal/sql/errors"); | ||
|
|
||
|
|
@@ -1037,6 +1056,31 @@ SQL.SQLError = SQLError; | |
| SQL.PostgresError = PostgresError; | ||
| SQL.SQLiteError = SQLiteError; | ||
| SQL.MySQLError = MySQLError; | ||
| SQL.camel = camel; | ||
| SQL.pascal = pascal; | ||
| SQL.kebab = kebab; | ||
| SQL.snake = snake; | ||
| SQL.toCamel = toCamel; | ||
| SQL.toPascal = toPascal; | ||
| SQL.toKebab = toKebab; | ||
| SQL.toSnake = toSnake; | ||
| SQL.fromCamel = fromCamelTransform; | ||
| SQL.fromPascal = fromPascalTransform; | ||
| SQL.fromKebab = fromKebabTransform; | ||
| SQL.fromSnake = fromSnakeTransform; | ||
|
|
||
| defaultSQLObject.camel = camel; | ||
| defaultSQLObject.pascal = pascal; | ||
| defaultSQLObject.kebab = kebab; | ||
| defaultSQLObject.snake = snake; | ||
| defaultSQLObject.toCamel = toCamel; | ||
| defaultSQLObject.toPascal = toPascal; | ||
| defaultSQLObject.toKebab = toKebab; | ||
| defaultSQLObject.toSnake = toSnake; | ||
| defaultSQLObject.fromCamel = fromCamelTransform; | ||
| defaultSQLObject.fromPascal = fromPascalTransform; | ||
| defaultSQLObject.fromKebab = fromKebabTransform; | ||
| defaultSQLObject.fromSnake = fromSnakeTransform; | ||
|
|
||
| // // Helper functions for native code to create error instances | ||
| // // These are internal functions used by native code | ||
|
|
@@ -1076,4 +1120,16 @@ export default { | |
| PostgresError, | ||
| MySQLError, | ||
| SQLiteError, | ||
| camel, | ||
| pascal, | ||
| kebab, | ||
| snake, | ||
| toCamel, | ||
| toPascal, | ||
| toKebab, | ||
| toSnake, | ||
| fromCamel: fromCamelTransform, | ||
| fromPascal: fromPascalTransform, | ||
| fromKebab: fromKebabTransform, | ||
| fromSnake: fromSnakeTransform, | ||
| }; | ||
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.
📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
Document
transform.valueandtransform.row.The section only covers
transform.column, butvalue({ to, from }, applied on read and to bound helper values on write) androware both supported and typed insql.d.ts. Worth a short example so the bidirectionalvaluesemantics aren't discovered by reading the source.🤖 Prompt for AI Agents