{"id":28330891,"url":"https://github.com/sirkostya009/vite-plugin-postgres-import","last_synced_at":"2026-03-05T10:01:45.137Z","repository":{"id":295468559,"uuid":"982173406","full_name":"sirkostya009/vite-plugin-postgres-import","owner":"sirkostya009","description":"A Vite plugin for importing sql files with sqlc-style annotations. Postgres-only.","archived":false,"fork":false,"pushed_at":"2025-09-22T15:29:02.000Z","size":81,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-02-21T17:29:11.240Z","etag":null,"topics":["import","pg","plugin","postgres","sql","sqlc","vite"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/sirkostya009.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-05-12T13:35:39.000Z","updated_at":"2025-12-07T12:04:07.000Z","dependencies_parsed_at":"2025-05-25T18:47:51.297Z","dependency_job_id":"e84496b0-0261-457f-8dd6-2528cb66254e","html_url":"https://github.com/sirkostya009/vite-plugin-postgres-import","commit_stats":null,"previous_names":["sirkostya009/vite-plugin-postgres-import"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sirkostya009/vite-plugin-postgres-import","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirkostya009%2Fvite-plugin-postgres-import","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirkostya009%2Fvite-plugin-postgres-import/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirkostya009%2Fvite-plugin-postgres-import/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirkostya009%2Fvite-plugin-postgres-import/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sirkostya009","download_url":"https://codeload.github.com/sirkostya009/vite-plugin-postgres-import/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirkostya009%2Fvite-plugin-postgres-import/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30118932,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T09:35:22.236Z","status":"ssl_error","status_checked_at":"2026-03-05T09:35:20.028Z","response_time":93,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["import","pg","plugin","postgres","sql","sqlc","vite"],"created_at":"2025-05-26T17:43:42.552Z","updated_at":"2026-03-05T10:01:45.109Z","avatar_url":"https://github.com/sirkostya009.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# vite-plugin-postgres-import\n\nEver wanted to ditch those ORMs and finally learn SQL?\n\nIf you use PostgreSQL and Vite - this plugin is right for you!\n\nStart importing `.sql` files with sqlc-style annotations right into your JavaScript code today!\n\n## Usage\n\n`module.sql`:\n\n```sql\n-- name: Query :one\nselect id\nfrom t\nwhere id = :id;\n\n-- name: UpdateQuery :execrows\nupdate t\nset foo = :newFoo\nwhere id = :id;\n\n-- name: QueryMany :many\nselect *\nfrom t;\n\n-- name: MultiStatement :many\nselect *\nfrom t;\n-- :one\nselect something\nfrom t\nlimit 1;\n-- :execrows\ndelete from t\nwhere foo = :foo;\n```\n\n`usage.js`:\n\n```js\nimport { Pool } from \"pg\";\nimport { Query, UpdateQuery, QueryMany, MultiStatement } from \"./module.sql\";\n\nconst pool = new Pool(\"...\");\n\nconst { id } = await Query(pool, { id: 1 });\n\nconst rowsUpdated = await UpdateQuery(pool, { id: 1, newFoo: \"bar\" });\n\nconst array = await QueryMany(pool);\n\n// array, object, number respectively\nconst [all, something, deleted] = await MultiStatement(pool, { foo: \"bar\" });\n```\n\n\u003e [!Warning]\n\u003e\n\u003e Postgres does _NOT_ allow using parameters in multi-statement queries.\n\u003e\n\u003e The current implementation for them is a hackaround that involes injecting escaped values into the SQL string.\n\n## Annotations\n\nThe only 4 sqlc annotations that are available are the following:\n\n-   `:execresult` - default if neither of the 3 below are provided, returns `QueryResult`.\n\n    ex.: `Query\u003cR extends QueryResultRow = { ... }\u003e(c): Promise\u003cQueryResult\u003cR\u003e\u003e`\n\n-   `:one` - returns template argument, or the default-parsed ones from the select/returning clause.\n\n    ex.: `Query\u003cR extends QueryResultRow = { ... }\u003e(c): Promise\u003cR\u003e`\n\n-   `:many` - returns template argument as an array, or the default-parsed ones from the select/returning clause.\n\n    ex.: `Query\u003cR extends QueryResultRow = { ... }\u003e(c): Promise\u003cR[]\u003e`\n\n-   `:execrows` - returns number of affected rows.\n\n    ex.: `Query(c): Promise\u003cnumber\u003e`\n\n4 additional annotations not found in sqlc are available:\n\n-   `:prepare` - prepares the statement by passing query's name to query config.\n\n\u003e [!Warning]\n\u003e\n\u003e Similarly to parameters, Postgres does _NOT_ allow preparing multi-statement queries. Using `:prepare` on a multi-statement query will result in an error. I warned you!\n\n\u003e [!Warning]\n\u003e\n\u003e Don't use identical names for prepared queries, regardless whether they're in different `.sql` modules or not:\n\u003e\n\u003e `one.sql`:\n\u003e\n\u003e ```sql\n\u003e -- name: One :one :prepare\n\u003e select *\n\u003e from t\n\u003e ```\n\u003e\n\u003e `another-one.sql`:\n\u003e\n\u003e ```sql\n\u003e -- name: One :one :prepare\n\u003e select id\n\u003e from t\n\u003e where id = :id;\n\u003e ```\n\u003e\n\u003e `script.js`:\n\u003e\n\u003e ```js\n\u003e import { One } from \"./one.sql\";\n\u003e import { One as AnotherOne } from \"./another-one.sql\";\n\u003e\n\u003e // definitely don't do this\n\u003e await One(c);\n\u003e await AnotherOne(c, { id: ... });\n\u003e ```\n\u003e\n\u003e At best, you'd get an error in the provided example, due to mismatch in provided values, and at worst, assuming different examples, you'd be getting obscure bugs related to incorrect data.\n\n-   `:array` - sets `rowMode` to `'array'`. Modifies to type declarations accordingly:\n\n    ex. `:execresult`: `Query\u003cR extends any[] = [ ... ]\u003e(c): Promise\u003cQueryArrayResult\u003cR\u003e\u003e`\n\n    ex. `:one`: `Query\u003cR extends any[] = [ ... ]\u003e(c): Promise\u003cR\u003e`\n\n    ex. `:many`: `Query\u003cR extends any[] = [ ... ]\u003e(c): Promise\u003cR[]\u003e`\n\n-   `:iterable` - returns an `AsyncGenerator`. Once [Async Iterator Helpers](https://github.com/tc39/proposal-async-iterator-helpers)\n    are in the standard, you can use crazy piping as following:\n\n```js\nconst result = await IterableQuery\u003c{ ... }\u003e(c, { foo: 'bar' })\n    .flatMap(superComplicatedCodeThatIsMoreUsefulToRunFromJS)\n    .filter(Boolean)\n    .toArray();\n```\n\nWell maybe that didn't look too crazy, but if some filtering forces your squeel to become convoluted, and you're fine with moving\nsome of that computation to JS, you can finally do that! Or at least when that proposal is in the language, that is.\n\n-   `:cursor` - returns an `Cursor`\n\nex.: `Query\u003cR extends QueryResultRow = { ... }\u003e(c): Promise\u003cCursor\u003cR\u003e\u003e`\n\n    ex. `:array`: `Query\u003cR extends any[] = [ ... ]\u003e(c): Promise\u003cCursor\u003cR\u003e\u003e`\n\n## Configuring\n\n### `typesFolder`\n\nPath to a folder where all declaration are kept relative to `rootFolder`, i.e.\na file at path `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`.\nMake sure you include this one in your `tsconfig.json` as `\"${typesFolder}/**/*.sql.d.ts\"`.\n\n_default:_ `'node_modules/@types/vite-plugin-postgres-import/'`\n\n### `rootFolder`\n\nRoot folder relative to which path calculation will be happening. May be useful for some I guess.\n\n_default:_ `process.cwd()`\n\n## What this plugin does NOT do\n\nThis plugin does not connect to the database or scan a schema folder, instead naively\nparsing select or returning clauses to figure out potential response types.\n\nIn a real TypeScript project you should probably still roll your own types?\n\nAnd JavaScript projects still get the benefits of completions.\n\n## SvelteKit use case\n\nI primarily use this in a SvelteKit project. The only thing I modify is setting `typesFolder` to `'.svelte-kit/types'` directory, and adding a `\".svelte-kit/types/**/*.sql.d.ts\"` record to my `include` array in `tsconfig.json`.\n\nAlso, when using SvelteKit aliases you can absolutely shove all your sql modules in a `src/sql` directory with a `$sql` alias and have module declarations generated for all of the files! Works automagically out of the box.\n\n## License\n\nMIT.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsirkostya009%2Fvite-plugin-postgres-import","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsirkostya009%2Fvite-plugin-postgres-import","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsirkostya009%2Fvite-plugin-postgres-import/lists"}