Skip to content

Commit 13fc7da

Browse files
committed
Add docs
1 parent 7222713 commit 13fc7da

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed

docs/docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 8
2+
sidebar_position: 9
33
---
44

55
# API Reference

docs/docs/key_value_storage.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
sidebar_position: 8
3+
---
4+
5+
# Key-Value Storage
6+
7+
OP-SQLite provides a simple key-value storage API compatible with react-native-async-storage. It should be much faster than async-storage (whilst slower than MMKV) but comes with the convenience of not having to add one more dependency to your app. For convenience it also has sync versions of the methods. If you use SQLCipher the data inside will also be encrypted.
8+
9+
```ts
10+
import { Storage } from '@op-engineering/op-sqlite';
11+
12+
// Storage is backed by it's own database
13+
// You can set the location like any other op-sqlite database
14+
const storage = new Storage({
15+
location: 'storage', // Optional, see location param on normal databases
16+
encryptionKey: 'myEncryptionKey', // Optional, only used when used against SQLCipher
17+
});
18+
19+
const item = storage.getItemSync('foo');
20+
21+
const item2 = await storage.getItem('foo');
22+
23+
await storage.setItem('foo', 'bar');
24+
25+
storage.setItemSync('foo', 'bar');
26+
27+
const allKeys = storage.getAllKeys();
28+
29+
// Clears the internal table
30+
storage.clear();
31+
```

src/Storage.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,48 @@ export class Storage {
1616
this.db = open({ ...options, name: '__opsqlite_storage' });
1717
this.db.executeSync('PRAGMA mmap_size=268435456');
1818
this.db.executeSync(
19-
'CREATE TABLE IF NOT EXISTS storage (key TEXT PRIMARY KEY, value TEXT) STRICT'
19+
'CREATE TABLE IF NOT EXISTS storage (key TEXT PRIMARY KEY, value TEXT) STRICT WITHOUT ROWID'
2020
);
2121
}
2222

23-
async getItem(key: string) {
23+
async getItem(key: string): Promise<string | undefined> {
2424
const result = await this.db.execute(
2525
'SELECT value FROM storage WHERE key = ?',
2626
[key]
2727
);
2828

29-
return result.rows[0]?.value;
29+
let value = result.rows[0]?.value;
30+
if (typeof value !== 'undefined' && typeof value !== 'string') {
31+
throw new Error('Value must be a string or undefined');
32+
}
33+
return value;
3034
}
3135

32-
getItemSync(key: string) {
36+
getItemSync(key: string): string | undefined {
3337
const result = this.db.executeSync(
3438
'SELECT value FROM storage WHERE key = ?',
3539
[key]
3640
);
3741

38-
return result.rows[0]?.value;
42+
let value = result.rows[0]?.value;
43+
if (typeof value !== 'undefined' && typeof value !== 'string') {
44+
throw new Error('Value must be a string or undefined');
45+
}
46+
47+
return value;
3948
}
4049

4150
async setItem(key: string, value: string) {
4251
await this.db.execute(
4352
'INSERT OR REPLACE INTO storage (key, value) VALUES (?, ?)',
44-
[key, value]
53+
[key, value.toString()]
4554
);
4655
}
4756

4857
setItemSync(key: string, value: string) {
4958
this.db.executeSync(
5059
'INSERT OR REPLACE INTO storage (key, value) VALUES (?, ?)',
51-
[key, value]
60+
[key, value.toString()]
5261
);
5362
}
5463

@@ -67,4 +76,10 @@ export class Storage {
6776
clearSync() {
6877
this.db.executeSync('DELETE FROM storage');
6978
}
79+
80+
getAllKeys() {
81+
return this.db
82+
.executeSync('SELECT key FROM storage')
83+
.rows.map((row: any) => row.key);
84+
}
7085
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { NativeModules, Platform } from 'react-native';
2+
export { Storage } from './Storage';
23

34
export type Scalar =
45
| string

0 commit comments

Comments
 (0)