app: lists on meta tables, schema-qualified SQL, typed columns
- lists read/write real lists.* tables via meta.tables/meta.columns and the SECURITY DEFINER meta functions; typed extra columns in the item forms - old numeric list URLs 308-redirect to the slug - include_recurring is boolean, osu accuracy/pp/rate numeric - raw SQL qualified with the new schemas
This commit is contained in:
@@ -1,35 +1,11 @@
|
||||
import { db } from '#lib/server/db/index.js';
|
||||
import { listItemCompletions, lists } from '#lib/server/db/schema/index.js';
|
||||
import { requireAdmin } from '#lib/server/authorization.js';
|
||||
import { uniqueListSlug } from '#lib/server/slugify.js';
|
||||
import { listImageUrl } from '#lib/server/r2.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { createList, listSummaries } from '#lib/server/lists.js';
|
||||
import { fail, redirect } from '@sveltejs/kit';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const rows = await db.query.lists.findMany({
|
||||
with: { items: { columns: { id: true } } },
|
||||
orderBy: { id: 'desc' }
|
||||
});
|
||||
const completed = new Set(
|
||||
locals.user
|
||||
? (
|
||||
await db
|
||||
.select({ itemId: listItemCompletions.itemId })
|
||||
.from(listItemCompletions)
|
||||
.where(eq(listItemCompletions.userId, locals.user.id))
|
||||
).map((row) => row.itemId)
|
||||
: []
|
||||
);
|
||||
return {
|
||||
lists: rows.map(({ items, ...list }) => ({
|
||||
...list,
|
||||
total: items.length,
|
||||
checked: list.isProgress ? items.filter((item) => completed.has(item.id)).length : 0
|
||||
}))
|
||||
};
|
||||
};
|
||||
export const load: PageServerLoad = async () => ({ lists: await listSummaries() });
|
||||
|
||||
export const actions: Actions = {
|
||||
newList: async (event) => {
|
||||
@@ -47,8 +23,9 @@ export const actions: Actions = {
|
||||
return fail(400, { message: 'Enable JavaScript to upload an image.' });
|
||||
const pictureUrl = pictureKey ? listImageUrl(pictureKey) : null;
|
||||
if (pictureKey && !pictureUrl) return fail(400, { message: 'Invalid uploaded picture.' });
|
||||
const slug = await uniqueListSlug(name);
|
||||
await db.insert(lists).values({ name, slug, isProgress: type === 'progress', pictureUrl });
|
||||
// meta.tables.slug allows at most 63 characters
|
||||
const slug = (await uniqueListSlug(name.slice(0, 60))).slice(0, 63).replace(/-+$/, '');
|
||||
await createList({ slug, name, isProgress: type === 'progress', pictureUrl });
|
||||
redirect(303, `/lists/${slug}`);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user