Created
June 26, 2024 12:46
-
-
Save encima/ebed580ccccb0696ef17ee85e4f0da9b to your computer and use it in GitHub Desktop.
PG Faker Function - Fake data on the edge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HTTPException, Hono } from "https://deno.land/x/hono@v4.3.11/mod.ts"; | |
import type { Context } from 'https://deno.land/x/hono/mod.ts'; | |
import { faker } from "https://deno.land/x/deno_faker@v1.0.3/mod.ts"; | |
import * as postgres from 'https://deno.land/x/postgres@v0.17.0/mod.ts' | |
const app = new Hono(); | |
const dbURL = Deno.env.get('SUPABASE_DB_URL'); | |
const pool = new postgres.Pool(dbURL, 3, true) | |
const schemas = { | |
"nextjs-slack-clone": "https://github.com/supabase/supabase/raw/master/examples/slack-clone/nextjs-slack-clone/supabase/migrations/20240214102356_init.sql" | |
} | |
app.get('/postgen', () => { | |
return new Response(`Hello World!`) | |
}); | |
app.post('/postgen/bootstrap/:schema', async (c: Context) => { | |
const conn = await pool.connect(); | |
const schemaName = c.req.param('schema'); | |
const resp = await fetch(schemas[schemaName]); | |
const text = await resp.text(); | |
await conn.queryArray(text); | |
return new Response(text, {status: 200}) | |
}); | |
app.post('/postgen/schema/:schema', async (c: Context) => { | |
const conn = await pool.connect(); | |
const schemaName = c.req.param('schema'); | |
await conn.queryArray(`drop schema if exists ${schemaName} cascade; create schema public;`); | |
return new Response("dropped", {status: 200}) | |
}); | |
app.post('/postgen/type/:count', async (c: Context) => { | |
const count = c.req.param('count'); | |
const body = await c.req.json(); | |
let bodyResp = []; | |
try { | |
for (let i = 0; i < count; i++) { | |
bodyResp[i] = {} | |
Object.entries(body).forEach(([key,value]) => { | |
bodyResp[i][key] = faker.fake(`{{${value}}}`) | |
}) | |
} | |
} catch(error) { | |
return new Response(JSON.stringify(error.message), {status: 500}) | |
} | |
return new Response(JSON.stringify(bodyResp), {status: 200}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment