Files
hub-monorepo/packages/hub-nodejs/examples/replicate-data-postgres/index.ts
Shane da Silva cc2f5a4fae Add syncing/replication example for Postgres (#938)
Provide a working end-to-end example of syncing data from hubs to a
Postgres database.

This should work with no additional dependencies besides what you
install with `yarn install` and Docker.
2023-05-05 09:56:48 -07:00

58 lines
1.4 KiB
TypeScript

import { HubReplicator } from './hubReplicator';
import { getDbClient, migrateToLatest } from './db';
import { log } from './log';
/**
* Populate the following constants with your own values.
*
* If you're running this from the examples directory, make sure you follow the
* README.
*/
const HUB_URL = 'nemes.farcaster.xyz:2283'; // URL of the Hub
const HUB_SSL = true; // Change if your hub isn't using SSL/TLS
const POSTGRES_URL = 'postgres://app:password@localhost:6543/hub';
const db = getDbClient(POSTGRES_URL);
let replicator: HubReplicator | undefined;
const shutdown = async () => {
if (replicator) {
await replicator.stop();
await replicator.destroy();
}
if (db) {
await db.destroy();
}
};
process.on('exit', (code) => {
log.info(`Exiting process with status code ${code}`);
});
for (const signal of ['SIGTERM', 'SIGINT']) {
process.once(signal, (signalName: string) => {
log.info(`Process received ${signalName}`);
process.exitCode =
// eslint-disable-next-line security/detect-object-injection
{
SIGINT: 130,
SIGTERM: 143,
}[signalName] || 1;
shutdown();
});
}
(async () => {
// Create DB tables
const result = await migrateToLatest(db, log);
if (result.isErr()) {
process.exit(1);
}
replicator = new HubReplicator(HUB_URL, HUB_SSL, db, log);
replicator.start();
})();