optcg-db 0.4.14 → 0.4.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Seeds the card_dictionary table from the current web app's generated dictionary.
3
+ * Run once after applying migration 018 to preserve existing deck hash indices.
4
+ *
5
+ * Usage: npx tsx src/db/seed-card-dictionary.ts
6
+ */
7
+ export {};
8
+ //# sourceMappingURL=seed-card-dictionary.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"seed-card-dictionary.d.ts","sourceRoot":"","sources":["../../src/db/seed-card-dictionary.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Seeds the card_dictionary table from the current web app's generated dictionary.
3
+ * Run once after applying migration 018 to preserve existing deck hash indices.
4
+ *
5
+ * Usage: npx tsx src/db/seed-card-dictionary.ts
6
+ */
7
+ import { readFileSync } from "node:fs";
8
+ import { resolve } from "node:path";
9
+ import { query, closePool, getPool } from "./client.js";
10
+ import { logger } from "../shared/logger.js";
11
+ const DICTIONARY_PATH = resolve(import.meta.dirname, "..", "..", "..", "optcg-web", "src", "decks", "cardDictionary.generated.ts");
12
+ function parseDictionary(filePath) {
13
+ const source = readFileSync(filePath, "utf8");
14
+ const match = source.match(/export const CARD_DICTIONARY: string\[\] = (\[[\s\S]*?\]);/);
15
+ if (!match) {
16
+ throw new Error("Could not parse CARD_DICTIONARY from source file");
17
+ }
18
+ const parsed = JSON.parse(match[1]);
19
+ if (!Array.isArray(parsed)) {
20
+ throw new Error("CARD_DICTIONARY is not an array");
21
+ }
22
+ return parsed.filter((value) => typeof value === "string");
23
+ }
24
+ async function main() {
25
+ const entries = parseDictionary(DICTIONARY_PATH);
26
+ logger.info("Parsed card dictionary", { entries: entries.length, source: DICTIONARY_PATH });
27
+ const existing = await query("SELECT COUNT(*)::text AS count FROM card_dictionary");
28
+ const existingCount = parseInt(existing.rows[0]?.count ?? "0", 10);
29
+ if (existingCount > 0) {
30
+ logger.info("Card dictionary already seeded", { existing: existingCount });
31
+ logger.info("To re-seed, TRUNCATE card_dictionary first");
32
+ return;
33
+ }
34
+ const client = await getPool().connect();
35
+ try {
36
+ await client.query("BEGIN");
37
+ // Batch insert in chunks of 500
38
+ const CHUNK_SIZE = 500;
39
+ for (let i = 0; i < entries.length; i += CHUNK_SIZE) {
40
+ const chunk = entries.slice(i, i + CHUNK_SIZE);
41
+ const values = chunk
42
+ .map((cardNumber, j) => `($${j * 2 + 1}, $${j * 2 + 2})`)
43
+ .join(", ");
44
+ const params = chunk.flatMap((cardNumber, j) => [i + j, cardNumber]);
45
+ await client.query(`INSERT INTO card_dictionary (index, card_number) VALUES ${values} ON CONFLICT DO NOTHING`, params);
46
+ }
47
+ await client.query("COMMIT");
48
+ logger.info("Seeded card dictionary", { entries: entries.length });
49
+ }
50
+ catch (error) {
51
+ await client.query("ROLLBACK");
52
+ throw error;
53
+ }
54
+ finally {
55
+ client.release();
56
+ }
57
+ }
58
+ try {
59
+ await main();
60
+ }
61
+ catch (error) {
62
+ logger.error("Seed failed", {
63
+ error: error instanceof Error ? error.message : String(error),
64
+ });
65
+ process.exitCode = 1;
66
+ }
67
+ finally {
68
+ await closePool();
69
+ }
70
+ //# sourceMappingURL=seed-card-dictionary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"seed-card-dictionary.js","sourceRoot":"","sources":["../../src/db/seed-card-dictionary.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAE7C,MAAM,eAAe,GAAG,OAAO,CAC7B,MAAM,CAAC,IAAI,CAAC,OAAO,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CAAC;AACH,CAAC;AAED,IAAI,CAAC;IACH,MAAM,IAAI,EAAE,CAAC;AACf,CAAC;AAAC,OAAO,KAAK,EAAE,CAAC;IACf,MAAM,CAAC,KAAK,CAAC,aAAa,EAAE;QAC1B,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;KAC9D,CAAC,CAAC;IACH,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC;QAAS,CAAC;IACT,MAAM,SAAS,EAAE,CAAC;AACpB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "optcg-db",
3
- "version": "0.4.14",
3
+ "version": "0.4.15",
4
4
  "type": "module",
5
5
  "description": "Shared database layer (schema, migrations, connection pool) for the OPTCG Scryfall project",
6
6
  "repository": {
@@ -22,6 +22,7 @@
22
22
  "build": "tsc",
23
23
  "migrate": "tsx src/db/migrate.ts",
24
24
  "restore": "tsx src/db/restore.ts",
25
+ "seed:card-dictionary": "tsx src/db/seed-card-dictionary.ts",
25
26
  "prepublishOnly": "tsc",
26
27
  "typecheck": "tsc --noEmit"
27
28
  },