@absurd-sqlite/sdk 0.2.1-alpha.2 → 0.3.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,149 +0,0 @@
1
- import type {
2
- ClaimedTask,
3
- JsonValue,
4
- SpawnOptions,
5
- SpawnResult,
6
- TaskHandler,
7
- TaskRegistrationOptions,
8
- WorkerOptions,
9
- } from "absurd-sdk";
10
-
11
- import type { SQLiteRestBindParams } from "./sqlite-types";
12
-
13
- /**
14
- * Minimal query interface compatible with Absurd's database operations.
15
- */
16
- export interface Queryable {
17
- /**
18
- * Execute a parameterized SQL query and return rows.
19
- * @param sql SQL text with parameter placeholders.
20
- * @param params Optional positional parameters.
21
- */
22
- query<R extends object = Record<string, any>>(
23
- sql: string,
24
- params?: SQLiteRestBindParams
25
- ): Promise<{ rows: R[] }>;
26
- }
27
-
28
- /**
29
- * Background worker handle returned by startWorker().
30
- */
31
- export interface Worker {
32
- /**
33
- * Stop the worker loop and wait for in-flight tasks to settle.
34
- */
35
- close(): Promise<void>;
36
- }
37
-
38
- /**
39
- * Absurd client interface.
40
- */
41
- export interface AbsurdClient {
42
- /**
43
- * Create a new client bound to the provided connection.
44
- * @param con Connection to use for queries.
45
- * @param owned If true, close the connection when close() is called.
46
- */
47
- // bindToConnection(con: Queryable, owned?: boolean): this;
48
-
49
- /**
50
- * Register a task handler.
51
- * @param options Task registration options.
52
- * @param handler Async task handler.
53
- */
54
- registerTask<P = any, R = any>(
55
- options: TaskRegistrationOptions,
56
- handler: TaskHandler<P, R>
57
- ): void;
58
-
59
- /**
60
- * Create a queue.
61
- * @param queueName Optional queue name (defaults to client queue).
62
- */
63
- createQueue(queueName?: string): Promise<void>;
64
- /**
65
- * Drop a queue.
66
- * @param queueName Optional queue name (defaults to client queue).
67
- */
68
- dropQueue(queueName?: string): Promise<void>;
69
- /**
70
- * List available queues.
71
- */
72
- listQueues(): Promise<Array<string>>;
73
-
74
- /**
75
- * Spawn a task execution.
76
- * @param taskName Task name.
77
- * @param params Task parameters.
78
- * @param options Spawn options including queue and retry behavior.
79
- */
80
- spawn<P = any>(
81
- taskName: string,
82
- params: P,
83
- options?: SpawnOptions
84
- ): Promise<SpawnResult>;
85
-
86
- /**
87
- * Emit an event on a queue.
88
- * @param eventName Non-empty event name.
89
- * @param payload Optional JSON payload.
90
- * @param queueName Optional queue name (defaults to client queue).
91
- */
92
- emitEvent(
93
- eventName: string,
94
- payload?: JsonValue,
95
- queueName?: string
96
- ): Promise<void>;
97
-
98
- /**
99
- * Cancel a task by ID.
100
- * @param taskID Task identifier.
101
- * @param queueName Optional queue name (defaults to client queue).
102
- */
103
- cancelTask(taskID: string, queueName?: string): Promise<void>;
104
-
105
- /**
106
- * Claim tasks for processing.
107
- * @param options Claiming options.
108
- */
109
- claimTasks(options?: {
110
- batchSize?: number;
111
- claimTimeout?: number;
112
- workerId?: string;
113
- }): Promise<ClaimedTask[]>;
114
-
115
- /**
116
- * Claim and process a batch of tasks sequentially.
117
- * @param workerId Worker identifier.
118
- * @param claimTimeout Lease duration in seconds.
119
- * @param batchSize Max tasks to process.
120
- */
121
- workBatch(
122
- workerId?: string,
123
- claimTimeout?: number,
124
- batchSize?: number
125
- ): Promise<void>;
126
-
127
- /**
128
- * Start a background worker that polls and executes tasks.
129
- * @param options Worker behavior options.
130
- */
131
- startWorker(options?: WorkerOptions): Promise<Worker>;
132
-
133
- /**
134
- * Close the client and any owned resources.
135
- */
136
- close(): Promise<void>;
137
-
138
- /**
139
- * Execute a claimed task (used by workers).
140
- * @param task Claimed task record.
141
- * @param claimTimeout Lease duration in seconds.
142
- * @param options Execution options.
143
- */
144
- executeTask(
145
- task: ClaimedTask,
146
- claimTimeout: number,
147
- options?: { fatalOnLeaseTimeout?: boolean }
148
- ): Promise<void>;
149
- }
package/src/sqlite.ts DELETED
@@ -1,211 +0,0 @@
1
- import type { Queryable } from "./absurd-types";
2
- import type {
3
- SQLiteRestBindParams,
4
- SQLiteDatabase,
5
- SQLiteStatement,
6
- SQLiteVerboseLog,
7
- } from "./sqlite-types";
8
-
9
- export class SqliteConnection implements Queryable {
10
- private readonly db: SQLiteDatabase;
11
- private readonly maxRetries = 5;
12
- private readonly baseRetryDelayMs = 50;
13
-
14
- constructor(db: SQLiteDatabase) {
15
- this.db = db;
16
- // TODO: verbose logging
17
- }
18
-
19
- async query<R extends object = Record<string, any>>(
20
- sql: string,
21
- params?: SQLiteRestBindParams
22
- ): Promise<{ rows: R[] }> {
23
- const sqliteQuery = rewritePostgresQuery(sql);
24
- const sqliteParams = rewritePostgresParams(params);
25
-
26
- const statement = this.db.prepare(sqliteQuery);
27
- if (!statement.readonly) {
28
- // this indicates `return_data` is false
29
- // https://github.com/WiseLibs/better-sqlite3/blob/6209be238d6a1b181f516e4e636986604b0f62e1/src/objects/statement.cpp#L134C83-L134C95
30
- throw new Error("The query() method is only statements that return data");
31
- }
32
-
33
- const rowsDecoded = await this.runWithRetry(() =>
34
- statement
35
- .all(sqliteParams)
36
- .map((row) => decodeRowValues(statement, row))
37
- );
38
-
39
- return { rows: rowsDecoded };
40
- }
41
-
42
- async exec(sql: string, params?: SQLiteRestBindParams): Promise<void> {
43
- const sqliteQuery = rewritePostgresQuery(sql);
44
- const sqliteParams = rewritePostgresParams(params);
45
-
46
- const statement = this.db.prepare(sqliteQuery);
47
- await this.runWithRetry(() => statement.run(sqliteParams));
48
- }
49
-
50
- private async runWithRetry<T>(operation: () => T): Promise<T> {
51
- let attempt = 0;
52
- while (true) {
53
- try {
54
- return operation();
55
- } catch (err) {
56
- if (!isRetryableSQLiteError(err) || attempt >= this.maxRetries) {
57
- throw err;
58
- }
59
- attempt++;
60
- await delay(this.baseRetryDelayMs * attempt);
61
- }
62
- }
63
- }
64
- }
65
-
66
- const namedParamPrefix = "p";
67
-
68
- function rewritePostgresQuery(text: string): string {
69
- return text
70
- .replace(/\$(\d+)/g, `:${namedParamPrefix}$1`)
71
- .replace(/absurd\.(\w+)/g, "absurd_$1");
72
- }
73
-
74
- function rewritePostgresParams<I = any>(
75
- params?: SQLiteRestBindParams
76
- ): Record<string, I> {
77
- if (!params) {
78
- return {};
79
- }
80
-
81
- const rewrittenParams: Record<string, I> = {};
82
- params.forEach((value, index) => {
83
- const paramKey = `${namedParamPrefix}${index + 1}`;
84
- const encodedParamValue = encodeColumnValue(value);
85
-
86
- rewrittenParams[paramKey] = encodedParamValue;
87
- });
88
- return rewrittenParams;
89
- }
90
-
91
- function decodeRowValues<U extends object, R extends object = any>(
92
- statement: SQLiteStatement,
93
- row: U,
94
- verbose?: SQLiteVerboseLog
95
- ): R {
96
- const columns = statement.columns();
97
-
98
- const decodedRow: any = {};
99
- for (const column of columns) {
100
- const columnName = column.name;
101
- const columnType = column.type;
102
- const rawValue = (row as Record<string, unknown>)[columnName];
103
- const decodedValue = decodeColumnValue(
104
- rawValue,
105
- columnName,
106
- columnType,
107
- verbose
108
- );
109
- decodedRow[columnName] = decodedValue;
110
- }
111
-
112
- return decodedRow as R;
113
- }
114
-
115
- function decodeColumnValue<V = any>(
116
- value: unknown | V,
117
- columnName: string,
118
- columnType: string | null,
119
- verbose?: SQLiteVerboseLog
120
- ): V | null {
121
- if (value === null || value === undefined) {
122
- return null;
123
- }
124
-
125
- if (columnType === null) {
126
- if (typeof value === "string") {
127
- // When column type is not known but the value is string
128
- // try parse it as JSON -- for cases where the column is computed
129
- // e.g. `SELECT json(x) as y from ....`
130
- // FIXME: better type detection
131
- let rv: V;
132
- let isValidJSON = false;
133
- try {
134
- rv = JSON.parse(value) as V;
135
- isValidJSON = true;
136
- } catch (e) {
137
- verbose?.(`Failed to decode string column ${columnName} as JSON`, e);
138
- rv = value as V;
139
- }
140
- if (isValidJSON) {
141
- verbose?.(`Decoded column ${columnName} with null as JSON`);
142
- }
143
- return rv;
144
- }
145
-
146
- verbose?.(`Column ${columnName} has null type, returning raw value`);
147
- return value as V;
148
- }
149
-
150
- const columnTypeName = columnType.toLowerCase();
151
- if (columnTypeName === "blob") {
152
- // BLOB values are JSON string decoded from JSONB
153
- try {
154
- return JSON.parse(value.toString()) as V;
155
- } catch (e) {
156
- verbose?.(`Failed to decode BLOB column ${columnName} as JSON`, e);
157
- throw e;
158
- }
159
- }
160
-
161
- if (columnTypeName === "datetime") {
162
- if (typeof value !== "number") {
163
- throw new Error(
164
- `Expected datetime column ${columnName} to be a number, got ${typeof value}`
165
- );
166
- }
167
- return new Date(value) as V;
168
- }
169
-
170
- // For other types, return as is
171
- return value as V;
172
- }
173
-
174
- function encodeColumnValue(value: any): any {
175
- if (value instanceof Date) {
176
- return value.toISOString();
177
- }
178
- if (typeof value === "number" && Number.isInteger(value)) {
179
- return value.toString();
180
- }
181
- return value;
182
- }
183
-
184
- const sqliteRetryableErrorCodes = new Set(["SQLITE_BUSY", "SQLITE_LOCKED"]);
185
- const sqliteRetryableErrnos = new Set([5, 6]);
186
-
187
- function isRetryableSQLiteError(err: unknown): boolean {
188
- if (!err || typeof err !== "object") {
189
- return false;
190
- }
191
-
192
- const code = (err as any).code;
193
- if (typeof code === "string") {
194
- for (const retryableCode of sqliteRetryableErrorCodes) {
195
- if (code.startsWith(retryableCode)) {
196
- return true;
197
- }
198
- }
199
- }
200
-
201
- const errno = (err as any).errno;
202
- if (typeof errno === "number" && sqliteRetryableErrnos.has(errno)) {
203
- return true;
204
- }
205
-
206
- return false;
207
- }
208
-
209
- function delay(ms: number): Promise<void> {
210
- return new Promise((resolve) => setTimeout(resolve, ms));
211
- }