~repos /edge-city

#react#js#ssr

git clone https://pyrossh.dev/repos/edge-city.git

edge-city is a next level meta-framework for react that runs only on edge runtimes


1142491c pyrossh

2 years ago
Fix db client
cli.js CHANGED
@@ -114,11 +114,13 @@ const bundlePages = async () => {
114
114
  const data = fs.readFileSync(args.path);
115
115
  const newSrc = `
116
116
  import renderPage from "edge-city/renderPage";
117
+ import init from "@/init";
117
118
  ${importAppComp}
118
119
 
119
120
  ${data.toString()}
120
121
 
121
- export function onRequest(context) {
122
+ export async function onRequest(context) {
123
+ await init();
122
124
  return renderPage(Page, App, context.request);
123
125
  }
124
126
  `
@@ -219,13 +221,15 @@ const bundleServices = async () => {
219
221
  setup(build) {
220
222
  build.onLoad({ filter: /\\*.service.js/, namespace: undefined }, async (args) => {
221
223
  const newSrc = `
222
- import renderApi from "edge-city/renderApi";
224
+ import renderApi from "edge-city/renderApi";
225
+ import init from "@/init";
223
- ${src.toString()}
226
+ ${src.toString()}
224
227
 
225
- export function onRequest(context) {
228
+ export async function onRequest(c) {
229
+ await init();
226
- return renderApi(${p}, context.request);
230
+ return renderApi(${p}, c.request);
227
- }
231
+ }
228
- `
232
+ `
229
233
  return {
230
234
  contents: newSrc,
231
235
  loader: "js",
example/globals.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { NeonDatabase } from "drizzle-orm/neon-serverless"
2
+
3
+ declare global {
4
+ const db: NeonDatabase;
5
+ }
example/src/init.js ADDED
@@ -0,0 +1,18 @@
1
+ import { drizzle } from 'drizzle-orm/neon-serverless';
2
+ import { Pool } from '@neondatabase/serverless';
3
+ import { highlight } from 'sql-highlight';
4
+
5
+ const init = async () => {
6
+ const pool = new Pool({ connectionString: process.env.EDGE_PG_CONN_URL });
7
+ const db = drizzle(pool, {
8
+ logger: {
9
+ logQuery: (query, params) => {
10
+ const sqlString = params.reduce((acc, v, i) => acc.replaceAll("$" + (i + 1), v), query);
11
+ console.log(highlight(sqlString));
12
+ }
13
+ }
14
+ });
15
+ globalThis.db = db;
16
+ }
17
+
18
+ export default init;
example/src/services/db.js DELETED
@@ -1,15 +0,0 @@
1
- import { drizzle } from 'drizzle-orm/neon-serverless';
2
- import { Pool } from '@neondatabase/serverless';
3
- import { highlight } from 'sql-highlight';
4
-
5
- export const pool = new Pool({ connectionString: process.env.EDGE_PG_CONN_URL });
6
- const db = drizzle(pool, {
7
- logger: {
8
- logQuery: (query, params) => {
9
- const sqlString = params.reduce((acc, v, i) => acc.replaceAll("$" + (i + 1), v), query);
10
- console.log(highlight(sqlString));
11
- }
12
- }
13
- });
14
-
15
- export default db;
example/src/services/todos.service.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { eq, asc } from 'drizzle-orm';
2
- import db from "./db";
3
2
  import { boolean, date, pgTable, serial, text } from 'drizzle-orm/pg-core';
4
3
  import { z } from 'zod';
5
4