-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(svelte-ui): use drizzle select API
This patch uses the Drizzle select API instead of the query API to reduce overhead. We only select the first look for each collection and use a single query with two LEFT JOINs to accomplish a nearly identical UX that is 10x faster.
- Loading branch information
1 parent
e22ccf9
commit 21334e6
Showing
4 changed files
with
73 additions
and
104 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,54 +1,30 @@ | ||
import { asc, ilike, sql } from "drizzle-orm" | ||
import { desc, ilike, and, eq, sql } from "drizzle-orm" | ||
import * as tables from "$lib/server/db/schema/tables" | ||
import { db } from "$lib/server/db" | ||
|
||
const collectionsPageQuery = db.query.collection | ||
.findMany({ | ||
columns: { id: true, name: true, level: true, location: true, sex: true }, | ||
with: { | ||
brand: { | ||
columns: { | ||
id: true, | ||
name: true, | ||
}, | ||
}, | ||
season: { | ||
columns: { id: true, year: true, name: true }, | ||
}, | ||
looks: { | ||
columns: { id: true, number: true }, | ||
with: { | ||
images: { | ||
limit: 1, | ||
columns: { id: true, url: true }, | ||
}, | ||
}, | ||
orderBy: [asc(tables.look.number)], | ||
}, | ||
}, | ||
where: ilike(tables.collection.name, sql.placeholder("search")), | ||
orderBy: [ | ||
asc(tables.collection.name), | ||
asc(tables.collection.level), | ||
asc(tables.collection.sex), | ||
], | ||
limit: sql.placeholder("limit"), | ||
const query = db | ||
.selectDistinctOn([tables.collection.name], { | ||
collectionId: tables.collection.id, | ||
collectionName: tables.collection.name, | ||
collectionDate: tables.collection.date, | ||
collectionLocation: tables.collection.location, | ||
lookNumber: tables.look.number, | ||
lookImageUrl: tables.image.url, | ||
}) | ||
.prepare("collectionsPageQuery") | ||
.from(tables.collection) | ||
.leftJoin( | ||
tables.look, | ||
and( | ||
eq(tables.look.collectionId, tables.collection.id), | ||
eq(tables.look.number, 1), | ||
), | ||
) | ||
.leftJoin(tables.image, eq(tables.image.lookId, tables.look.id)) | ||
.orderBy(desc(tables.collection.name)) | ||
.where(ilike(tables.collection.name, sql.placeholder("search"))) | ||
.prepare("collections") | ||
|
||
export async function load({ url }) { | ||
const search = url.searchParams.get("q") ?? "" | ||
const query = collectionsPageQuery.getQuery() | ||
let sql = query.sql | ||
for (let i = 0; i < query.params.length; i++) { | ||
sql = sql.replace(`$${i + 1}`, `${query.params[i]}`) | ||
} | ||
return { | ||
search, | ||
sql, | ||
collections: collectionsPageQuery.execute({ | ||
search: `%${search}%`, | ||
limit: search ? 100_000 : 100, | ||
}), | ||
} | ||
return { collections: query.execute({ search: `%${search}%` }) } | ||
} |
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