SweetIQ/schemats

Postgres support not compatible with Node 14

sbichenko opened this issue · 11 comments

schemats depends on "pg-promise": "^6.3.6", which in turn depends on pg. Node 14 support in pg-promise was added only recently, in version 10.5.2.

schemats currently fails silently when trying to run against a Postgres database.

olso commented

So far, I've done (I use yarn)

"resolutions": {
    "schemats/pg-promise": "10.5.2"
  },

but now I'm trying to force rejectUnauthorized into the url, so far without luck

npx schemats generate -c ${process.env.DATABASE_URL}?ssl=true

npx schemats generate -c ${process.env.DATABASE_URL}?ssl%5BrejectUnauthorized%5D=false

olso commented

Okay, I just

NODE_TLS_REJECT_UNAUTHORIZED=0 npx schemats generate -c ${process.env.DATABASE_URL}?ssl=true --camelCase -s public -o ${outputFile},

any updates on this? having the same issue when running against postgres 12.4 and node v14.15.0.

i'm using Knex.js to do migrations and i've checked that all my desired tables are in the "public" schema.

olso commented

@Korede-TA

"dev": {
    "driver": "pg",
    "url": {
      "ENV": "DATABASE_URL"
    },
    "ssl": {
      "rejectUnauthorized": false
    }
  },
"resolutions": {
    "schemats/pg-promise": "^10.6.2",

works for me node 14.14

olso commented

I use a custom script/bin

/* eslint-disable no-console */
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-useless-escape */
const util = require("util");
const path = require("path");
const exec = util.promisify(require("child_process").exec);

const main = async () => {
  console.log(`Cwd: ${process.cwd()}`);
  console.log(`Using ${process.env.DATABASE_URL} to generate db types`);
  const outputFile = path.join(process.cwd(), "src/types/GeneratedDb.ts");
  console.log({ outputFile });

  try {
    console.log("Running schemats");
    await exec(
      `NODE_TLS_REJECT_UNAUTHORIZED=0 npx schemats generate -c ${process.env.DATABASE_URL}?ssl=true --camelCase -s public -o ${outputFile}`,
    );
  } catch (e) {
    console.error(e);
  }

  try {
    console.log("Replace Date with string");
    await exec(`sed -i '.bak' 's/Date/string/g' ${outputFile}`);
  } catch (e) {
    console.error(e);
  }

  try {
    console.log("Skip deprecated namespaces");
    await exec(`sed -i '.bak' 's/export namespace/export declare namespace/g' ${outputFile}`);
  } catch (e) {
    console.error(e);
  }

  // try {
  //   console.log("Make nullable fields also optional");
  //   await exec(`sed -i '.bak' 's/| null/| null | undefined/g' src/types/GeneratedDb.ts`);
  // } catch (e) {
  //   console.error(e);
  // }

  // try {
  //   // # TODO: use ts-morph to make all fields that are nullable also optional
  //   console.log("");
  // } catch (e) {
  //   console.error(e);
  // }

  try {
    console.log("Remove bak");
    await exec(`rm ${outputFile}.bak`);
  } catch (e) {
    console.error(e);
  }

  process.exit(0);
};

main();

export {};

@olso Awesome custom script! Thanks a lot.

Did you already figure out the ts-morph part to make also all nullable fields optional?

Also you can use NVM to Switch between node version because in fact schematics not working with node 14

I hate myself for not posting the solution earlier!

Adding this to package.json fixes the problem for me:

"resolutions": {
    "schemats/pg-promise": "^10.6.2"
}

Pity the author abandoned the project. (Author of pg-promise).

@xiamx