rhizome/examples/app.ts

106 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-12-25 16:13:48 -06:00
import Debug from 'debug';
import {RhizomeNode} from "../src/node";
import {Entity} from "../src/entity";
2024-12-29 14:35:30 -06:00
import {Collection} from "../src/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();
2024-12-29 17:50:20 -06:00
// Enable API to read lossless view
rhizomeNode.httpServer.httpApi.serveLossless();
2024-12-29 14:35:30 -06:00
const users = new Collection("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
});
await rhizomeNode.start();
2024-12-25 16:13:48 -06:00
// Let's use the rhizomic database for some more things.
// Like what?
// - Logging
// - Chat
//
2024-12-29 14:35:30 -06:00
const taliesinData: User = {
id: 'taliesin-1',
2024-12-23 17:29:38 -06:00
name: 'Taliesin',
nameLong: 'Taliesin (Ladd)',
age: Math.floor(Math.random() * 1000)
2024-12-29 14:35:30 -06:00
};
const taliesinPutResult = await users.put(undefined, taliesinData);
{
const result = JSON.stringify(taliesinPutResult);
const expected = JSON.stringify(taliesinData);
if (result === expected) {
debug('Put result matches expected: ' + expected);
} else {
debug(`Put result does not match expected.` +
`\n\nExpected \n${expected}` +
`\nReceived\n${result}`);
}
}
2024-12-23 17:29:38 -06:00
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-29 14:35:30 -06:00
const resolved = users.resolve('taliesin-1');
if (!resolved) throw new Error('unable to resolve entity we just created');
2024-12-29 17:50:20 -06:00
debug('resolved', resolved);
2024-12-29 14:35:30 -06:00
const resolvedUser = {
id: resolved.id,
...resolved.properties
} as User;
/*
function sortKeys (o: {[key: string]: unknown}): {[key: string]: unknown} {
const r: {[key: string]: unknown} = {};
r.id = o.id;
Object.keys(o).sort().forEach((key) => {
if (key === "id") return;
r[key] = o[key];
})
return r;
}
*/
const result = JSON.stringify(resolvedUser);
const expected = JSON.stringify(taliesinData);
if (result === expected) {
debug('Get result matches expected: ' + expected);
2024-12-22 14:38:01 -06:00
} else {
2024-12-29 14:35:30 -06:00
debug(`Get result does not match expected.` +
`\n\nExpected \n${expected}` +
`\nReceived\n${result}`);
2024-12-22 14:38:01 -06:00
}
2024-12-21 21:16:18 -06:00
2024-12-29 14:35:30 -06:00
2024-12-21 21:16:18 -06:00
})();