Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 45x 6x 6x 39x 39x 39x 39x 39x 1x 158x 158x 158x 158x 158x 159x 159x 159x 159x 158x 158x 158x 158x 158x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 4x 4x 38x 4x 4x 4x 38x 1x 1x 1x 1x 1x 1x 38x 38x 38x 1x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 111x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { Client, InValue, Row, Transaction } from '@libsql/client';
import type { z } from 'zod';
import { normaliseRow } from './rows';
/**
* The query layer the connector methods sit on.
*
* PostgREST turns `{ agent_id: 'eq.x', limit: '10', order: 'name.asc' }` into
* SQL on the server. These helpers do the same job in-process, which is why the
* connector methods below read almost identically to their Supabase
* counterparts: they build the same filter objects, and only the executor
* differs.
*/
/** Columns stored as JSON text. Everything Postgres declared `JSONB`, plus the array columns. */
const JSON_COLUMNS: Record<string, string[]> = {
agents: ['metadata'],
skills: ['metadata', 'allowed_template_variables'],
skill_optimization_clusters: ['centroid'],
tools: ['raw_data'],
logs: [
'base_sa_config',
'ai_provider_request_log',
'hook_logs',
'metadata',
'embedding',
'user_metadata',
],
improved_responses: ['original_response_body', 'improved_response_body'],
ai_providers: ['custom_fields'],
skill_optimization_arms: ['params'],
skill_optimization_evaluations: ['params'],
skill_optimization_evaluation_runs: ['results'],
skill_events: ['metadata'],
};
// The view is `SELECT l.*` plus two computed columns, so it decodes like logs.
JSON_COLUMNS.logs_with_eval_scores = JSON_COLUMNS.logs;
/** Columns stored as INTEGER 0/1 that the schemas expect as booleans. */
const BOOL_COLUMNS: Record<string, string[]> = {
skills: ['optimize'],
system_settings: ['developer_mode'],
};
/** Executes statements; a `Transaction` satisfies this as well as a `Client`. */
type Executor = Pick<Client, 'execute'> | Pick<Transaction, 'execute'>;
export interface Filters {
[column: string]: InValue | undefined;
}
export interface SelectOptions {
/** e.g. `'name asc'`, `'start_time desc'`. Applied verbatim, never user input. */
orderBy?: string;
limit?: number;
offset?: number;
}
const whereClause = (filters: Filters): { sql: string; args: InValue[] } => {
const entries = Object.entries(filters).filter(
([, value]) => value !== undefined,
) as [string, InValue][];
if (entries.length === 0) {
return { sql: '', args: [] };
}
return {
sql: ` WHERE ${entries.map(([column]) => `${column} = ?`).join(' AND ')}`,
args: entries.map(([, value]) => value),
};
};
/** Decode a table's rows and validate them against the schema they belong to. */
export const parseRows = <T extends z.ZodType>(
table: string,
rows: Row[],
schema: T,
): z.infer<T> => {
const decoded = rows.map((row) =>
normaliseRow(row, {
json: JSON_COLUMNS[table],
bool: BOOL_COLUMNS[table],
}),
);
const parsed = schema.safeParse(decoded);
if (!parsed.success) {
throw new Error(
`Failed to parse rows from libSQL table ${table}: ${parsed.error.message}`,
);
}
return parsed.data;
};
export const selectFrom = async <T extends z.ZodType>(
executor: Executor,
table: string,
filters: Filters,
schema: T,
options: SelectOptions = {},
): Promise<z.infer<T>> => {
const where = whereClause(filters);
const args: InValue[] = [...where.args];
let sql = `SELECT * FROM ${table}${where.sql}`;
if (options.orderBy) {
sql += ` ORDER BY ${options.orderBy}`;
}
if (options.limit !== undefined) {
sql += ' LIMIT ?';
args.push(options.limit);
}
if (options.offset !== undefined) {
// SQLite rejects OFFSET without LIMIT, so supply the no-op limit Postgres
// implies. PostgREST allows offset alone.
if (options.limit === undefined) {
sql += ' LIMIT -1';
}
sql += ' OFFSET ?';
args.push(options.offset);
}
const result = await executor.execute({ sql, args });
return parseRows(table, result.rows, schema);
};
/**
* Insert one row and return it as stored, so that defaults and trigger-written
* columns come back the way PostgREST's `return=representation` provides them.
*/
export const insertInto = async <T extends z.ZodType>(
executor: Executor,
table: string,
values: Record<string, InValue | undefined>,
schema: T,
): Promise<z.infer<T>> => {
const entries = Object.entries(values).filter(
([, value]) => value !== undefined,
) as [string, InValue][];
const columns = entries.map(([column]) => column);
const result = await executor.execute({
sql: `INSERT INTO ${table} (${columns.join(', ')})
VALUES (${columns.map(() => '?').join(', ')})
RETURNING *`,
args: entries.map(([, value]) => value),
});
return parseRows(table, result.rows, schema);
};
export const updateIn = async <T extends z.ZodType>(
executor: Executor,
table: string,
filters: Filters,
values: Record<string, InValue | undefined>,
schema: T,
): Promise<z.infer<T>> => {
const entries = Object.entries(values).filter(
([, value]) => value !== undefined,
) as [string, InValue][];
if (entries.length === 0) {
// Nothing to change; report the current state, as a no-op PATCH would.
return selectFrom(executor, table, filters, schema);
}
const where = whereClause(filters);
await executor.execute({
sql: `UPDATE ${table} SET ${entries.map(([c]) => `${c} = ?`).join(', ')}${where.sql}`,
args: [...entries.map(([, value]) => value), ...where.args],
});
/**
* Read the row back rather than using `UPDATE ... RETURNING *`.
*
* SQLite computes RETURNING before AFTER triggers fire, so it would hand back
* the `updated_at` from before the trigger rewrote it. Postgres uses a BEFORE
* trigger, where RETURNING already sees the new value. Re-selecting is what
* makes the two backends agree.
*
* Safe because every filter here is an equality on a column the update never
* touches (`id`, or a bridge table's keys).
*/
return selectFrom(executor, table, filters, schema);
};
export const deleteFrom = async (
executor: Executor,
table: string,
filters: Filters,
): Promise<void> => {
const where = whereClause(filters);
await executor.execute({
sql: `DELETE FROM ${table}${where.sql}`,
args: where.args,
});
};
|