-
-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathdocuments.js
More file actions
163 lines (145 loc) · 4.59 KB
/
Copy pathdocuments.js
File metadata and controls
163 lines (145 loc) · 4.59 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @namespace Databases-Documents
* @memberof module:Databases
* @description Documents database.
* @example <caption>Create documents db with custom index</caption>
* import { createHelia } from 'helia'
* import { createOrbitDB, Documents } from 'orbitdb'
*
* const ipfs = createHelia()
* const orbitdb = await createOrbitDB({ ipfs })
* const db = await orbitdb.open('my-docs', { Database: Documents({ indexBy: 'myCustomId'} ) }
*
* @augments module:Databases~Database
*/
import Database from '../database.js'
const type = 'documents'
const DefaultOptions = { indexBy: '_id' }
/**
* Defines a Documents database.
* @param {Object} options Various options for configuring the Document store.
* @param {string} [options.indexBy=_id] An index.
* @return {module:Databases.Databases-Documents} A Documents function.
* @memberof module:Databases
*/
const Documents = ({ indexBy } = DefaultOptions) => async ({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, onUpdate, encrypt }) => {
const database = await Database({ ipfs, identity, address, name, access, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically, encrypt })
const { addOperation, log } = database
/**
* Stores a document to the store.
* @function
* @param {Object} doc An object representing a key/value list of fields.
* @return {string} The hash of the new oplog entry.
* @memberof module:Databases.Databases-Documents
* @instance
*/
const put = async (doc) => {
const key = doc[indexBy]
if (!key) { throw new Error(`The provided document doesn't contain field '${indexBy}'`) }
return addOperation({ op: 'PUT', key, value: doc })
}
/**
* Deletes a document from the store.
* @function
* @param {string} key The key of the doc to delete.
* @return {string} The hash of the new oplog entry.
* @memberof module:Databases.Databases-Documents
* @instance
*/
const del = async (key) => {
if (!await get(key)) { throw new Error(`No document with key '${key}' in the database`) }
return addOperation({ op: 'DEL', key, value: null })
}
/**
* Gets a document from the store by key.
* @function
* @param {string} key The key of the doc to get.
* @return {Object} The doc corresponding to key or null.
* @memberof module:Databases.Databases-Documents
* @instance
*/
const get = async (key) => {
for await (const doc of iterator()) {
if (key === doc.key) {
return doc
}
}
}
/**
* Queries the document store for documents matching mapper filters.
* @function
* @param {function(Object)} findFn A function for querying for specific
* results.
*
* The findFn function's signature takes the form `function(doc)` where doc
* is a document's value property. The function should return true if the
* document should be included in the results, false otherwise.
* @return {Array} Found documents.
* @memberof module:Databases.Databases-Documents
* @instance
*/
const query = async (findFn) => {
const results = []
for await (const doc of iterator()) {
if (findFn(doc.value)) {
results.push(doc.value)
}
}
return results
}
/**
* Iterates over documents.
* @function
* @param {Object} [filters={}] Various filters to apply to the iterator.
* @param {string} [filters.amount=-1] The number of results to fetch.
* @yields [string, string, string] The next document as hash/key/value.
* @memberof module:Databases.Databases-Documents
* @instance
*/
const iterator = async function * ({ amount } = {}) {
const keys = {}
let count = 0
for await (const entry of log.iterator()) {
const { op, key, value } = entry.payload
if (op === 'PUT' && !keys[key]) {
keys[key] = true
count++
const hash = entry.hash
yield { hash, key, value }
} else if (op === 'DEL' && !keys[key]) {
keys[key] = true
}
if (count >= amount) {
break
}
}
}
/**
* Returns all documents.
* @function
* @return [][string, string, string] An array of documents as hash/key
* value entries.
* @memberof module:Databases.Databases-Documents
* @instance
*/
const all = async () => {
const values = []
for await (const entry of iterator()) {
values.unshift(entry)
}
return values
}
return {
...database,
type,
put,
del,
get,
iterator,
query,
indexBy,
all
}
}
Documents.type = type
export default Documents