Setup sync_runs table
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
import { Kysely, PostgresDialect } from 'kysely';
|
||||||
|
import { type DB } from 'kysely-codegen';
|
||||||
|
import { Pool } from "pg";
|
||||||
|
import { env } from '../util';
|
||||||
|
|
||||||
|
export const db = new Kysely<DB>({
|
||||||
|
dialect: new PostgresDialect({
|
||||||
|
pool: new Pool({
|
||||||
|
connectionString: env.DATABASE_URL,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,12 +1,28 @@
|
|||||||
import { Kysely, PostgresDialect } from 'kysely';
|
import { CreateTableBuilder, sql } from "kysely";
|
||||||
import { type DB } from 'kysely-codegen';
|
import { db } from "./db";
|
||||||
import { Pool } from "pg";
|
import { log } from "../util";
|
||||||
import { env } from '../index';
|
|
||||||
|
|
||||||
const db = new Kysely<DB>({
|
// =====================
|
||||||
dialect: new PostgresDialect({
|
// SYNC RUNS
|
||||||
pool: new Pool({
|
// =====================
|
||||||
connectionString: env.DATABASE_URL,
|
{
|
||||||
}),
|
log.info({ name: "sync_runs" }, "Creating table");
|
||||||
})
|
await db.schema.dropTable("sync_runs").ifExists().execute()
|
||||||
});
|
await db.schema.createTable("sync_runs")
|
||||||
|
.$call(addDefaultColumns)
|
||||||
|
.addColumn("completed_at", "timestamptz")
|
||||||
|
.execute()
|
||||||
|
}
|
||||||
|
|
||||||
|
function addDefaultColumns(ctb: CreateTableBuilder<any, any>) {
|
||||||
|
return ctb
|
||||||
|
.addColumn("id", "bigint", (cb) => cb
|
||||||
|
.generatedAlwaysAsIdentity()
|
||||||
|
.primaryKey()
|
||||||
|
)
|
||||||
|
.addColumn("created_at", "timestamptz", (cb) => cb
|
||||||
|
.notNull()
|
||||||
|
.defaultTo(sql`now()`)
|
||||||
|
.unique()
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|||||||
+1
-40
@@ -1,28 +1,7 @@
|
|||||||
import cluster from 'node:cluster'
|
import cluster from 'node:cluster'
|
||||||
import os from 'node:os'
|
import os from 'node:os'
|
||||||
import process from 'node:process'
|
import process from 'node:process'
|
||||||
import { pino } from 'pino';
|
import { log } from "./util";
|
||||||
|
|
||||||
export const log = pino({
|
|
||||||
level: Bun.env.LOG_LEVEL ?? "info",
|
|
||||||
transport: Bun.env.NODE_ENV != "production"
|
|
||||||
? { target: "pino-pretty" }
|
|
||||||
: undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
export const env = {
|
|
||||||
PRINTFUL_AUTH: requireEnv("PRINTFUL_AUTH"),
|
|
||||||
PRINTFUL_STORE_ID: requireEnv("PRINTFUL_STORE_ID"),
|
|
||||||
WEBFLOW_SITE_ID: requireEnv("WEBFLOW_SITE_ID"),
|
|
||||||
WEBFLOW_COLLECTION_ID: requireEnv("WEBFLOW_COLLECTION_ID"),
|
|
||||||
WEBFLOW_AUTH: requireEnv("WEBFLOW_AUTH"),
|
|
||||||
WEBFLOW_WEBHOOK_SECRET: requireEnv("WEBFLOW_WEBHOOK_SECRET"),
|
|
||||||
AUTH_SECRET: requireEnv("AUTH_SECRET"),
|
|
||||||
ADMIN_PASSWORD: requireEnv("ADMIN_PASSWORD"),
|
|
||||||
DATABASE_URL: requireEnv("DATABASE_URL"),
|
|
||||||
NODE_ENV: optionEnv("NODE_ENV", "development"),
|
|
||||||
LOG_LEVEL: optionEnv("LOG_LEVEL", "info"),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (cluster.isPrimary) {
|
if (cluster.isPrimary) {
|
||||||
for (let i = 0; i < os.availableParallelism(); i++)
|
for (let i = 0; i < os.availableParallelism(); i++)
|
||||||
@@ -31,21 +10,3 @@ if (cluster.isPrimary) {
|
|||||||
await import('./server')
|
await import('./server')
|
||||||
log.info(`Worker ${process.pid} started`)
|
log.info(`Worker ${process.pid} started`)
|
||||||
}
|
}
|
||||||
|
|
||||||
function requireEnv(key: string): string {
|
|
||||||
const val = Bun.env[key];
|
|
||||||
if (!val) {
|
|
||||||
log.error({ name: key }, "Missing required environment variable");
|
|
||||||
throw new Error(`Missing required environment variable: ${key}`)
|
|
||||||
};
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
function optionEnv(key: string, fallback: string = ""): string {
|
|
||||||
const val = Bun.env[key];
|
|
||||||
if (!val) {
|
|
||||||
log.warn({ name: key }, `Missing optional environmental variable, falling back to "${fallback}"`)
|
|
||||||
return fallback
|
|
||||||
};
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import {
|
|||||||
Webflow,
|
Webflow,
|
||||||
} from "@blade-and-brawn/commerce";
|
} from "@blade-and-brawn/commerce";
|
||||||
import zipcodesUs from "zipcodes-us";
|
import zipcodesUs from "zipcodes-us";
|
||||||
import { env, log } from "./index";
|
import { env, log } from "./util";
|
||||||
import serverTiming from "@elysia/server-timing";
|
import serverTiming from "@elysia/server-timing";
|
||||||
import jwt from "@elysia/jwt";
|
import jwt from "@elysia/jwt";
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import { pino } from 'pino';
|
||||||
|
|
||||||
|
export const log = pino({
|
||||||
|
level: Bun.env.LOG_LEVEL ?? "info",
|
||||||
|
transport: Bun.env.NODE_ENV != "production"
|
||||||
|
? { target: "pino-pretty" }
|
||||||
|
: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const env = {
|
||||||
|
PRINTFUL_AUTH: requireEnv("PRINTFUL_AUTH"),
|
||||||
|
PRINTFUL_STORE_ID: requireEnv("PRINTFUL_STORE_ID"),
|
||||||
|
WEBFLOW_SITE_ID: requireEnv("WEBFLOW_SITE_ID"),
|
||||||
|
WEBFLOW_COLLECTION_ID: requireEnv("WEBFLOW_COLLECTION_ID"),
|
||||||
|
WEBFLOW_AUTH: requireEnv("WEBFLOW_AUTH"),
|
||||||
|
WEBFLOW_WEBHOOK_SECRET: requireEnv("WEBFLOW_WEBHOOK_SECRET"),
|
||||||
|
AUTH_SECRET: requireEnv("AUTH_SECRET"),
|
||||||
|
ADMIN_PASSWORD: requireEnv("ADMIN_PASSWORD"),
|
||||||
|
DATABASE_URL: requireEnv("DATABASE_URL"),
|
||||||
|
NODE_ENV: optionEnv("NODE_ENV", "development"),
|
||||||
|
LOG_LEVEL: optionEnv("LOG_LEVEL", "info"),
|
||||||
|
};
|
||||||
|
|
||||||
|
function requireEnv(key: string): string {
|
||||||
|
const val = Bun.env[key];
|
||||||
|
if (!val) {
|
||||||
|
log.error({ name: key }, "Missing required environment variable");
|
||||||
|
throw new Error(`Missing required environment variable: ${key}`)
|
||||||
|
};
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
function optionEnv(key: string, fallback: string = ""): string {
|
||||||
|
const val = Bun.env[key];
|
||||||
|
if (!val) {
|
||||||
|
log.warn({ name: key }, `Missing optional environmental variable, falling back to "${fallback}"`)
|
||||||
|
return fallback
|
||||||
|
};
|
||||||
|
return val;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user