afterDeploy hook
The afterDeploy hook is a TypeScript function that runs after each deploy, once your database is migrated. Use it to keep reference data in step with your code: activity codes, default settings, and lookup rows that your functions refer to by a constant.
The hook can run more than once for the same deploy, and it runs on every reload and test reset. Write it so that running it again leaves the database unchanged. See Write it to be idempotent.
Declaring the hook
Add afterDeploy to the top-level hooks list in your keelconfig.yaml. This list is separate from auth.hooks.
hooks:
- afterDeployRun keel generate (or keel run) to scaffold the hook. Keel creates functions/hooks/afterDeploy.ts if the file doesn't exist:
import { AfterDeploy } from "@teamkeel/sdk";
export default AfterDeploy(async (ctx) => {});The hook receives the same ctx as your other functions, with access to secrets and environment variables. It runs with permission granted and outside any request, so ctx.identity is not set.
When it runs
| Where | When |
|---|---|
| Hosted deploy | Once per deploy, in the Run afterDeploy hook deploy step. The step runs after the database is migrated, the API is live, and your functions pass their health check. |
keel run | After the first start (once seed data has loaded), and after every reload once the functions server is serving the new code. It doesn't run while the schema, migrations or functions have an error. |
keel test | Once before the tests start, and again inside every resetDatabase() call, after the tables are cleared. |
Self-hosted keel deploy | After keel deploy runs the migrations. |
Write it to be idempotent
The hook runs on every deploy and every reload, and nothing records whether it ran before. On a hosted deploy it is delivered at least once: if a deploy step is retried, the hook can run again for the same deploy. Write it so that the database ends up in the same state however many times it runs. Upsert rows; never blind-insert them.
The Model API has no upsert. Use onConflict from the query builder returned by useDatabase(), keyed on a @unique field.
Given this schema:
enum ActivityCategory {
Fulfilment
Storage
}
model Activity {
fields {
code Text @unique
name Text
category ActivityCategory
}
}The hook inserts each activity, or updates its name and category if a row with that code already exists:
import { ActivityCategory, AfterDeploy, useDatabase } from "@teamkeel/sdk";
const activities = [
{ code: "pick", name: "Pick", category: ActivityCategory.Fulfilment },
{ code: "pack", name: "Pack", category: ActivityCategory.Fulfilment },
{ code: "pallet-store", name: "Pallet storage", category: ActivityCategory.Storage },
];
export default AfterDeploy(async (ctx) => {
await useDatabase()
.insertInto("activity")
.values(activities.map((a) => ({ code: a.code, name: a.name, category: a.category })))
.onConflict((oc) =>
oc.column("code").doUpdateSet((eb) => ({
name: eb.ref("excluded.name"),
category: eb.ref("excluded.category"),
}))
)
.execute();
});The database fills in id, createdAt and updatedAt, so the insert leaves them out. Enum columns take the generated enum (ActivityCategory.Fulfilment), not a string literal.
Subscriber events
Database writes made by the hook don't send events. A subscriber on a model the hook writes to doesn't fire for the rows the hook inserts or updates.
Failures
- Hosted deploys: best-effort. The migrations have already run, so a failed hook never fails the deploy. The error appears in the logs of the Run afterDeploy hook step on the build details page, and the deploy carries on.
- Self-hosted deploys: also best-effort.
keel deployprintsafterDeploy hook failed: …and carries on. keel run: a failed hook shows as a warning, and the project keeps serving. The next reload runs the hook again.keel test: a failure before the tests stops the run. A failure insideresetDatabase()throws from that call.
Timeout
| Where | Limit |
|---|---|
| Hosted deploy | 75 seconds, the timeout of the runtime Lambda that runs the hook. |
keel run and keel test | 2 minutes. |
| Self-hosted deploy | The timeout of your api and functions Lambdas. A self-hosted deploy leaves both at the AWS default of 3 seconds, so a hook that does more than a few quick upserts needs a longer timeout on those functions. |