@absurd-sqlite/sdk 0.2.0-alpha.0
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.
- package/README.md +1 -0
- package/dist/absurd-types.d.ts +109 -0
- package/dist/absurd-types.d.ts.map +1 -0
- package/dist/absurd-types.js +2 -0
- package/dist/absurd-types.js.map +1 -0
- package/dist/cjs/absurd-types.js +2 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sqlite-types.js +2 -0
- package/dist/cjs/sqlite.js +117 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/sqlite-types.d.ts +23 -0
- package/dist/sqlite-types.d.ts.map +1 -0
- package/dist/sqlite-types.js +2 -0
- package/dist/sqlite-types.js.map +1 -0
- package/dist/sqlite.d.ts +11 -0
- package/dist/sqlite.d.ts.map +1 -0
- package/dist/sqlite.js +114 -0
- package/dist/sqlite.js.map +1 -0
- package/package.json +51 -0
- package/src/absurd-types.ts +149 -0
- package/src/index.ts +46 -0
- package/src/sqlite-types.ts +35 -0
- package/src/sqlite.ts +162 -0
- package/test/basic.test.ts +505 -0
- package/test/events.test.ts +207 -0
- package/test/hooks.test.ts +347 -0
- package/test/idempotent.test.ts +195 -0
- package/test/index.test.ts +92 -0
- package/test/retry.test.ts +389 -0
- package/test/setup.ts +563 -0
- package/test/sqlite.test.ts +85 -0
- package/test/step.test.ts +259 -0
- package/test/worker.test.ts +193 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.cjs.json +11 -0
- package/tsconfig.json +20 -0
- package/typedoc.json +11 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,149 @@
|
|
|
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/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Absurd as AbsurdBase } from "absurd-sdk";
|
|
2
|
+
|
|
3
|
+
import type { AbsurdClient } from "./absurd-types";
|
|
4
|
+
import type { SQLiteDatabase } from "./sqlite-types";
|
|
5
|
+
import { SqliteConnection } from "./sqlite";
|
|
6
|
+
|
|
7
|
+
export type { AbsurdClient, Queryable, Worker } from "./absurd-types";
|
|
8
|
+
export type {
|
|
9
|
+
AbsurdHooks,
|
|
10
|
+
AbsurdOptions,
|
|
11
|
+
CancellationPolicy,
|
|
12
|
+
ClaimedTask,
|
|
13
|
+
JsonObject,
|
|
14
|
+
JsonValue,
|
|
15
|
+
RetryStrategy,
|
|
16
|
+
SpawnOptions,
|
|
17
|
+
SpawnResult,
|
|
18
|
+
TaskContext,
|
|
19
|
+
TaskHandler,
|
|
20
|
+
TaskRegistrationOptions,
|
|
21
|
+
WorkerOptions,
|
|
22
|
+
} from "absurd-sdk";
|
|
23
|
+
export type {
|
|
24
|
+
SQLiteBindParams,
|
|
25
|
+
SQLiteBindValue,
|
|
26
|
+
SQLiteColumnDefinition,
|
|
27
|
+
SQLiteDatabase,
|
|
28
|
+
SQLiteRestBindParams,
|
|
29
|
+
SQLiteStatement,
|
|
30
|
+
SQLiteVerboseLog,
|
|
31
|
+
} from "./sqlite-types";
|
|
32
|
+
|
|
33
|
+
export class Absurd extends AbsurdBase implements AbsurdClient {
|
|
34
|
+
private db: SQLiteDatabase;
|
|
35
|
+
|
|
36
|
+
constructor(db: SQLiteDatabase, extensionPath: string) {
|
|
37
|
+
db.loadExtension(extensionPath);
|
|
38
|
+
const queryable = new SqliteConnection(db);
|
|
39
|
+
super(queryable);
|
|
40
|
+
this.db = db;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async close(): Promise<void> {
|
|
44
|
+
this.db.close();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type SQLiteBindValue = number | string | Buffer | bigint | Date | null;
|
|
2
|
+
|
|
3
|
+
export type SQLiteBindParams =
|
|
4
|
+
| SQLiteBindValue[]
|
|
5
|
+
| Record<string, SQLiteBindValue>;
|
|
6
|
+
|
|
7
|
+
export type SQLiteRestBindParams = SQLiteBindValue[] | [SQLiteBindParams];
|
|
8
|
+
|
|
9
|
+
export interface SQLiteColumnDefinition {
|
|
10
|
+
name: string;
|
|
11
|
+
column: string | null;
|
|
12
|
+
table: string | null;
|
|
13
|
+
database: string | null;
|
|
14
|
+
type: string | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SQLiteStatement<Result extends object = Record<string, any>> {
|
|
18
|
+
readonly: boolean;
|
|
19
|
+
columns(): SQLiteColumnDefinition[];
|
|
20
|
+
all(...args: SQLiteRestBindParams): Result[];
|
|
21
|
+
run(...args: SQLiteRestBindParams): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SQLiteDatabase {
|
|
25
|
+
prepare<Result extends object = Record<string, any>>(
|
|
26
|
+
sql: string
|
|
27
|
+
): SQLiteStatement<Result>;
|
|
28
|
+
close(): void;
|
|
29
|
+
loadExtension(path: string): void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type SQLiteVerboseLog = (
|
|
33
|
+
message?: any,
|
|
34
|
+
...optionalParams: any[]
|
|
35
|
+
) => void;
|
package/src/sqlite.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
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
|
+
|
|
12
|
+
constructor(db: SQLiteDatabase) {
|
|
13
|
+
this.db = db;
|
|
14
|
+
// TODO: verbose logging
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async query<R extends object = Record<string, any>>(
|
|
18
|
+
sql: string,
|
|
19
|
+
params?: SQLiteRestBindParams
|
|
20
|
+
): Promise<{ rows: R[] }> {
|
|
21
|
+
const sqliteQuery = rewritePostgresQuery(sql);
|
|
22
|
+
const sqliteParams = rewritePostgresParams(params);
|
|
23
|
+
|
|
24
|
+
const statement = this.db.prepare(sqliteQuery);
|
|
25
|
+
if (!statement.readonly) {
|
|
26
|
+
// this indicates `return_data` is false
|
|
27
|
+
// https://github.com/WiseLibs/better-sqlite3/blob/6209be238d6a1b181f516e4e636986604b0f62e1/src/objects/statement.cpp#L134C83-L134C95
|
|
28
|
+
throw new Error("The query() method is only statements that return data");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const rowsDecoded = statement
|
|
32
|
+
.all(sqliteParams)
|
|
33
|
+
.map((row) => decodeRowValues(statement, row));
|
|
34
|
+
|
|
35
|
+
return { rows: rowsDecoded };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async exec(sql: string, params?: SQLiteRestBindParams): Promise<void> {
|
|
39
|
+
const sqliteQuery = rewritePostgresQuery(sql);
|
|
40
|
+
const sqliteParams = rewritePostgresParams(params);
|
|
41
|
+
|
|
42
|
+
this.db.prepare(sqliteQuery).run(sqliteParams);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const namedParamPrefix = "p";
|
|
47
|
+
|
|
48
|
+
function rewritePostgresQuery(text: string): string {
|
|
49
|
+
return text
|
|
50
|
+
.replace(/\$(\d+)/g, `:${namedParamPrefix}$1`)
|
|
51
|
+
.replace(/absurd\.(\w+)/g, "absurd_$1");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function rewritePostgresParams<I = any>(
|
|
55
|
+
params?: SQLiteRestBindParams
|
|
56
|
+
): Record<string, I> {
|
|
57
|
+
if (!params) {
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const rewrittenParams: Record<string, I> = {};
|
|
62
|
+
params.forEach((value, index) => {
|
|
63
|
+
const paramKey = `${namedParamPrefix}${index + 1}`;
|
|
64
|
+
const encodedParamValue = encodeColumnValue(value);
|
|
65
|
+
|
|
66
|
+
rewrittenParams[paramKey] = encodedParamValue;
|
|
67
|
+
});
|
|
68
|
+
return rewrittenParams;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function decodeRowValues<U extends object, R extends object = any>(
|
|
72
|
+
statement: SQLiteStatement,
|
|
73
|
+
row: U,
|
|
74
|
+
verbose?: SQLiteVerboseLog
|
|
75
|
+
): R {
|
|
76
|
+
const columns = statement.columns();
|
|
77
|
+
|
|
78
|
+
const decodedRow: any = {};
|
|
79
|
+
for (const column of columns) {
|
|
80
|
+
const columnName = column.name;
|
|
81
|
+
const columnType = column.type;
|
|
82
|
+
const rawValue = (row as Record<string, unknown>)[columnName];
|
|
83
|
+
const decodedValue = decodeColumnValue(
|
|
84
|
+
rawValue,
|
|
85
|
+
columnName,
|
|
86
|
+
columnType,
|
|
87
|
+
verbose
|
|
88
|
+
);
|
|
89
|
+
decodedRow[columnName] = decodedValue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return decodedRow as R;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function decodeColumnValue<V = any>(
|
|
96
|
+
value: unknown | V,
|
|
97
|
+
columnName: string,
|
|
98
|
+
columnType: string | null,
|
|
99
|
+
verbose?: SQLiteVerboseLog
|
|
100
|
+
): V | null {
|
|
101
|
+
if (value === null || value === undefined) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (columnType === null) {
|
|
106
|
+
if (typeof value === "string") {
|
|
107
|
+
// When column type is not known but the value is string
|
|
108
|
+
// try parse it as JSON -- for cases where the column is computed
|
|
109
|
+
// e.g. `SELECT json(x) as y from ....`
|
|
110
|
+
// FIXME: better type detection
|
|
111
|
+
let rv: V;
|
|
112
|
+
let isValidJSON = false;
|
|
113
|
+
try {
|
|
114
|
+
rv = JSON.parse(value) as V;
|
|
115
|
+
isValidJSON = true;
|
|
116
|
+
} catch (e) {
|
|
117
|
+
verbose?.(`Failed to decode string column ${columnName} as JSON`, e);
|
|
118
|
+
rv = value as V;
|
|
119
|
+
}
|
|
120
|
+
if (isValidJSON) {
|
|
121
|
+
verbose?.(`Decoded column ${columnName} with null as JSON`);
|
|
122
|
+
}
|
|
123
|
+
return rv;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
verbose?.(`Column ${columnName} has null type, returning raw value`);
|
|
127
|
+
return value as V;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const columnTypeName = columnType.toLowerCase();
|
|
131
|
+
if (columnTypeName === "blob") {
|
|
132
|
+
// BLOB values are JSON string decoded from JSONB
|
|
133
|
+
try {
|
|
134
|
+
return JSON.parse(value.toString()) as V;
|
|
135
|
+
} catch (e) {
|
|
136
|
+
verbose?.(`Failed to decode BLOB column ${columnName} as JSON`, e);
|
|
137
|
+
throw e;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (columnTypeName === "datetime") {
|
|
142
|
+
if (typeof value !== "number") {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Expected datetime column ${columnName} to be a number, got ${typeof value}`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return new Date(value) as V;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// For other types, return as is
|
|
151
|
+
return value as V;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function encodeColumnValue(value: any): any {
|
|
155
|
+
if (value instanceof Date) {
|
|
156
|
+
return value.toISOString();
|
|
157
|
+
}
|
|
158
|
+
if (typeof value === "number" && Number.isInteger(value)) {
|
|
159
|
+
return value.toString();
|
|
160
|
+
}
|
|
161
|
+
return value;
|
|
162
|
+
}
|