uber
uber
Explore posts from servers
DTDrizzle Team
Created by uber on 1/10/2025 in #help
🚨 Issue: Testing with PostgreSQL and Drizzle ORM using Testcontainers
Yeah i was just lookin at this, i kinda like the idea of override the import, thats what i need (I think)
7 replies
DTDrizzle Team
Created by uber on 1/10/2025 in #help
🚨 Issue: Testing with PostgreSQL and Drizzle ORM using Testcontainers
EDIT EDIT EDIT: i now have this but i dont rly like it:
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { Resource } from "sst";
import * as schema from "./schemas";

export type DrizzlePgDB = PostgresJsDatabase<typeof schema> & {
$client: postgres.Sql<{}>;
};

// Maintain a mutable connection that can be swapped in tests
let sqlConnection = postgres(Resource.Database.url);

export function setTestConnection(connectionString: string) {
if (process.env.NODE_ENV !== "test") {
throw new Error(
"setTestConnection should only be called in test environment"
);
}
sqlConnection = postgres(connectionString);
}

// Create a proxy to always use the current connection
const db = new Proxy({} as DrizzlePgDB, {
get: (target, prop) => {
const drizzleDB = drizzle(sqlConnection, { schema, logger: false });
return drizzleDB[prop as keyof typeof drizzleDB];
},
});

export default db;
import { drizzle, PostgresJsDatabase } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { Resource } from "sst";
import * as schema from "./schemas";

export type DrizzlePgDB = PostgresJsDatabase<typeof schema> & {
$client: postgres.Sql<{}>;
};

// Maintain a mutable connection that can be swapped in tests
let sqlConnection = postgres(Resource.Database.url);

export function setTestConnection(connectionString: string) {
if (process.env.NODE_ENV !== "test") {
throw new Error(
"setTestConnection should only be called in test environment"
);
}
sqlConnection = postgres(connectionString);
}

// Create a proxy to always use the current connection
const db = new Proxy({} as DrizzlePgDB, {
get: (target, prop) => {
const drizzleDB = drizzle(sqlConnection, { schema, logger: false });
return drizzleDB[prop as keyof typeof drizzleDB];
},
});

export default db;
tests
import {
PostgreSqlContainer,
StartedPostgreSqlContainer,
} from "@testcontainers/postgresql";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { Project } from "../project";
import { setTestConnection } from "../../db";

let container: StartedPostgreSqlContainer;

beforeAll(async () => {
container = await new PostgreSqlContainer().start();
const connectionString = container.getConnectionUri();
console.log("Container started with connection >>", connectionString);

// Set test connection
setTestConnection(connectionString);
});

afterAll(async () => {
await container.stop().catch(console.error);
console.log("Container stopped");
});

describe("listProjects", () => {
it("should return a list of projects", async () => {
const result = await Project.listProjects();
expect(result).toBeInstanceOf(Array);
expect(result.length).toBeGreaterThan(0);
});
});
import {
PostgreSqlContainer,
StartedPostgreSqlContainer,
} from "@testcontainers/postgresql";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { Project } from "../project";
import { setTestConnection } from "../../db";

let container: StartedPostgreSqlContainer;

beforeAll(async () => {
container = await new PostgreSqlContainer().start();
const connectionString = container.getConnectionUri();
console.log("Container started with connection >>", connectionString);

// Set test connection
setTestConnection(connectionString);
});

afterAll(async () => {
await container.stop().catch(console.error);
console.log("Container stopped");
});

describe("listProjects", () => {
it("should return a list of projects", async () => {
const result = await Project.listProjects();
expect(result).toBeInstanceOf(Array);
expect(result.length).toBeGreaterThan(0);
});
});
7 replies
DTDrizzle Team
Created by uber on 1/10/2025 in #help
🚨 Issue: Testing with PostgreSQL and Drizzle ORM using Testcontainers
const container = await new PostgreSqlContainer().start(); would have to move from my test files and would go in my global db handler file to be used as db instance if NODE_ENV is test ? Is that a clean approach
7 replies
DTDrizzle Team
Created by uber on 1/10/2025 in #help
🚨 Issue: Testing with PostgreSQL and Drizzle ORM using Testcontainers
If no, what is a good simple approach. If yes how would i differenciate between dev/prod vs test? envs .. NODE_ENV?
7 replies
DTDrizzle Team
Created by uber on 1/10/2025 in #help
🚨 Issue: Testing with PostgreSQL and Drizzle ORM using Testcontainers
I dont want to have to pass a db instance to my methods.. that leads me to handle the global instance to return the proper db instance based on the use case: In my global db instance i would either return my dev/prod db instance but if test return the testcontainers postgres db instance ? Is this the right approach?
7 replies
DTDrizzle Team
Created by uber on 10/29/2024 in #help
Evolving form data and typesafety .
Cant find anybody having my issue so I must be doing something wrong or thinking about if the wrong way. The only way i came up with is adding versioning to my schemas (in my codebase) everytime my schema changes but then my code will become messy real quick
2 replies
HHono
Created by uber on 10/21/2024 in #help
Append Authorization access token to RPC client in asynchronous way (amplify auth/cognito)
fixed ✅: Hono client allows us to pass a function for headers that can be async :
const rpcClient = hc<AdminRPC>(process.env.NEXT_PUBLIC_API_URL!, {
headers: async () => ({
Authorization: `Bearer ${await getAccessToken()}`,
}),
});
const rpcClient = hc<AdminRPC>(process.env.NEXT_PUBLIC_API_URL!, {
headers: async () => ({
Authorization: `Bearer ${await getAccessToken()}`,
}),
});
3 replies
HHono
Created by uber on 10/21/2024 in #help
Append Authorization access token to RPC client in asynchronous way (amplify auth/cognito)
Is this terrible? having to create the client in a async way everytime i want to perform a http call.. :
import { AdminRPC } from "@my-sst-monorepo/functions/admins/rpc";
import { fetchAuthSession } from "aws-amplify/auth";
import { hc } from "hono/client";

const getAccessToken = async () => {
const session = await fetchAuthSession();
if (!session.tokens) {
throw new Error("No session");
}
return session.tokens.accessToken.toString();
};

const createRpcClient = async () => {
const accessToken = await getAccessToken();
return hc<AdminRPC>(process.env.NEXT_PUBLIC_API_URL!, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
};

export async function listAdmins() {
const rpc = await createRpcClient();
const res = await rpc.admins.$get();
if (!res.ok) {
throw new Error(`Failed to list admins: ${res.status}`);
}
return await res.json();
}
import { AdminRPC } from "@my-sst-monorepo/functions/admins/rpc";
import { fetchAuthSession } from "aws-amplify/auth";
import { hc } from "hono/client";

const getAccessToken = async () => {
const session = await fetchAuthSession();
if (!session.tokens) {
throw new Error("No session");
}
return session.tokens.accessToken.toString();
};

const createRpcClient = async () => {
const accessToken = await getAccessToken();
return hc<AdminRPC>(process.env.NEXT_PUBLIC_API_URL!, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
};

export async function listAdmins() {
const rpc = await createRpcClient();
const res = await rpc.admins.$get();
if (!res.ok) {
throw new Error(`Failed to list admins: ${res.status}`);
}
return await res.json();
}
3 replies