{"id":13834733,"url":"https://github.com/xialvjun/ts-sql-plugin","last_synced_at":"2025-05-16T07:05:39.094Z","repository":{"id":34880859,"uuid":"186672832","full_name":"xialvjun/ts-sql-plugin","owner":"xialvjun","description":"TypeScript Language Service Plugin for SQL with a tagged template strings SQL builder.","archived":false,"fork":false,"pushed_at":"2025-02-19T12:45:13.000Z","size":4275,"stargazers_count":180,"open_issues_count":6,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-01T02:38:50.868Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xialvjun.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-14T17:51:44.000Z","updated_at":"2025-04-11T15:20:35.000Z","dependencies_parsed_at":"2024-01-15T18:56:42.162Z","dependency_job_id":"6e1bd0f9-9190-44d6-8312-f1dc935abcb9","html_url":"https://github.com/xialvjun/ts-sql-plugin","commit_stats":{"total_commits":119,"total_committers":7,"mean_commits":17.0,"dds":"0.40336134453781514","last_synced_commit":"89d2bc0e6f311eade9d29bb49d373d41432083c0"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xialvjun%2Fts-sql-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xialvjun%2Fts-sql-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xialvjun%2Fts-sql-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xialvjun%2Fts-sql-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xialvjun","download_url":"https://codeload.github.com/xialvjun/ts-sql-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253650947,"owners_count":21942232,"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":[],"created_at":"2024-08-04T14:00:51.621Z","updated_at":"2025-05-16T07:05:34.067Z","avatar_url":"https://github.com/xialvjun.png","language":"TypeScript","readme":"# ts-sql-plugin\nTypeScript Language Service Plugin for SQL with a tagged template strings SQL builder. Inspired by [andywer/squid](https://github.com/andywer/squid)\n\n#### check sql error demo:\n![readme_0](./docs/readme_0.gif)\n\n#### autocomplete demo:\n![autocomplete](./docs/autocomplete.gif)\n\n# Usage\n\n## run as a language service plugin\n\nInstall the plugin, run:\n\n```sh\nnpm install ts-sql-plugin -D\n```\n\nThen, configure the `plugins` section in your *tsconfig.json*:\n\n```json\n{\n  \"compilerOptions\": {\n    \"module\": \"commonjs\",\n    \"target\": \"es5\",\n    \"plugins\": [\n      {\n        \"name\": \"ts-sql-plugin\",\n        \"command\": \"psql -c\", // optionnal\n        \"tags\": { // optionnal\n          \"sql\": \"sql\",\n          \"raw\": \"raw\",\n          \"cond\": \"cond\",\n          \"and\": \"and\",\n          \"or\": \"or\",\n          \"ins\": \"ins\",\n          \"upd\": \"upd\",\n          \"mock\": \"mock\"\n        },\n        \"mock\": \"0\",\n        \"cost_pattern\": \"/\\\\(cost=\\\\d+\\\\.?\\\\d*\\\\.\\\\.(\\\\d+\\\\.?\\\\d*)/\",\n        \"error_cost\": null, // 100,\n        \"warn_cost\": null, // 50,\n        \"info_cost\": null, // -1,\n        \"schema_command\": \"pg\" // pg | mysql | custom - read the source\n      }\n    ]\n  }\n}\n```\n\n**Note**: If you're using Visual Studio Code, you'll have to use the first approach above, with a\npath to the module, or run the \"TypeScript: Select TypeScript Version\" command and choose \"Use\nWorkspace Version\", or click the version number between \"TypeScript\" and 😃 in the lower-right\ncorner. Otherwise, VS Code will not be able to find your plugin. See https://github.com/microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin#testing-locally\n\n## run as a command line application\n\nInstall the plugin, run:\n\n```sh\nnpm install ts-sql-plugin -g\n```\n\nThen run:\n\n```sh\nts-sql-plugin -p ./my_ts_project -c 'psql -U postgres -c'\n```\n\n# Then in your code:\n\n\u003cspan style=\"color:red;\"\u003eIn the code below, some of the table names or column names are intentionally wrong.\u003c/span\u003e `ts-sql-plugin` will show you the errors.\n\n```ts\nimport sql from 'ts-sql-plugin/sql';\n\nsql`select * from wrong_table_name where wrong_column_name=${name}`;\n// { text: \"select * from wrong_table_name where wrong_column_name=??\", values: [name] }\n// you should transform the text to pg specific or mysql specific query text\n\n// sql.and\nsql`select * from person where ${sql.and({ wrong_column_name: value, name: name })}`;\n\n// sql.or\nsql`select * from person where ${sql.or([{ 'name like': 'abc%', age: 23 }, { 'age \u003e': 23 }])}`;\nsql`select * from person where (name like ${'abc%'} and age=${23}) or age \u003e ${23}`;\n\n// sql.ins\nsql`insert into person ${sql.ins({ id: uuid(), name: name, ageeee: wrong_column_name_value })}`;\nsql`insert into person ${sql.ins([{ id: uuid(), name, age: 23 }, {id, name:'ppp', age:30}])}`;\n\n// sql.upd\nsql`update person set ${sql.upd({ wrong_name_column: name, age: 23 })} where id=${id}`;\n\n// like, \u003e, \u003c etc\nsql`select * from person where ${sql.and({ 'name like': '%'+name_like+'%', 'ageee \u003e': age_bigger_than })}`;\n\n// sql.raw with ?: operator\nsql`select * from person order by age ${reverse ? sql.raw`desc` : sql.raw`asc`}`;\n\n// sql.cond\nsql`select * from person where name=${name} ${sql.cond(!!age_bigger_than)` and ageeee \u003e ${age_bigger_than}`}`;\n\n// ! where in will produce error because of node-postgres doesn't support it. use where column=any()\nsql`select * from person where id in (${[uuid(), uuid()]})`;\nsql`select * from person where id = any(${[uuid(), uuid()]})`;\nsql`select * from ${sql.mock\u003c'person' | 'book'\u003e(sql.raw([table_name]))}`\nsql`select * from person where age = ${sql.mock\u003c'12'\u003e(some_value_can_not_use_the_default_mock__0)}`\n\n// you can use sql queries inside each other\nconst s1 = sql`select * from subscriptions.attribute where entity_id = any(${[7045]})`;\nconst s2 = sql`select * from (${s1}) attr where attribute_id = any(${[7049, 7050]})`;\n\n// ignore cost for query stuff implemented\nconst s1 = sql`\n  -- ts-sql-plugin:ignore-cost\n  select * from subscriptions.attribute\n`;\n\n// you can emit sql to explicit file\n// this will be emitted to emit-sql/AllAttribute.sql file\n// you may change `emit-sql` folder to another via `--emit_dir` option of cli\n// also `--watch` option of cli can be used to emit it in realtime\nconst s1 = sql`\n  /* ts-sql-plugin:emit(\"AllAttribute\") */\n  /* @name AllAttribute */\n  select * from subscriptions.attribute\n`;\n```\n\n**And there is a complete example using [ts-sql-plugin](https://github.com/xialvjun/ts-sql-plugin) and [skm_ts](https://github.com/xialvjun/skm_ts) in folder test_ts_sql_plugin.**\n\n## Generate types from emitted sql\n\nYou can use [pgtyped](https://github.com/adelsz/pgtyped) tool\nfor generating types from your emitted sql via directive:\n```\n/* ts-sql-plugin:emit(\"SomeInterface\") */\n/* @name SomeInterface */\n``` \n\nFor generate types in realtime, use `--watch` option of cli.\n\n## VSCode syntax highlight extension\n\nhttps://marketplace.visualstudio.com/items?itemName=darky.vscode-ts-sql-plugin\n","funding_links":[],"categories":["Language service plugins"],"sub_categories":["Optimization"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxialvjun%2Fts-sql-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxialvjun%2Fts-sql-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxialvjun%2Fts-sql-plugin/lists"}