Skip to content
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

Flat Config & ESM/CJS & Vitest #216

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
"$schema": "https://unpkg.com/@changesets/config@2.1.1/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [["@ts-safeql/eslint-plugin", "@ts-safeql/generate", "@ts-safeql/shared"]],
"fixed": [
[
"@ts-safeql/eslint-plugin",
"@ts-safeql/generate",
"@ts-safeql/shared",
"@ts-safeql/sql-ast"
]
],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
}
30 changes: 30 additions & 0 deletions .changeset/neat-cheetahs-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
"@ts-safeql/eslint-plugin": minor
"@ts-safeql/generate": minor
"@ts-safeql/sql-tag": minor
"@ts-safeql/docs": minor
---

This release introduces a lot of (internal) changes, but to be honest, I'm too lazy to write them all down so I'll mention the highlights:

### SafeQL supports Flat Config! 🎉

You can now use SafeQL with the new ESLint [Flat Config](https://eslint.org/docs/latest/use/configure/configuration-files#configuration-file) API:

```js
// eslint.config.js

import safeql from "@ts-safeql/eslint-plugin/config";
import tseslint from "typescript-eslint";

export default tseslint.config(
// ...
safeql.configs.connections({
// ...
}),
);
```

### SafeQL is now built for both ESM and CJS

Up until now, I built SafeQL using only TSC (targeting CJS). In order to support both ESM and CJS, I had to use a different build system. I chose to use [unbuild](https://github.com/unjs/unbuild) because it's awesome.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/switch-exhaustiveness-check": "error"
"@typescript-eslint/no-non-null-assertion": "off"
}
}
20 changes: 13 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ concurrency:

jobs:
check:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
node-version: ["18", "20"]
fail-fast: false
services:
postgres:
image: postgres:14
Expand All @@ -27,13 +32,13 @@ jobs:
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2.2.2
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
- uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: ${{ matrix.node-version }}
cache: "pnpm"
- run: pnpm install
- run: pnpm run setup
Expand All @@ -43,6 +48,7 @@ jobs:
PGHOST: localhost
PGDATABASE: postgres
- run: pnpm build
- run: pnpm typecheck
- run: pnpm lint
- run: pnpm run prettier
- run: pnpm format
- run: pnpm test
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/demos/**/lib
/packages/**/lib
/demos/**/dist
/packages/**/dist
1 change: 1 addition & 0 deletions demos/basic-flat-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
3 changes: 3 additions & 0 deletions demos/basic-flat-config/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint.experimental.useFlatConfig": true
}
12 changes: 12 additions & 0 deletions demos/basic-flat-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @ts-check

import safeql from "@ts-safeql/eslint-plugin/config";
import tseslint from "typescript-eslint";

export default tseslint.config(
...tseslint.configs.recommendedTypeCheckedOnly,
safeql.configs.connections({
databaseUrl: "postgres://postgres:postgres@localhost:5432/safeql_basic_flat_config",
targets: [{ tag: "sql", transform: "{type}[]" }],
})
);
25 changes: 25 additions & 0 deletions demos/basic-flat-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@ts-safeql-demos/basic-flat-config",
"version": "0.0.0",
"type": "module",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"setup": "tsx src/setup.ts",
"lint": "eslint src",
"lint!": "eslint src --fix"
},
"devDependencies": {
"@types/node": "^18.15.11",
"@eslint/js": "^8.56.0",
"@types/eslint": "^8.56.10",
"eslint": "^8.57.0",
"tsx": "^4.9.1",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
},
"dependencies": {
"@ts-safeql/eslint-plugin": "workspace:*",
"postgres": "^3.3.4"
}
}
42 changes: 42 additions & 0 deletions demos/basic-flat-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import postgres from "postgres";

type ID = number;

export async function check(idsFromParameter: ID[]) {
const sql = postgres();

await sql<{ id: number }[]>`
SELECT id
FROM person
WHERE TRUE
AND id = ${idsFromParameter[0] > 5 ? 5 : 5}
AND id = ANY(${idsFromParameter})
AND name = ${"John"} -- string literal
`;

// Conditional expression
await sql<{ id: number }[]>`SELECT id FROM starship WHERE id = ${
idsFromParameter[0] > 5 ? 5 : 5
}`;

// Nullish coalescing operator
const x = 5 as number | null;
await sql<{ id: number }[]>`SELECT id FROM starship WHERE id = ${x ?? 10}`;

interface AgencyIdNameType {
id: number;
name: string;
}

await sql<AgencyIdNameType[]>`SELECT id, name FROM person`;

interface AgencyIdNameInterface {
id: number;
name: string;
}

await sql<AgencyIdNameInterface[]>`SELECT id, name FROM person`;
await sql<{ id: number; name: string }[]>`SELECT id, name FROM person`;

await sql.end();
}
38 changes: 38 additions & 0 deletions demos/basic-flat-config/src/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { execSync } from "child_process";
import postgres from "postgres";

