Skip to content

Commit

Permalink
adds planetscale db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
wmalarski committed Apr 10, 2023
1 parent ad829c5 commit 86d83c7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@auth/core": "^0.5.1",
"@auth/solid-start": "^0.1.1",
"@paralleldrive/cuid2": "^2.2.0",
"@planetscale/database": "^1.7.0",
"@solid-primitives/i18n": "^1.2.4",
"@solidjs/meta": "^0.28.4",
"@solidjs/router": "^0.8.2",
Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

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

30 changes: 9 additions & 21 deletions src/db/db.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";
import { connect } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";

declare global {
// eslint-disable-next-line no-var
var db: NodePgDatabase | undefined;
}
// Create the connection.
const connection = connect({
host: process.env.DB_HOST,
password: process.env.DB_PASSWORD,
username: process.env.DB_USERNAME,
});

export const createDrizzle = () => {
const pool = new Pool({
connectionString: env.DATABASE_URL,
});

return drizzle(pool);
};

export const db =
typeof global !== "undefined"
? global.db || createDrizzle()
: createDrizzle();

if (process.env.NODE_ENV !== "production" && typeof global !== "undefined") {
global.db = db;
}
export const db = drizzle(connection);
5 changes: 3 additions & 2 deletions src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
type SolidAuthConfig,
} from "@auth/solid-start";
import { ServerError } from "solid-start";
import { db } from "~/db/db";
import { createDrizzleAdapter } from "./adapters/drizzleOrm";
import { serverEnv } from "./env";
import { prisma } from "./prisma";

export const authOptions: SolidAuthConfig = {
adapter: PrismaAdapter(prisma),
adapter: createDrizzleAdapter(db),
callbacks: {
session({ session, user }) {
if (session.user) {
Expand Down

0 comments on commit 86d83c7

Please sign in to comment.