@tomic/lib: The Atomic Data library for typescript/javascript
Core typescript library for fetching data, handling JSON-AD parsing, storing data, signing Commits, setting up WebSockets and full-text search and more.
Runs in most common JS contexts like the browser, node, bun etc.
Installation
npm install @tomic/lib
TL;DR
Create a Store
import { Store, Agent, core } from '@tomic/lib';
const store = new Store({
// You can create a secret from the `User settings` page using the AtomicServer UI
agent: Agent.fromSecret('my-secret-key'),
// Set a default server URL
serverUrl: 'https://my-atomic-server.dev',
});
Fetching a resource and reading its data
// When the class is known.
const resource = await store.getResource<Person>('https://my-atomic-server.dev/some-resource');
const job = resource.props.job;
// When the class is unknown
const resource = await store.getResource('https://my-atomic-server.dev/some-resource');
const job = resource.get(myOntology.properties.job);
Editing a resource
resource.set(core.properties.description, 'Hello World');
// Commit the changes to the server.
await resource.save();
Creating a new resource
const newResource = await store.newResource({
isA: myOntology.classes.person,
propVals: {
[core.properties.name]: 'Jeff',
},
});
// Commit the new resource to the server.
await newResource.save();
Subscribing to changes
// --------- Subscribe to changes (using websockets) ---------
const unsub = store.subscribe('https://my-atomic-server.dev/some-resource', resource => {
// This callback is called each time a change is made to the resource on the server.
// Do something with the changed resource...
});
What's next?
Next check out Store to learn how to set up a store and fetch data. Or read the Generated Typedocs
If you rather want to see a step-by-step guide on how to use the library in a project check out the Astro + AtomicServer Guide