-
-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add slonik plugin #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Newbie012
wants to merge
3
commits into
main
Choose a base branch
from
feat-slonik-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "@ts-safeql/eslint-plugin": patch | ||
| "@ts-safeql/plugin-utils": patch | ||
| "@ts-safeql/zod-annotator": patch | ||
| "@ts-safeql/plugin-slonik": minor | ||
| --- | ||
|
|
||
| Publish the experimental Slonik plugin and tighten plugin resolution behavior. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // @ts-check | ||
|
|
||
| import safeql from "@ts-safeql/eslint-plugin/config"; | ||
| import slonik from "@ts-safeql/plugin-slonik"; | ||
| import tseslint from "typescript-eslint"; | ||
|
|
||
| export default tseslint.config({ | ||
| files: ["src/**/*.ts"], | ||
| languageOptions: { | ||
| parser: tseslint.parser, | ||
| parserOptions: { | ||
| projectService: true, | ||
| }, | ||
| }, | ||
| extends: [ | ||
| safeql.configs.connections({ | ||
| databaseUrl: "postgres://postgres:postgres@localhost:5432/postgres", | ||
| plugins: [slonik()], | ||
| }), | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "name": "@ts-safeql-demos/plugin-slonik", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "tsc", | ||
| "lint": "eslint src" | ||
| }, | ||
| "devDependencies": { | ||
| "@eslint/js": "catalog:", | ||
| "@slonik/pg-driver": "^48.13.2", | ||
| "@types/node": "catalog:", | ||
| "eslint": "catalog:", | ||
| "slonik": "^48.13.2", | ||
| "typescript": "catalog:", | ||
| "typescript-eslint": "catalog:", | ||
| "zod": "^4.3.6" | ||
| }, | ||
| "dependencies": { | ||
| "@ts-safeql/eslint-plugin": "workspace:*", | ||
| "@ts-safeql/plugin-slonik": "workspace:*" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| import { createPool, sql } from "slonik"; | ||
| import { createPgDriverFactory } from "@slonik/pg-driver"; | ||
| import { z } from "zod"; | ||
|
|
||
| const pool = await createPool("postgres://", { driverFactory: createPgDriverFactory() }); | ||
|
|
||
| section("sql.type — Zod schema validates result", () => { | ||
| // inline schema | ||
| pool.one(sql.type(z.object({ id: z.number(), version: z.string() }))` | ||
| SELECT oid::int4 AS id, typname::text AS version FROM pg_type LIMIT 1 | ||
| `); | ||
|
|
||
| // referenced schema variable | ||
| const TypeRow = z.object({ id: z.number(), version: z.string() }); | ||
| pool.one(sql.type(TypeRow)` | ||
| SELECT oid::int4 AS id, typname::text AS version FROM pg_type LIMIT 1 | ||
| `); | ||
|
|
||
| // nullable column | ||
| pool.one(sql.type(z.object({ v: z.string().nullable() }))`SELECT NULL::text AS v`); | ||
|
|
||
| // multiple columns with mixed types | ||
| pool.one(sql.type(z.object({ n: z.number(), s: z.string(), b: z.boolean() }))` | ||
| SELECT 1::int4 AS n, 'hello'::text AS s, true AS b | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.typeAlias — alias→schema is runtime-only", () => { | ||
| // validates SQL but skips type annotations | ||
| pool.query(sql.typeAlias("id")`SELECT 1 AS id`); | ||
| }); | ||
|
|
||
| section("sql.unsafe — opt-out of type checking", () => { | ||
| // no type annotation needed | ||
| pool.query(sql.unsafe`SELECT 1`); | ||
| }); | ||
|
|
||
| section("sql.identifier", () => { | ||
| // single name | ||
| pool.query(sql.unsafe`SELECT typname::text FROM ${sql.identifier(["pg_type"])} LIMIT 1`); | ||
|
|
||
| // schema-qualified | ||
| pool.one(sql.type(z.object({ oid: z.number() }))` | ||
| SELECT oid::int4 AS oid FROM ${sql.identifier(["pg_catalog", "pg_type"])} LIMIT 1 | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.json / sql.jsonb", () => { | ||
| // sql.json | ||
| pool.one(sql.type(z.object({ p: z.string().nullable() }))` | ||
| SELECT ${sql.json({ id: 1 })}::jsonb ->> 'id' AS p | ||
| `); | ||
|
|
||
| // sql.jsonb | ||
| pool.one(sql.type(z.object({ p: z.string().nullable() }))` | ||
| SELECT ${sql.jsonb([1, 2, 3])}::jsonb ->> 0 AS p | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.binary", () => { | ||
| // bytea parameter | ||
| pool.query(sql.unsafe`SELECT ${sql.binary(Buffer.from("foo"))}`); | ||
| }); | ||
|
|
||
| section("sql.date / sql.timestamp / sql.interval", () => { | ||
| // sql.date | ||
| pool.one(sql.type(z.object({ d: z.string().nullable() }))` | ||
| SELECT ${sql.date(new Date("2022-08-19T03:27:24.951Z"))}::text AS d | ||
| `); | ||
|
|
||
| // sql.timestamp | ||
| pool.one(sql.type(z.object({ d: z.string().nullable() }))` | ||
| SELECT ${sql.timestamp(new Date("2022-08-19T03:27:24.951Z"))}::text AS d | ||
| `); | ||
|
|
||
| // sql.interval | ||
| pool.one(sql.type(z.object({ i: z.string().nullable() }))` | ||
| SELECT ${sql.interval({ days: 3 })}::text AS i | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.uuid", () => { | ||
| // uuid parameter | ||
| pool.one(sql.type(z.object({ u: z.string().nullable() }))` | ||
| SELECT ${sql.uuid("00000000-0000-0000-0000-000000000000")}::text AS u | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.array", () => { | ||
| // typed array | ||
| pool.one(sql.type(z.object({ a: z.number().nullable() }))` | ||
| SELECT ${sql.array([1, 2, 3], "int4")} AS a | ||
| `); | ||
|
|
||
| // ANY() pattern from README | ||
| pool.query(sql.typeAlias("id")` | ||
| SELECT oid::int4 AS id FROM pg_type | ||
| WHERE oid = ANY(${sql.array([1, 2, 3], "int4")}) | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.join — too dynamic, query skipped", () => { | ||
| // comma-separated values | ||
| pool.query(sql.unsafe`SELECT ${sql.join([1, 2, 3], sql.fragment`, `)}`); | ||
|
|
||
| // boolean expressions | ||
| pool.query(sql.unsafe`SELECT ${sql.join([1, 2], sql.fragment` AND `)}`); | ||
|
|
||
| // tuple list | ||
| pool.query(sql.unsafe` | ||
| SELECT ${sql.join( | ||
| [ | ||
| sql.fragment`(${sql.join([1, 2], sql.fragment`, `)})`, | ||
| sql.fragment`(${sql.join([3, 4], sql.fragment`, `)})`, | ||
| ], | ||
| sql.fragment`, `, | ||
| )} | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.unnest — too dynamic, query skipped", () => { | ||
| // bulk insert with string type names | ||
| pool.query(sql.unsafe` | ||
| SELECT bar, baz | ||
| FROM ${sql.unnest( | ||
| [ | ||
| [1, "foo"], | ||
| [2, "bar"], | ||
| ], | ||
| ["int4", "text"], | ||
| )} AS foo(bar, baz) | ||
| `); | ||
| }); | ||
|
|
||
| section("sql.literalValue — too dynamic, query skipped", () => { | ||
| // raw literal interpolation | ||
| pool.query(sql.unsafe`SELECT ${sql.literalValue("foo")}`); | ||
| }); | ||
|
|
||
| section("sql.fragment — composable pieces", () => { | ||
| // standalone fragment (not linted) | ||
| sql.fragment`WHERE 1 = 1`; | ||
|
|
||
| // fragment as expression (query skipped) | ||
| const whereFragment = sql.fragment`WHERE typname = ${"bool"}`; | ||
| pool.query(sql.unsafe`SELECT typname FROM pg_type ${whereFragment}`); | ||
|
|
||
| // nested fragments | ||
| const nestedCondition = sql.fragment`typname = ${"bool"}`; | ||
| const nestedWhereFragment = sql.fragment`WHERE ${nestedCondition}`; | ||
| pool.query(sql.unsafe`SELECT typname FROM pg_type ${nestedWhereFragment}`); | ||
| }); | ||
|
|
||
| section("value placeholders — plain variables", () => { | ||
| // plain value becomes $N parameter | ||
| const name = "bool"; | ||
| pool.query(sql.unsafe`SELECT typname FROM pg_type WHERE typname = ${name}`); | ||
| }); | ||
|
|
||
| section("invalid cases SafeQL catches", () => { | ||
| // bad column | ||
| // eslint-disable-next-line @ts-safeql/check-sql -- column "nonexistent" does not exist | ||
| pool.query(sql.unsafe`SELECT nonexistent FROM pg_type`); | ||
|
|
||
| // bad table | ||
| // eslint-disable-next-line @ts-safeql/check-sql -- relation "nonexistent" does not exist | ||
| pool.query(sql.type(z.object({}))`SELECT 1 FROM nonexistent`); | ||
|
|
||
| // wrong zod schema | ||
| // eslint-disable-next-line @ts-safeql/check-sql -- Expected: z.object({ id: z.number() }) | ||
| pool.one(sql.type(z.object({ id: z.string() }))`SELECT 1::int4 AS id`); | ||
| }); | ||
|
|
||
| function section(_: string, fn: () => void) { | ||
| fn(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "extends": "../../tsconfig.node.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.