@allmightypush/push-storage-sqlite
v1.0.0
Published
SQLite storage adapter for push notification library
Maintainers
Readme
@allmightypush/push-storage-sqlite
SQLite storage adapter for the push notification library.
Installation
npm install @allmightypush/push-storage-sqliteUsage
import { SQLiteStorageAdapter } from '@allmightypush/push-storage-sqlite';
// Create adapter with file-based database
const adapter = new SQLiteStorageAdapter({
filename: './push-notifications.db',
enableWAL: true, // Enable WAL mode for better concurrency (default: true)
autoMigrate: true, // Run migrations automatically (default: true)
});
// Or use in-memory database for testing
const testAdapter = new SQLiteStorageAdapter({
filename: ':memory:',
});
// Use with push core
import { PushCore } from '@allmightypush/push-core';
const push = new PushCore();
push.configure({
storageAdapter: adapter,
// ... other configuration
});Configuration Options
SQLiteStorageOptions
filename(required): Path to the SQLite database file, or':memory:'for an in-memory databaseenableWAL(optional, default:true): Enable Write-Ahead Logging mode for better concurrencyautoMigrate(optional, default:true): Automatically run database migrations on initialization
Database Schema
Subscriptions Table
Stores push notification subscriptions with the following columns:
id(TEXT, PRIMARY KEY): UUID identifierendpoint(TEXT, NOT NULL): Push service endpoint URLkeys(TEXT, NOT NULL): JSON-encoded encryption keys (p256dh and auth)user_id(TEXT): Optional user identifiercreated_at(INTEGER, NOT NULL): Creation timestamp (Unix milliseconds)updated_at(INTEGER, NOT NULL): Last update timestamp (Unix milliseconds)last_used_at(INTEGER): Last successful send timestamp (Unix milliseconds)failed_count(INTEGER, NOT NULL, DEFAULT 0): Consecutive failure countstatus(TEXT, NOT NULL): Subscription status ('active', 'blocked', or 'expired')expires_at(INTEGER): Optional expiration timestamp (Unix milliseconds)metadata(TEXT): JSON-encoded arbitrary metadata
Indexes:
idx_subscriptions_user_idonuser_ididx_subscriptions_statusonstatus
Retry Queue Table
Stores failed notifications awaiting retry:
id(TEXT, PRIMARY KEY): UUID identifiersubscription_id(TEXT, NOT NULL): Foreign key to subscriptions tablepayload(TEXT, NOT NULL): JSON-encoded notification payloadattempt(INTEGER, NOT NULL): Current attempt number (0-indexed)next_retry_at(INTEGER, NOT NULL): Next retry timestamp (Unix milliseconds)last_error(TEXT): Last error messagecreated_at(INTEGER, NOT NULL): Creation timestamp (Unix milliseconds)
Indexes:
idx_retry_queue_next_retryonnext_retry_at
Foreign Keys:
subscription_idreferencessubscriptions(id)with CASCADE DELETE
Migrations
The adapter automatically creates the required tables and indexes on initialization (when autoMigrate is true). You can also run migrations manually:
await adapter.migrate();Migrations are idempotent and can be safely called multiple times.
Features
- ✅ Full StorageAdapter interface implementation
- ✅ Automatic schema creation and migrations
- ✅ WAL mode for better concurrency
- ✅ Efficient indexes for common queries
- ✅ Foreign key constraints with cascade delete
- ✅ In-memory database support for testing
- ✅ TypeScript type definitions included
License
MIT
