This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-gun.js
133 lines (107 loc) · 3.48 KB
/
test-gun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var Gun = require('gun')
var gun = Gun()
TestTwo()
function TestTwo() {
// https://github.com/amark/gun/wiki/Graphs
var alice = gun.get('person/alice').put({name: 'alice', age: 22})
var bob = gun.get('person/bob').put({name: 'bob', age: 24})
var carl = gun.get('person/carl').put({name: 'carl', age: 16})
var dave = gun.get('person/dave').put({name: 'dave', age: 42})
alice.on(function(node){
log('SUBSCRIBED TO ALICE!', node)
})
gun.get('person/bob').once(function(node){
log('BOB!', node)
})
// Create a SET - "think of this as a table in relational databases or a collection in NoSQL databases"
var people = gun.get('people')
people.set(alice)
people.set(bob)
people.set(carl)
people.set(dave)
people.map().once(function(person){
log('THE PERSON IS: ', person)
})
var company = gun.get('startup/hype').put({
name: 'hype',
profitable: false,
address: {
street: '123 Hipster Lane',
city: 'San Francisco',
state: 'CA',
country: 'USA'
}
})
company.once(function(startup){
log('THE STARTUP:', startup)
})
var employees = company.get('employees')
employees.set(dave)
employees.set(alice)
employees.set(bob)
alice.get('spouse').put(bob)
bob.get('spouse').put(alice)
alice.get('spouse').get('employer').put(company)
alice.get('employer').put(company)
dave.get('kids').set(carl)
carl.get('dad').put(dave)
carl.get('friends').set(alice)
carl.get('friends').set(bob)
// gun.get('person/alice').get('spouse').get('employer').get('employees').map().get('name').once(function(data, key){
// console.log('THE EMPLOYEES ', key, '= ', data)
// })
//console.log(gun.get('person/alice').get('spouse').get('employer').get('employees'))
}
function TestOne() {
//populate
for (var i=0;i<10;++i) { gun.get('db').get('test1').set(i) }
//verify:
// gun.get('db').get('test1').on( (value,key) => {
// console.log( 'Key=', key, ', Value=', value)
// })
gun.get('db').get('test1').once(console.log)
//delete:
// gun.get('db').get('test1').map().once( function(value,key) {
// this.put(null)
// })
setTimeout(() => {
gun.get('db').get('test1').put(null)
}, 500)
//verify:
setTimeout(() => {
console.log('================================')
//gun.get('db').get('test1').once(console.log)
gun.get('db').once(console.log)
}, 5000)
//seems ok
}
function log(txt, value, maxdepth=3, currdepth=0) {
//if (currdepth>0) console.log(value)
//msg.payload = Object.assign({}, value)
const payload = {}
Object.entries(value).forEach( ([key, val]) => {
if (key === '_') return
//console.log(key, value)
if (Object.prototype.toString.call(val) === '[object Object]') {
// Recurse if needed
if ( (currdepth < maxdepth) && (val['#']) ) {
gun.get(val['#']).once( function(newval) {
payload[key] = log(txt, newval, maxdepth, currdepth+1)
})
}
} else {
payload[key] = val
}
})
//delete msg.payload._
if (currdepth === 0) {
const msg = {
topic:value['_']['#'],
payload: payload
}
console.log(txt, msg)
return msg
} else {
return payload
}
}