UNPKG

6.74 kBMarkdownView Raw
1Getting Started
2================
3
4The Browser
5-------
6
7A ready to go minified version of the library is available in the [/dist][dist]
8directory on [github][github] along with a sourcemap. Or you you can download it here.
9
10> [Download >][download]
11
12Once you have the youbase.min.js included in your project load it on your page
13as you would any library.
14
15``` html
16<script src="/js/youbase.min.js"></script>
17```
18
19Once the script loads you will have access to YouBase exposed in the global scope.
20
21``` javascript
22var youbase = YouBase('http://localhost:9090');
23```
24
25NPM Install
26-----------
27
28To use YouBase with Node.js or browserify you can install it via NPM.
29
30``` shell
31> npm install -g youbase
32```
33
34Once installed you also have access to the command line tool.
35
36``` shell
37> youbase help
38```
39
40Custodians
41----------
42
43YouBase relies on custodians to provide storage to the network. Custodians can
44use any storage method on the backend that supports key/value lookup.
45
46### Local Custodian
47
48Using the command line tool included in the npm package a custodian can
49be run locally for development. By default the custodian will run on port 9090
50and uses a in memory storage engine that does not persist to disk.
51
52``` shell
53> youbase api
54```
55
56### Connecting
57
58Once your custodian is up and running it is time to connect. We do this using
59the YouBase class that is available globally when the script is loaded in the
60browser and is the default export of the NPM package.
61
62``` javascript
63var YouBase = require('youbase');
64```
65
66Once you have the YouBase class you can create a new instance by passing it the
67url of the custodian.
68
69``` javascript
70var youbase = YouBase('http://localhost:9090');
71```
72
73Document Definition
74-------------------
75
76A Document Definition tells YouBase how the data in a document should be
77structured, how it should be encrypted, and what to index.
78
79### Permissions
80
81YouBase has three levels of permissions that control how a document is
82encrypted. The lowest level is **public** which leaves the data unencrypted.
83
84``` json
85"permissions": "public"
86```
87
88The second permission level is hardened. This encrypts the data using the
89documents extended public key as the encryption key.
90
91``` json
92"permissions": "hardened"
93```
94
95Last we have private which encrypts the data using the documents private key as
96the encryption key.
97
98``` json
99"permissions": "private"
100```
101
102### Meta
103
104Meta describes which attributes should be available without
105retrieving and decrypting the documents data. The meta information is pulled
106from the document data and stored unencrypted. Since it is unencrypted it should
107always be considered public.
108
109``` json
110"meta": {
111 "height": "height",
112 "weight": "weight"
113}
114```
115
116The attributes can also be described using dot notation in order to pull data from
117nested attributes.
118
119``` json
120"meta": {
121 "height": "stats.height",
122 "weight": "stats.weight"
123}
124```
125
126### Form
127
128Form gives hints on how a UI should create a form. It is based on
129[schemaform.io](http://schemaform.io) and combined with the schema information
130let's us automatically generate an interface for any document.
131
132``` json
133"form": ['*']
134```
135
136### Schema
137
138[JSON Schema](http://json-schema.org/) is used to define the structure of a
139document and validate it. This lets YouBase plug into existing schemas such as
140[Open mHealth](http://www.openmhealth.org/documentation/#/schema-docs/schema-library)
141with little change.
142
143``` json
144"schema": {
145 "title": "Health Profile",
146 "type": "object",
147 "properties": {
148 "height": {
149 "title": "Height",
150 "type": "number"
151 },
152 "weight": {
153 "title": "Weight",
154 "type": "number"
155 }
156 }
157}
158```
159
160### Children
161
162In YouBase a Document both stores data and has children documents allowing for
163rich structured data. Every Definition includes a list of children definitions
164along with a name that can be used when inserting children documents.
165
166``` json
167"children": {
168 "allergy": {
169 "permissions": "public",
170 "meta": {},
171 "form": ['*'],
172 "schema": {
173 "title": "Allergy",
174 "type": "object",
175 "properties": {
176 "allergen": {
177 "title": "Allergen",
178 "type": "string"
179 }
180 },
181 "required": ["allergen"]
182 }
183 }
184}
185```
186
187Wallet
188------
189
190In the real world we store much more in our wallets than just money. We have
191IDs, insurance cards, pictures, etc. In the same way YouBase lets you store
192the personal data that you want to keep close in a digital wallet.
193
194To use your digital wallet it must first be created.
195
196``` javascript
197var wallet = youbase.wallet();
198```
199
200When created a YouBase wallet generates a random 12 word mnemonic passphrase that is used
201to generate all the rest of the keys in your wallet. If this passphrase is lost
202you will lose all your data, so write it down.
203
204```javascript
205var passphrase = wallet.mnemonic;
206```
207
208Once you have your passphrase you can use it to recreate your wallet.
209
210```javascript
211var wallet = youbase.wallet(passphrase);
212```
213
214Collection
215----------
216
217A Collection is a group of Documents all under the same parent key. A wallet
218starts out with a Collection called profiles.
219
220```javascript
221wallet.profiles;
222```
223
224Before you can add a document to a collection you must give it a definition describing the
225document.
226
227```javascript
228var HealthProfile = require('youbase-health-profile-definition');
229wallet.profiles.definition('health', HealthProfile);
230```
231
232With a definition in place we can now insert a document.
233
234```javascript
235var healthProfile = wallet.profiles.insert('health', {heightt: 72, weight: 200});
236```
237
238And get an array of all documents in a collection.
239
240```javascript
241wallet.profiles.all().then(function (profiles) { console.log(profiles; )});
242```
243
244Document
245--------
246
247Each document corresponds to a public key / private key pair in the wallet.
248Unsing a document instance you can get the documents meta information as well as
249pull the full document data..
250
251``` javascript
252healthProfile.meta().then(function (meta) { console.log(meta); });
253healthProfile.data().then(function (data) { console.log(data); });
254```
255
256If you have the private key you will be able to update the record
257
258``` javascript
259healthProfile.data({height: 73, weight: 200});
260healthProfile.save();
261```
262
263To make sure you have the recent version of a document fetch the document from
264the custodian.
265
266``` javascript
267healthProfile.fetch();
268```
269
270When you have the extended key you can also access all the children documents
271through a collection.
272
273``` javascript
274healthProfile.children.all();
275```
276
277Messages
278--------
279
280**(in-progress)**
281
282[download]: https://raw.githubusercontent.com/YouBase/youbase/master/dist/youbase.min.js
283[github]: https://github.com/YouBase/youbase
284[dist]: https://github.com/YouBase/youbase/tree/master/dist