-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: extend camelcasePlugin to ignore rich types
- Loading branch information
Showing
6 changed files
with
73 additions
and
17 deletions.
There are no files selected for viewing
17 changes: 10 additions & 7 deletions
17
integration/testdata/duration/functions/createDurationInHook.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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { CreateDurationInHook, CreateDurationInHookHooks, Duration } from '@teamkeel/sdk'; | ||
import { | ||
CreateDurationInHook, | ||
CreateDurationInHookHooks, | ||
Duration, | ||
} from "@teamkeel/sdk"; | ||
|
||
// To learn more about what you can do with hooks, visit https://docs.keel.so/functions | ||
const hooks: CreateDurationInHookHooks = { | ||
beforeWrite: async (ctx, inputs) => { | ||
return { | ||
dur: Duration.fromISOString("PT1H") | ||
} | ||
}, | ||
beforeWrite: async (ctx, inputs) => { | ||
return { | ||
dur: Duration.fromISOString("PT1H"), | ||
}; | ||
}, | ||
}; | ||
|
||
export default CreateDurationInHook(hooks); | ||
|
10 changes: 5 additions & 5 deletions
10
integration/testdata/duration/functions/writeCustomFunction.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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { WriteCustomFunction, models } from '@teamkeel/sdk'; | ||
import { WriteCustomFunction, models } from "@teamkeel/sdk"; | ||
|
||
// To learn more about what you can do with custom functions, visit https://docs.keel.so/functions | ||
export default WriteCustomFunction(async (ctx, inputs) => { | ||
const mod = await models.myDuration.create({ dur: inputs.dur }) | ||
return { | ||
model: mod | ||
} | ||
const mod = await models.myDuration.create({ dur: inputs.dur }); | ||
return { | ||
model: mod, | ||
}; | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { CamelCasePlugin } = require("kysely"); | ||
const { isPlainObject, isRichType } = require("./parsing"); | ||
// KeelCamelCasePlugin is a wrapper around kysely's camel case plugin. | ||
class KeelCamelCasePlugin { | ||
constructor(opt) { | ||
this.opt = opt; | ||
this.CamelCasePlugin = new CamelCasePlugin(opt); | ||
} | ||
|
||
transformQuery(args) { | ||
return this.CamelCasePlugin.transformQuery(args); | ||
} | ||
|
||
async transformResult(args) { | ||
if (args.result.rows && Array.isArray(args.result.rows)) { | ||
return { | ||
...args.result, | ||
rows: args.result.rows.map((row) => this.mapRow(row)), | ||
}; | ||
} | ||
return args.result; | ||
} | ||
mapRow(row) { | ||
return Object.keys(row).reduce((obj, key) => { | ||
let value = row[key]; | ||
if (Array.isArray(value)) { | ||
value = value.map((it) => | ||
canMap(it, this.opt) ? this.mapRow(it) : it | ||
); | ||
} else if (canMap(value, this.opt)) { | ||
value = this.mapRow(value); | ||
} | ||
obj[this.CamelCasePlugin.camelCase(key)] = value; | ||
return obj; | ||
}, {}); | ||
} | ||
} | ||
|
||
function canMap(obj, opt) { | ||
return ( | ||
isPlainObject(obj) && !opt?.maintainNestedObjectKeys && !isRichType(obj) | ||
); | ||
} | ||
|
||
module.exports.KeelCamelCasePlugin = KeelCamelCasePlugin; |
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