@ayepi/sqlite 0.0.0-stage → 0.6.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/LICENSE +21 -0
- package/README.md +33 -2
- package/ayepi-sqlite.md +179 -0
- package/dist/index.cjs +427 -0
- package/dist/index.d.cts +169 -0
- package/dist/index.d.ts +169 -0
- package/dist/index.js +419 -0
- package/package.json +77 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Backend, PubSub, PulledWork, Queue, Store } from "@ayepi/work";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { RateLimitStore, applyHit } from "@ayepi/rate";
|
|
4
|
+
|
|
5
|
+
//#region src/db.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A value bound to a statement parameter. There is no `null`: the stores write an absent value as a SQL `NULL`
|
|
9
|
+
* literal (a statement of its own), which keeps the port satisfiable by better-sqlite3's statement types.
|
|
10
|
+
*/
|
|
11
|
+
type SqlValue = string | number | bigint;
|
|
12
|
+
/** A prepared statement: run it, or read one row or all rows. */
|
|
13
|
+
interface SqliteStatement {
|
|
14
|
+
run(...params: SqlValue[]): {
|
|
15
|
+
readonly changes: number;
|
|
16
|
+
};
|
|
17
|
+
get(...params: SqlValue[]): unknown;
|
|
18
|
+
all(...params: SqlValue[]): unknown[];
|
|
19
|
+
}
|
|
20
|
+
/** The SQLite connection surface the stores drive. A `better-sqlite3` `Database` is one. */
|
|
21
|
+
interface SqliteDatabase {
|
|
22
|
+
prepare(sql: string): SqliteStatement;
|
|
23
|
+
exec(sql: string): unknown;
|
|
24
|
+
/** Whether a transaction is open (better-sqlite3 reports it); nested work then uses a savepoint. */
|
|
25
|
+
readonly inTransaction?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** Options every store shares. */
|
|
28
|
+
interface SqliteOptions {
|
|
29
|
+
/** Prefix of every table this store creates (default `'ayepi_'`): letters, digits and `_`, not starting with a digit. */
|
|
30
|
+
readonly prefix?: string;
|
|
31
|
+
/** Clock (default `Date.now`). */
|
|
32
|
+
readonly now?: () => number;
|
|
33
|
+
}
|
|
34
|
+
/** The default table-name prefix. */
|
|
35
|
+
declare const DEFAULT_PREFIX = "ayepi_";
|
|
36
|
+
/** A table-name prefix: it is spliced into SQL, so only identifier characters are allowed. */
|
|
37
|
+
declare const Prefix: z.ZodString;
|
|
38
|
+
/** Resolve the shared options: a validated prefix and a clock. */
|
|
39
|
+
|
|
40
|
+
/** Run `fn` atomically and with the write lock held: `BEGIN IMMEDIATE`, or a savepoint inside an open transaction. */
|
|
41
|
+
declare function atomic<T>(db: SqliteDatabase, fn: () => T): T;
|
|
42
|
+
/** Calls a function once every `every` calls (amortized cleanup). */
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/queue.d.ts
|
|
45
|
+
/** Options for {@link sqliteQueue}. */
|
|
46
|
+
interface SqliteQueueOptions extends SqliteOptions {
|
|
47
|
+
/** Which queue in the shared table (default `'default'`); give a work type its own queue by name. */
|
|
48
|
+
readonly name?: string;
|
|
49
|
+
}
|
|
50
|
+
/** A dead-lettered body. */
|
|
51
|
+
interface SqliteDeadLetter {
|
|
52
|
+
readonly body: string;
|
|
53
|
+
readonly error: string;
|
|
54
|
+
/** When it was dead-lettered (epoch ms). */
|
|
55
|
+
readonly at: number;
|
|
56
|
+
}
|
|
57
|
+
/** A {@link Queue} whose operations are synchronous, plus a look at its dead letters. */
|
|
58
|
+
interface SqliteQueue extends Queue {
|
|
59
|
+
push(body: string, opts?: {
|
|
60
|
+
readonly delay?: number;
|
|
61
|
+
readonly dedupeKey?: string;
|
|
62
|
+
}): void;
|
|
63
|
+
pop(max: number, visibility: number): PulledWork[];
|
|
64
|
+
heartbeat(pulled: PulledWork, visibility: number): void;
|
|
65
|
+
ack(pulled: PulledWork): void;
|
|
66
|
+
fail(pulled: PulledWork, delay?: number): void;
|
|
67
|
+
deadLetter(body: string, error: string): void;
|
|
68
|
+
/** Items in the queue, leased or not. */
|
|
69
|
+
size(): number;
|
|
70
|
+
/** Dead-lettered bodies, oldest first (up to `limit`, default 100). */
|
|
71
|
+
dead(limit?: number): SqliteDeadLetter[];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a `@ayepi/work` {@link Queue} on `db` (tables `<prefix>queue` and `<prefix>queue_dead`).
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const bulk = sqliteQueue(db, { name: 'bulk' }) // a separate queue for a noisy type
|
|
79
|
+
* const ingest = defineWork('ingest', handler, { queue: bulk })
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function sqliteQueue(db: SqliteDatabase, opts?: SqliteQueueOptions): SqliteQueue;
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/store.d.ts
|
|
85
|
+
/** Options for {@link sqliteStore}. */
|
|
86
|
+
interface SqliteStoreOptions extends SqliteOptions {
|
|
87
|
+
/** Delete expired keys once every this many writes (default 1000). Reads never return an expired key. */
|
|
88
|
+
readonly sweepEvery?: number;
|
|
89
|
+
}
|
|
90
|
+
/** A {@link Store} whose operations are synchronous. */
|
|
91
|
+
interface SqliteStore extends Store {
|
|
92
|
+
get(key: string): string | undefined;
|
|
93
|
+
set(key: string, value: string, ttl?: number): void;
|
|
94
|
+
delete(key: string): void;
|
|
95
|
+
setIfNotExists(key: string, value: string, ttl?: number): boolean;
|
|
96
|
+
increment(key: string, by: number, ttl?: number): number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Create the `@ayepi/work` {@link Store} on `db` (table `<prefix>kv`).
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import Database from 'better-sqlite3'
|
|
104
|
+
* const db = new Database('hub.db')
|
|
105
|
+
* createWork({ work, ...sqliteBackend(db) }) // queue + store + pubsub in one file
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function sqliteStore(db: SqliteDatabase, opts?: SqliteStoreOptions): SqliteStore;
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/pubsub.d.ts
|
|
111
|
+
/** Options for {@link sqlitePubSub}. */
|
|
112
|
+
interface SqlitePubSubOptions extends SqliteOptions {
|
|
113
|
+
/**
|
|
114
|
+
* How often to read messages other processes published (ms, default 100), or `false` when this process is the only
|
|
115
|
+
* one using the database (messages then never touch the table).
|
|
116
|
+
*/
|
|
117
|
+
readonly poll?: number | false;
|
|
118
|
+
/** Keep published messages this long for slow pollers (ms, default 60 000). */
|
|
119
|
+
readonly retention?: number;
|
|
120
|
+
}
|
|
121
|
+
/** A {@link PubSub} that can stop polling. */
|
|
122
|
+
interface SqlitePubSub extends PubSub {
|
|
123
|
+
publish(message: string): void;
|
|
124
|
+
/** Stop polling and drop every subscriber. */
|
|
125
|
+
close(): void;
|
|
126
|
+
}
|
|
127
|
+
/** Create the `@ayepi/work` {@link PubSub} on `db` (table `<prefix>pubsub`). */
|
|
128
|
+
declare function sqlitePubSub(db: SqliteDatabase, opts?: SqlitePubSubOptions): SqlitePubSub;
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/rate.d.ts
|
|
131
|
+
/** Options for {@link sqliteRateStore}. */
|
|
132
|
+
interface SqliteRateStoreOptions extends Omit<SqliteOptions, 'now'> {
|
|
133
|
+
/** Delete rows no window can read any more once every this many hits (default 1000). */
|
|
134
|
+
readonly sweepEvery?: number;
|
|
135
|
+
/** Drop a token bucket idle this long (ms, default 10 minutes); it would have refilled anyway. */
|
|
136
|
+
readonly idle?: number;
|
|
137
|
+
}
|
|
138
|
+
/** A {@link RateLimitStore} whose operations are synchronous. */
|
|
139
|
+
interface SqliteRateStore extends RateLimitStore {
|
|
140
|
+
consume(...args: Parameters<RateLimitStore['consume']>): ReturnType<typeof applyHit>;
|
|
141
|
+
reset(key: string): void;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create an `@ayepi/rate` {@link RateLimitStore} on `db` (tables `<prefix>rate_counter` and `<prefix>rate_bucket`).
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* const spend = limiter({ limit: 5_000_000, window: 86_400_000, store: sqliteRateStore(db) })
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function sqliteRateStore(db: SqliteDatabase, opts?: SqliteRateStoreOptions): SqliteRateStore;
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/index.d.ts
|
|
154
|
+
/** Options for {@link sqliteBackend}: the shared prefix and clock, and per-port options. */
|
|
155
|
+
interface SqliteBackendOptions extends SqliteOptions {
|
|
156
|
+
readonly queue?: Omit<SqliteQueueOptions, keyof SqliteOptions>;
|
|
157
|
+
readonly store?: Omit<SqliteStoreOptions, keyof SqliteOptions>;
|
|
158
|
+
readonly pubsub?: Omit<SqlitePubSubOptions, keyof SqliteOptions>;
|
|
159
|
+
}
|
|
160
|
+
/** The three `@ayepi/work` ports on one database. */
|
|
161
|
+
interface SqliteBackend extends Backend {
|
|
162
|
+
readonly queue: SqliteQueue;
|
|
163
|
+
readonly store: SqliteStore;
|
|
164
|
+
readonly pubsub: SqlitePubSub;
|
|
165
|
+
}
|
|
166
|
+
/** Create the `@ayepi/work` queue, store and pub/sub on `db` — spread it into `createWork`. */
|
|
167
|
+
declare function sqliteBackend(db: SqliteDatabase, opts?: SqliteBackendOptions): SqliteBackend;
|
|
168
|
+
//#endregion
|
|
169
|
+
export { DEFAULT_PREFIX, Prefix, type SqlValue, SqliteBackend, SqliteBackendOptions, type SqliteDatabase, type SqliteDeadLetter, type SqliteOptions, type SqlitePubSub, type SqlitePubSubOptions, type SqliteQueue, type SqliteQueueOptions, type SqliteRateStore, type SqliteRateStoreOptions, type SqliteStatement, type SqliteStore, type SqliteStoreOptions, atomic, sqliteBackend, sqlitePubSub, sqliteQueue, sqliteRateStore, sqliteStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { RateLimitStore, applyHit } from "@ayepi/rate";
|
|
3
|
+
import { Backend, PubSub, PulledWork, Queue, Store } from "@ayepi/work";
|
|
4
|
+
|
|
5
|
+
//#region src/db.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A value bound to a statement parameter. There is no `null`: the stores write an absent value as a SQL `NULL`
|
|
9
|
+
* literal (a statement of its own), which keeps the port satisfiable by better-sqlite3's statement types.
|
|
10
|
+
*/
|
|
11
|
+
type SqlValue = string | number | bigint;
|
|
12
|
+
/** A prepared statement: run it, or read one row or all rows. */
|
|
13
|
+
interface SqliteStatement {
|
|
14
|
+
run(...params: SqlValue[]): {
|
|
15
|
+
readonly changes: number;
|
|
16
|
+
};
|
|
17
|
+
get(...params: SqlValue[]): unknown;
|
|
18
|
+
all(...params: SqlValue[]): unknown[];
|
|
19
|
+
}
|
|
20
|
+
/** The SQLite connection surface the stores drive. A `better-sqlite3` `Database` is one. */
|
|
21
|
+
interface SqliteDatabase {
|
|
22
|
+
prepare(sql: string): SqliteStatement;
|
|
23
|
+
exec(sql: string): unknown;
|
|
24
|
+
/** Whether a transaction is open (better-sqlite3 reports it); nested work then uses a savepoint. */
|
|
25
|
+
readonly inTransaction?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** Options every store shares. */
|
|
28
|
+
interface SqliteOptions {
|
|
29
|
+
/** Prefix of every table this store creates (default `'ayepi_'`): letters, digits and `_`, not starting with a digit. */
|
|
30
|
+
readonly prefix?: string;
|
|
31
|
+
/** Clock (default `Date.now`). */
|
|
32
|
+
readonly now?: () => number;
|
|
33
|
+
}
|
|
34
|
+
/** The default table-name prefix. */
|
|
35
|
+
declare const DEFAULT_PREFIX = "ayepi_";
|
|
36
|
+
/** A table-name prefix: it is spliced into SQL, so only identifier characters are allowed. */
|
|
37
|
+
declare const Prefix: z.ZodString;
|
|
38
|
+
/** Resolve the shared options: a validated prefix and a clock. */
|
|
39
|
+
|
|
40
|
+
/** Run `fn` atomically and with the write lock held: `BEGIN IMMEDIATE`, or a savepoint inside an open transaction. */
|
|
41
|
+
declare function atomic<T>(db: SqliteDatabase, fn: () => T): T;
|
|
42
|
+
/** Calls a function once every `every` calls (amortized cleanup). */
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/queue.d.ts
|
|
45
|
+
/** Options for {@link sqliteQueue}. */
|
|
46
|
+
interface SqliteQueueOptions extends SqliteOptions {
|
|
47
|
+
/** Which queue in the shared table (default `'default'`); give a work type its own queue by name. */
|
|
48
|
+
readonly name?: string;
|
|
49
|
+
}
|
|
50
|
+
/** A dead-lettered body. */
|
|
51
|
+
interface SqliteDeadLetter {
|
|
52
|
+
readonly body: string;
|
|
53
|
+
readonly error: string;
|
|
54
|
+
/** When it was dead-lettered (epoch ms). */
|
|
55
|
+
readonly at: number;
|
|
56
|
+
}
|
|
57
|
+
/** A {@link Queue} whose operations are synchronous, plus a look at its dead letters. */
|
|
58
|
+
interface SqliteQueue extends Queue {
|
|
59
|
+
push(body: string, opts?: {
|
|
60
|
+
readonly delay?: number;
|
|
61
|
+
readonly dedupeKey?: string;
|
|
62
|
+
}): void;
|
|
63
|
+
pop(max: number, visibility: number): PulledWork[];
|
|
64
|
+
heartbeat(pulled: PulledWork, visibility: number): void;
|
|
65
|
+
ack(pulled: PulledWork): void;
|
|
66
|
+
fail(pulled: PulledWork, delay?: number): void;
|
|
67
|
+
deadLetter(body: string, error: string): void;
|
|
68
|
+
/** Items in the queue, leased or not. */
|
|
69
|
+
size(): number;
|
|
70
|
+
/** Dead-lettered bodies, oldest first (up to `limit`, default 100). */
|
|
71
|
+
dead(limit?: number): SqliteDeadLetter[];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a `@ayepi/work` {@link Queue} on `db` (tables `<prefix>queue` and `<prefix>queue_dead`).
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const bulk = sqliteQueue(db, { name: 'bulk' }) // a separate queue for a noisy type
|
|
79
|
+
* const ingest = defineWork('ingest', handler, { queue: bulk })
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function sqliteQueue(db: SqliteDatabase, opts?: SqliteQueueOptions): SqliteQueue;
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/store.d.ts
|
|
85
|
+
/** Options for {@link sqliteStore}. */
|
|
86
|
+
interface SqliteStoreOptions extends SqliteOptions {
|
|
87
|
+
/** Delete expired keys once every this many writes (default 1000). Reads never return an expired key. */
|
|
88
|
+
readonly sweepEvery?: number;
|
|
89
|
+
}
|
|
90
|
+
/** A {@link Store} whose operations are synchronous. */
|
|
91
|
+
interface SqliteStore extends Store {
|
|
92
|
+
get(key: string): string | undefined;
|
|
93
|
+
set(key: string, value: string, ttl?: number): void;
|
|
94
|
+
delete(key: string): void;
|
|
95
|
+
setIfNotExists(key: string, value: string, ttl?: number): boolean;
|
|
96
|
+
increment(key: string, by: number, ttl?: number): number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Create the `@ayepi/work` {@link Store} on `db` (table `<prefix>kv`).
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import Database from 'better-sqlite3'
|
|
104
|
+
* const db = new Database('hub.db')
|
|
105
|
+
* createWork({ work, ...sqliteBackend(db) }) // queue + store + pubsub in one file
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function sqliteStore(db: SqliteDatabase, opts?: SqliteStoreOptions): SqliteStore;
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/pubsub.d.ts
|
|
111
|
+
/** Options for {@link sqlitePubSub}. */
|
|
112
|
+
interface SqlitePubSubOptions extends SqliteOptions {
|
|
113
|
+
/**
|
|
114
|
+
* How often to read messages other processes published (ms, default 100), or `false` when this process is the only
|
|
115
|
+
* one using the database (messages then never touch the table).
|
|
116
|
+
*/
|
|
117
|
+
readonly poll?: number | false;
|
|
118
|
+
/** Keep published messages this long for slow pollers (ms, default 60 000). */
|
|
119
|
+
readonly retention?: number;
|
|
120
|
+
}
|
|
121
|
+
/** A {@link PubSub} that can stop polling. */
|
|
122
|
+
interface SqlitePubSub extends PubSub {
|
|
123
|
+
publish(message: string): void;
|
|
124
|
+
/** Stop polling and drop every subscriber. */
|
|
125
|
+
close(): void;
|
|
126
|
+
}
|
|
127
|
+
/** Create the `@ayepi/work` {@link PubSub} on `db` (table `<prefix>pubsub`). */
|
|
128
|
+
declare function sqlitePubSub(db: SqliteDatabase, opts?: SqlitePubSubOptions): SqlitePubSub;
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/rate.d.ts
|
|
131
|
+
/** Options for {@link sqliteRateStore}. */
|
|
132
|
+
interface SqliteRateStoreOptions extends Omit<SqliteOptions, 'now'> {
|
|
133
|
+
/** Delete rows no window can read any more once every this many hits (default 1000). */
|
|
134
|
+
readonly sweepEvery?: number;
|
|
135
|
+
/** Drop a token bucket idle this long (ms, default 10 minutes); it would have refilled anyway. */
|
|
136
|
+
readonly idle?: number;
|
|
137
|
+
}
|
|
138
|
+
/** A {@link RateLimitStore} whose operations are synchronous. */
|
|
139
|
+
interface SqliteRateStore extends RateLimitStore {
|
|
140
|
+
consume(...args: Parameters<RateLimitStore['consume']>): ReturnType<typeof applyHit>;
|
|
141
|
+
reset(key: string): void;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create an `@ayepi/rate` {@link RateLimitStore} on `db` (tables `<prefix>rate_counter` and `<prefix>rate_bucket`).
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* const spend = limiter({ limit: 5_000_000, window: 86_400_000, store: sqliteRateStore(db) })
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
declare function sqliteRateStore(db: SqliteDatabase, opts?: SqliteRateStoreOptions): SqliteRateStore;
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region src/index.d.ts
|
|
154
|
+
/** Options for {@link sqliteBackend}: the shared prefix and clock, and per-port options. */
|
|
155
|
+
interface SqliteBackendOptions extends SqliteOptions {
|
|
156
|
+
readonly queue?: Omit<SqliteQueueOptions, keyof SqliteOptions>;
|
|
157
|
+
readonly store?: Omit<SqliteStoreOptions, keyof SqliteOptions>;
|
|
158
|
+
readonly pubsub?: Omit<SqlitePubSubOptions, keyof SqliteOptions>;
|
|
159
|
+
}
|
|
160
|
+
/** The three `@ayepi/work` ports on one database. */
|
|
161
|
+
interface SqliteBackend extends Backend {
|
|
162
|
+
readonly queue: SqliteQueue;
|
|
163
|
+
readonly store: SqliteStore;
|
|
164
|
+
readonly pubsub: SqlitePubSub;
|
|
165
|
+
}
|
|
166
|
+
/** Create the `@ayepi/work` queue, store and pub/sub on `db` — spread it into `createWork`. */
|
|
167
|
+
declare function sqliteBackend(db: SqliteDatabase, opts?: SqliteBackendOptions): SqliteBackend;
|
|
168
|
+
//#endregion
|
|
169
|
+
export { DEFAULT_PREFIX, Prefix, type SqlValue, SqliteBackend, SqliteBackendOptions, type SqliteDatabase, type SqliteDeadLetter, type SqliteOptions, type SqlitePubSub, type SqlitePubSubOptions, type SqliteQueue, type SqliteQueueOptions, type SqliteRateStore, type SqliteRateStoreOptions, type SqliteStatement, type SqliteStore, type SqliteStoreOptions, atomic, sqliteBackend, sqlitePubSub, sqliteQueue, sqliteRateStore, sqliteStore };
|