mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
refactor: Use doc directly. Var renaming.
This commit is contained in:
parent
ef60cb252c
commit
2af2f7d8fb
@ -10,9 +10,11 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
|
|||||||
* @returns {string} The hash of the new oplog entry.
|
* @returns {string} The hash of the new oplog entry.
|
||||||
*/
|
*/
|
||||||
const put = async (doc) => {
|
const put = async (doc) => {
|
||||||
if (!doc[indexBy]) { throw new Error(`The provided document doesn't contain field '${indexBy}'`) }
|
const key = doc[indexBy]
|
||||||
|
|
||||||
return addOperation({ op: 'PUT', key: doc[indexBy], value: doc })
|
if (!key) { throw new Error(`The provided document doesn't contain field '${indexBy}'`) }
|
||||||
|
|
||||||
|
return addOperation({ op: 'PUT', key, value: doc })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,29 +36,25 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
|
|||||||
* @returns {Object} The doc corresponding to key or null.
|
* @returns {Object} The doc corresponding to key or null.
|
||||||
*/
|
*/
|
||||||
const get = async (key) => {
|
const get = async (key) => {
|
||||||
for await (const entry of iterator()) {
|
for await (const doc of iterator()) {
|
||||||
const { key: k, value } = entry
|
if (key === doc[indexBy]) {
|
||||||
|
return doc
|
||||||
if (key === k) {
|
|
||||||
return value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Queries the document store for documents matching mapper filters.
|
* Queries the document store for documents matching mapper filters.
|
||||||
*
|
*
|
||||||
* @param {function(Object)} mapper A function for querying for specific results.
|
* @param {function(Object)} findFn A function for querying for specific results.
|
||||||
* @returns {Array} Found documents.
|
* @returns {Array} Found documents.
|
||||||
*/
|
*/
|
||||||
const query = async (mapper) => {
|
const query = async (findFn) => {
|
||||||
const results = []
|
const results = []
|
||||||
|
|
||||||
for await (const entry of iterator()) {
|
for await (const doc of iterator()) {
|
||||||
if (Object.values(entry).find(mapper)) {
|
if (findFn(doc)) {
|
||||||
results.push(entry.value)
|
results.push(doc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +67,7 @@ const DocumentStore = async ({ OpLog, Database, ipfs, identity, databaseId, acce
|
|||||||
const { op, key, value } = entry.payload
|
const { op, key, value } = entry.payload
|
||||||
if (op === 'PUT' && !keys[key]) {
|
if (op === 'PUT' && !keys[key]) {
|
||||||
keys[key] = true
|
keys[key] = true
|
||||||
yield { key, value }
|
yield value
|
||||||
} else if (op === 'DEL' && !keys[key]) {
|
} else if (op === 'DEL' && !keys[key]) {
|
||||||
keys[key] = true
|
keys[key] = true
|
||||||
}
|
}
|
||||||
|
@ -105,35 +105,37 @@ Object.keys(testAPIs).forEach((IPFS) => {
|
|||||||
describe('using database', () => {
|
describe('using database', () => {
|
||||||
it('gets a document', async () => {
|
it('gets a document', async () => {
|
||||||
const key = 'hello world 1'
|
const key = 'hello world 1'
|
||||||
|
|
||||||
|
const expected = { _id: key, msg: 'writing 1 to db1' }
|
||||||
|
|
||||||
await db1.put({ _id: key, doc: 'writing 1 to db1' })
|
await db1.put(expected)
|
||||||
|
|
||||||
const doc = await db1.get(key)
|
const doc = await db1.get(key)
|
||||||
strictEqual(doc._id, key)
|
deepStrictEqual(doc, expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('deletes a document', async () => {
|
it('deletes a document', async () => {
|
||||||
const key = 'hello world 1'
|
const key = 'hello world 1'
|
||||||
|
|
||||||
await db1.put({ _id: key, doc: 'writing 1 to db1' })
|
await db1.put({ _id: key, msg: 'writing 1 to db1' })
|
||||||
await db1.del(key)
|
await db1.del(key)
|
||||||
|
|
||||||
const doc = await db1.get(key)
|
const doc = await db1.get(key)
|
||||||
strictEqual(doc, null)
|
strictEqual(doc, undefined)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('queries a document', async () => {
|
it('queries a document', async () => {
|
||||||
const expected = { _id: 'hello world 1', doc: 'writing new 1 to db1', views: 10 }
|
const expected = { _id: 'hello world 1', msg: 'writing new 1 to db1', views: 10 }
|
||||||
|
|
||||||
const doc3 = { _id: 'hello world 3', doc: 'writing 3 to db1', views: 12 }
|
await db1.put({ _id: 'hello world 1', msg: 'writing 1 to db1', views: 10 })
|
||||||
|
await db1.put({ _id: 'hello world 2', msg: 'writing 2 to db1', views: 5 })
|
||||||
await db1.put({ _id: 'hello world 1', doc: 'writing 1 to db1', views: 10 })
|
await db1.put({ _id: 'hello world 3', msg: 'writing 3 to db1', views: 12 })
|
||||||
await db1.put({ _id: 'hello world 2', doc: 'writing 2 to db1', views: 5 })
|
|
||||||
await db1.put({ _id: 'hello world 3', doc: 'writing 3 to db1', views: 12 })
|
|
||||||
await db1.del('hello world 3')
|
await db1.del('hello world 3')
|
||||||
await db1.put(expected)
|
await db1.put(expected)
|
||||||
|
|
||||||
deepStrictEqual(await db1.query((e) => e.views > 5), [expected])
|
const findFn = (doc) => doc.views > 5
|
||||||
|
|
||||||
|
deepStrictEqual(await db1.query(findFn), [expected])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user