Skip to content

Commit 05446cd

Browse files
committed
feat: Generate types for DB schema and setup kysely
Run the kysely-codegen task to regenerate the types. Don't forget to reformat the generated code to follow our linting rules. Since this is using DB introspection, you have to specify a DB connection string via the --url param or the DATABASE_URL env var: https://github.com/RobinBlomberg/kysely-codegen
1 parent 2909201 commit 05446cd

File tree

4 files changed

+1178
-11
lines changed

4 files changed

+1178
-11
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"scripts": {
1111
"build": "rimraf lib/* && babel src --out-dir lib --extensions .js,.ts && tsc",
1212
"build-js-for-test": "rimraf lib/* && babel src --out-dir lib --source-maps inline --extensions .js,.ts",
13+
"kysely-codegen": "kysely-codegen --out-file src/types/schema.ts --schema bookbrainz --camel-case",
1314
"lint": "eslint .",
1415
"lint-errors": "eslint --quiet .",
1516
"lint-staged": "lint-staged",
@@ -100,6 +101,7 @@
100101
"glob": "^7.1.2",
101102
"husky": "^8.0.0",
102103
"jsinspect": "^0.12.7",
104+
"kysely-codegen": "^0.17.0",
103105
"lint-staged": "^13.1.0",
104106
"mocha": "^10.2.0",
105107
"node-uuid": "^1.4.8",

src/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import * as func from './func';
2020
import * as util from './util'; // eslint-disable-line import/no-namespace
2121

22+
import {CamelCasePlugin, Kysely, PostgresDialect} from 'kysely';
2223
import {type Knex, knex} from 'knex';
2324

2425
import Bookshelf from '@metabrainz/bookshelf';
26+
import type {DB} from './types/schema';
27+
import {Pool} from 'pg';
2528
import achievementType from './models/achievementType';
2629
import achievementUnlock from './models/achievementUnlock';
2730
import adminLog from './models/adminLog';
@@ -117,6 +120,15 @@ export default function init(config: Knex.Config) {
117120
const SeriesData = seriesData(bookshelf);
118121
const WorkData = workData(bookshelf);
119122

123+
const kysely = new Kysely<DB>({
124+
dialect: new PostgresDialect({
125+
pool: new Pool(config.connection as Knex.ConnectionConfig),
126+
}),
127+
plugins: [
128+
new CamelCasePlugin(),
129+
],
130+
});
131+
120132
return {
121133
AchievementType: achievementType(bookshelf),
122134
AchievementUnlock: achievementUnlock(bookshelf),
@@ -197,6 +209,7 @@ export default function init(config: Knex.Config) {
197209
WorkType: workType(bookshelf),
198210
bookshelf,
199211
func,
212+
kysely,
200213
util
201214
};
202215
}

0 commit comments

Comments
 (0)