Skip to content

Commit 368aa5c

Browse files
authored
Use the same database driver for all postgress (tests and runtime) (#223)
Ideally this should work without issues. ### Why the exploration Moving from "pg" to this database driver to "postgres" allows us also to move from `node_compat=true` flag in favor of `compatibility_flags = [ "nodejs_compat" ]` which allows us to implement open telemetry distributed traced. ❤️ (Which should come in a different PR once I feel OK this is good 🙏🏼 ) Queries seem to be working without issues <details><summary>Queries work</summary> <p> ![image](https://github.com/user-attachments/assets/98edde27-3c76-4192-ab9d-ed8fb6649cd7) ![image](https://github.com/user-attachments/assets/a7f19292-dff2-4478-bdc1-09819ea4f553) ![image](https://github.com/user-attachments/assets/0e399d26-fdea-4d95-88b8-3ea9188b652b) </p> </details>
1 parent 1f72ac1 commit 368aa5c

11 files changed

Lines changed: 26 additions & 89 deletions

File tree

package-lock.json

Lines changed: 4 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@
100100
"hono": "^3.9.0",
101101
"mercadopago": "^2.0.9",
102102
"p-map": "^6.0.0",
103-
"pg": "^8.11.5",
104103
"pino": "^9.2.0",
105104
"pino-pretty": "^11.2.1",
106-
"postgres": "^3.4.3",
105+
"postgres": "^3.4.4",
107106
"react": "^18.2.0",
108107
"resend": "^3.3.0",
109108
"slugify": "^1.6.6",

src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const createGraphqlContext = async ({
9696

9797
const GET_STRIPE_CLIENT = () => getStripeClient(STRIPE_KEY);
9898
const GET_MERCADOPAGO_CLIENT = getMercadoPagoFetch(MERCADOPAGO_KEY, logger);
99-
const DB = await getDb({
99+
const DB = getDb({
100100
neonUrl: DB_URL,
101101
logger,
102102
});

src/datasources/db/index.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,34 @@
11
import { ExtractTablesWithRelations } from "drizzle-orm";
2+
import { PgTransaction } from "drizzle-orm/pg-core";
23
import {
3-
NodePgDatabase,
4-
NodePgQueryResultHKT,
4+
PostgresJsDatabase,
5+
PostgresJsQueryResultHKT,
56
drizzle,
6-
} from "drizzle-orm/node-postgres";
7-
import { PgTransaction } from "drizzle-orm/pg-core";
8-
import { Client } from "pg";
7+
} from "drizzle-orm/postgres-js";
98
import { Logger } from "pino";
9+
import postgres from "postgres";
1010

1111
import * as schema from "./schema";
1212

13-
export type ORM_TYPE = NodePgDatabase<typeof schema>;
13+
export type ORM_TYPE = PostgresJsDatabase<typeof schema>;
1414
export type TRANSACTION_HANDLER = PgTransaction<
15-
NodePgQueryResultHKT,
15+
PostgresJsQueryResultHKT,
1616
typeof schema,
1717
ExtractTablesWithRelations<typeof schema>
1818
>;
1919

20-
export const getDb = async ({
20+
export const getDb = ({
2121
neonUrl,
2222
logger,
2323
}: {
2424
neonUrl: string;
2525
logger: Logger<never>;
2626
}) => {
27-
const client = new Client({
28-
connectionString: neonUrl,
29-
});
27+
const client = postgres(neonUrl);
3028

3129
try {
32-
await client.connect();
3330
const db = drizzle(client, {
3431
schema,
35-
// logger: {
36-
// logQuery(query, params) {
37-
// logger.info(query, params);
38-
// },
39-
// },
4032
});
4133

4234
return db;

src/schema/purchaseOrder/actions.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ export const createPaymentIntent = async ({
335335
});
336336
const currencyCode = currency?.currency;
337337

338+
if (!purchaseOrder) {
339+
throw new GraphQLError("Orden de compra no encontrada");
340+
}
341+
338342
if (purchaseOrder?.userId !== USER.id) {
339343
throw new GraphQLError("No authorizado");
340344
}
@@ -343,10 +347,6 @@ export const createPaymentIntent = async ({
343347
throw new GraphQLError("No encontramos un currency con ese ID");
344348
}
345349

346-
if (!purchaseOrder) {
347-
throw new GraphQLError("Orden de compra no encontrada");
348-
}
349-
350350
if (purchaseOrder.purchaseOrderPaymentStatus === "paid") {
351351
throw new GraphQLError("Orden de compra ya pagada");
352352
}

src/schema/userTickets/mutations.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,8 @@ builder.mutationFields((t) => ({
464464
default_redirect_url: PURCHASE_CALLBACK_URL,
465465
});
466466

467-
logger.info("Generating payment link for purchase order");
468467
const { purchaseOrder, ticketsIds } = await createPaymentIntent({
469-
DB,
468+
DB: trx,
470469
USER,
471470
purchaseOrderId: createdPurchaseOrder.id,
472471
GET_STRIPE_CLIENT,

workers/purchase_order_payment_sync_cron/scheduled.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const scheduled: ExportedHandlerScheduledHandler<ENV> = async (
2525
) => {
2626
ensureKeys(env, ["DB_URL", "STRIPE_KEY", "MERCADOPAGO_KEY"]);
2727
const logger = createLogger("purchase_order_payment_sync_cron");
28-
const DB = await getDb({
28+
const DB = getDb({
2929
neonUrl: env.DB_URL,
3030
logger,
3131
});

workers/sanity_asset_importer/importSanity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ENV } from "./types";
1010

1111
export const importFromSanity = async (env: ENV, logger: Logger<never>) => {
1212
try {
13-
const DB = await getDb({
13+
const DB = getDb({
1414
neonUrl: env.NEON_URL,
1515
logger,
1616
});

workers/wall_of_fame_cron/api.mercadopago.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const syncMercadopagoPaymentsAndSubscriptions = async (
3232
env: ENV,
3333
logger: Logger<never>,
3434
) => {
35-
const DB = await getDb({
35+
const DB = getDb({
3636
neonUrl: env.NEON_URL,
3737
logger,
3838
});

workers/wall_of_fame_cron/api.stripe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const getSubscriptions = async (env: ENV) => {
4444
};
4545

4646
export const syncStripePayments = async (env: ENV) => {
47-
const DB = await getDb({ neonUrl: env.NEON_URL, logger: defaultLogger });
47+
const DB = getDb({ neonUrl: env.NEON_URL, logger: defaultLogger });
4848
const stripe = new Stripe(env.ST_KEY);
4949

5050
const results = await stripe.charges.list({ limit: 100 });

0 commit comments

Comments
 (0)