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