-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple.js
176 lines (144 loc) · 5.43 KB
/
simple.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* This program provides a few simple examples of reading, writing, and
* querying e3db records. For more detailed information, please see the
* documentation home page: https://tozny.com/documentation/e3db/
*
* @copyright Copyright (c) 2017 Tozny, LLC (https://tozny.com)
* @license MIT License
*/
const e3db = require('../dist/index.js')
/**
* ---------------------------------------------------------
* Initialization
* ---------------------------------------------------------
*/
// Configuration values must be set in an immutable configuration object.
// You can use whatever "profiles" or client credentials you want.
let config = new e3db.Config(
process.env.CLIENT_ID,
process.env.API_KEY_ID,
process.env.API_SECRET,
process.env.PUBLIC_KEY,
process.env.PRIVATE_KEY,
process.env.API_URL,
process.env.PUBLIC_SIGN_KEY,
process.env.PRIVATE_SIGN_KEY
)
// Now create a client using that configuration
let client = new e3db.Client(config)
async function main() {
/**
* ---------------------------------------------------------
* Writing a record
* ---------------------------------------------------------
*/
// Create a record by first creating a local version as an object:
let doc = {
name: 'Jon Snow',
whatHeKnows: 'Nothing'
}
// Now encrypt the *value* part of the record, write it to the server and
// the server returns the newly created record:
var record = await client.write('test-contact', doc)
let recordId = record.meta.recordId
console.log('Wrote: ' + recordId)
/**
* ---------------------------------------------------------
* Simple reading and queries
* ---------------------------------------------------------
*/
// Use the new record's unique ID to read the same record again from E3DB:
var newRecord = await client.read(record.meta.recordId)
console.log('Record: ' + newRecord.data.name + ' ' + record.data.whatHeKnows)
// Query for all records of type 'test-contact' and print out
// a little bit of data and metadata.
var data = true
var writer = null
var record = null
var type = 'test-contact'
let queryResult = await client.query(data, writer, record, type).next()
for (let rec of queryResult) {
console.log('Data: ' + rec.data.name + ' ' + rec.data.whatHeKnows)
console.log('Metadata: ' + rec.meta.recordId + ' ' + rec.meta.type)
}
/**
* ---------------------------------------------------------
* Simple sharing by record type
* ---------------------------------------------------------
*/
// Share all of the records of type 'test-contact' with Isaac's client ID:
let isaacClientId = 'db1744b9-3fb6-4458-a291-0bc677dba08b'
await client.share('test-contact', isaacClientId)
/**
* ---------------------------------------------------------
* More complex queries
* ---------------------------------------------------------
*/
// Create some new records of the same type (note that they are also shared
// automatically since they are a type that we have shared above. We
// will also add some "plain" fields that are not secret but can be used
// for efficient querying:
let branData = { name: 'Bran', whatHeKnows: 'Crow' }
let branPlain = { house: 'Stark', ageRange: 'child' }
await client.write('test-contact', branData, branPlain)
let hodorData = { name: 'Hodor', whatHeKnows: 'Hodor' }
let hodorPlain = { house: 'Stark', ageRange: 'adult' }
await client.write('test-contact', hodorData, hodorPlain)
let doranData = { name: 'Doran', whatHeKnows: 'Oberyn' }
let doranPlain = { house: 'Martell', ageRange: 'adult' }
await client.write('test-contact', doranData, doranPlain)
// Create a query that finds everyone from house Stark, but not others:
let queryWesteros = { eq: { name: 'house', value: 'Stark' } }
// Execute that query:
data = true
writer = null
record = null
type = null
queryResult = await client.query(data, writer, record, type, queryWesteros).next()
for (let record of queryResult) {
console.log(record.data.name)
}
// Now create a more complex query with only the adults from house Stark:
queryWesteros = {
and: [
{ eq: { name: 'house', value: 'Stark' } },
{ eq: { name: 'ageRange', value: 'adult' } }
]
}
// Execute that query:
data = true
writer = null
record = null
type = null
queryResult = await client.query(data, writer, record, type, queryWesteros).next()
for (let record of queryResult) {
console.log(record.data.name)
}
/**
* ---------------------------------------------------------
* Learning about other clients
* ---------------------------------------------------------
*/
// Fetch the public key:
let isaacPubKey = await client.clientKey(isaacClientId)
console.log(isaacPubKey)
/**
* ---------------------------------------------------------
* Clean up - Comment these out if you want to experiment
* ---------------------------------------------------------
*/
// Revoke the sharing created by the client.share
await client.revoke('test-contact', isaacClientId)
// Delete the record we created above
await client.delete(recordId)
// Delete all of the records of type test-contact from previous runs:
data = false
writer = null
record = null
type = 'test-contact'
queryResult = await client.query(data, writer, record, type).next()
for (let record of queryResult) {
await client.delete(record.meta.recordId)
}
}
main()