diff --git a/src/crontabs/update-prices.ts b/src/crontabs/update-prices.ts index 97f0950..3129bdf 100644 --- a/src/crontabs/update-prices.ts +++ b/src/crontabs/update-prices.ts @@ -7,7 +7,7 @@ let isRunning = false, blockRunning = false export default new Crontab() .cron(env.LOG_LEVEL === 'debug' ? '*/3 * * * * *' : '0 */6 * * *') .listen(async(ctx) => { - if (isRunning || blockRunning) return + if (isRunning || blockRunning || !ctx.env.UPDATE_PRICES) return isRunning = true if (env.LOG_LEVEL === 'debug') blockRunning = true @@ -62,7 +62,7 @@ export default new Crontab() } if (JSON.stringify(platforms) !== JSON.stringify(extension.platforms)) { - await ctx.database.update(ctx.database.schema.extensions) + await ctx.database.write.update(ctx.database.schema.extensions) .set({ platforms }) diff --git a/src/globals/database.ts b/src/globals/database.ts index cb2c4b0..e4b72ed 100644 --- a/src/globals/database.ts +++ b/src/globals/database.ts @@ -8,10 +8,18 @@ const pool = new Pool({ connectionString: env.DATABASE_URL }) +const writePool = env.DATABASE_URL_PRIMARY ? new Pool({ + connectionString: env.DATABASE_URL_PRIMARY +}) : pool + const db = drizzle(pool, { schema }), + writeDb = drizzle(writePool, { schema }), startTime = performance.now() -db.$client.connect().then(() => { +Promise.all([ + db.$client.connect(), + env.DATABASE_URL_PRIMARY ? writeDb.$client.connect() : Promise.resolve() +]).then(() => { logger() .text('Database', (c) => c.cyan) .text('Connection established!') @@ -19,7 +27,10 @@ db.$client.connect().then(() => { .info() }) -export default Object.assign(db, { +type DbWithoutWrite = Omit + +export default Object.assign(db as DbWithoutWrite, { + write: writeDb, schema, fields: Object.freeze({ diff --git a/src/globals/env.ts b/src/globals/env.ts index f6451da..4d473e2 100644 --- a/src/globals/env.ts +++ b/src/globals/env.ts @@ -14,6 +14,9 @@ try { const infos = z.object({ DATABASE_URL: z.string(), + DATABASE_URL_PRIMARY: z.string().optional(), + + UPDATE_PRICES: z.enum(['true', 'false']).transform((str) => str === 'true').default('false'), PORT: z.string().transform((str) => parseInt(str)).optional(), INTERNAL_KEY: z.string(), diff --git a/src/globals/telemetry.ts b/src/globals/telemetry.ts index e25d7b7..916e550 100644 --- a/src/globals/telemetry.ts +++ b/src/globals/telemetry.ts @@ -50,7 +50,7 @@ const process = async(): Promise => { const panels = new Set(telemetry.map((t) => t.panelId)) - await Promise.all(Array.from(panels).map((id) => cache.use(`panel:${id}`, () => database.insert(schema.telemetryPanels) + await Promise.all(Array.from(panels).map((id) => cache.use(`panel:${id}`, () => database.write.insert(schema.telemetryPanels) .values({ id, version: telemetry.find((t) => t.panelId === id)!.version }) .onConflictDoUpdate({ target: schema.telemetryPanels.id, @@ -60,7 +60,7 @@ const process = async(): Promise => { }) ))) - await database.insert(schema.telemetryData) + await database.write.insert(schema.telemetryData) .values(telemetry.map((t) => Object.assign(t, { ip: string.hash(t.ip, { algorithm: 'sha256' }) }))) .onConflictDoNothing() } catch (err) {