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
Desktop (please complete the following information):
Additional context
Add any other context about the problem here.
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?
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 ?
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
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>
Copy the .env.example
file to a new file called .env
(ignored from Git) and fill in the necessary information.
This sets up the required table in the database
yarn migrate up
To reverse the last single migration, run:
yarn migrate down
@Newbie012 the whole PR is here: upleveled/next-js-example-spring-2022#11 (and the branch: https://github.com/upleveled/next-js-example-spring-2022/tree/add-safeql-plugin)
You can see the failing eslint
command in this CI run: https://github.com/upleveled/next-js-example-spring-2022/actions/runs/3141711623/jobs/5104464969
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:
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.
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"
}
}
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 |
Issue Title | Created Date | Updated Date |
---|