leinelissen / embedded-postgres

🐘 A Node package that allows you to spawn a Postgresql cluster programatically.
MIT License
53 stars 10 forks source link

Feature request: allow passing in custom loggers instead of using console.log / stdout directly #5

Closed panta82 closed 1 year ago

panta82 commented 1 year ago

I've implemented this locally using patch-package.

The diff looks something like this (in compiled js):

// The default configuration options for the class
const defaults = {
    database_dir: path_1.default.join(process.cwd(), 'data', 'db'),
    port: 5432,
    user: 'postgres',
    password: 'password',
    auth_method: 'password',
    persistent: true,
    on_log: console.log,
    on_error: console.error,
};

// ...

        // Initialize the database
        await new Promise((resolve, reject) => {
            const process = (0, child_process_1.spawn)(initdb, [
                `--pgdata=${this.options.database_dir}`,
                `--auth=${this.options.auth_method}`,
                `--username=${this.options.user}`,
                `--pwfile=${passwordFile}`,
            ]);

            process.stdout.on('data', (data) => {
              this.options.on_log(data.toString('utf-8'));
            });

            process.stderr.on('data', (data) => {
              this.options.on_error(data.toString('utf-8'));
            });

            process.on('exit', (code) => {
                if (code === 0) {
                    resolve();
                }
                else {
                    reject(`Postgres init script exited with code ${code}. Please check the logs for extra info. The data directory might already exist.`);
                }
            });
        });

// ....

               // Parse the data as a string and log it
                const message = chunk.toString('utf-8');
                this.options.on_log(message);

I wish I could do a proper PR. Unfortunately, I couldn't set up the project on linux machine. All the builds failed and froze the machine at the same time :)

leinelissen commented 1 year ago

Thanks for the input! I have taken a look at this and implemented them in the new beta.9 release, which is on its way to NPM. Do you have any idea what went wrong with the local setup so I can maybe take a look at this as well?