{"id":13847417,"url":"https://github.com/joshdover/sql-typed","last_synced_at":"2025-05-12T00:41:46.769Z","repository":{"id":34986914,"uuid":"176004921","full_name":"joshdover/sql-typed","owner":"joshdover","description":"a simple, typesafe SQL DSL implementation in TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-03T17:56:36.000Z","size":1109,"stargazers_count":5,"open_issues_count":9,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-12T00:41:40.554Z","etag":null,"topics":["orm","postgresql","sql","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joshdover.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-16T17:47:16.000Z","updated_at":"2021-11-21T20:17:42.000Z","dependencies_parsed_at":"2023-01-15T11:32:16.802Z","dependency_job_id":null,"html_url":"https://github.com/joshdover/sql-typed","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshdover%2Fsql-typed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshdover%2Fsql-typed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshdover%2Fsql-typed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joshdover%2Fsql-typed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joshdover","download_url":"https://codeload.github.com/joshdover/sql-typed/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253655919,"owners_count":21943072,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["orm","postgresql","sql","typescript"],"created_at":"2024-08-04T18:01:19.516Z","updated_at":"2025-05-12T00:41:46.744Z","avatar_url":"https://github.com/joshdover.png","language":"TypeScript","readme":"# SQLTyped\n\n[![CircleCI](https://circleci.com/gh/joshdover/sql-typed.svg?style=svg)](https://circleci.com/gh/joshdover/sql-typed)\n\nSQLTyped is a simple, typesafe SQL DSL implementation in TypeScript that aims to provide a natural\nSQL interface for TypeScript applications. Goals of SQLTyped:\n- **No magic.** SQLTyped is not an ORM, you need to know SQL to use SQLTyped.\n- **No performance surprises.** The DSL looks like SQL and compiles down to predictable queries.\n- **Best-in-class type safety.** The DSL should avoid as many invalid queries as possible.\n- **As few dependencies as possible.** As of now, SQLTyped only depends on the `pg` module for querying PostgreSQL.\n\n```\nnpm install sql-typed\n```\n\nSQLTyped is in active development and the API is not guaranteed to be stable.\n\n## Basic Example\n\n```typescript\nimport { createPool, createTable, TableAttributes, ColumnType } from 'sql-typed';\n\ninterface User extends TableAttributes {\n  id: number;\n  name: string;\n}\n\n// Type safe columns are created from the table definition.\nconst userTable = createTable\u003cUser\u003e(\"users\", {\n  id: { type: ColumnType.PrimaryKey },\n  name: { type: ColumnType.String },\n  age: { type: ColumnType.Number, nullable: true }\n});\n\nconst pool = createPool({\n  host: 'localhost',\n  user: 'postgres',\n  password: 'postgres',\n  database: 'postgres',\n});\n\npool.transaction(async transaction =\u003e {\n  // Create a user\n  const [insertedUser] = await userTable\n    .insert()\n    .values([{ name: 'Josh' }])\n    .execute(transaction);\n\n  // Simple query\n  const usersNamedJosh = await userTable\n    .select()\n    .where(userTable.columns.name.eql('Josh'))\n    .execute(transaction);\n  \n  // Complex queries\n  const millennialsNamedMia = await userTable\n    .select()\n    // Multiple `where` is equivalent to `and` chaining for conjunction logic\n    .where(\n      userTable.columns.name.like('Mia%')\n    ).where(\n      userTable.columns.age.gte(25).and(userTable.columns.age.lte(35))\n    ).execute(\n      transaction\n    );\n\n  // Count queries\n  const countOver50 = await userTable\n    // `where` also accepts a function to build predicates\n    .where(({ age }) =\u003e age.gte(50))\n    .count()\n    .execute(transaction);\n});\n```\n\n## Developing\n\nTo run tests:\n\n```\ndocker run --rm -d -p 5432:5432 postgres\nnpm test\n```\n\n## Type Safety\n\nThere are some cases where TypeScript's type system cannot express a needed type (or I haven't figured out how). Known\ncases:\n\n- **Joins with 3 or more tables.** While joining two tables preserves type information, querying across 3 or more does\n  not. Example:\n  ```ts\n  const rows = await articleTable\n  .select()\n  /**\n   * This is typesafe. The args to the predication function are table-specific\n   * and the return type of the query is table-specific.\n   */\n  .join(userTable, ([a, u]) =\u003e a.userId.eqls(u.id), JoinType.Left)\n  /**\n   * Adding a 3rd table drops down to very wide vague types. This could be\n   * solved if TypesScript supported variadic types parameters.\n   */\n  .join(\n    commentTable,\n    ([a, _u, c]) =\u003e a.id.eqls(c.articleId),\n    JoinType.Left\n  )\n  .execute(transaction);\n  ```\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshdover%2Fsql-typed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoshdover%2Fsql-typed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoshdover%2Fsql-typed/lists"}