Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/key_value_storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand Down
8 changes: 6 additions & 2 deletions src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { isTurso, open } from './functions';
import type { DB } from './types';

type StorageOptions = {
name?: string;
location?: string;
encryptionKey?: string;
};
Expand All @@ -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');
}
Expand Down
8 changes: 6 additions & 2 deletions src/Storage.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ import { openAsync } from './functions.web';
import { type DB } from './types';

type StorageOptions = {
name?: string;
location?: string;
encryptionKey?: string;
};

export class Storage {
private dbPromise: Promise<DB>;

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)'
);
Expand Down