From 2b46b5895be29a0b0bc7ffd20b83899a2546b21e Mon Sep 17 00:00:00 2001 From: Joe Krill Date: Tue, 21 Jul 2026 11:55:06 -0400 Subject: [PATCH] Allow customizing Storage database name --- docs/docs/key_value_storage.md | 1 + src/Storage.ts | 8 ++++++-- src/Storage.web.ts | 8 ++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/docs/key_value_storage.md b/docs/docs/key_value_storage.md index 9d217e4c..ee25b7e6 100644 --- a/docs/docs/key_value_storage.md +++ b/docs/docs/key_value_storage.md @@ -12,6 +12,7 @@ import { Storage } from '@op-engineering/op-sqlite'; // Storage is backed by it's own database // You can set the location like any other op-sqlite database const storage = new Storage({ + name: 'myStorage.sqlite', // Optional, defaults to '__opsqlite_storage.sqlite' location: 'storage', // Optional, see location param on normal databases encryptionKey: 'myEncryptionKey', // Optional, only used when used against SQLCipher }); diff --git a/src/Storage.ts b/src/Storage.ts index 6725cab5..34a377e1 100644 --- a/src/Storage.ts +++ b/src/Storage.ts @@ -2,6 +2,7 @@ import { isTurso, open } from './functions'; import type { DB } from './types'; type StorageOptions = { + name?: string; location?: string; encryptionKey?: string; }; @@ -13,8 +14,11 @@ type StorageOptions = { export class Storage { private db: DB; - constructor(options: StorageOptions) { - this.db = open({ ...options, name: '__opsqlite_storage.sqlite' }); + constructor({ + name = '__opsqlite_storage.sqlite', + ...options + }: StorageOptions) { + this.db = open({ ...options, name }); if (!isTurso()) { this.db.executeSync('PRAGMA mmap_size=268435456'); } diff --git a/src/Storage.web.ts b/src/Storage.web.ts index f851c358..e6e61e46 100644 --- a/src/Storage.web.ts +++ b/src/Storage.web.ts @@ -2,6 +2,7 @@ import { openAsync } from './functions.web'; import { type DB } from './types'; type StorageOptions = { + name?: string; location?: string; encryptionKey?: string; }; @@ -9,9 +10,12 @@ type StorageOptions = { export class Storage { private dbPromise: Promise; - constructor(options: StorageOptions) { + constructor({ + name = '__opsqlite_storage.sqlite', + ...options + }: StorageOptions) { this.dbPromise = (async () => { - const db = await openAsync({ ...options, name: '__opsqlite_storage.sqlite' }); + const db = await openAsync({ ...options, name }); await db.execute( 'CREATE TABLE IF NOT EXISTS storage (key TEXT PRIMARY KEY, value TEXT)' );