|
| 1 | +# Ultimate PostgreSQL & SQLC Snippets for VS Code |
| 2 | + |
| 3 | +Boost your database development productivity with this comprehensive collection of 50+ powerful snippets for PostgreSQL and SQLC. Designed to reduce boilerplate, enforce best practices, and speed up your workflow when working with modern SQL databases and the SQLC code generator. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +* **Comprehensive SQLC Coverage**: Snippets for all core SQLC operations (`:one`, `:many`, `:exec`, `:batchexec`), including common patterns like returning inserted IDs, pagination, and batch inserts. |
| 8 | +* **Advanced SQLC Patterns**: Tackle complex scenarios with snippets for JOINs with aggregation, JSONB queries, array operations, full-text search, and transaction blocks. |
| 9 | +* **PostgreSQL DDL Mastery**: Quickly scaffold tables (with audit columns), indexes, enums, functions, and triggers (like auto-updating `updated_at`). |
| 10 | +* **Modern PostgreSQL Types**: Easy-to-use snippets for `UUID`, `JSONB` (with defaults), `TEXT[]` arrays, and full-text search setup (`TSVECTOR`). |
| 11 | +* **SQLC Configuration**: A ready-to-use `sqlc.yaml` template for both Go (pgx/v5) and Kotlin, including common type overrides. |
| 12 | +* **SQLC v2 Ready**: Includes snippets leveraging SQLC v2 features like named parameters (`@param`) and batch operations with returning values. |
| 13 | +* **Performance & Optimization**: Snippets for `EXPLAIN ANALYZE`, partial indexes, expression indexes, and Row Level Security (RLS) setup. |
| 14 | +* **Migration & Utility**: Helpers for creating extensions, adding/dropping columns, and common SQL expressions. |
| 15 | +* **Clear Descriptions**: Each snippet comes with a concise description of its purpose. |
| 16 | +* **Placeholder Guidance**: Smart placeholders (`${1:name}`, `${2|option1,option2|}`) guide you through snippet completion. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +1. Open **Visual Studio Code**. |
| 21 | +2. Go to the **Extensions** view (Ctrl+Shift+X or Cmd+Shift+X). |
| 22 | +3. Search for `[Your Extension Name Here - e.g., PostgreSQL SQLC Snippets Pro]`. |
| 23 | +4. Click **Install**. |
| 24 | +5. Reload VS Code if prompted. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +Simply start typing the snippet `prefix` in a `.sql` file (or any filetype you configure snippets for, like `.sqlc`, `.query.sql`), and VS Code's IntelliSense will suggest the available snippets. Press `Tab` or `Enter` to insert the snippet. Use `Tab` to navigate through the placeholders. |
| 29 | + |
| 30 | +**Example:** |
| 31 | + |
| 32 | +1. Type `sqlcone` in a `.sql` file. |
| 33 | +2. Press `Tab`. |
| 34 | +3. The following snippet will be inserted: |
| 35 | + ```sql |
| 36 | + -- name: GetUser :one |
| 37 | + SELECT * FROM users |
| 38 | + WHERE id = $1; |
| 39 | + ``` |
| 40 | +4. `GetUser` will be selected. Type your desired query name, then press `Tab` to move to `users`, and so on. |
| 41 | + |
| 42 | +## Available Snippets |
| 43 | + |
| 44 | +Here's a categorized list of the main snippets included: |
| 45 | +
|
| 46 | +### SQLC Core Templates |
| 47 | +
|
| 48 | +| Prefix | Description | |
| 49 | +| :--------- | :-------------------------------- | |
| 50 | +| `sqlcone` | Get single row by ID | |
| 51 | +| `sqlcmany` | Get paginated results | |
| 52 | +| `sqlcinsert`| Insert with returning columns | |
| 53 | +| `sqlcupdate`| Update record | |
| 54 | +| `sqlcdelete`| Delete record | |
| 55 | +| `sqlcbatch`| Batch insert operation | |
| 56 | +
|
| 57 | +### SQLC Advanced Patterns |
| 58 | +
|
| 59 | +| Prefix | Description | |
| 60 | +| :--------- | :-------------------------------- | |
| 61 | +| `sqlcjoin` | Join with aggregation | |
| 62 | +| `sqlcjson` | Query JSONB field | |
| 63 | +| `sqlcarray`| Query array column | |
| 64 | +| `sqlcfts` | Full-text search query | |
| 65 | +| `sqlctx` | Transaction block | |
| 66 | +
|
| 67 | +### SQLC v2 Features |
| 68 | +
|
| 69 | +| Prefix | Description | |
| 70 | +| :------------ | :--------------------------------- | |
| 71 | +| `sqlcv2` | sqlc v2 named parameters | |
| 72 | +| `sqlcv2batch` | sqlc v2 batch with returning | |
| 73 | +| `sqlcv2type` | sqlc v2 with custom types | |
| 74 | +
|
| 75 | +### PostgreSQL DDL (Data Definition Language) |
| 76 | +
|
| 77 | +| Prefix | Description | |
| 78 | +| :---------- | :-------------------------------- | |
| 79 | +| `pgtbl` | Create table with audit columns | |
| 80 | +| `pgidx` | Create index | |
| 81 | +| `pgenum` | Create enum type | |
| 82 | +| `pgfunc` | Create PL/pgSQL function | |
| 83 | +| `pgtrigger` | Auto-update timestamp trigger | |
| 84 | +
|
| 85 | +### PostgreSQL Advanced DML & Queries |
| 86 | +
|
| 87 | +| Prefix | Description | |
| 88 | +| :------------ | :-------------------------------- | |
| 89 | +| `pgcte` | Common Table Expression | |
| 90 | +| `pgjsonb` | JSONB column with default | |
| 91 | +| `pgarray` | Text array column | |
| 92 | +| `pgfts` | Full-text search setup | |
| 93 | +| `pguuid` | Generate UUID v4 (`gen_random_uuid()`) | |
| 94 | +
|
| 95 | +### SQLC Configuration & Comments |
| 96 | +
|
| 97 | +| Prefix | Description | |
| 98 | +| :----------- | :-------------------------------- | |
| 99 | +| `sqlcconfig` | sqlc configuration template (`sqlc.yaml`) | |
| 100 | +| `sqlcmodel` | Add comments for sqlc model | |
| 101 | +
|
| 102 | +### Migration Utilities |
| 103 | +
|
| 104 | +| Prefix | Description | |
| 105 | +| :--------- | :-------------------------------- | |
| 106 | +| `pgext` | Create PostgreSQL extension | |
| 107 | +| `pgaddcol` | Add column to table | |
| 108 | +| `pgdropcol`| Drop column from table | |
| 109 | +
|
| 110 | +### Security |
| 111 | +
|
| 112 | +| Prefix | Description | |
| 113 | +| :-------- | :-------------------------------- | |
| 114 | +| `pgrls` | Row Level Security policy | |
| 115 | +| `pggrant` | Grant table permissions | |
| 116 | +
|
| 117 | +### Performance & Indexing |
| 118 | +
|
| 119 | +| Prefix | Description | |
| 120 | +| :------------- | :-------------------------------- | |
| 121 | +| `pgexpridx` | Create index on expression | |
| 122 | +| `pgpartialidx` | Create partial index | |
| 123 | +
|
| 124 | +### Utility & Testing Snippets |
| 125 | +
|
| 126 | +| Prefix | Description | |
| 127 | +| :------------ | :-------------------------------- | |
| 128 | +| `pgexplain` | Detailed query analysis (`EXPLAIN ANALYZE BUFFERS VERBOSE`) | |
| 129 | +| `pgnow` | Current timestamp (`NOW()`) | |
| 130 | +| `pginterval` | Time interval | |
| 131 | +| `pgjsonbuild` | Build JSONB object | |
| 132 | +| `pgarrayagg` | Aggregate to array | |
| 133 | +| `pgbegin` | Test transaction block (BEGIN/ROLLBACK) | |
| 134 | +
|
| 135 | +*(Note: The `pgexplain` prefix from the `TESTING` section is listed above due to its comprehensiveness. The original JSON has two `pgexplain` entries; this one includes `VERBOSE`.)* |
| 136 | +
|
| 137 | +
|
| 138 | +## Why These Snippets? |
| 139 | +
|
| 140 | +* **Reduce Syntax Errors**: Pre-defined structures minimize typos and syntax mistakes. |
| 141 | +* **Enforce Best Practices**: Snippets incorporate common patterns like audit columns, proper indexing, and SQLC naming conventions. |
| 142 | +* **Accelerate Development**: Spend less time writing boilerplate and more time on business logic. |
| 143 | +* **Full SQLC Compatibility**: Designed to work seamlessly with SQLC's query parsing and code generation. |
| 144 | + |
| 145 | + |
| 146 | +## Contributing |
| 147 | + |
| 148 | +Contributions are welcome! If you have a snippet that you think would be a great addition, or if you have improvements for existing ones: |
| 149 | + |
| 150 | +1. Fork the repository. |
| 151 | +2. Create a new branch (`git checkout -b feature/your-feature-name`). |
| 152 | +3. Add or modify snippets in the `snippets/snippets.json` file (or wherever the source JSON is kept). |
| 153 | +4. Commit your changes (`git commit -am 'Add some feature'`). |
| 154 | +5. Push to the branch (`git push origin feature/your-feature-name`). |
| 155 | +6. Create a new Pull Request. |
| 156 | + |
| 157 | +Please ensure your snippets are well-formatted, include a clear `prefix` and `description`, and use placeholders effectively. |
| 158 | + |
| 159 | +## License |
| 160 | + |
| 161 | +MIT License. |
0 commit comments