generated from prisma/prisma-client-extension-starter
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from eoin-obrien/read-replicas
read replicas
- Loading branch information
Showing
10 changed files
with
3,513 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Generated files | ||
prisma/generated | ||
|
||
# Prisma local database file | ||
prisma/dev.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import { | ||
Kysely, | ||
SqliteAdapter, | ||
SqliteIntrospector, | ||
SqliteQueryCompiler, | ||
} from "kysely"; | ||
import kyselyExtension, { | ||
PrismaKyselyExtensionArgs, | ||
} from "prisma-extension-kysely"; | ||
import type { DB } from "./prisma/generated/types.js"; | ||
import { readReplicas } from "@prisma/extension-read-replicas"; | ||
|
||
// Use a common config for primary and replica clients | ||
const kyselyExtensionArgs: PrismaKyselyExtensionArgs<DB> = { | ||
kysely: (driver) => | ||
new Kysely<DB>({ | ||
dialect: { | ||
createAdapter: () => new SqliteAdapter(), | ||
createDriver: () => driver, | ||
createIntrospector: (db) => new SqliteIntrospector(db), | ||
createQueryCompiler: () => new SqliteQueryCompiler(), | ||
}, | ||
}), | ||
}; | ||
|
||
// Initialize the replica client(s) and add the Kysely extension | ||
const replicaClient = new PrismaClient({ | ||
// For demonstration purposes, use the same SQLite database | ||
datasourceUrl: "file:./dev.db", | ||
log: [{ level: "query", emit: "event" }], | ||
}).$extends(kyselyExtension(kyselyExtensionArgs)); | ||
|
||
// Initialize the primary client and add the Kysely extension | ||
const prisma = new PrismaClient() | ||
.$extends(kyselyExtension(kyselyExtensionArgs)) | ||
.$extends( | ||
readReplicas({ | ||
replicas: [replicaClient], | ||
}), | ||
); | ||
|
||
async function main() { | ||
console.log( | ||
"Is prisma.$kysely the same as prisma.$primary().$kysely?", | ||
prisma.$kysely === prisma.$primary().$kysely, | ||
); | ||
console.log( | ||
"Is prisma.$kysely the same as prisma.$replica().$kysely?", | ||
prisma.$kysely === prisma.$replica().$kysely, | ||
); | ||
console.log( | ||
"Is prisma.$primary().$kysely the same as prisma.$replica().$kysely?", | ||
prisma.$primary().$kysely === prisma.$replica().$kysely, | ||
); | ||
|
||
// Clear the database before running the example | ||
const deletedPosts = await prisma.$kysely | ||
.deleteFrom("Post") | ||
.executeTakeFirstOrThrow(); | ||
console.log("Deleted posts:", Number(deletedPosts.numDeletedRows)); | ||
const deletedUsers = await prisma.$kysely | ||
.deleteFrom("User") | ||
.executeTakeFirstOrThrow(); | ||
console.log("Deleted users:", Number(deletedUsers.numDeletedRows)); | ||
|
||
// Create and update a user | ||
const insertedUser = await prisma.$transaction(async (tx) => { | ||
const [insertedUser] = await tx.$kysely | ||
.insertInto("User") | ||
.values({ name: "John", email: "[email protected]" }) | ||
.returningAll() | ||
.execute(); | ||
const affectedRows = await tx.$kysely | ||
.updateTable("User") | ||
.set({ name: "John Doe" }) | ||
.where("id", "=", insertedUser.id) | ||
.executeTakeFirstOrThrow(); | ||
console.log("Updated users:", Number(affectedRows.numUpdatedRows)); | ||
|
||
return insertedUser; | ||
}); | ||
|
||
// Create a post | ||
await prisma | ||
.$primary() | ||
.$kysely.insertInto("Post") | ||
.values({ | ||
title: "Hello prisma!", | ||
content: "This is my first post about prisma", | ||
updatedAt: new Date().toISOString(), | ||
published: 1, | ||
authorId: insertedUser.id, | ||
}) | ||
.returningAll() | ||
.execute(); | ||
|
||
// Select a user and a post | ||
const userQuery = prisma | ||
.$replica() | ||
.$kysely.selectFrom("User") | ||
.selectAll() | ||
.where("id", "=", insertedUser.id); | ||
const user = await userQuery.executeTakeFirstOrThrow(); | ||
|
||
const postQuery = prisma | ||
.$replica() | ||
.$kysely.selectFrom("Post") | ||
.selectAll() | ||
.where((eb) => | ||
eb.or([ | ||
eb("title", "like", "%prisma%"), | ||
eb("content", "like", "%prisma%"), | ||
]), | ||
) | ||
.where("published", "=", 1); | ||
const post = await postQuery.executeTakeFirstOrThrow(); | ||
|
||
console.log("Query results:", { user, post }); | ||
} | ||
|
||
main(); |
Oops, something went wrong.