-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Slonik/interceptor/camelize result (#184)
* feat(slonik): add fieldNameCaseConverter interceptor * feat(slonik)!: camelize query result BREAKING CHANGE: SqlFactory arguments have changed. * fix(multi-tenant): update service factory * chore(slonik): cleanup configuration
- Loading branch information
Showing
14 changed files
with
1,120 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: "Test suite" | ||
|
||
on: | ||
push | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: [16.x, 18.x] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
name: Use node ${{ matrix.node-version }} | ||
|
||
- uses: pnpm/action-setup@v2 | ||
name: Install pnpm | ||
with: | ||
version: 7 | ||
|
||
- name: Install Dependencies | ||
run: pnpm install --frozen-lockfile | ||
|
||
- name: Build packages | ||
run: pnpm build | ||
|
||
- name: Run tests | ||
run: pnpm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/slonik/src/factories/__test__/createClientConfiguration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import fieldNameCaseConverter from "../../interceptors/fieldNameCaseConverter"; | ||
import createClientConfiguration from "../createClientConfiguration"; | ||
|
||
import type { Query, QueryContext } from "slonik"; | ||
|
||
describe("createClientConfiguration helper", () => { | ||
const defaultConfiguration = { | ||
captureStackTrace: false, | ||
connectionRetryLimit: 3, | ||
connectionTimeout: 5000, | ||
idleInTransactionSessionTimeout: 60000, | ||
idleTimeout: 5000, | ||
interceptors: [fieldNameCaseConverter], | ||
maximumPoolSize: 10, | ||
queryRetryLimit: 5, | ||
statementTimeout: 60000, | ||
transactionRetryLimit: 5, | ||
}; | ||
|
||
it("creates default configuration", () => { | ||
const configuration = createClientConfiguration(); | ||
|
||
expect(configuration).toEqual(defaultConfiguration); | ||
}); | ||
|
||
it("includes fieldNameCaseConvertor interceptor", () => { | ||
const interceptor = { | ||
transformQuery: (context: QueryContext, query: Query): Query => { | ||
return query; | ||
}, | ||
}; | ||
|
||
const configuration = createClientConfiguration({ | ||
interceptors: [interceptor], | ||
}); | ||
|
||
expect(configuration.interceptors).toContain(fieldNameCaseConverter); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/slonik/src/factories/createClientConfiguration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import fieldNameCaseConverter from "../interceptors/fieldNameCaseConverter"; | ||
|
||
import type { ClientConfigurationInput } from "slonik/dist/src/types"; | ||
|
||
const createClientConfiguration = ( | ||
config?: ClientConfigurationInput | ||
): ClientConfigurationInput => { | ||
const configuration = { | ||
captureStackTrace: false, | ||
connectionRetryLimit: 3, | ||
connectionTimeout: 5000, | ||
idleInTransactionSessionTimeout: 60000, | ||
idleTimeout: 5000, | ||
interceptors: [], | ||
maximumPoolSize: 10, | ||
queryRetryLimit: 5, | ||
statementTimeout: 60000, | ||
transactionRetryLimit: 5, | ||
|
||
...config, | ||
}; | ||
|
||
configuration.interceptors = [ | ||
fieldNameCaseConverter, | ||
...(config?.interceptors ?? []), | ||
]; | ||
|
||
return configuration; | ||
}; | ||
|
||
export default createClientConfiguration; |
73 changes: 73 additions & 0 deletions
73
packages/slonik/src/interceptors/__test__/fieldNameCaseConverter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import createQueryContext from "./helpers/createQueryContext"; | ||
import fieldNameCaseConverter from "../fieldNameCaseConverter"; | ||
|
||
describe("fielNameCaseConverter interceptor", () => { | ||
const interceptor = fieldNameCaseConverter; | ||
|
||
it("transforms result field names to camelcase", () => { | ||
const { transformRow } = interceptor; | ||
|
||
if (!transformRow) { | ||
throw new Error("Unexpected state."); | ||
} | ||
|
||
const result = transformRow( | ||
createQueryContext(), | ||
{ | ||
sql: "SELECT 1", | ||
values: [], | ||
}, | ||
{ | ||
created_at: "2022-01-01 00:00:01", | ||
foo_bar: 1, | ||
is_enabled: true, | ||
is_enabled_some_of_the_time: false, | ||
/* [OP 2023-01-28] See https://github.com/gajus/slonik/issues/428 | ||
object_child: { | ||
user_id: 1, | ||
}, | ||
some_children: [ | ||
{ | ||
user_id: 1, | ||
}, | ||
{ | ||
user_id: 2, | ||
}, | ||
], | ||
*/ | ||
updated_at: "2022-01-01 00:00:01", | ||
}, | ||
[ | ||
{ | ||
dataTypeId: 1, | ||
name: "foo_bar", | ||
}, | ||
] | ||
); | ||
|
||
const expected = { | ||
createdAt: "2022-01-01 00:00:01", | ||
fooBar: 1, | ||
isEnabled: true, | ||
isEnabledSomeOfTheTime: false, | ||
/* [OP 2023-01-28] See https://github.com/gajus/slonik/issues/428 | ||
firstChild: { | ||
userId: 1, | ||
}, | ||
someChildren: [ | ||
{ | ||
userId: 1, | ||
}, | ||
{ | ||
userId: 2, | ||
}, | ||
], | ||
*/ | ||
updatedAt: "2022-01-01 00:00:01", | ||
}; | ||
|
||
expect(expected).toEqual(result); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/slonik/src/interceptors/__test__/helpers/createQueryContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { QueryContext } from "slonik"; | ||
|
||
const helper = (): QueryContext => { | ||
return { | ||
connectionId: "1", | ||
log: { | ||
getContext: () => { | ||
return { | ||
connectionId: "1", | ||
poolId: "1", | ||
}; | ||
}, | ||
}, | ||
poolId: "1", | ||
sandbox: {}, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} as any; | ||
}; | ||
|
||
export default helper; |
24 changes: 24 additions & 0 deletions
24
packages/slonik/src/interceptors/fieldNameCaseConverter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import humps from "humps"; | ||
|
||
import type { | ||
Field, | ||
Interceptor, | ||
Query, | ||
QueryContext, | ||
QueryResultRow, | ||
} from "slonik"; | ||
|
||
const fieldNameCaseConverter: Interceptor = { | ||
transformRow: ( | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
queryContext: QueryContext, | ||
query: Query, | ||
row: QueryResultRow, | ||
fields: readonly Field[] | ||
/* eslint-enable */ | ||
): QueryResultRow => { | ||
return humps.camelizeKeys(row) as QueryResultRow; | ||
}, | ||
}; | ||
|
||
export default fieldNameCaseConverter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { sql } from "slonik"; | ||
|
||
import { | ||
createFilterFragment, | ||
createLimitFragment, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.