porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.26k stars 262 forks source link

sql.simple() for DB initialization and typescript support #714

Closed mukulgupta21 closed 6 months ago

mukulgupta21 commented 10 months ago

Problem

I have a use-case where I have a DDL script to initialise the database. Something similar to below:

CREATE DATABASE dbName with owner dbuser ;

CREATE TABLE IF NOT EXISTS table (
  col1 VARCHAR(255) NOT NULL,
  col2 VARCHAR(255) NOT NULL,
);

I'm trying to use it as below:

await (sql`${sql.unsafe(sqlScript)}` as any).simple();

which results in the following error:

Error creating database PostgresError: CREATE DATABASE cannot run inside a transaction block

EDIT: I realize this is not possible to do in postgresql since one cannot switch a database without re-initializing the connection (aka backend).

Questions:

iby commented 8 months ago

Have a look at https://github.com/porsager/postgres/issues/198 – looks like a similar case 👍