rhizome/src/example-app.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-12-25 16:13:48 -06:00
import Debug from 'debug';
import {RhizomeNode} from "./node";
import {Entity} from "./entity";
2024-12-23 17:29:38 -06:00
import {TypedCollection} from "./typed-collection";
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();
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) => {
debug('User updated:', u);
2024-12-22 09:13:44 -06:00
});
2024-12-23 17:29:38 -06:00
users.onCreate((u: Entity) => {
debug('New user!:', u);
2024-12-21 21:16:18 -06:00
});
2024-12-25 16:13:48 -06:00
await rhizomeNode.start()
2024-12-23 17:29:38 -06:00
const taliesin = users.put(undefined, {
// id: 'taliesin-1',
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) {
debug('Result matches expected: ' + JSON.stringify(taliesin));
2024-12-22 14:38:01 -06:00
} else {
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
})();