Repository navigation
Expand file tree
/
Copy pathmetadataStore.ts
More file actions
67 lines (57 loc) · 1.69 KB
/
Copy pathmetadataStore.ts
File metadata and controls
67 lines (57 loc) · 1.69 KB
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
import type { PublicSearchMetadata, SearchMetadataStore } from "./backends";
type D1Statement = {
bind(...values: string[]): {
all(): Promise<{ results?: D1KeyframeRow[] }>;
};
};
export type D1Database = {
prepare(sql: string): D1Statement;
};
type D1KeyframeRow = {
id?: unknown;
label?: unknown;
floor?: unknown;
zone?: unknown;
imagePath?: unknown;
x?: unknown;
y?: unknown;
};
function stringValue(value: unknown): string | undefined {
return typeof value === "string" ? value : undefined;
}
function numberValue(value: unknown): number | undefined {
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
}
function rowToMetadata(row: D1KeyframeRow): PublicSearchMetadata {
return {
label: stringValue(row.label),
floor: stringValue(row.floor),
zone: stringValue(row.zone),
imagePath: stringValue(row.imagePath),
x: numberValue(row.x),
y: numberValue(row.y),
};
}
export function createD1SearchMetadataStore(db: D1Database): SearchMetadataStore {
return {
async getByIds(ids) {
if (ids.length === 0) {
return new Map();
}
const placeholders = ids.map(() => "?").join(", ");
const statement = db.prepare(
`SELECT id, label, floor, zone, image_r2_key AS imagePath, x, y FROM keyframes WHERE id IN (${placeholders})`,
);
const response = await statement.bind(...ids).all();
const rows = response.results ?? [];
const metadata = new Map<string, PublicSearchMetadata>();
for (const row of rows) {
const id = stringValue(row.id);
if (id) {
metadata.set(id, rowToMetadata(row));
}
}
return metadata;
},
};
}