-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
30 lines (23 loc) · 938 Bytes
/
index.ts
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
import { createClient } from "@libsql/client";
const turso = createClient({
url: Bun.env.DB_URL as string,
authToken: Bun.env.DB_TOKEN as string,
});
const db = createClient({ url: "file:./sample.db" });
const tableSchema = await turso.execute(
"select sql from sqlite_schema where type='table' and name = 'tana_links'",
);
const sampleRows = await turso.execute({
sql: "select * from tana_links where rowid in (select rowid from tana_links order by random() limit :limit);",
args: { limit: 200 },
});
const createTableSql = (tableSchema.rows[0].sql as string)
.replace("CREATE TABLE", "CREATE TABLE IF NOT EXISTS")
.replace("tana_", "")
.replaceAll("`", "");
const insertSql = `insert into links values (${sampleRows.columns.map((col) => `:${col}`).join(", ")})`;
await db.execute(createTableSql);
for (const row of sampleRows.rows) {
const { id, ...insert } = row;
await db.execute({ sql: insertSql, args: insert });
}