Public
Script
Readme

Simple wrapper class around valtown's sqlite

https://www.val.town/v/yawnxyz/dobby

// Example usage: const dobby = new Dobby("myDatabase", [ { name: "id", type: "INTEGER", primaryKey: true }, { name: "name", type: "TEXT", notNull: true }, { name: "age", type: "INTEGER" }, { name: "balance", type: "REAL" }, ]); await dobby.createDatabase(); // Insert some sample data await dobby.insert({ id: 1, name: "John Doe", age: 30, balance: 1000.50 }); await dobby.insert({ id: 2, name: "Jane Smith", age: 25, balance: 2000.75 }); await dobby.insert({ id: 3, name: "Bob Johnson", age: 40, balance: 1500.25 }); // Filter examples const results1 = await dobby.filter({ age: { gt: 25 } }); console.log("Users older than 25:", results1); const results2 = await dobby.filter({ name: { like: '%John%' } }); console.log("Users with 'John' in their name:", results2); const results3 = await dobby.filter({ balance: { between: [1000, 2000] } }); console.log("Users with balance between 1000 and 2000:", results3); const results4 = await dobby.filter({ age: { in: [25, 30, 35] } }); console.log("Users aged 25, 30, or 35:", results4); const results5 = await dobby.filter({ name: { ne: 'John Doe' } }, { limit: 2 }); console.log("Up to 2 users not named John Doe:", results5); // Search examples const searchResults1 = await dobby.search("name", "John"); console.log("Users with 'John' in their name:", searchResults1); const searchResults2 = await dobby.search("name", "son", { limit: 1 }); console.log("First user with 'son' in their name:", searchResults2); // You can search in any text column const searchResults3 = await dobby.search("age", "3"); console.log("Users with '3' in their age:", searchResults3); // Example of using the new dropDatabase function await dropDatabase("myDatabase");
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { sqlite } from "https://esm.town/v/std/sqlite";
import { sql, eq, ne, gt, gte, lt, lte, like, between, and, or, inArray, isNull, isNotNull } from "npm:drizzle-orm";
import { drizzle } from "npm:drizzle-orm/libsql";
import { sqliteTable, text, blob, integer, real, index, uniqueIndex, customType } from "npm:drizzle-orm/sqlite-core";
type ColumnType = 'TEXT' | 'INTEGER' | 'REAL' | 'BLOB' | 'JSON' | 'JSONARRAY' | 'VECTOR' | 'SELF_REFERENCE' | 'BOOLEAN' | 'TIMESTAMP' | 'TIMESTAMP_MS' | 'NUMBER';
const embedding = customType<{
data: ArrayBuffer;
configRequired: true;
config: {
size: number;
};
}>({
dataType(config) {
return `F32_BLOB(${config.size})`;
},
fromDriver(value) {
const isBuffer = Buffer.isBuffer(value);
if (!isBuffer) throw new Error('Expected a Buffer');
const arrayBuffer = value.buffer as ArrayBuffer;
return arrayBuffer;
},
toDriver(value) {
return value;
},
});
interface ColumnDefinition {
name: string;
type: ColumnType;
primaryKey?: boolean;
notNull?: boolean;
size?: number;
autoIncrement?: boolean;
}
interface IndexDefinition {
name: string;
columns: string[];
unique?: boolean;
}
interface FTSIndexDefinition {
name: string;
columns: string[];
}
export class Dobby {
private db: any;
private table: any;
private dbName: string;
private columns: ColumnDefinition[];
private indexes: IndexDefinition[];
private ftsIndexes: FTSIndexDefinition[];
constructor(dbName: string, columns: ColumnDefinition[], indexes: IndexDefinition[] = [], ftsIndexes: FTSIndexDefinition[] = []) {
this.db = drizzle(sqlite as any);
this.dbName = dbName;
this.columns = columns;
this.indexes = indexes;
this.ftsIndexes = ftsIndexes;
this.table = this.createTableSchema();
}
private createTableSchema() {
const schema: Record<string, any> = {};
this.columns.forEach(col => {
switch (col.type) {
case 'TEXT':
schema[col.name] = text(col.name);
break;
case 'INTEGER':
case 'NUMBER':
schema[col.name] = integer(col.name, { mode: 'number' });
break;
case 'BOOLEAN':
schema[col.name] = integer(col.name, { mode: 'boolean' });
break;
case 'TIMESTAMP':
schema[col.name] = integer(col.name, { mode: 'timestamp' });
break;
case 'TIMESTAMP_MS':
schema[col.name] = integer(col.name, { mode: 'timestamp_ms' });
break;
case 'REAL':
schema[col.name] = real(col.name);
break;
case 'JSON':
schema[col.name] = text(col.name, { mode: 'json' });
break;
case 'JSONARRAY':
schema[col.name] = text(col.name, { mode: 'json' }).$type<Record<string, any>[]>();
break;
case 'BLOB':
schema[col.name] = blob(col.name);
break;
case 'VECTOR':
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!
September 10, 2024