Skip to content

Commit 286448b

Browse files
committed
Finish porting Data Modules docs
1 parent 1f3da93 commit 286448b

File tree

4 files changed

+77
-109
lines changed

4 files changed

+77
-109
lines changed

docs/data-modules/defining-a-module.md

+20-18
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@
33
A data module is just a JavaScript object containing a module name and a
44
builder function.
55

6-
The builder function receives two
7-
`base clients </js-api/base-client>`{.interpreted-text role="doc"} when
8-
loaded: one for private data stored in `/my-module-name/` and one for
9-
public data stored in `/public/my-module-name/`. It must return an
10-
object, defining the properties and functions to be used in the app as
11-
`exports`:
6+
The builder function receives two [BaseClient][2] instances when loaded: one
7+
for private data stored in `/my-module-name/` and one for public data stored in
8+
`/public/my-module-name/`. A module must return an object, with the properties
9+
and functions to be used in an app as `exports`:
1210

1311
``` javascript
14-
var Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) {
12+
const Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) {
1513
return {
1614
exports: {
17-
addBookmark: function() {}
15+
addBookmark: function() {}
1816
}
1917
}
2018
}};
2119
```
2220

23-
You can then load it into your
24-
`RemoteStorage </js-api/remotestorage>`{.interpreted-text role="doc"}
25-
instance either on initialization, or later using the `addModule()`
26-
function:
21+
You can then load the module into your [RemoteStorage][1] instance, either on
22+
initialization or later using the `addModule()` function:
2723

28-
const remoteStorage = new RemoteStorage({ modules: [ Bookmarks ] });
24+
```js
25+
const remoteStorage = new RemoteStorage({ modules: [ Bookmarks ] });
2926

30-
// or later:
27+
// or later:
28+
remoteStorage.addModule(Bookmarks);
29+
```
3130

32-
remoteStorage.addModule(Bookmarks);
31+
The module will then be accessible on the instance by its name, allowing
32+
you to call the functions and properties that it exports:
3333

34-
It will then be available on the instance as its module name, allowing
35-
you to call the functions and properties that the module exports:
34+
```js
35+
remoteStorage.bookmarks.addBookmark()
36+
```
3637

37-
remoteStorage.bookmarks.addBookmark();
38+
[1]: ../api/remotestorage/classes/RemoteStorage.html
39+
[2]: ../api/baseclient/classes/BaseClient.html
+39-43
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# Defining data types
22

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.
79

810
Consider this simplified example of an archive bookmark:
911

10-
``` javascript
11-
var Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) {
12+
```js
13+
const Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) {
1214

1315
privateClient.declareType('archive-bookmark', {
1416
"type": "object",
@@ -40,48 +42,42 @@ can add a function for storing them. This will actually validate the
4042
incoming data against the type\'s schema, and reject the promise with
4143
detailed validation errors in case the data format doesn\'t match:
4244

43-
var Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) {
44-
// ...
45+
```js
46+
const Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) {
47+
// ...
4548

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
4854

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+
};
5262

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:
5864

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+
```
8177
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
8380
about it, check out the free e-book [Understanding JSON
8481
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)
8783
:::

docs/data-modules/index.md

+11-13
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,22 @@ Traditional Web apps make this possible with custom, proprietary APIs,
88
which are dependent on the app provider. Third party developers usually
99
need to register an application with the original provider to access
1010
that data via the API, and this access can be revoked at any time.
11-
remoteStorage and [unhosted web apps](https://unhosted.org) in general
12-
give end users ultimate control over which apps have access to their
13-
data.
1411

15-
In order to make it easy and safe for your app data to be compatible
16-
with other apps, we created the concept of data modules for rs.js, which
17-
can be shared and developed collaboratively in the open.
12+
remoteStorage apps (and [unhosted web apps](https://unhosted.org) in general)
13+
give end users ultimate control over which apps have access to their data. This
14+
makes users more independent from single app providers, and ensures that any
15+
app developer can create new apps for users' existing data.
16+
17+
In order to make it easy and safe for your app data to be compatible with other
18+
apps, we created the concept of data modules for remoteStorage.js. They are
19+
little add-on libraries, which can be shared between apps and developers, and
20+
that ideally are developed collaboratively in the open.
1821

1922
Data modules can contain as much or as little functionality as desired,
20-
from defining data formats and types to data validation, formatting,
23+
from defining data formats and types, to data validation, formatting,
2124
processing, transformation, encryption, indexing, and other use cases.
2225

2326
Data modules make your app and its data interoperable with other apps.
24-
Sharing your modules is generally encouraged, but it is also possible to
27+
Sharing your modules is generally encouraged, but it's also possible to
2528
encapsulate custom functionality in a custom module made just for your
2629
app.
27-
28-
::: {.toctree caption="Contents" maxdepth="2"}
29-
data-modules/defining-a-module data-modules/defining-data-types
30-
data-modules/publishing-and-finding-modules
31-
:::

docs/data-modules/publishing-and-finding-modules.md

+7-35
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,23 @@ The recommended way for publishing data modules is as
88
Our naming convention for rs.js modules is
99
`remotestorage-module-mymodulename`. Thus, you can also find them by
1010
[searching npm for
11-
\"remotestorage-module\"](https://www.npmjs.com/search?q=remotestorage-module).
11+
"remotestorage-module"](https://www.npmjs.com/search?q=remotestorage-module).
1212

13-
You can also add \"remotestorage-module\" and \"remotestorage\" to the
13+
You can also add "remotestorage-module" and "remotestorage" to the
1414
`keywords` property of your `package.json`.
1515

1616
## GitHub & Co.
1717

1818
If you use GitHub ‒ or any other code hosting/collaboration platform for
19-
that matter ‒ for publishing your module\'s source code, please use the
19+
that matter ‒ for publishing your module's source code, please use the
2020
same naming convention as for the npm module for the repo name. And
21-
it\'s a good idea to add the topic/tag/label \"remotestorage-module\"
21+
it's a good idea to add the topic/tag/label "remotestorage-module"
2222
there as well, of course.
2323

2424
<https://github.com/topics/remotestorage-module>
2525

26-
::: hint
27-
::: title
28-
Hint
29-
:::
30-
31-
With npm, you can also install modules directly from a Git repo or
26+
::: tip
27+
With npm, you can also install modules directly from a Git repository or
3228
GitHub, pointing to just the repo or a branch name, tag, or commit:
33-
<https://docs.npmjs.com/files/package.json#github-urls>
34-
:::
35-
36-
## Examples
37-
38-
- For a real-world example of a data module package, see e.g. the
39-
shares module [on
40-
GitHub](https://github.com/skddc/remotestorage-module-shares) and
41-
[on npm](https://www.npmjs.com/package/remotestorage-module-shares).
42-
Check out `webpack.config.js` and the source code in `index.js` to
43-
see how it is built and exported.
44-
45-
::: note
46-
::: title
47-
Note
48-
:::
49-
50-
Unfortunately, we didn\'t have any package management for data modules
51-
before rs.js 1.0. To be fair, JavaScript package managers weren\'t
52-
actually a thing yet, when this functionality was added to the library.
53-
However, it means we\'re still in the process of porting and publishing
54-
modules and you won\'t find very many existing data modules on npm right
55-
now. You can check the [old modules
56-
repo](https://github.com/remotestorage/modules) for source code of
57-
legacy modules.
29+
https://docs.npmjs.com/cli/v10/configuring-npm/package-json#github-urls
5830
:::

0 commit comments

Comments
 (0)