Failing interpolation

This issue has been tracked since 2022-09-26.

Describe the bug
When using Postgres.js, interpolation causes SafeQL error

To Reproduce
Steps to reproduce the behavior:

async function query(id: number) {
  type Animal = {
    id: number;
  };

// Works
  await sql<Animal[]>`SELECT * FROM animals WHERE id = 1`;

// Fail
  await sql<Animal[]>`SELECT * FROM animals WHERE id = ${id}`;
}

Expected behavior
Shouldn't fail with interpolation

Screenshots
Screenshot 2022-09-26 at 16 37 10

Screenshot 2022-09-26 at 16 24 08

Desktop (please complete the following information):

  • OS: Mac OS
  • PostgreSQL version 13
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Newbie012 wrote this answer on 2022-09-26

Looks odd, since I have a test case and an actual demo as shown here https://github.com/ts-safeql/safeql/blob/main/demos/basic/src/index.ts

If you run eslint from the cli, do you still get that error message? Are you using the latest version?

karlhorky wrote this answer on 2022-09-26

I added an example of interpolation in Postgres.js in PR #59, maybe this will be a failing test for this issue

Edit: interesting, the test is not failing in #59, maybe it's in indeed a problem with your setup @Eprince-hub ?

Newbie012 wrote this answer on 2022-09-26

I think it's a problem with the vscode ESLint extension rather than ESLint itself.

How can it be reproduced?

Eprince-hub wrote this answer on 2022-09-27

I am getting the same error when i run eslint from the cli, I am using the latest version

The error is reproduced in this repo https://github.com/upleveled/next-js-example-spring-2022 in add-safeql-plugin branch

Steps to reproduce

Database Setup

You can install / setup PostgreSQL following the steps in UpLeveled's System Setup Instructions.

If you have PostgreSQL already setup then connect to the built-in postgres database as administrator in order to create the database:

Windows

psql -U postgres

macOS

psql postgres

Linux

sudo -u postgres psql

If it asks for a password, use postgres.

Once you have connected, run the following to create the database:

CREATE DATABASE <database name>;
CREATE USER <user name> WITH ENCRYPTED PASSWORD '<user password>';
GRANT ALL PRIVILEGES ON DATABASE <database name> TO <user name>;

Quit psql using the following command:

\q

On Linux, you will also need to create a Linux system user with a name matching the user name you used in the database. It will prompt you to create a password for the user - choose the same password as for the database above.

sudo adduser <user name>

Adding the environment variables

Copy the .env.example file to a new file called .env (ignored from Git) and fill in the necessary information.

Running the migrations

This sets up the required table in the database

yarn migrate up

To reverse the last single migration, run:

yarn migrate down
Newbie012 wrote this answer on 2022-09-28

I figured it out.

The error there is no parameter ... indicates that the query is actually being executed, which explains why when you ran drop table did actually drop the table.

This happens due to a bug that I reported here in Postgres.js.

Luckily, a fix was pushed, although there hasn't been a new release since May 26. I opened an issue a few weeks ago that requests to publish a new release to npm.

The reason it didn't happen in my repository:

  1. Currently, as a workaround for this issue, I'm installing the package directly from GitHub (which is not recommended)
  2. I'm using PNPM instead of Yarn, which handles dependencies differently. I'm not an expert when it comes to package managers, but all I know is that when I installed from github:porsager/postgres and ran yarn, I still got an error. When I switched to PNPM, everything worked. I think it's since (I might be misleading tho) PNPM can download different versions of the same package.

My recommendations are -either install directly from GitHub and use PNPM instead. Or, wait until the maintainer of Postgres.js will create a new release and install it. Once he does, I should probably prevent users from using this plugin if they have a lower version for safety.

Eprince-hub wrote this answer on 2022-09-29

In Yarn you can specify a GitHub repo using the full url, this seems to work:

yarn add https://github.com/porsager/postgres

In order to force the version of any transitive dependencies on postgres, we also copied the dependency to our "resolutions" object in package.json, eg:

{
  "resolutions": {
    "postgres": "https://github.com/porsager/postgres"
  }
}
porsager wrote this answer on 2022-09-30

Sorry for the late release including the fix, but v3.3.0 is out now

Newbie012 wrote this answer on 2022-10-01

Thanks @porsager !

karlhorky wrote this answer on 2022-10-01

Thanks @porsager ! Added a PR to update the versions to ^3.3.0 in all SafeQL packages:

More Details About Repo
Owner Name ts-safeql
Repo Name safeql
Full Name ts-safeql/safeql
Language TypeScript
Created Date 2022-09-08
Updated Date 2023-03-16
Star Count 795
Watcher Count 5
Fork Count 14
Issue Count 7

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date