Skip to content

Commit

Permalink
Add kyselyCamelCaseHook
Browse files Browse the repository at this point in the history
Fix #547
  • Loading branch information
alecmev committed May 28, 2024
1 parent 47ade7c commit 82cd07d
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 5 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/kanel-kysely/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,17 @@ export type ActorId = number & { __flavor?: "ActorId" };
To pass a string value as primary key or foreign key reference, just add a type assertion for the `<table>Id` generated type.

In cases such as subqueries, the type assertion will happen automatically.

## Usage with `CamelCasePlugin`

If you use Kysely with `CamelCasePlugin` then append `kyselyCamelCaseHook` to `preRenderHooks`:

```ts
const { makeKyselyHook, kyselyCamelCaseHook } = require("kanel-kysely");

module.exports = {
// ... your config here.

preRenderHooks: [makeKyselyHook(), kyselyCamelCaseHook],
};
```
5 changes: 3 additions & 2 deletions packages/kanel-kysely/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
"license": "MIT",
"scripts": {
"build": "rm -rf ./build && tsc",
"test": "echo TODO...",
"test": "vitest run",
"run-example": "kanel -c ./example/.kanelrc"
},
"devDependencies": {
"extract-pg-schema": "^5.0.0",
"kanel": "*"
"kanel": "*",
"vitest": "^1.6.0"
},
"dependencies": {
"@kristiandupont/recase": "^1.2.1"
Expand Down
1 change: 1 addition & 0 deletions packages/kanel-kysely/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as makeKyselyHook } from "./makeKyselyHook";
export { kyselyCamelCaseHook } from "./kyselyCamelCaseHook";
94 changes: 94 additions & 0 deletions packages/kanel-kysely/src/kyselyCamelCaseHook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import type { InstantiatedConfig } from "kanel";
import { assert, expect, it } from "vitest";

import { kyselyCamelCaseHook } from "./kyselyCamelCaseHook.js";

it("Should transform all properties to camelCase", async () => {
const output = await kyselyCamelCaseHook(
{
foo: {
declarations: [
{
declarationType: "interface",
name: "Member",
exportAs: "default",
properties: [
{
name: "snake_case",
typeName: "string",
dimensions: 0,
isOptional: false,
isNullable: false,
},
],
},
{
declarationType: "interface",
name: "Member",
exportAs: "default",
properties: [
{
name: "SCREAMING_SNAKE_CASE",
typeName: "string",
dimensions: 0,
isOptional: false,
isNullable: false,
},
],
},
],
},
bar: {
declarations: [
{
declarationType: "interface",
name: "Member",
exportAs: "default",
properties: [
{
name: "kebab-case",
typeName: "string",
dimensions: 0,
isOptional: false,
isNullable: false,
},
{
name: "PascalCase",
typeName: "string",
dimensions: 0,
isOptional: false,
isNullable: false,
},
{
name: "sTuDlYcApS",
typeName: "string",
dimensions: 0,
isOptional: false,
isNullable: false,
},
],
},
],
},
},
undefined as InstantiatedConfig,
);

assert("properties" in output["foo"].declarations[0]);
assert("properties" in output["foo"].declarations[1]);
assert("properties" in output["bar"].declarations[0]);

expect(
[
...output["foo"].declarations[0].properties,
...output["foo"].declarations[1].properties,
...output["bar"].declarations[0].properties,
].map((x) => x.name),
).toEqual([
"snakeCase",
"sCREAMINGSNAKECASE",
"kebabCase",
"pascalCase",
"sTuDlYcApS",
]);
});
25 changes: 25 additions & 0 deletions packages/kanel-kysely/src/kyselyCamelCaseHook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { recase } from "@kristiandupont/recase";
import type { PreRenderHook } from "kanel";

const toCamelCase = recase(null, "camel");

export const kyselyCamelCaseHook: PreRenderHook = (output) =>
Object.fromEntries(
Object.entries(output).map(([path, fileContents]) => [
path,
{
...fileContents,
declarations: fileContents.declarations.map((declaration) =>
declaration.declarationType === "interface"
? {
...declaration,
properties: declaration.properties.map((property) => ({
...property,
name: toCamelCase(property.name),
})),
}
: declaration,
),
},
]),
);
7 changes: 7 additions & 0 deletions packages/kanel-kysely/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
include: ["src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
},
});

0 comments on commit 82cd07d

Please sign in to comment.