orbit-db-feedstore
Advanced tools
Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "orbit-db-feedstore", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Feed store for orbit-db", | ||
@@ -10,3 +10,7 @@ "main": "src/FeedStore.js", | ||
"author": "Haad", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"dependencies": { | ||
"lazy.js": "^0.4.2", | ||
"orbit-db-eventstore": "0.0.4" | ||
} | ||
} |
'use strict'; | ||
class FeedIndex { | ||
constructor() { | ||
this._index = {}; | ||
} | ||
const EventIndex = require('orbit-db-eventstore/src/EventIndex'); | ||
get() { | ||
return Object.keys(this._index).map((f) => this._index[f]); | ||
} | ||
class FeedIndex extends EventIndex { | ||
updateIndex(oplog, added) { | ||
@@ -13,0 +7,0 @@ added.reduce((handled, item) => { |
'use strict'; | ||
const Lazy = require('lazy.js'); | ||
const Store = require('orbit-db-store'); | ||
const EventIndex = require('./FeedIndex'); | ||
const EventStore = require('orbit-db-eventstore'); | ||
const FeedIndex = require('./FeedIndex'); | ||
class FeedStore extends Store { | ||
class FeedStore extends EventStore { | ||
constructor(ipfs, id, dbname, options) { | ||
Object.assign(options || {}, { Index: EventIndex }); | ||
Object.assign(options || {}, { Index: FeedIndex }); | ||
super(ipfs, id, dbname, options) | ||
} | ||
add(data) { | ||
const operation = { | ||
op: 'ADD', | ||
key: null, | ||
value: data, | ||
meta: { | ||
ts: new Date().getTime() | ||
} | ||
}; | ||
return this._addOperation(operation); | ||
} | ||
remove(hash) { | ||
@@ -36,52 +24,4 @@ const operation = { | ||
} | ||
iterator(options) { | ||
const messages = this._query(this.dbname, options); | ||
let currentIndex = 0; | ||
let iterator = { | ||
[Symbol.iterator]() { | ||
return this; | ||
}, | ||
next() { | ||
let item = { value: null, done: true }; | ||
if(currentIndex < messages.length) { | ||
item = { value: messages[currentIndex], done: false }; | ||
currentIndex ++; | ||
} | ||
return item; | ||
}, | ||
collect: () => messages | ||
} | ||
return iterator; | ||
} | ||
_query(dbname, opts) { | ||
if(!opts) opts = {}; | ||
const amount = opts.limit ? (opts.limit > -1 ? opts.limit : this._index.get().length) : 1; // Return 1 if no limit is provided | ||
const operations = this._index.get(); | ||
let result = []; | ||
if(opts.gt || opts.gte) { | ||
// Greater than case | ||
result = this._read(operations, opts.gt ? opts.gt : opts.gte, amount, opts.gte ? true : false) | ||
} else { | ||
// Lower than and lastN case, search latest first by reversing the sequence | ||
result = this._read(operations.reverse(), opts.lt ? opts.lt : opts.lte, amount, opts.lte || !opts.lt).reverse() | ||
} | ||
if(opts.reverse) result.reverse(); | ||
return result.toArray(); | ||
} | ||
_read(ops, hash, amount, inclusive) { | ||
return Lazy(ops) | ||
.skipWhile((f) => hash && f.hash !== hash) | ||
.drop(inclusive ? 0 : 1) | ||
.take(amount); | ||
} | ||
} | ||
module.exports = FeedStore; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2527
2
40
+ Addedlazy.js@^0.4.2
+ Addedorbit-db-eventstore@0.0.4
+ Addedbluebird@3.7.2(transitive)
+ Addedipfs-log@1.5.2(transitive)
+ Addedlazy.js@0.4.3(transitive)
+ Addedlodash.differencewith@4.5.0(transitive)
+ Addedlodash.flatten@4.4.0(transitive)
+ Addedlodash.take@4.1.1(transitive)
+ Addedlodash.unionwith@4.6.0(transitive)
+ Addedorbit-db-eventstore@0.0.4(transitive)
+ Addedorbit-db-store@0.0.3(transitive)