/
nodes.ts
34 lines (31 loc) · 899 Bytes
/
nodes.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
import { ActionsUnion, IGatsbyNode } from "../../../redux/types"
import type { Database } from "lmdb"
type NodeId = string
export function updateNodes(
nodesDb: Database<IGatsbyNode, NodeId>,
action: ActionsUnion
): Promise<boolean> | boolean {
switch (action.type) {
case `CREATE_NODE`:
case `ADD_FIELD_TO_NODE`:
case `ADD_CHILD_NODE_TO_PARENT_NODE`: {
return nodesDb.put(action.payload.id, action.payload)
}
case `DELETE_NODE`: {
if (action.payload) {
return nodesDb.remove(action.payload.id)
}
return false
}
case `MATERIALIZE_PAGE_MODE`: {
const id = `SitePage ${action.payload.path}`
const node = nodesDb.get(id)
if (!node) {
throw new Error(`Could not find SitePage node by id: ${id}`)
}
node.mode = action.payload.pageMode
return nodesDb.put(id, node)
}
}
return false
}