|
1 | 1 | # Defining data types
|
2 | 2 |
|
3 |
| -Data types can be defined using the `declareType()` method. It expects a |
4 |
| -name (which you can later use with `storeObject()`), as well as a [JSON |
5 |
| -Schema](http://json-schema.org) object defining the actual structure and |
6 |
| -formatting of your data. |
| 3 | +Data types can be defined using the |
| 4 | +[declareType()](../api/baseclient/classes/BaseClient.html#declaretype) method. |
| 5 | +It expects a name (which you can later use with |
| 6 | +[storeObject()](../api/baseclient/classes/BaseClient.html#storeobject), as well |
| 7 | +as a [JSON Schema](http://json-schema.org) object defining the actual structure |
| 8 | +and formatting of your data. |
7 | 9 |
|
8 | 10 | Consider this simplified example of an archive bookmark:
|
9 | 11 |
|
10 |
| -``` javascript |
11 |
| -var Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) { |
| 12 | +```js |
| 13 | +const Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) { |
12 | 14 |
|
13 | 15 | privateClient.declareType('archive-bookmark', {
|
14 | 16 | "type": "object",
|
@@ -40,48 +42,42 @@ can add a function for storing them. This will actually validate the
|
40 | 42 | incoming data against the type\'s schema, and reject the promise with
|
41 | 43 | detailed validation errors in case the data format doesn\'t match:
|
42 | 44 |
|
43 |
| - var Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) { |
44 |
| - // ... |
| 45 | +```js |
| 46 | +const Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) { |
| 47 | +// ... |
45 | 48 |
|
46 |
| - return { |
47 |
| - exports: { |
| 49 | +return { |
| 50 | + exports: { |
| 51 | + add: function (bookmark) { |
| 52 | + bookmark.id = md5Hash(bookmark.url); // hash URL for nice ID |
| 53 | + var path = "archive/" + bookmark.id; // use hashed URL as filename as well |
48 | 54 |
|
49 |
| - add: function (bookmark) { |
50 |
| - bookmark.id = md5Hash(bookmark.url); // hash URL for nice ID |
51 |
| - var path = "archive/" + bookmark.id; // use hashed URL as filename as well |
| 55 | + return privateClient.storeObject("archive-bookmark", path, bookmark). |
| 56 | + then(function() { |
| 57 | + return bookmark; // return bookmark with added ID property |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | +}; |
52 | 62 |
|
53 |
| - return privateClient.storeObject("archive-bookmark", path, bookmark). |
54 |
| - then(function() { |
55 |
| - return bookmark; // return bookmark with added ID property |
56 |
| - }); |
57 |
| - } |
| 63 | +// and in your app: |
58 | 64 |
|
59 |
| - } |
60 |
| - } |
61 |
| - }}; |
62 |
| - |
63 |
| - // and in your app: |
64 |
| - |
65 |
| - remoteStorage.bookmarks.add({ |
66 |
| - title: 'Unhosted Web Apps', |
67 |
| - url: 'https://unhosted.org', |
68 |
| - tags: ['unhosted', 'remotestorage', 'offline-first'] |
69 |
| - }) |
70 |
| - .then(() => { |
71 |
| - console.log('stored bookmark successfully'); |
72 |
| - }) |
73 |
| - .catch((err) => { |
74 |
| - console.error('validation error:', err); |
75 |
| - }); |
76 |
| - |
77 |
| -::: hint |
78 |
| -::: title |
79 |
| -Hint |
80 |
| -::: |
| 65 | +remoteStorage.bookmarks.add({ |
| 66 | + title: 'Unhosted Web Apps', |
| 67 | + url: 'https://unhosted.org', |
| 68 | + tags: ['unhosted', 'remotestorage', 'offline-first'] |
| 69 | +}) |
| 70 | +.then(() => { |
| 71 | + console.log('stored bookmark successfully'); |
| 72 | +}) |
| 73 | +.catch((err) => { |
| 74 | + console.error('validation error:', err); |
| 75 | +}); |
| 76 | +``` |
81 | 77 |
|
82 |
| -JSON Schema is very powerful and flexible. If you want to learn more |
| 78 | +::: tip |
| 79 | +JSON Schema is rather powerful and flexible. If you want to learn more |
83 | 80 | about it, check out the free e-book [Understanding JSON
|
84 | 81 | Schema](https://spacetelescope.github.io/understanding-json-schema/) for
|
85 |
| -example. The complete official specs can be found at |
86 |
| -<http://json-schema.org/documentation.html> |
| 82 | +example. The complete documentation can be found on [json-schema.org](https://json-schema.org) |
87 | 83 | :::
|
0 commit comments