alainux
alainux
DTDrizzle Team
Created by alainux on 6/16/2024 in #help
Correct way to instantiate a different client per environment?
I have a sst project which I also would like to run locally with a local postgresql connection and without instantiating a new AWS stack for it. This is my current approach, but I don't think it's great since the type of db ends up being polymorphic.
import { drizzle as pgDrizzle } from 'drizzle-orm/postgres-js';
import { drizzle as awsDrizzle } from 'drizzle-orm/aws-data-api/pg';

import * as schema from '../schema/schema';
import postgres from 'postgres';

import { Resource } from "sst";
import { RDSDataClient } from "@aws-sdk/client-rds-data";
import config from "../constants/config";

const isLocal = config.ENV !== 'production';

const localDrizzle = () => {
const client = postgres(process.env.DB_URL!, { max: 1 });
const db = pgDrizzle(client, { schema: schema });
return { client, db };
};

const remoteDrizzle = () => {
const client = new RDSDataClient({});
const db = awsDrizzle(client, {
database: Resource.RegPostgres.database,
secretArn: Resource.RegPostgres.secretArn,
resourceArn: Resource.RegPostgres.clusterArn,
schema: schema
});
return { client, db };
}

const { client, db } = isLocal ? localDrizzle() : remoteDrizzle();

export { client, db }
import { drizzle as pgDrizzle } from 'drizzle-orm/postgres-js';
import { drizzle as awsDrizzle } from 'drizzle-orm/aws-data-api/pg';

import * as schema from '../schema/schema';
import postgres from 'postgres';

import { Resource } from "sst";
import { RDSDataClient } from "@aws-sdk/client-rds-data";
import config from "../constants/config";

const isLocal = config.ENV !== 'production';

const localDrizzle = () => {
const client = postgres(process.env.DB_URL!, { max: 1 });
const db = pgDrizzle(client, { schema: schema });
return { client, db };
};

const remoteDrizzle = () => {
const client = new RDSDataClient({});
const db = awsDrizzle(client, {
database: Resource.RegPostgres.database,
secretArn: Resource.RegPostgres.secretArn,
resourceArn: Resource.RegPostgres.clusterArn,
schema: schema
});
return { client, db };
}

const { client, db } = isLocal ? localDrizzle() : remoteDrizzle();

export { client, db }
What would be the recommended approach here?
2 replies