2024-12-25 16:13:48 -06:00
|
|
|
import Debug from 'debug';
|
2024-12-27 13:43:43 -06:00
|
|
|
import {RhizomeNode} from "../src/node";
|
|
|
|
import {Entity} from "../src/entity";
|
|
|
|
import {TypedCollection} from "../src/typed-collection";
|
2024-12-24 13:41:31 -06:00
|
|
|
const debug = Debug('example-app');
|
2024-12-21 21:16:18 -06:00
|
|
|
|
|
|
|
// As an app we want to be able to write and read data.
|
|
|
|
// The data is whatever shape we define it to be in a given context.
|
|
|
|
// So we want access to an API that is integrated with our declarations of
|
|
|
|
// e.g. entities and their properties.
|
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
type User = {
|
2024-12-21 21:16:18 -06:00
|
|
|
id?: string;
|
|
|
|
name: string;
|
|
|
|
nameLong?: string;
|
|
|
|
email?: string;
|
|
|
|
age: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
(async () => {
|
2024-12-25 16:13:48 -06:00
|
|
|
const rhizomeNode = new RhizomeNode();
|
2024-12-26 16:52:46 -06:00
|
|
|
const users = new TypedCollection<User>("user");
|
2024-12-25 16:13:48 -06:00
|
|
|
users.rhizomeConnect(rhizomeNode);
|
2024-12-21 21:16:18 -06:00
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
users.onUpdate((u: Entity) => {
|
2024-12-24 13:41:31 -06:00
|
|
|
debug('User updated:', u);
|
2024-12-22 09:13:44 -06:00
|
|
|
});
|
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
users.onCreate((u: Entity) => {
|
2024-12-24 13:41:31 -06:00
|
|
|
debug('New user!:', u);
|
2024-12-21 21:16:18 -06:00
|
|
|
});
|
|
|
|
|
2024-12-27 13:43:43 -06:00
|
|
|
await rhizomeNode.start();
|
2024-12-25 16:13:48 -06:00
|
|
|
|
2024-12-27 13:43:43 -06:00
|
|
|
// Let's use the rhizomic database for some more things.
|
|
|
|
// Like what?
|
|
|
|
// - Logging
|
|
|
|
// - Chat
|
|
|
|
//
|
2024-12-25 16:13:48 -06:00
|
|
|
|
2024-12-27 13:43:43 -06:00
|
|
|
const taliesin = await users.put(undefined, {
|
|
|
|
id: 'taliesin-1',
|
2024-12-23 17:29:38 -06:00
|
|
|
name: 'Taliesin',
|
|
|
|
nameLong: 'Taliesin (Ladd)',
|
|
|
|
age: Math.floor(Math.random() * 1000)
|
|
|
|
});
|
|
|
|
|
2024-12-21 21:16:18 -06:00
|
|
|
// TODO: Allow configuration regarding read/write concern i.e.
|
|
|
|
// if we perform a read immediately do we see the value we wrote?
|
|
|
|
// Intuition says yes, we want that-- but how do we expose the propagation status?
|
|
|
|
|
2024-12-23 17:29:38 -06:00
|
|
|
const result = users.get(taliesin.id);
|
2024-12-21 21:16:18 -06:00
|
|
|
const matches: boolean = JSON.stringify(result) === JSON.stringify(taliesin);
|
2024-12-22 14:38:01 -06:00
|
|
|
if (matches) {
|
2024-12-24 13:41:31 -06:00
|
|
|
debug('Result matches expected: ' + JSON.stringify(taliesin));
|
2024-12-22 14:38:01 -06:00
|
|
|
} else {
|
2024-12-24 13:41:31 -06:00
|
|
|
debug(`Result does not match expected.` +
|
2024-12-22 14:38:01 -06:00
|
|
|
`\n\nExpected \n${JSON.stringify(taliesin)}` +
|
|
|
|
`\nReceived\n${JSON.stringify(result)}`);
|
|
|
|
}
|
2024-12-21 21:16:18 -06:00
|
|
|
|
|
|
|
})();
|
|
|
|
|