Kysely code generation in different environments
Posted on: 2025-03-22
11:52:56
Pretty fun
After initially being unable to generate any code on a mill worker
last night, I ran the process on my own machine without a problem.
Re-examining everything on the worker, I realized the variable I was
pulling for DATABASE_URL
was mistakenly set to be a youtube
API key, which once I fixed resulted in the following:
this script -
mkdir kysely
cd kysely
npm init -y
npm install -D kysely-codegen
npm install kysely pg
DATABASE_URL=$(curl -s -H "Authorization: Bearer $WM_TOKEN" \
"$BASE_INTERNAL_URL/api/w/$WM_WORKSPACE/variables/get_value/f/db/tonka_railway_pg" | jq -r .)
echo "DATABASE_URL=$DATABASE_URL" > .env
npx kysely-codegen --out-file ./db.d.ts
cat ./db.d.ts
yields the following results from some scaffolded data I seeded the other day:
job=0195bf32-6858-2319-f08d-1ea9d633f775 tag=bash worker=wk-default-dbc66e36aae7-DCh4C hostname=dbc66e36aae7
--- BASH CODE EXECUTION ---
Wrote to /tmp/windmill/wk-default-dbc66e36aae7-DCh4C/0195bf32-6858-2319-f08d-1ea9d633f775/kysely/package.json:
{
"name": "kysely",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": ""
}
npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported
added 67 packages, and audited 68 packages in 3s
16 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
added 14 packages, and audited 82 packages in 930ms
16 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
• Loaded environment variables from '.env'.
• No dialect specified. Assuming 'postgres'.
• Introspecting database...
✓ Introspected 4 tables and generated ./db.d.ts in 306ms.
/**
* This file was generated by kysely-codegen.
* Please do not edit it manually.
*/
import type { ColumnType } from "kysely";
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;
export type Numeric = ColumnType<string, number | string, number | string>;
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export interface Categories {
category_id: Generated<number>;
created_at: Generated<Timestamp>;
description: string | null;
name: string;
}
export interface Inventory {
inventory_id: Generated<number>;
last_updated: Generated<Timestamp>;
product_id: number;
quantity: Generated<number>;
}
export interface Products {
category_id: number | null;
created_at: Generated<Timestamp>;
description: string | null;
image_url: string | null;
is_active: Generated<boolean | null>;
name: string;
price: Numeric;
product_id: Generated<number>;
sku: string | null;
updated_at: Timestamp | null;
}
export interface ProductVariants {
additional_price: Generated<Numeric | null>;
color: string | null;
inventory_count: Generated<number>;
product_id: number;
size: string | null;
variant_id: Generated<number>;
}
export interface DB {
categories: Categories;
inventory: Inventory;
product_variants: ProductVariants;
products: Products;
}
Next is to pick a way to persist these types so I can access them in services/repositories across other mill scripts.