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
// SPDX-License-Identifier: 0BSD
import { sqlite } from "https://esm.town/v/std/sqlite";
import type { Store } from "npm:@keyhq/core";
import KeyvSql from "npm:@keyvhq/sql";
export type QueryFn = (sql: string) => Promise<Record<string, any>[]>;
export type KeyvSqliteOptions = {
table?: string;
keySize?: number;
iterationLimit?: number;
connect?: () => Promise<QueryFn>;
};
export const sqliteStore = createStore();
export function createStore<T>(options?: KeyvSqliteOptions): Store<T> {
return new KeyvSql({
...options,
dialect: "sqlite",
connect: options?.connect ?? connect,
});
}
async function connect() {
return async function query(sql: string): Promise<Record<string, any>[]> {
const { columns, rows } = await sqlite.execute(sql);
return rows.map(row => {
const entries = row.map((value, i) => [columns[i], value]);
return Object.fromEntries(entries);
});
};
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 1, 2024