Skip to content

Commit

Permalink
fix: use camel case for camelCase option (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
bombillazo authored Feb 16, 2024
1 parent 37f41f5 commit 08320fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -856,14 +856,14 @@ for that such as aliasing every query field that is done to the database, one
easy built-in solution allows developers to transform the incoming query names
into the casing of their preference without any extra steps

##### Camelcase
##### Camel case

To transform a query result into camelcase, you only need to provide the
`camelcase` option on your query call
To transform a query result into camel case, you only need to provide the
`camelCase` option on your query call

```ts
const { rows: result } = await client.queryObject({
camelcase: true,
camelCase: true,
text: "SELECT FIELD_X, FIELD_Y FROM MY_TABLE",
});

Expand Down
14 changes: 7 additions & 7 deletions query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ export interface QueryObjectOptions extends QueryOptions {
// TODO
// Support multiple case options
/**
* Enabling camelcase will transform any snake case field names coming from the database into camel case ones
* Enabling camel case will transform any snake case field names coming from the database into camel case ones
*
* Ex: `SELECT 1 AS my_field` will return `{ myField: 1 }`
*
* This won't have any effect if you explicitly set the field names with the `fields` parameter
*/
camelcase?: boolean;
camelCase?: boolean;
/**
* This parameter supersedes query column names coming from the databases in the order they were provided.
* Fields must be unique and be in the range of (a-zA-Z0-9_), otherwise the query will throw before execution.
* A field can not start with a number, just like JavaScript variables
*
* This setting overrides the camelcase option
* This setting overrides the camel case option
*
* Ex: `SELECT 'A', 'B' AS my_field` with fields `["field_1", "field_2"]` will return `{ field_1: "A", field_2: "B" }`
*/
Expand Down Expand Up @@ -324,7 +324,7 @@ export class QueryObjectResult<
this.columns = this.query.fields;
} else {
let column_names: string[];
if (this.query.camelcase) {
if (this.query.camelCase) {
column_names = this.rowDescription.columns.map((column) =>
snakecaseToCamelcase(column.name)
);
Expand Down Expand Up @@ -380,7 +380,7 @@ export class QueryObjectResult<
*/
export class Query<T extends ResultType> {
public args: EncodedArg[];
public camelcase?: boolean;
public camelCase?: boolean;
/**
* The explicitly set fields for the query result, they have been validated beforehand
* for duplicates and invalid names
Expand Down Expand Up @@ -408,7 +408,7 @@ export class Query<T extends ResultType> {
this.text = config_or_text;
this.args = args.map(encodeArgument);
} else {
const { camelcase, encoder = encodeArgument, fields } = config_or_text;
const { camelCase, encoder = encodeArgument, fields } = config_or_text;
let { args = [], text } = config_or_text;

// Check that the fields passed are valid and can be used to map
Expand All @@ -432,7 +432,7 @@ export class Query<T extends ResultType> {
this.fields = fields;
}

this.camelcase = camelcase;
this.camelCase = camelCase;

if (!Array.isArray(args)) {
[text, args] = objectQueryToQueryArgs(text, args);
Expand Down
14 changes: 7 additions & 7 deletions tests/query_client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ Deno.test(
);

Deno.test(
"Object query field names aren't transformed when camelcase is disabled",
"Object query field names aren't transformed when camel case is disabled",
withClient(async (client) => {
const record = {
pos_x: "100",
Expand All @@ -806,7 +806,7 @@ Deno.test(

const { rows: result } = await client.queryObject({
args: [record.pos_x, record.pos_y, record.prefix_name_suffix],
camelcase: false,
camelCase: false,
text: "SELECT $1 AS POS_X, $2 AS POS_Y, $3 AS PREFIX_NAME_SUFFIX",
});

Expand All @@ -815,7 +815,7 @@ Deno.test(
);

Deno.test(
"Object query field names are transformed when camelcase is enabled",
"Object query field names are transformed when camel case is enabled",
withClient(async (client) => {
const record = {
posX: "100",
Expand All @@ -825,7 +825,7 @@ Deno.test(

const { rows: result } = await client.queryObject({
args: [record.posX, record.posY, record.prefixNameSuffix],
camelcase: true,
camelCase: true,
text: "SELECT $1 AS POS_X, $2 AS POS_Y, $3 AS PREFIX_NAME_SUFFIX",
});

Expand All @@ -846,13 +846,13 @@ Deno.test(
);

Deno.test(
"Object query explicit fields override camelcase",
"Object query explicit fields override camel case",
withClient(async (client) => {
const record = { field_1: "A", field_2: "B", field_3: "C" };

const { rows: result } = await client.queryObject({
args: [record.field_1, record.field_2, record.field_3],
camelcase: true,
camelCase: true,
fields: ["field_1", "field_2", "field_3"],
text: "SELECT $1 AS POS_X, $2 AS POS_Y, $3 AS PREFIX_NAME_SUFFIX",
});
Expand Down Expand Up @@ -888,7 +888,7 @@ Deno.test(
await assertRejects(
() =>
client.queryObject({
camelcase: true,
camelCase: true,
text: `SELECT 1 AS "fieldX", 2 AS field_x`,
}),
Error,
Expand Down

1 comment on commit 08320fa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No typecheck tests failure

This error was most likely caused by incorrect type stripping from the SWC crate

Please report the following failure to https://github.com/denoland/deno with a reproduction of the current commit

Failure log

Please sign in to comment.