-
Notifications
You must be signed in to change notification settings - Fork 2
/
indexed-db.ts
103 lines (92 loc) · 4.13 KB
/
indexed-db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { BotAction, BotIndexedDBAction } from '../interfaces/bot-actions'
import { unpipeInjects } from '../helpers/pipe'
import { getIndexedDBStoreValue, setIndexedDBStoreValue } from '../helpers/indexed-db'
import { inject } from './inject'
import { PipeValue } from '../types/pipe-value'
import { isObjectWithKey, isObjectWithValue } from '../types/objects'
import { getQueryKey, getQueryKeyValue } from '../types/database'
import { pipe } from './assembly-lines'
/**
* @description It's a higher-order BotAction that sets injects for identifying information of one IndexedDB store
* Database name & version, and Store name are accepted then injected into all provided actions
* Provided actions can overwride the injected params, on an individual basis
* Parent assembly-line injects are not passed in
* @param databaseName
* @param databaseVersion
* @param storeName
*/
export const indexedDBStore = (databaseName: string, storeName: string, databaseVersion?: number) =>
(...actions: BotAction<PipeValue|void>[]): BotAction<any> =>
pipe()(
inject(databaseVersion, databaseName, storeName)(
...actions
)
)
/**
* @description Set an IndexedDB Store's key value
* Supports setting the 'key' and/or 'value' from the Pipe's value
* Pipe value can be either the value to set, or an object {key: string, value: any}
* @param key
* @param value
* @param storeName
* @param databaseName
* @param databaseVersion
*/
export const setIndexedDBValue =
(key?: string, value?: any, storeName?: string, databaseName?: string, databaseVersion?: number): BotIndexedDBAction<void> =>
async(page, ...injects) => {
const [pipedValue, injectDatabaseVersion, injectDatabaseName, injectStoreName] = unpipeInjects<getQueryKeyValue>(injects, 3)
if (!value) {
if (pipedValue) {
// idea here is that the piped value is another object with keys {key: '', value: ''} -> to map as what we are setting in the DB
if (isObjectWithValue(pipedValue)) {
value = pipedValue.value
} else {
value = pipedValue
}
}
}
if (!key) {
if (isObjectWithKey(pipedValue)) {
key = pipedValue.key
}
}
await page.evaluate(
setIndexedDBStoreValue,
databaseName ? databaseName : injectDatabaseName ? injectDatabaseName : 'missing-db-name',
databaseVersion ? databaseVersion : injectDatabaseVersion ? injectDatabaseVersion : undefined, // grab latest version
storeName ? storeName : injectStoreName ? injectStoreName : 'missing-store',
key ? key : 'missing-key',
value ? value : 'missing-value'
)
}
/**
* @description Get an IndexedDB Store's key value
* Supports 'key' from the Pipe's value
* Pipe value can be either the key whose value to get, or an object that specifies the key such as {key: string}
* @param key
* @param storeName
* @param databaseName
* @param databaseVersion
*/
export const getIndexedDBValue =
(key?: string, storeName?: string, databaseName?: string, databaseVersion?: number): BotIndexedDBAction<PipeValue> =>
async(page, ...injects) => {
const [pipeValue, injectDatabaseVersion, injectDatabaseName, injectStoreName] = unpipeInjects<getQueryKey>(injects, 3)
if (!key) {
if (pipeValue) {
if (isObjectWithKey(pipeValue)) {
key = pipeValue.key
} else {
key = pipeValue
}
}
}
return await page.evaluate(
getIndexedDBStoreValue,
databaseName ? databaseName : injectDatabaseName ? injectDatabaseName : 'missing-db-name',
databaseVersion ? databaseVersion : injectDatabaseVersion ? injectDatabaseVersion : undefined, // on open, it will grab latest version if undefined
storeName ? storeName : injectStoreName ? injectStoreName : 'missing-store',
key ? key : 'missing-key'
) as PipeValue
}