### Install sqliteq and better-sqlite3 Source: https://github.com/minnzen/sqliteq/blob/main/README.md Install the sqliteq package and the recommended SQLite driver, better-sqlite3, using npm. ```bash npm install @minnzen/sqliteq better-sqlite3 ``` -------------------------------- ### Initialize and Start a Processor Source: https://github.com/minnzen/sqliteq/blob/main/README.md Demonstrates how to create a new Processor instance with a queue and custom options, then start it. The handler function processes messages and auto-deletes them on success or leaves them for retry on error. ```typescript import { Queue, Processor } from '@minnzen/sqliteq' const q = new Queue(db, 'jobs') const p = new Processor(q, { handler(msg) { console.log('processing', msg.body) // return normally = auto-delete // throw = leave for retry after timeout }, pollInterval: 200, concurrency: 4, }) p.start() // later: await p.stop() // waits for in-flight handlers to finish ``` -------------------------------- ### Get queue statistics Source: https://github.com/minnzen/sqliteq/blob/main/README.md Retrieves counts of messages categorized by their state (total, ready, delayed, in-flight, dead). ```typescript queue.stats(): QueueStats ``` -------------------------------- ### Get total message count Source: https://github.com/minnzen/sqliteq/blob/main/README.md Returns the total number of messages currently in the queue across all states. ```typescript queue.size(): number ``` -------------------------------- ### Get dead letter messages Source: https://github.com/minnzen/sqliteq/blob/main/README.md Retrieves messages that have exceeded the maximum receive count and are now considered dead letters. ```typescript queue.deadLetters(): Message[] ``` -------------------------------- ### Basic Usage of sqliteq Queue Source: https://github.com/minnzen/sqliteq/blob/main/README.md Demonstrates initializing a queue with better-sqlite3, sending a message, receiving it, and deleting it. The table and indexes are created automatically. ```typescript import Database from 'better-sqlite3' import { Queue } from '@minnzen/sqliteq' const db = new Database('app.db') const q = new Queue(db, 'emails') q.send({ to: 'user@example.com', subject: 'Hello' }) const msg = q.receive() if (msg) { console.log(msg.body) // { to: 'user@example.com', subject: 'Hello' } q.delete(msg.id, msg.received) } ``` -------------------------------- ### new Queue(db, name, options?) Source: https://github.com/minnzen/sqliteq/blob/main/README.md Creates or connects to a named queue. The schema is created automatically if it does not exist. Options can configure visibility timeout, maximum receives before dead-lettering, and maximum message body size. ```APIDOC ## new Queue(db, name, options?) ### Description Create or connect to a named queue. Schema is created if it does not exist. ### Parameters #### Path Parameters - **db** (Database) - Required - The SQLite database connection object. - **name** (string) - Required - The name of the queue. #### Query Parameters - **timeout** (number) - Optional - Visibility timeout in ms. Default: `5000`. - **maxReceive** (number) - Optional - Max receives before dead-lettering. Default: `3`. - **maxBodyBytes** (number) - Optional - Max body size in bytes after JSON serialization. Default: `1048576`. ### Database Interface ```ts interface Database { prepare(sql: string): Statement exec(sql: string): void transaction(fn: (...args: unknown[]) => R): (...args: unknown[]) => R } ``` ``` -------------------------------- ### Send a message with options Source: https://github.com/minnzen/sqliteq/blob/main/README.md Sends a single message to the queue. Supports options for delivery delay and priority. ```typescript queue.send(body: T, options?): string ``` -------------------------------- ### queue.size() Source: https://github.com/minnzen/sqliteq/blob/main/README.md Returns the total number of messages currently in the queue, across all states (ready, delayed, in-flight, dead). ```APIDOC ## queue.size() ### Description Total messages in the queue (all states). ### Returns - **number** - The total number of messages in the queue. ``` -------------------------------- ### queue.stats() Source: https://github.com/minnzen/sqliteq/blob/main/README.md Retrieves statistics about the queue, including counts for ready, delayed, in-flight, and dead messages. ```APIDOC ## queue.stats() ### Description Get queue counts grouped by state. ### Returns - **QueueStats** - An object containing queue statistics. ### QueueStats Interface ```ts interface QueueStats { total: number ready: number delayed: number inFlight: number dead: number } ``` ``` -------------------------------- ### Database Interface for sqliteq Source: https://github.com/minnzen/sqliteq/blob/main/README.md Defines the expected interface for a database object that can be used with sqliteq, compatible with better-sqlite3 and bun:sqlite. ```typescript interface Database { prepare(sql: string): Statement exec(sql: string): void transaction(fn: (...args: unknown[]) => R): (...args: unknown[]) => R } ``` -------------------------------- ### queue.send(body: T, options?) Source: https://github.com/minnzen/sqliteq/blob/main/README.md Sends a single message to the queue. The message body can be any JSON-serializable type. Options can specify a delivery delay and priority. ```APIDOC ## queue.send(body: T, options?) ### Description Send a message. Returns the message ID. ### Parameters #### Path Parameters - **body** (T) - Required - The message body. #### Query Parameters - **delay** (number) - Optional - Delivery delay in ms. Default: `0`. - **priority** (number) - Optional - Higher values are received first. Default: `0`. ### Returns - **string** - The ID of the sent message. ``` -------------------------------- ### queue.sendBatch(messages) Source: https://github.com/minnzen/sqliteq/blob/main/README.md Sends multiple messages to the queue in a single atomic transaction. Each message can optionally have send options. ```APIDOC ## queue.sendBatch(messages) ### Description Send multiple messages in one transaction. Returns an array of IDs. ### Parameters #### Path Parameters - **messages** (Array<{ body: T, options?: SendOptions }>) - Required - An array of message objects, each with a body and optional send options. ### Returns - **string[]** - An array of IDs for the sent messages. ``` -------------------------------- ### queue.receiveBatch(limit) Source: https://github.com/minnzen/sqliteq/blob/main/README.md Atomically claims up to a specified limit of available messages. Returns fewer messages if the queue has fewer available. Claimed messages are subject to the same visibility timeout as single receives. ```APIDOC ## queue.receiveBatch(limit) ### Description Atomically claim up to `limit` available messages. Returns fewer than requested when the queue runs dry. Claimed messages follow the same visibility timeout and fencing semantics as `receive()`. ### Parameters #### Path Parameters - **limit** (number) - Required - The maximum number of messages to claim. ### Returns - **Message[]** - An array of claimed message objects. ``` -------------------------------- ### QueueStats interface Source: https://github.com/minnzen/sqliteq/blob/main/README.md Defines the structure for queue statistics, including counts for total, ready, delayed, in-flight, and dead messages. ```typescript interface QueueStats { total: number ready: number delayed: number inFlight: number dead: number } ``` -------------------------------- ### queue.receive() Source: https://github.com/minnzen/sqliteq/blob/main/README.md Claims the next available message from the queue. The message becomes invisible to other consumers for a configured timeout period. Returns null if the queue is empty or all messages are in-flight. ```APIDOC ## queue.receive() ### Description Claim the next available message. Returns `null` when the queue is empty or all messages are in-flight. The message becomes invisible to other consumers for `timeout` ms. ### Returns - **Message | null** - The claimed message object, or null if no message is available. ### Message Interface ```ts interface Message { id: string body: T received: number // receive count (1 on first delivery) } ``` ``` -------------------------------- ### Receive multiple messages in a batch Source: https://github.com/minnzen/sqliteq/blob/main/README.md Atomically claims up to a specified limit of available messages. Returns fewer messages if the queue is empty or all messages are in-flight. ```typescript queue.receiveBatch(limit): Message[] ``` -------------------------------- ### Receive a message Source: https://github.com/minnzen/sqliteq/blob/main/README.md Claims the next available message from the queue. The message becomes invisible to other consumers for a configured timeout period. ```typescript queue.receive(): Message | null ``` -------------------------------- ### Send multiple messages in a batch Source: https://github.com/minnzen/sqliteq/blob/main/README.md Sends multiple messages efficiently within a single database transaction. Returns an array of message IDs. ```typescript queue.sendBatch(messages): string[] ``` -------------------------------- ### queue.requeueDeadLetters(options?) Source: https://github.com/minnzen/sqliteq/blob/main/README.md Requeues all current dead letter messages, making them available for redelivery. Requeued messages receive new IDs and can optionally have a new delay applied. ```APIDOC ## queue.requeueDeadLetters(options?) ### Description Requeue all current dead letters as fresh messages and return their new IDs. Requeued messages preserve body and priority, optionally apply a new delay, and always get new IDs so stale handles from previous deliveries stay invalid. ### Parameters #### Query Parameters - **delay** (number) - Optional - Delay in ms before requeued messages become visible. Default: `0`. ### Returns - **string[]** - An array of new IDs for the requeued messages. ``` -------------------------------- ### Message structure Source: https://github.com/minnzen/sqliteq/blob/main/README.md Defines the structure of a message received from the queue, including its ID, body, and receive count. ```typescript interface Message { id: string body: T received: number // receive count (1 on first delivery) } ``` -------------------------------- ### queue.extend(id, received, delay) Source: https://github.com/minnzen/sqliteq/blob/main/README.md Extends the visibility timeout for a message. This is useful for long-processing messages. Returns false if the message handle is stale (e.g., already redelivered). ```APIDOC ## queue.extend(id, received, delay) ### Description Extend a message's visibility timeout by `delay` ms. Returns `false` if the message was already redelivered to another consumer (stale handle). ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the message to extend. - **received** (number) - Required - The `received` timestamp from the message object. - **delay** (number) - Required - The additional delay in ms for the visibility timeout. ### Returns - **boolean** - `true` if the timeout was extended, `false` otherwise. ``` -------------------------------- ### queue.purge() Source: https://github.com/minnzen/sqliteq/blob/main/README.md Deletes all messages from the queue. Returns the count of messages that were removed. ```APIDOC ## queue.purge() ### Description Delete all messages. Returns the count removed. ### Returns - **number** - The number of messages removed from the queue. ``` -------------------------------- ### queue.deadLetters() Source: https://github.com/minnzen/sqliteq/blob/main/README.md Retrieves all messages that have exceeded the maximum receive count and are considered dead letters. These messages will not be delivered again. ```APIDOC ## queue.deadLetters() ### Description Get messages that exceeded `maxReceive`. These will never be delivered again and should be inspected or moved. ### Returns - **Message[]** - An array of dead letter message objects. ``` -------------------------------- ### Purge all messages from the queue Source: https://github.com/minnzen/sqliteq/blob/main/README.md Deletes all messages currently in the queue. Returns the number of messages removed. ```typescript queue.purge(): number ``` -------------------------------- ### Extend message visibility timeout Source: https://github.com/minnzen/sqliteq/blob/main/README.md Extends the visibility timeout for a claimed message. Returns false if the message handle is stale (e.g., already redelivered). ```typescript queue.extend(id, received, delay): boolean ``` -------------------------------- ### queue.purgeDeadLetters() Source: https://github.com/minnzen/sqliteq/blob/main/README.md Deletes all messages currently in the dead letter state. Returns the count of messages removed. ```APIDOC ## queue.purgeDeadLetters() ### Description Delete all current dead letters. Returns the count removed. ### Returns - **number** - The number of dead letter messages removed. ``` -------------------------------- ### Delete a message Source: https://github.com/minnzen/sqliteq/blob/main/README.md Acknowledges and permanently removes a message from the queue. Returns false if the message handle is stale. ```typescript queue.delete(id, received): boolean ``` -------------------------------- ### Requeue dead letters Source: https://github.com/minnzen/sqliteq/blob/main/README.md Requeues all current dead letter messages as new messages. Optionally applies a delay to the requeued messages. Returns new message IDs. ```typescript queue.requeueDeadLetters(options?): string[] ``` -------------------------------- ### queue.delete(id, received) Source: https://github.com/minnzen/sqliteq/blob/main/README.md Acknowledges and removes a message from the queue. Returns false if the message handle is stale, indicating it may have already been processed or redelivered. ```APIDOC ## queue.delete(id, received) ### Description Acknowledge and remove a message. Returns `false` on stale handle (safe no-op). ### Parameters #### Path Parameters - **id** (string) - Required - The ID of the message to delete. - **received** (number) - Required - The `received` timestamp from the message object. ### Returns - **boolean** - `true` if the message was deleted, `false` if the handle was stale. ``` -------------------------------- ### Purge all dead letters Source: https://github.com/minnzen/sqliteq/blob/main/README.md Deletes all messages currently in the dead letter state. Returns the count of messages removed. ```typescript queue.purgeDeadLetters(): number ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.