-
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.
- Loading branch information
Showing
7 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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") | ||
} | ||
}, | ||
}; | ||
|
||
export default CreateDurationInHook(hooks); | ||
|
9 changes: 9 additions & 0 deletions
9
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
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 | ||
} | ||
}); |
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 @@ | ||
model MyDuration { | ||
fields { | ||
dur Duration? | ||
} | ||
actions { | ||
create createDuration() with (dur) | ||
update updateDuration(id) with (dur) | ||
get getDuration(id) | ||
list listDurations() { | ||
@orderBy(createdAt: asc) | ||
} | ||
create createDurationInHook() @function | ||
write writeCustomFunction(DurationMessage) returns (ModelMessage) { | ||
@permission(expression: true) | ||
} | ||
} | ||
|
||
@permission(expression: true, actions: [get, list, create, update]) | ||
} | ||
|
||
message NestedDurationMessage { | ||
msg DurationMessage | ||
} | ||
|
||
message DurationMessage { | ||
dur Duration | ||
} | ||
|
||
message ModelMessage { | ||
model MyDuration | ||
} |
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,60 @@ | ||
import { actions, resetDatabase, models } from "@teamkeel/testing"; | ||
import { beforeEach, expect, test } from "vitest"; | ||
import { useDatabase, Duration } from "@teamkeel/sdk"; | ||
import { sql } from "kysely"; | ||
|
||
beforeEach(resetDatabase); | ||
|
||
test("duration - create action with duration input", async () => { | ||
const result = await actions.createDuration({ | ||
dur: Duration.fromISOString("PT2H3M4S"), | ||
}); | ||
|
||
expect(result.dur).toEqual("PT2H3M4S"); | ||
}); | ||
|
||
test("duration - update action with duration input", async () => { | ||
const result = await actions.createDuration({ | ||
dur: Duration.fromISOString("PT2H3M4S"), | ||
}); | ||
|
||
const updated = await actions.updateDuration({ | ||
where: { | ||
id: result.id, | ||
}, | ||
values: { | ||
dur: Duration.fromISOString("PT1S"), | ||
}, | ||
}); | ||
|
||
expect(updated.dur).toEqual("PT1S"); | ||
}); | ||
|
||
test("duration - write custom function", async () => { | ||
const result = await actions.writeCustomFunction({ | ||
dur: Duration.fromISOString("PT1H2M3S"), | ||
}); | ||
|
||
expect(result.model.dur).toEqual("PT1H2M3S"); | ||
|
||
const mydurs = await useDatabase() | ||
.selectFrom("my_duration") | ||
.selectAll() | ||
.execute(); | ||
|
||
expect(mydurs.length).toEqual(1); | ||
expect(mydurs[0].id).toEqual(result.model.id); | ||
expect(mydurs[0].dur?.toISOString()).toEqual("PT1H2M3S"); | ||
}); | ||
|
||
test("durs - create and store duration in hook", async () => { | ||
await actions.createDurationInHook({}); | ||
|
||
const mydurs = await useDatabase() | ||
.selectFrom("my_duration") | ||
.selectAll() | ||
.execute(); | ||
|
||
expect(mydurs.length).toEqual(1); | ||
expect(mydurs[0].dur?.toISOString()).toEqual("PT1H"); | ||
}); |
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