Skip to content

Commit e791bfb

Browse files
Pollepsjoepio
authored andcommitted
#798 Changed docs to use store.newResource
1 parent 9454cfd commit e791bfb

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

docs/src/js.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,37 @@ npm install @tomic/lib
1313
```
1414

1515
```ts
16-
// Import the Store
17-
import { Store, Agent, urls } from "@tomic/lib";
16+
import { Store, Agent, core } from "@tomic/lib";
1817

19-
const opts = {
18+
// --------- Create a Store ---------.
19+
const store = new Store({
2020
// You can create a secret from the `User settings` page using the AtomicServer UI
2121
agent: Agent.fromSecret("my-secret-key"),
2222
// Set a default server URL
23-
serverUrl: "https://atomicdata.dev",
24-
}
25-
const store = new Store(opts);
26-
27-
// Get a resource
28-
const gotResource = store.getResourceLoading(subject);
29-
const atomString = gotResource!
30-
.get(urls.properties.description)!
31-
.toString();
32-
33-
// Create & save a new resource
34-
const subject = 'https://atomicdata.dev/test';
35-
const newResource = new Resource(subject);
36-
await newResource.set(urls.properties.description, 'Hi world');
37-
newResource.save(store);
23+
serverUrl: "https://my-atomic-server.dev",
24+
});
25+
26+
// --------- Get a resource ---------
27+
const gotResource = await store.getResourceAsync(subject);
28+
29+
const atomString = gotResource.get(core.properties.description)
30+
31+
// --------- Create & save a new resource ---------
32+
const newResource = await store.newResource({
33+
subject: 'https://my-atomic-server.dev/test',
34+
propVals: {
35+
[core.properties.description]: 'Hi World :)'
36+
}
37+
});
38+
39+
await newResource.save(store);
40+
41+
// --------- Subscribe to changes (using websockets) ---------
42+
const unsub = store.subscribe('https://my-atomic-server.dev/test', (resource) => {
43+
// This callback is called each time a change is made to the resource client or serverside.
44+
45+
// Do something with the changed resource...
46+
})
3847
```
3948

4049
## Advanced usage

0 commit comments

Comments
 (0)