@medflyt/test-db-service
Version:
Developer tool to instantly provision test PostgreSQL databases from a template
106 lines • 3.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PostgresClusterInstance = void 0;
const assert_never_1 = require("assert-never");
const launch_postgres_1 = require("../launch_postgres");
/**
* Creates and launches a new blank PostgreSQL database cluster.
*
* Use `getPostgresUrl` to get the url of the new cluster and then you should
* create your own new database inside the cluster (using CREATE DATABASE)
*/
class PostgresClusterInstance {
/**
* @param postgresVersion The version number of PostgreSQL that should be
* used. Example: `10.10`
*/
constructor(postgresVersion) {
this.serverReadyCallbacks = [];
this.postgresVersion = postgresVersion;
this.status = { type: "Starting" };
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.launch();
}
/**
* Get the connection url for the "master" database (usually called "postgres")
*/
async getPostgresUrl() {
switch (this.status.type) {
case "Starting":
return await new Promise((resolve) => {
this.serverReadyCallbacks.push((server) => {
resolve(server.url);
});
});
case "Running":
return this.status.server.url;
default:
return assert_never_1.assertNever(this.status);
}
}
/**
* Shutdown and delete the cluster
*/
async close() {
switch (this.status.type) {
case "Running": {
await this.status.server.close();
break;
}
case "Starting": {
const server = await new Promise((resolve) => {
this.serverReadyCallbacks.push((server) => {
resolve(server);
});
});
await server.close();
break;
}
default:
return assert_never_1.assertNever(this.status);
}
}
async launch() {
const server = await launch_postgres_1.PostgresServer.start(this.postgresVersion);
this.status = {
type: "Running",
server: server
};
for (const serverReadyCallback of this.serverReadyCallbacks) {
serverReadyCallback(server);
}
// Free memory:
this.serverReadyCallbacks = [];
}
}
exports.PostgresClusterInstance = PostgresClusterInstance;
// --------------------------------------------------------------------------
// Test code
// --------------------------------------------------------------------------
async function test() {
const postgresVersion = "10.10";
console.log("start");
const instance = new PostgresClusterInstance(postgresVersion);
console.log("get url");
// eslint-disable-next-line @typescript-eslint/no-floating-promises
instance.getPostgresUrl().then(url => console.log("url a", url));
// eslint-disable-next-line @typescript-eslint/no-floating-promises
instance.getPostgresUrl().then(url => console.log("url b", url));
console.log("get url2");
const url2 = await instance.getPostgresUrl();
console.log(url2);
console.log("delay 2000");
await delay(2000);
console.log("close");
await instance.close();
}
async function delay(millis) {
return await new Promise((resolve) => {
setTimeout(resolve, millis);
});
}
if (require.main === module) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
test();
}
//# sourceMappingURL=PostgresClusterInstance.js.map