git ssb

0+

Dominic / weather



Tree: 5c787a5d6a07428ce30664e68a71eaa3278ea46b

Files: 5c787a5d6a07428ce30664e68a71eaa3278ea46b / db.js

1535 bytesRaw
1
2var urls = require('./urls')
3var config = require('./config')
4var Sublevel = require('level-sublevel/bytewise')
5var retrive = require('./')
6var cont = require('cont')
7var crypto = require('crypto')
8
9var db = Sublevel(require('level')(config.db, {valueEncoding: 'json'}))
10var rawdb = db.sublevel('raw')
11var sample = db.sublevel('sample')
12
13function hash (data) {
14 return crypto.createHash('sha256').update(JSON.stringify(data, null, 2)).digest('hex')
15}
16
17db.update = function (cb) {
18 cont.para(
19 Object.keys(urls).map(function (name) {
20 return function (cb) {
21 retrive(name, function (err, data) {
22 if(err) return cb(err)
23 var text = JSON.stringify(data, null, 2)
24 var id = hash(text)
25 rawdb.get(id, function (err) {
26 return cb(
27 null,
28 err ? { key: id, value: data, type: 'put', prefix: rawdb } : null
29 )
30 })
31 })
32 }
33 })
34 ) (function (err, batch) {
35 if(err) return cb(err)
36 var ts = Date.now()
37 batch.filter(Boolean).forEach(function (e) {
38 console.log(e)
39 batch.push({key: [e.value.name, ts], value: e.key, type: 'put', prefix: sample})
40 batch.push({key: e.value.name, value: e.key, type: 'put', prefix: db})
41 })
42 if(!batch.length) return cb(null, [])
43 console.error(batch)
44 batch = batch.filter(Boolean)
45 db.batch(batch, function (err) {
46 cb(null, batch.map(function (e) {
47 return {key: e.key, value: e.value}
48 }))
49 })
50 })
51}
52
53module.exports = db
54
55
56
57

Built with git-ssb-web