| 81 | } |
| 82 | |
| 83 | export function generate(inPath, outPath) { |
| 84 | const dbPath = outPath; |
| 85 | mkdirSync(dirname(outPath), {recursive: true}); |
| 86 | |
| 87 | if (existsSync(dbPath)) { |
| 88 | rmSync(dbPath); |
| 89 | } |
| 90 | const db = new DatabaseSync(dbPath); |
| 91 | |
| 92 | // Create a table to store metadata. |
| 93 | db.exec(` |
| 94 | CREATE TABLE metadata ( |
| 95 | key TEXT PRIMARY KEY NOT NULL, |
| 96 | value TEXT NOT NULL |
| 97 | ); |
| 98 | `); |
| 99 | |
| 100 | db.exec(` |
| 101 | INSERT INTO metadata (key, value) VALUES |
| 102 | ('schema_version', '1'), |
| 103 | ('created_at', '${new Date().toISOString()}'); |
| 104 | `); |
| 105 | |
| 106 | // Create a relational table to store the structured example data. |
| 107 | db.exec(` |
| 108 | CREATE TABLE examples ( |
| 109 | id INTEGER PRIMARY KEY, |
| 110 | title TEXT NOT NULL, |
| 111 | summary TEXT NOT NULL, |
| 112 | keywords TEXT, |
| 113 | required_packages TEXT, |
| 114 | related_concepts TEXT, |
| 115 | related_tools TEXT, |
| 116 | experimental INTEGER NOT NULL DEFAULT 0, |
| 117 | content TEXT NOT NULL |
| 118 | ); |
| 119 | `); |
| 120 | |
| 121 | // Create an FTS5 virtual table to provide full-text search capabilities. |
| 122 | db.exec(` |
| 123 | CREATE VIRTUAL TABLE examples_fts USING fts5( |
| 124 | title, |
| 125 | summary, |
| 126 | keywords, |
| 127 | required_packages, |
| 128 | related_concepts, |
| 129 | related_tools, |
| 130 | content, |
| 131 | content='examples', |
| 132 | content_rowid='id', |
| 133 | tokenize = 'porter ascii' |
| 134 | ); |
| 135 | `); |
| 136 | |
| 137 | // Create triggers to keep the FTS table synchronized with the examples table. |
| 138 | db.exec(` |
| 139 | CREATE TRIGGER examples_after_insert AFTER INSERT ON examples BEGIN |
| 140 | INSERT INTO examples_fts( |