const DATABASE_NAME = "safeql_basic_flat_config";

async function main() {
// 1. Drop the database if exists
console.log("Dropping database if exists...");
execSync(`psql -c "DROP DATABASE IF EXISTS ${DATABASE_NAME} WITH (FORCE);"`);

// 2. Create a new database
console.log("Creating database...");
execSync(`psql -U postgres -c "CREATE DATABASE ${DATABASE_NAME};"`);

// 3. Connect to the database
console.log("Connecting to database...");
const sql = postgres(`postgres://postgres@localhost:5432/${DATABASE_NAME}`);

// 4. Create tables
console.log("Creating tables...");
await sql.unsafe(`
CREATE TABLE person (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(255) NOT NULL
);

CREATE TABLE starship (
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(255) NOT NULL,
captain_id INTEGER REFERENCES person(id)
);
`);

console.log("✅ Done!");
await sql.end();
}

await main();
6 changes: 6 additions & 0 deletions demos/basic-flat-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.node.json",
"compilerOptions": {
"noEmit": true
}
}
8 changes: 4 additions & 4 deletions demos/basic-migrations-raw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
"lint!": "eslint src --fix"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^8.39.0",
"typescript": "^5.3.3"
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"typescript": "^5.4.5"
},
"dependencies": {
"@ts-safeql-demos/shared": "workspace:*",
Expand Down
10 changes: 5 additions & 5 deletions demos/basic-transform-type/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"lint!": "eslint src --fix"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^8.39.0",
"tsx": "^4.6.2",
"typescript": "^5.3.3"
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"tsx": "^4.9.1",
"typescript": "^5.4.5"
},
"dependencies": {
"@ts-safeql-demos/shared": "workspace:*",
Expand Down
10 changes: 5 additions & 5 deletions demos/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"lint!": "eslint src --fix"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^8.39.0",
"tsx": "^4.6.2",
"typescript": "^5.3.3"
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"tsx": "^4.9.1",
"typescript": "^5.4.5"
},
"dependencies": {
"@ts-safeql-demos/shared": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion demos/big-project/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib
dist
_fake_src
10 changes: 5 additions & 5 deletions demos/big-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"lint!": "eslint src --fix"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"eslint": "^8.39.0",
"tsx": "^4.6.2",
"typescript": "^5.3.3"
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"tsx": "^4.9.1",
"typescript": "^5.4.5"
},
"dependencies": {
"@ts-safeql/eslint-plugin": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions demos/config-file-flat-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib
3 changes: 3 additions & 0 deletions demos/config-file-flat-config/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"eslint.experimental.useFlatConfig": true
}
9 changes: 9 additions & 0 deletions demos/config-file-flat-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @ts-check

import safeql from "@ts-safeql/eslint-plugin/config";
import tseslint from "typescript-eslint";

export default tseslint.config(
...tseslint.configs.recommendedTypeCheckedOnly,
safeql.configs.useConfigFile
);
25 changes: 25 additions & 0 deletions demos/config-file-flat-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@ts-safeql-demos/config-file-flat-config",
"version": "0.0.0",
"type": "module",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"setup": "tsx src/setup.ts",
"lint": "eslint src",
"lint!": "eslint src --fix"
},
"devDependencies": {
"@types/node": "^18.15.11",
"@eslint/js": "^8.56.0",
"@types/eslint": "^8.56.10",
"eslint": "^8.57.0",
"tsx": "^4.9.1",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
},
"dependencies": {
"@ts-safeql/eslint-plugin": "workspace:*",
"postgres": "^3.3.4"
}
}
8 changes: 8 additions & 0 deletions demos/config-file-flat-config/safeql.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "@ts-safeql/eslint-plugin";

export default defineConfig({
connections: {
databaseUrl: "postgres://postgres:postgres@localhost:5432/safeql_config_file_flat_config",
targets: [{ tag: "sql", transform: "{type}[]" }],
},
});
Loading