diff --git a/plugins/replication/README.md b/plugins/replication/README.md new file mode 100644 index 0000000..3beb774 --- /dev/null +++ b/plugins/replication/README.md @@ -0,0 +1,13 @@ +# Replication Plugin + +Syncs data from an external source to the StarbaseDB instance at regular intervals. + +## Configuration + +Enable it in `src/index.ts` by creating a new instance of `ReplicationPlugin`. + +```typescript +new ReplicationPlugin(cronPlugin) +``` + +It uses the `CronPlugin` to schedule the sync task. diff --git a/plugins/replication/index.ts b/plugins/replication/index.ts new file mode 100644 index 0000000..68f064b --- /dev/null +++ b/plugins/replication/index.ts @@ -0,0 +1,20 @@ +import { Plugin } from '../../src/index' +import { CronPlugin } from '../cron' + +export class ReplicationPlugin implements Plugin { + constructor(private cronPlugin: CronPlugin) {} + + async init() { + this.cronPlugin.addJob('replication', '*/5 * * * *', async () => { + console.log('Running replication task...') + // Logic for syncing external data to internal database + try { + const response = await fetch('https://api.example.com/data') + const data = await response.json() + console.log('Data fetched for replication:', data) + } catch (error) { + console.error('Replication failed:', error) + } + }) + } +} diff --git a/src/index.ts b/src/index.ts index 4d08932..21719aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,7 @@ import { QueryLogPlugin } from '../plugins/query-log' import { StatsPlugin } from '../plugins/stats' import { CronPlugin } from '../plugins/cron' import { InterfacePlugin } from '../plugins/interface' - +import { ReplicationPlugin } from '../plugins/replication' export { StarbaseDBDurableObject } from './do' const DURABLE_OBJECT_ID = 'sql-durable-object' @@ -212,7 +212,7 @@ export default { const interfacePlugin = new InterfacePlugin() const plugins = [ - webSocketPlugin, + webSocketPlugin,new ReplicationPlugin(cronPlugin), new StudioPlugin({ username: env.STUDIO_USER, password: env.STUDIO_PASS,