Skip to content

Instantly share code, notes, and snippets.

@natew
Created April 28, 2016 02:54
Show Gist options
  • Save natew/d28c095b79dc54ce702eab18602de801 to your computer and use it in GitHub Desktop.
Save natew/d28c095b79dc54ce702eab18602de801 to your computer and use it in GitHub Desktop.
'use strict'
/* @flow */
import { Emitter, CompositeDisposable } from 'sb-event-kit'
import { getRandomInt } from '$helpers.js'
import type { Disposable } from 'sb-event-kit'
type Config = {
host: string
}
type Horizon = Object
type Question = {
id: string,
type: 'unknown' | 'table' | 'line' | 'list' | 'map' | 'heatmap';
result: ?Array<Object>,
status: 'pending' | 'accepted' | 'resolved',
question: string,
datetime: string,
previousQuestion: ?string // UUID
}
export class Database {
@observable questions
@observable current
config: Config;
connection: Horizon;
constructor() {
this.connection = new Horizon(this.config)
this.connection('questions').watch().forEach(questions => {
this.questions = questions
})
}
addQuestion(query: string, previousQuestion: ?string): Question {
// TODO: Replace with a UUID generator
const id = String(getRandomInt(111111111, 111111111 * 9))
const question = {
id,
type: 'unknown',
result: null,
status: 'pending',
question: query,
datetime: new Date().toISOString(),
previousQuestion,
}
this.connection('questions').store(question)
return question
}
getQuestion(id: string): Promise<?Question> {
return new Promise((resolve, reject) => {
let result
this.connection('questions').find({ id }).fetch().forEach(function(item) {
this.current = item
}, reject, function() {
resolve(result)
})
})
}
updateQuestion(question: Object) {
this.connection('questions').upsert([question])
}
}
const database = new Database({ host: `${location.hostname}:8189` })
export default database
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment