Skip to content

Add Drizzle Select benchmark #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ results
data*
test.ts
src/prisma/client-mysql
src/prisma/client-postgresql
src/prisma/client-pg
backup
*/migrations
10 changes: 7 additions & 3 deletions prisma-mysql/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
generator client {
provider = "prisma-client-js"
output = "../src/prisma/client-mysql"
output = "../src/prisma/client-mysql"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
provider = "mysql"
url = env("DATABASE_URL")
// shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}

Expand All @@ -28,6 +28,8 @@ model Address {
country String?
customerId Int @unique
customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)

@@index([customerId], name: "Address_customerId_idx")
}

model Order {
Expand All @@ -37,6 +39,8 @@ model Order {
customerId Int
customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
products Product[] @relation("OrderProducts")

@@index([customerId], name: "Order_customerId_idx")
}

model Product {
Expand Down
42 changes: 23 additions & 19 deletions prisma-pg/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generator client {
provider = "prisma-client-js"
output = "../src/prisma/client-pg"
output = "../src/prisma/client-pg"
}

datasource db {
Expand All @@ -9,40 +9,44 @@ datasource db {
}

model Customer {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
name String?
email String // @unique
address Address?
isActive Boolean @default(false)
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
name String?
email String // @unique
address Address?
isActive Boolean @default(false)
// extraInfo Json?
orders Order[]
orders Order[]
}

model Address {
id Int @id @default(autoincrement())
street String?
city String?
postalCode String?
country String?
customerId Int @unique
customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
id Int @id @default(autoincrement())
street String?
city String?
postalCode String?
country String?
customerId Int @unique
customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)

@@index([customerId], name: "Address_customerId_idx")
}

model Order {
id Int @id @default(autoincrement())
date DateTime
date DateTime
totalAmount Float
customerId Int
customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
products Product[] @relation("OrderProducts")

@@index([customerId], name: "Order_customerId_idx")
}

model Product {
id Int @id @default(autoincrement())
id Int @id @default(autoincrement())
name String
price Float
quantity Int
description String?
orders Order[] @relation("OrderProducts")
}
orders Order[] @relation("OrderProducts")
}
10 changes: 7 additions & 3 deletions src/drizzle/drizzle-mysql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { drizzle } from "drizzle-orm/mysql2";
import * as schema from "./schema/schema-mysql";
import * as relations from "./schema/relations-mysql";
import { eq, desc } from "drizzle-orm";
import { eq, desc, sql } from "drizzle-orm";
import measure from "../lib/measure";
import mysql from "mysql2/promise";
import { QueryResult } from "../lib/types";
Expand All @@ -18,6 +18,8 @@ export async function drizzleMySQL(databaseUrl: string): Promise<QueryResult[]>
schema: { ...schema, ...relations },
mode: "default",
});
// connect
await db.execute(sql`select 1`);

console.log(`run drizzle benchmarks: `, databaseUrl);

Expand Down Expand Up @@ -167,15 +169,17 @@ export async function drizzleMySQL(databaseUrl: string): Promise<QueryResult[]>
"drizzle-nested-update",
db.transaction(async (trx) => {
// Update customer name
await trx.update(schema.Customer).set({ name: "John Doe Updated" }).where(eq(schema.Customer.id, 1));
const customerUpdate = trx.update(schema.Customer).set({ name: "John Doe Updated" }).where(eq(schema.Customer.id, 1));

// Update address
await trx
const addressUpdate = trx
.update(schema.Address)
.set({
street: "456 New St",
})
.where(eq(schema.Address.customerId, 1));

await Promise.all([customerUpdate, addressUpdate]);
})
)
);
Expand Down
16 changes: 10 additions & 6 deletions src/drizzle/drizzle-pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { drizzle } from "drizzle-orm/postgres-js";
// import { Customer, Order, Address } from "./schema/schema-postgres";
import * as schema from "./schema/schema-postgres";
import * as relations from "./schema/relations-postgres";
import { eq, desc } from "drizzle-orm";
import { eq, desc, sql } from "drizzle-orm";
import measure from "../lib/measure";
import postgres from "postgres";
import { QueryResult } from "../lib/types";
Expand All @@ -13,6 +13,8 @@ export async function drizzlePg(databaseUrl: string): Promise<QueryResult[]> {
ssl: databaseUrl.includes("localhost") ? undefined : { rejectUnauthorized: false }
});
const db = drizzle(client, { schema: { ...schema, ...relations } });
// connect
await db.execute(sql`select 1`);
console.log(`Run drizzle benchmarks: `, databaseUrl);

const results: QueryResult[] = [];
Expand Down Expand Up @@ -115,7 +117,7 @@ export async function drizzlePg(databaseUrl: string): Promise<QueryResult[]> {
email: "john.doe@example.com",
isActive: false,
})
.returning();
.returning({id: schema.Customer.id});

const customerId = customer[0].id;

Expand All @@ -127,7 +129,7 @@ export async function drizzlePg(databaseUrl: string): Promise<QueryResult[]> {
date: `${new Date().toISOString()}`,
totalAmount: "100.5",
})
.returning();
.returning({id: schema.Order.id});

const orderId = insertedOrder[0].id;

Expand Down Expand Up @@ -163,18 +165,20 @@ export async function drizzlePg(databaseUrl: string): Promise<QueryResult[]> {
"drizzle-nested-update",
db.transaction(async (trx) => {
// Update customer name
await trx
const customerUpdate = trx
.update(schema.Customer)
.set({ name: "John Doe Updated" })
.where(eq(schema.Customer.id, 1));

// Update address
await trx
const addressUpdate = trx
.update(schema.Address)
.set({
street: "456 New St",
})
.where(eq(schema.Address.customerId, 1));

await Promise.all([customerUpdate, addressUpdate]);
})
)
);
Expand Down Expand Up @@ -214,7 +218,7 @@ export async function drizzlePg(databaseUrl: string): Promise<QueryResult[]> {
target: schema.Customer.id,
set: { name: "John Doe Upserted" },
})
.returning();
.returning({id: schema.Customer.id});
const customerId = customer[0].id;

// Update address
Expand Down
Loading