Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | 465x 264x 264x 264x 44x 44x 172x 333x 7x 7x 7x 7x 62x 60x 60x 103x 63x 7x 7x 14x 12x 633x | import { eq, and, sql } from "drizzle-orm";
import type { Database } from "@/lib/db";
import { preferencesTable } from "@/lib/db/schema";
import type { CollectionPreferenceItem, RankBucket } from "@/types";
import { generateId } from "@/lib/id";
export class CollectionPreferenceItemRepository {
constructor(private db: Database) {}
async create(data: {
id?: string;
collectionId: string;
curatorId: string;
itemId: string;
score: number;
confidence: number;
rankBucket: "top" | "high" | "medium" | "low";
comparisonCount: number;
orderRank?: number | null;
}): Promise<CollectionPreferenceItem> {
const id = data.id || generateId("pref");
const [pref] = await this.db
.insert(preferencesTable)
.values({
id,
collectionId: data.collectionId,
curatorId: data.curatorId,
itemId: data.itemId,
score: data.score.toString(),
confidence: data.confidence.toString(),
rankBucket: data.rankBucket,
comparisonCount: data.comparisonCount,
orderRank: data.orderRank ?? null,
lastSeen: new Date(),
createdAt: new Date(),
updatedAt: new Date(),
})
.returning();
return this.mapToPreference(pref);
}
async findById(id: string): Promise<CollectionPreferenceItem | null> {
const [pref] = await this.db
.select()
.from(preferencesTable)
.where(eq(preferencesTable.id, id));
return pref ? this.mapToPreference(pref) : null;
}
async findByItem(itemId: string, curatorId: string): Promise<CollectionPreferenceItem | null> {
const [pref] = await this.db
.select()
.from(preferencesTable)
.where(and(eq(preferencesTable.itemId, itemId), eq(preferencesTable.curatorId, curatorId)));
return pref ? this.mapToPreference(pref) : null;
}
async findByCollection(
collectionId: string,
curatorId: string,
): Promise<CollectionPreferenceItem[]> {
const prefs = await this.db
.select()
.from(preferencesTable)
.where(
and(
eq(preferencesTable.collectionId, collectionId),
eq(preferencesTable.curatorId, curatorId),
),
);
return prefs.map((p) => this.mapToPreference(p));
}
async update(id: string, data: Partial<CollectionPreferenceItem>): Promise<CollectionPreferenceItem> {
// score/confidence are `numeric` columns: drizzle reads AND writes them as
// strings, while the domain type carries them as numbers. Destructure them out
// so the rest of the patch can be spread under the real row type — a bare
// `{...data}` is not assignable, which is exactly what the old `any` hid.
const { score, confidence, ...rest } = data;
const updateData: Partial<typeof preferencesTable.$inferInsert> = {
...rest,
...(score !== undefined && { score: score.toString() }),
...(confidence !== undefined && { confidence: confidence.toString() }),
updatedAt: new Date(),
};
const [pref] = await this.db
.update(preferencesTable)
.set(updateData)
.where(eq(preferencesTable.id, id))
.returning();
return this.mapToPreference(pref);
}
// ONE upsert statement for the whole recomputed set. Conflict target is the
// (item_id, curator_id) unique index (preferences_item_curator_unique). Callers
// MUST clearOrderRanks first: because the partial unique index on order_rank is
// checked per-row within the statement (not deferred), assigning a permuted rank
// to a row whose OLD rank another row is about to take would transiently collide.
// Nulling all ranks first, then upserting DISTINCT ranks, keeps every mid-statement
// non-null rank unique. No-op on an empty set.
async bulkUpsert(
rows: Array<{
collectionId: string;
curatorId: string;
itemId: string;
score: number;
confidence: number;
rankBucket: "top" | "high" | "medium" | "low";
comparisonCount: number;
orderRank: number | null;
}>,
): Promise<void> {
if (rows.length === 0) return;
const now = new Date();
await this.db
.insert(preferencesTable)
.values(
rows.map((r) => ({
id: generateId("pref"),
collectionId: r.collectionId,
curatorId: r.curatorId,
itemId: r.itemId,
score: r.score.toString(),
confidence: r.confidence.toString(),
rankBucket: r.rankBucket,
comparisonCount: r.comparisonCount,
orderRank: r.orderRank,
lastSeen: now,
createdAt: now,
updatedAt: now,
})),
)
.onConflictDoUpdate({
target: [preferencesTable.itemId, preferencesTable.curatorId],
set: {
score: sql`excluded.score`,
confidence: sql`excluded.confidence`,
rankBucket: sql`excluded.rank_bucket`,
comparisonCount: sql`excluded.comparison_count`,
orderRank: sql`excluded.order_rank`,
lastSeen: now,
updatedAt: now,
},
});
}
async clear(collectionId: string, curatorId: string): Promise<void> {
await this.db
.delete(preferencesTable)
.where(
and(
eq(preferencesTable.collectionId, collectionId),
eq(preferencesTable.curatorId, curatorId),
),
);
}
async clearOrderRanks(collectionId: string, curatorId: string): Promise<void> {
await this.db
.update(preferencesTable)
.set({ orderRank: null, updatedAt: new Date() })
.where(
and(
eq(preferencesTable.collectionId, collectionId),
eq(preferencesTable.curatorId, curatorId),
),
);
}
// Permanently DELETE every preference row for an item, across ALL curators
// (distinct from clear, which is collection+curator-scoped). Returns the
// affected count via RETURNING (postgres-js exposes delete rowCount
// inconsistently — mirrors SortResultItemRepository.deleteByItemInDraftResults).
// Purge-only.
async deleteByItem(itemId: string): Promise<number> {
const deleted = await this.db
.delete(preferencesTable)
.where(eq(preferencesTable.itemId, itemId))
.returning({ id: preferencesTable.id });
return deleted.length;
}
// Permanently DELETE every preference row of the collection, across ALL
// curators (distinct from clear, which is collection+curator-scoped, and from
// deleteByItem). Purge-only.
async deleteByCollection(collectionId: string): Promise<void> {
await this.db.delete(preferencesTable).where(eq(preferencesTable.collectionId, collectionId));
}
// Permanently DELETE every preference row for the given curator across ALL
// collections. Used during account deletion to remove the curator's borrowed-
// collection slice. Account-deletion only.
async deleteByCurator(curatorId: string): Promise<void> {
await this.db.delete(preferencesTable).where(eq(preferencesTable.curatorId, curatorId));
}
private mapToPreference(row: typeof preferencesTable.$inferSelect): CollectionPreferenceItem {
return {
id: row.id,
collectionId: row.collectionId,
curatorId: row.curatorId,
itemId: row.itemId,
score: parseFloat(row.score),
confidence: parseFloat(row.confidence),
rankBucket: row.rankBucket as RankBucket,
comparisonCount: row.comparisonCount,
orderRank: row.orderRank,
lastSeen: row.lastSeen,
createdAt: row.createdAt,
updatedAt: row.updatedAt,
};
}
}
|