OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-social / hyper-feed / lib / feed.js

60 lines · javascript ref:
import Corestore from 'corestore'
import Hyperbee from 'hyperbee'
import { FeedStore } from './feed-store.js'

class Feed {
  constructor (storage) {
    this.store = new Corestore(storage)
    this.bee = new Hyperbee(this.store.get({ name: 'feed' }), {
      keyEncoding: 'utf-8',
      valueEncoding: 'json'
    })
    this.items = new FeedStore(this.bee)
  }

  async ready () {
    await this.store.ready()
    await this.items.ready()
  }

  // Item CRUD
  async add (opts) {
    return this.items.add(opts)
  }

  async get (id) {
    return this.items.get(id)
  }

  async remove (id) {
    return this.items.remove(id)
  }

  // Queries
  async timeline (opts) {
    return this.items.timeline(opts)
  }

  async bySource (source, opts) {
    return this.items.bySource(source, opts)
  }

  async byAuthor (authorId, opts) {
    return this.items.byAuthor(authorId, opts)
  }

  async search (query) {
    return this.items.search(query)
  }

  async stats () {
    return this.items.stats()
  }

  async close () {
    await this.store.close()
  }
}

export { Feed }