Skip to content

Commit

Permalink
WIP (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
samwillis authored Aug 7, 2024
1 parent cdc51b2 commit 36090a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/pglite/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface DumpDataDirResult {
export interface PGliteOptions {
dataDir?: string;
username?: string;
database?: string;
fs?: Filesystem;
debug?: DebugLevel;
relaxedDurability?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class PGlite implements PGliteInterface, AsyncDisposable {
`PGDATA=${PGDATA}`,
`PREFIX=${WASM_PREFIX}`,
`PGUSER=${options.username ?? "postgres"}`,
`PGDATABASE=template1`, // TODO: allow custom db
`PGDATABASE=${options.database ?? "template1"}`,
"MODE=REACT",
"REPL=N",
// "-F", // Disable fsync (TODO: Only for in-memory mode?)
Expand Down Expand Up @@ -308,7 +308,7 @@ export class PGlite implements PGliteInterface, AsyncDisposable {
} else if (idb & 0b0010) {
// initdb was called to init PGDATA if required
const pguser = options.username ?? "postgres";
const pgdatabase = "template1"; // TODO: allow custom db
const pgdatabase = options.database ?? "template1";
if (idb & 0b0100) {
// initdb has found a previous database
if (idb & (0b0100 | 0b1000)) {
Expand Down
23 changes: 23 additions & 0 deletions packages/pglite/tests/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,26 @@ test.serial("switch to user created after initial run", async (t) => {
message: `permission denied to set role "postgres"`,
});
});

test.serial("create database and switch to it", async (t) => {
await fs.rm("./pgdata-test-user", { force: true, recursive: true });

const db = new PGlite("./pgdata-test-user");
await db.exec(
"CREATE USER test_user WITH PASSWORD 'md5abdbecd56d5fbd2cdaee3d0fa9e4f434';"
);

await db.exec("CREATE DATABASE test_db OWNER test_user;");
await db.close();

const db2 = new PGlite({
dataDir: "./pgdata-test-user",
username: "test_user",
database: "test_db",
});

const currentUsername = await db2.query("SELECT current_user;");
t.deepEqual(currentUsername.rows, [{ current_user: "test_user" }]);
const currentDatabase = await db2.query("SELECT current_database();");
t.deepEqual(currentDatabase.rows, [{ current_database: "test_db" }]);
});

0 comments on commit 36090a4

Please sign in to comment.