Skip to content

Commit

Permalink
fix: neon dialect for kysely
Browse files Browse the repository at this point in the history
fix: using neon kysely dialect

fix: neon db conn type

fix: reintroduced idletimeout for pg conn

chore: prettier

chore: commonjs fix
  • Loading branch information
davenewza committed Jun 25, 2024
1 parent b591e31 commit ca2747c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/functions-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"vitest": "^0.34.6"
},
"dependencies": {
"@neondatabase/serverless": "^0.9.3",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/exporter-trace-otlp-proto": "^0.46.0",
"@opentelemetry/resources": "^1.19.0",
Expand All @@ -28,7 +29,9 @@
"json-rpc-2.0": "^1.7.0",
"ksuid": "^3.0.0",
"kysely": "^0.25.0",
"kysely-neon": "^1.3.0",
"pg": "^8.11.3",
"traceparent": "^1.0.0"
"traceparent": "^1.0.0",
"ws": "^8.17.1"
}
}
99 changes: 99 additions & 0 deletions packages/functions-runtime/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions packages/functions-runtime/src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { AuditContextPlugin } = require("./auditing");
const pg = require("pg");
const { PROTO_ACTION_TYPES } = require("./consts");
const { withSpan } = require("./tracing");
const { NeonDialect } = require("kysely-neon");
const ws = require("ws");

// withDatabase is responsible for setting the correct database client in our AsyncLocalStorage
// so that the the code in a custom function uses the correct client.
Expand Down Expand Up @@ -139,15 +141,15 @@ class InstrumentedClient extends pg.Client {
}

function getDialect() {
// Adding a custom type parser for numeric fields: see https://kysely.dev/docs/recipes/data-types#configuring-runtime-javascript-types
// 1700 = type for NUMERIC
pg.types.setTypeParser(1700, function (val) {
return parseFloat(val);
});

const dbConnType = process.env["KEEL_DB_CONN_TYPE"];
switch (dbConnType) {
case "pg":
// Adding a custom type parser for numeric fields: see https://kysely.dev/docs/recipes/data-types#configuring-runtime-javascript-types
// 1700 = type for NUMERIC
pg.types.setTypeParser(1700, function (val) {
return parseFloat(val);
});

return new PostgresDialect({
pool: new InstrumentedPool({
Client: InstrumentedClient,
Expand All @@ -167,7 +169,11 @@ function getDialect() {
connectionString: mustEnv("KEEL_DB_CONN"),
}),
});

case "neon":
return new NeonDialect({
connectionString: mustEnv("KEEL_DB_CONN"),
webSocketConstructor: ws,
});
default:
throw Error("unexpected KEEL_DB_CONN_TYPE: " + dbConnType);
}
Expand Down

0 comments on commit ca2747c

Please sign in to comment.