@take-out/postgres 0.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +242 -0
  3. package/cli.cjs +3 -0
  4. package/dist/cjs/build.cjs +26 -0
  5. package/dist/cjs/build.js +21 -0
  6. package/dist/cjs/build.js.map +6 -0
  7. package/dist/cjs/build.native.js +29 -0
  8. package/dist/cjs/build.native.js.map +1 -0
  9. package/dist/cjs/cli.cjs +156 -0
  10. package/dist/cjs/cli.js +128 -0
  11. package/dist/cjs/cli.js.map +6 -0
  12. package/dist/cjs/cli.native.js +164 -0
  13. package/dist/cjs/cli.native.js.map +1 -0
  14. package/dist/cjs/createServerHelpers.cjs +37 -0
  15. package/dist/cjs/createServerHelpers.js +28 -0
  16. package/dist/cjs/createServerHelpers.js.map +6 -0
  17. package/dist/cjs/createServerHelpers.native.js +43 -0
  18. package/dist/cjs/createServerHelpers.native.js.map +1 -0
  19. package/dist/cjs/helpers/chunkedQuery.cjs +60 -0
  20. package/dist/cjs/helpers/chunkedQuery.js +51 -0
  21. package/dist/cjs/helpers/chunkedQuery.js.map +6 -0
  22. package/dist/cjs/helpers/chunkedQuery.native.js +73 -0
  23. package/dist/cjs/helpers/chunkedQuery.native.js.map +1 -0
  24. package/dist/cjs/helpers/getDBClient.cjs +172 -0
  25. package/dist/cjs/helpers/getDBClient.js +179 -0
  26. package/dist/cjs/helpers/getDBClient.js.map +6 -0
  27. package/dist/cjs/helpers/getDBClient.native.js +189 -0
  28. package/dist/cjs/helpers/getDBClient.native.js.map +1 -0
  29. package/dist/cjs/index.cjs +59 -0
  30. package/dist/cjs/index.js +45 -0
  31. package/dist/cjs/index.js.map +6 -0
  32. package/dist/cjs/index.native.js +64 -0
  33. package/dist/cjs/index.native.js.map +1 -0
  34. package/dist/cjs/migrate.cjs +117 -0
  35. package/dist/cjs/migrate.js +106 -0
  36. package/dist/cjs/migrate.js.map +6 -0
  37. package/dist/cjs/migrate.native.js +185 -0
  38. package/dist/cjs/migrate.native.js.map +1 -0
  39. package/dist/cjs/scripts/build-migrations.cjs +81 -0
  40. package/dist/cjs/scripts/build-migrations.js +69 -0
  41. package/dist/cjs/scripts/build-migrations.js.map +6 -0
  42. package/dist/cjs/scripts/build-migrations.native.js +86 -0
  43. package/dist/cjs/scripts/build-migrations.native.js.map +1 -0
  44. package/dist/cjs/scripts/drizzle-migrations-sync.cjs +111 -0
  45. package/dist/cjs/scripts/drizzle-migrations-sync.js +101 -0
  46. package/dist/cjs/scripts/drizzle-migrations-sync.js.map +6 -0
  47. package/dist/cjs/scripts/drizzle-migrations-sync.native.js +209 -0
  48. package/dist/cjs/scripts/drizzle-migrations-sync.native.js.map +1 -0
  49. package/dist/cjs/scripts/migration-add.cjs +53 -0
  50. package/dist/cjs/scripts/migration-add.js +40 -0
  51. package/dist/cjs/scripts/migration-add.js.map +6 -0
  52. package/dist/cjs/scripts/migration-add.native.js +72 -0
  53. package/dist/cjs/scripts/migration-add.native.js.map +1 -0
  54. package/dist/cjs/scripts/pg_dump.cjs +49 -0
  55. package/dist/cjs/scripts/pg_dump.js +36 -0
  56. package/dist/cjs/scripts/pg_dump.js.map +6 -0
  57. package/dist/cjs/scripts/pg_dump.native.js +55 -0
  58. package/dist/cjs/scripts/pg_dump.native.js.map +1 -0
  59. package/dist/cjs/scripts/psql.cjs +50 -0
  60. package/dist/cjs/scripts/psql.js +37 -0
  61. package/dist/cjs/scripts/psql.js.map +6 -0
  62. package/dist/cjs/scripts/psql.native.js +56 -0
  63. package/dist/cjs/scripts/psql.native.js.map +1 -0
  64. package/dist/cjs/sql.cjs +40 -0
  65. package/dist/cjs/sql.js +35 -0
  66. package/dist/cjs/sql.js.map +6 -0
  67. package/dist/cjs/sql.native.js +49 -0
  68. package/dist/cjs/sql.native.js.map +1 -0
  69. package/dist/esm/build.js +5 -0
  70. package/dist/esm/build.js.map +6 -0
  71. package/dist/esm/build.mjs +3 -0
  72. package/dist/esm/build.mjs.map +1 -0
  73. package/dist/esm/build.native.js +3 -0
  74. package/dist/esm/build.native.js.map +1 -0
  75. package/dist/esm/cli.js +134 -0
  76. package/dist/esm/cli.js.map +6 -0
  77. package/dist/esm/cli.mjs +157 -0
  78. package/dist/esm/cli.mjs.map +1 -0
  79. package/dist/esm/cli.native.js +162 -0
  80. package/dist/esm/cli.native.js.map +1 -0
  81. package/dist/esm/createServerHelpers.js +13 -0
  82. package/dist/esm/createServerHelpers.js.map +6 -0
  83. package/dist/esm/createServerHelpers.mjs +14 -0
  84. package/dist/esm/createServerHelpers.mjs.map +1 -0
  85. package/dist/esm/createServerHelpers.native.js +17 -0
  86. package/dist/esm/createServerHelpers.native.js.map +1 -0
  87. package/dist/esm/helpers/chunkedQuery.js +35 -0
  88. package/dist/esm/helpers/chunkedQuery.js.map +6 -0
  89. package/dist/esm/helpers/chunkedQuery.mjs +36 -0
  90. package/dist/esm/helpers/chunkedQuery.mjs.map +1 -0
  91. package/dist/esm/helpers/chunkedQuery.native.js +46 -0
  92. package/dist/esm/helpers/chunkedQuery.native.js.map +1 -0
  93. package/dist/esm/helpers/getDBClient.js +155 -0
  94. package/dist/esm/helpers/getDBClient.js.map +6 -0
  95. package/dist/esm/helpers/getDBClient.mjs +136 -0
  96. package/dist/esm/helpers/getDBClient.mjs.map +1 -0
  97. package/dist/esm/helpers/getDBClient.native.js +150 -0
  98. package/dist/esm/helpers/getDBClient.native.js.map +1 -0
  99. package/dist/esm/index.js +29 -0
  100. package/dist/esm/index.js.map +6 -0
  101. package/dist/esm/index.mjs +18 -0
  102. package/dist/esm/index.mjs.map +1 -0
  103. package/dist/esm/index.native.js +20 -0
  104. package/dist/esm/index.native.js.map +1 -0
  105. package/dist/esm/migrate.js +91 -0
  106. package/dist/esm/migrate.js.map +6 -0
  107. package/dist/esm/migrate.mjs +94 -0
  108. package/dist/esm/migrate.mjs.map +1 -0
  109. package/dist/esm/migrate.native.js +159 -0
  110. package/dist/esm/migrate.native.js.map +1 -0
  111. package/dist/esm/scripts/build-migrations.js +46 -0
  112. package/dist/esm/scripts/build-migrations.js.map +6 -0
  113. package/dist/esm/scripts/build-migrations.mjs +47 -0
  114. package/dist/esm/scripts/build-migrations.mjs.map +1 -0
  115. package/dist/esm/scripts/build-migrations.native.js +49 -0
  116. package/dist/esm/scripts/build-migrations.native.js.map +1 -0
  117. package/dist/esm/scripts/drizzle-migrations-sync.js +87 -0
  118. package/dist/esm/scripts/drizzle-migrations-sync.js.map +6 -0
  119. package/dist/esm/scripts/drizzle-migrations-sync.mjs +88 -0
  120. package/dist/esm/scripts/drizzle-migrations-sync.mjs.map +1 -0
  121. package/dist/esm/scripts/drizzle-migrations-sync.native.js +183 -0
  122. package/dist/esm/scripts/drizzle-migrations-sync.native.js.map +1 -0
  123. package/dist/esm/scripts/migration-add.js +25 -0
  124. package/dist/esm/scripts/migration-add.js.map +6 -0
  125. package/dist/esm/scripts/migration-add.mjs +30 -0
  126. package/dist/esm/scripts/migration-add.mjs.map +1 -0
  127. package/dist/esm/scripts/migration-add.native.js +46 -0
  128. package/dist/esm/scripts/migration-add.native.js.map +1 -0
  129. package/dist/esm/scripts/pg_dump.js +20 -0
  130. package/dist/esm/scripts/pg_dump.js.map +6 -0
  131. package/dist/esm/scripts/pg_dump.mjs +26 -0
  132. package/dist/esm/scripts/pg_dump.mjs.map +1 -0
  133. package/dist/esm/scripts/pg_dump.native.js +29 -0
  134. package/dist/esm/scripts/pg_dump.native.js.map +1 -0
  135. package/dist/esm/scripts/psql.js +21 -0
  136. package/dist/esm/scripts/psql.js.map +6 -0
  137. package/dist/esm/scripts/psql.mjs +27 -0
  138. package/dist/esm/scripts/psql.mjs.map +1 -0
  139. package/dist/esm/scripts/psql.native.js +30 -0
  140. package/dist/esm/scripts/psql.native.js.map +1 -0
  141. package/dist/esm/sql.js +19 -0
  142. package/dist/esm/sql.js.map +6 -0
  143. package/dist/esm/sql.mjs +15 -0
  144. package/dist/esm/sql.mjs.map +1 -0
  145. package/dist/esm/sql.native.js +21 -0
  146. package/dist/esm/sql.native.js.map +1 -0
  147. package/package.json +67 -0
  148. package/src/build.ts +2 -0
  149. package/src/cli.ts +153 -0
  150. package/src/createServerHelpers.ts +20 -0
  151. package/src/helpers/chunkedQuery.ts +91 -0
  152. package/src/helpers/getDBClient.ts +264 -0
  153. package/src/index.ts +36 -0
  154. package/src/migrate.ts +192 -0
  155. package/src/scripts/build-migrations.ts +66 -0
  156. package/src/scripts/drizzle-migrations-sync.ts +179 -0
  157. package/src/scripts/migration-add.ts +54 -0
  158. package/src/scripts/pg_dump.ts +46 -0
  159. package/src/scripts/psql.ts +51 -0
  160. package/src/sql.ts +36 -0
  161. package/types/build.d.ts +2 -0
  162. package/types/build.d.ts.map +1 -0
  163. package/types/cli.d.ts +3 -0
  164. package/types/cli.d.ts.map +1 -0
  165. package/types/createServerHelpers.d.ts +9 -0
  166. package/types/createServerHelpers.d.ts.map +1 -0
  167. package/types/helpers/chunkedQuery.d.ts +17 -0
  168. package/types/helpers/chunkedQuery.d.ts.map +1 -0
  169. package/types/helpers/getDBClient.d.ts +11 -0
  170. package/types/helpers/getDBClient.d.ts.map +1 -0
  171. package/types/index.d.ts +10 -0
  172. package/types/index.d.ts.map +1 -0
  173. package/types/migrate.d.ts +25 -0
  174. package/types/migrate.d.ts.map +1 -0
  175. package/types/scripts/build-migrations.d.ts +8 -0
  176. package/types/scripts/build-migrations.d.ts.map +1 -0
  177. package/types/scripts/drizzle-migrations-sync.d.ts +11 -0
  178. package/types/scripts/drizzle-migrations-sync.d.ts.map +1 -0
  179. package/types/scripts/migration-add.d.ts +6 -0
  180. package/types/scripts/migration-add.d.ts.map +1 -0
  181. package/types/scripts/pg_dump.d.ts +11 -0
  182. package/types/scripts/pg_dump.d.ts.map +1 -0
  183. package/types/scripts/psql.d.ts +11 -0
  184. package/types/scripts/psql.d.ts.map +1 -0
  185. package/types/sql.d.ts +9 -0
  186. package/types/sql.d.ts.map +1 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 vxrn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,242 @@
1
+ # @take-out/postgres
2
+
3
+ PostgreSQL database utilities and migration system for vxrn applications.
4
+
5
+ Note: feel free to eject from this package and just copy parts of it out, it's
6
+ not the most flexible abstraction just meant to provide a starting point.
7
+
8
+ ## Features
9
+
10
+ - **Connection Management**: Production-grade PostgreSQL connection pooling with
11
+ retry logic and monitoring
12
+ - **Migration System**: Support for both SQL (Drizzle Kit) and TypeScript
13
+ migrations
14
+ - **Database Utilities**: Chunked query processing, SQL template helpers, and
15
+ more
16
+ - **CLI Tools**: Interactive psql, pg_dump wrappers, and migration commands
17
+ - **Type Safety**: Full TypeScript support with Drizzle ORM integration
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ bun add @take-out/postgres
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ### Basic Setup
28
+
29
+ ```typescript
30
+ import { createPool, createDb } from '@take-out/postgres'
31
+ import * as schema from './schema'
32
+
33
+ // create raw pg pool
34
+ const pool = createPool(process.env.DATABASE_URL)
35
+
36
+ // create drizzle instance with schema
37
+ const db = createDb(process.env.DATABASE_URL, schema)
38
+ ```
39
+
40
+ ### Connection Management
41
+
42
+ ```typescript
43
+ import { getDBClient, queryDb } from '@take-out/postgres/helpers'
44
+
45
+ // get client with automatic retry and monitoring
46
+ const client = await getDBClient()
47
+ try {
48
+ await client.query('SELECT * FROM users')
49
+ } finally {
50
+ client.release()
51
+ }
52
+
53
+ // or use queryDb for automatic client management
54
+ const result = await queryDb('SELECT * FROM users WHERE id = $1', [userId])
55
+ ```
56
+
57
+ ### SQL Template Helper
58
+
59
+ ```typescript
60
+ import { sql } from '@take-out/postgres'
61
+
62
+ const users = await sql<User>`
63
+ SELECT * FROM users WHERE created_at > ${since}
64
+ `
65
+ ```
66
+
67
+ ### Chunked Query Processing
68
+
69
+ ```typescript
70
+ import { processInChunks, updateInChunks } from '@take-out/postgres/helpers'
71
+
72
+ // process large datasets in chunks
73
+ await processInChunks(
74
+ 'SELECT * FROM users',
75
+ async (rows) => {
76
+ for (const user of rows) {
77
+ await processUser(user)
78
+ }
79
+ },
80
+ { chunkSize: 1000 },
81
+ )
82
+
83
+ // bulk update with transformer
84
+ await updateInChunks('users', users, (user) => ({ ...user, processed: true }), {
85
+ chunkSize: 500,
86
+ })
87
+ ```
88
+
89
+ ### Migrations
90
+
91
+ The package includes a custom migration runner that supports both SQL and
92
+ TypeScript migrations:
93
+
94
+ ```typescript
95
+ import { migrate } from '@take-out/postgres/migrate'
96
+
97
+ await migrate({
98
+ connectionString: process.env.DATABASE_URL,
99
+ migrationsPath: './migrations',
100
+ // optional: create additional databases (e.g., for Zero sync)
101
+ createDatabases: ['myapp_cvr', 'myapp_change'],
102
+ })
103
+ ```
104
+
105
+ ## CLI
106
+
107
+ The package includes a CLI with several database utilities:
108
+
109
+ ```bash
110
+ # show help
111
+ db --help
112
+
113
+ # connect to database with psql
114
+ db psql
115
+
116
+ # create a new migration
117
+ db migrate:add my-migration-name
118
+
119
+ # run migrations
120
+ db migrate
121
+
122
+ # dump database
123
+ db pg_dump > backup.sql
124
+
125
+ # sync drizzle migrations to TypeScript
126
+ db sync
127
+ ```
128
+
129
+ ### CLI Configuration
130
+
131
+ The CLI uses environment variables for configuration:
132
+
133
+ - `ZERO_UPSTREAM_DB`: Main database connection string
134
+ - `ZERO_CVR_DB`: CVR database for Zero sync (optional)
135
+ - `ZERO_CHANGE_DB`: Change database for Zero sync (optional)
136
+
137
+ ## Migration System
138
+
139
+ ### SQL Migrations (Drizzle Kit)
140
+
141
+ 1. Update your schema files
142
+ 2. Generate migration: `drizzle-kit generate`
143
+ 3. Sync to TypeScript: `db sync`
144
+ 4. Run migrations: `db migrate`
145
+
146
+ ### TypeScript Migrations
147
+
148
+ For custom data migrations:
149
+
150
+ ```bash
151
+ db migrate:add fix-user-data
152
+ ```
153
+
154
+ This creates a new TypeScript file:
155
+
156
+ ```typescript
157
+ import type { PoolClient } from 'pg'
158
+
159
+ export async function up(client: PoolClient) {
160
+ // your migration code here
161
+ await client.query(`
162
+ UPDATE users SET email = lower(email)
163
+ `)
164
+ }
165
+ ```
166
+
167
+ ### Migration Sync
168
+
169
+ The `db sync` command automatically wraps Drizzle-generated SQL migrations in
170
+ TypeScript:
171
+
172
+ ```typescript
173
+ // migrations/0001_example.sql → migrations/0001_example.ts
174
+ import type { PoolClient } from 'pg'
175
+ import sql from './0001_example.sql?raw'
176
+
177
+ export async function up(client: PoolClient) {
178
+ await client.query(sql)
179
+ }
180
+ ```
181
+
182
+ ## Connection Pooling
183
+
184
+ The package includes production-grade connection pooling with:
185
+
186
+ - **Retry Logic**: Exponential backoff with configurable attempts
187
+ - **Pool Monitoring**: Tracks connection saturation and automatically resets
188
+ - **Idle Timeout**: Configurable client and server idle timeouts
189
+ - **Error Handling**: Detects "too many connections" errors and manages pool
190
+ lifecycle
191
+
192
+ ## API
193
+
194
+ ### Core Functions
195
+
196
+ #### `createPool(connectionString: string): Pool`
197
+
198
+ Creates a raw PostgreSQL connection pool.
199
+
200
+ #### `createDb(connectionString: string, schema: any): DrizzleDb`
201
+
202
+ Creates a Drizzle ORM instance with the provided schema.
203
+
204
+ #### `getDb(): DrizzleDb`
205
+
206
+ Singleton pattern for database instance (uses `ZERO_UPSTREAM_DB` env var).
207
+
208
+ ### Helpers
209
+
210
+ #### `getDBClient(options?): Promise<PoolClient>`
211
+
212
+ Get a database client with retry logic and monitoring.
213
+
214
+ Options:
215
+
216
+ - `pool?: Pool` - Custom pool instance
217
+ - `retries?: number` - Retry attempts (default: 3)
218
+ - `onRetry?: (error: Error, attempt: number) => void` - Retry callback
219
+
220
+ #### `queryDb<T>(query: string, params?: any[]): Promise<T[]>`
221
+
222
+ Execute a query with automatic client management.
223
+
224
+ #### `processInChunks(query, processor, options?)`
225
+
226
+ Process large result sets in chunks to avoid memory issues.
227
+
228
+ #### `updateInChunks(table, rows, transformer, options?)`
229
+
230
+ Bulk update rows with a transformer function.
231
+
232
+ ## Environment Variables
233
+
234
+ - `ZERO_UPSTREAM_DB`: PostgreSQL connection string (required)
235
+ - `ZERO_CVR_DB`: CVR database for Zero sync (optional)
236
+ - `ZERO_CHANGE_DB`: Change database for Zero sync (optional)
237
+ - `IS_SERVERLESS`: Set to 'true' for serverless environments (affects pool
238
+ config)
239
+
240
+ ## License
241
+
242
+ MIT
package/cli.cjs ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+
3
+ require('./dist/cjs/cli.cjs')
@@ -0,0 +1,26 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: !0
9
+ });
10
+ },
11
+ __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
13
+ get: () => from[key],
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ return to;
17
+ };
18
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
19
+ value: !0
20
+ }), mod);
21
+ var build_exports = {};
22
+ __export(build_exports, {
23
+ buildMigrations: () => import_build_migrations.buildMigrations
24
+ });
25
+ module.exports = __toCommonJS(build_exports);
26
+ var import_build_migrations = require("./scripts/build-migrations.cjs");
@@ -0,0 +1,21 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: !0 });
8
+ }, __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from == "object" || typeof from == "function")
10
+ for (let key of __getOwnPropNames(from))
11
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
15
+ var build_exports = {};
16
+ __export(build_exports, {
17
+ buildMigrations: () => import_build_migrations.buildMigrations
18
+ });
19
+ module.exports = __toCommonJS(build_exports);
20
+ var import_build_migrations = require("./scripts/build-migrations");
21
+ //# sourceMappingURL=build.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/build.ts"],
4
+ "mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,8BAAgC;",
5
+ "names": []
6
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
+ value: !0
22
+ }), mod);
23
+ var build_exports = {};
24
+ __export(build_exports, {
25
+ buildMigrations: () => import_build_migrations.buildMigrations
26
+ });
27
+ module.exports = __toCommonJS(build_exports);
28
+ var import_build_migrations = require("./scripts/build-migrations.native.js");
29
+ //# sourceMappingURL=build.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","build_exports","__export","buildMigrations","import_build_migrations","module","exports"],"sources":["../../src/build.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,QAAA,CAAAD,aAAA;EAAAE,eAAA,EAAAA,CAAA,KAAAC,uBAAA,CAAAD;AAAA;AACAE,MAAA,CAAAC,OAAA,GAAAV,YAAA,CAAgCK,aAAA","ignoreList":[]}
@@ -0,0 +1,156 @@
1
+ #!/usr/bin/env node
2
+ var import_citty = require("citty"),
3
+ import_node_path = require("node:path"),
4
+ import_build_migrations = require("./scripts/build-migrations.cjs"),
5
+ import_drizzle_migrations_sync = require("./scripts/drizzle-migrations-sync.cjs"),
6
+ import_migration_add = require("./scripts/migration-add.cjs"),
7
+ import_pg_dump = require("./scripts/pg_dump.cjs"),
8
+ import_psql = require("./scripts/psql.cjs");
9
+ const syncDrizzle = (0, import_citty.defineCommand)({
10
+ meta: {
11
+ name: "sync-drizzle",
12
+ description: "Sync Drizzle SQL migrations to TypeScript wrappers"
13
+ },
14
+ args: {
15
+ dir: {
16
+ type: "string",
17
+ description: "Migrations directory",
18
+ required: !1,
19
+ default: "./src/database/migrations"
20
+ }
21
+ },
22
+ async run({
23
+ args
24
+ }) {
25
+ const migrationsDir = (0, import_node_path.join)(process.cwd(), args.dir);
26
+ console.info(`Syncing migrations in ${migrationsDir}`), await (0, import_drizzle_migrations_sync.syncDrizzleMigrations)({
27
+ migrationsDir
28
+ });
29
+ }
30
+ }),
31
+ buildMigrations = (0, import_citty.defineCommand)({
32
+ meta: {
33
+ name: "build-migrations",
34
+ description: "Build migration bundle for deployment"
35
+ },
36
+ args: {
37
+ dir: {
38
+ type: "string",
39
+ description: "Migrations directory",
40
+ required: !1,
41
+ default: "./src/database/migrations"
42
+ },
43
+ out: {
44
+ type: "string",
45
+ description: "Output file name",
46
+ required: !1,
47
+ default: "migrate-dist.js"
48
+ }
49
+ },
50
+ async run({
51
+ args
52
+ }) {
53
+ const migrationsDir = (0, import_node_path.join)(process.cwd(), args.dir);
54
+ await (0, import_build_migrations.buildMigrations)({
55
+ migrationsDir,
56
+ outFile: args.out
57
+ });
58
+ }
59
+ }),
60
+ migrationAdd = (0, import_citty.defineCommand)({
61
+ meta: {
62
+ name: "migrate:add",
63
+ description: "Create a new custom TypeScript migration"
64
+ },
65
+ args: {
66
+ name: {
67
+ type: "positional",
68
+ description: "Migration name",
69
+ required: !1
70
+ },
71
+ dir: {
72
+ type: "string",
73
+ description: "Migrations directory",
74
+ required: !1,
75
+ default: "./src/database/migrations"
76
+ }
77
+ },
78
+ async run({
79
+ args
80
+ }) {
81
+ const migrationsDir = (0, import_node_path.join)(process.cwd(), args.dir);
82
+ (0, import_migration_add.addMigration)({
83
+ migrationsDir,
84
+ name: args.name
85
+ });
86
+ }
87
+ }),
88
+ psql = (0, import_citty.defineCommand)({
89
+ meta: {
90
+ name: "psql",
91
+ description: "Connect to PostgreSQL database with psql"
92
+ },
93
+ args: {
94
+ connectionString: {
95
+ type: "string",
96
+ description: "PostgreSQL connection string",
97
+ required: !1
98
+ },
99
+ query: {
100
+ type: "string",
101
+ description: "Query to execute",
102
+ required: !1
103
+ }
104
+ },
105
+ async run({
106
+ args
107
+ }) {
108
+ const connectionString = args.connectionString || process.env.ZERO_UPSTREAM_DB;
109
+ connectionString || (console.error("No connection string provided. Set ZERO_UPSTREAM_DB or pass --connectionString"), process.exit(1));
110
+ const exitCode = (0, import_psql.runPsql)({
111
+ connectionString,
112
+ query: args.query
113
+ });
114
+ process.exit(exitCode || 0);
115
+ }
116
+ }),
117
+ pgDump = (0, import_citty.defineCommand)({
118
+ meta: {
119
+ name: "pg_dump",
120
+ description: "Dump PostgreSQL database using pg_dump"
121
+ },
122
+ args: {
123
+ connectionString: {
124
+ type: "string",
125
+ description: "PostgreSQL connection string",
126
+ required: !1
127
+ }
128
+ },
129
+ async run({
130
+ args
131
+ }) {
132
+ const connectionString = args.connectionString || process.env.ZERO_UPSTREAM_DB;
133
+ connectionString || (console.error("No connection string provided. Set ZERO_UPSTREAM_DB or pass --connectionString"), process.exit(1));
134
+ const cliArgs = process.argv.slice(3),
135
+ exitCode = (0, import_pg_dump.runPgDump)({
136
+ connectionString,
137
+ args: cliArgs
138
+ });
139
+ process.exit(exitCode || 0);
140
+ }
141
+ }),
142
+ main = (0, import_citty.defineCommand)({
143
+ meta: {
144
+ name: "postgres",
145
+ description: "PostgreSQL database utilities and migration tools",
146
+ version: "0.0.1"
147
+ },
148
+ subCommands: {
149
+ "sync-drizzle": syncDrizzle,
150
+ "build-migrations": buildMigrations,
151
+ "migrate:add": migrationAdd,
152
+ psql,
153
+ pg_dump: pgDump
154
+ }
155
+ });
156
+ (0, import_citty.runMain)(main);
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env node
2
+ var import_citty = require("citty"), import_node_path = require("node:path"), import_build_migrations = require("./scripts/build-migrations"), import_drizzle_migrations_sync = require("./scripts/drizzle-migrations-sync"), import_migration_add = require("./scripts/migration-add"), import_pg_dump = require("./scripts/pg_dump"), import_psql = require("./scripts/psql");
3
+ const syncDrizzle = (0, import_citty.defineCommand)({
4
+ meta: {
5
+ name: "sync-drizzle",
6
+ description: "Sync Drizzle SQL migrations to TypeScript wrappers"
7
+ },
8
+ args: {
9
+ dir: {
10
+ type: "string",
11
+ description: "Migrations directory",
12
+ required: !1,
13
+ default: "./src/database/migrations"
14
+ }
15
+ },
16
+ async run({ args }) {
17
+ const migrationsDir = (0, import_node_path.join)(process.cwd(), args.dir);
18
+ console.info(`Syncing migrations in ${migrationsDir}`), await (0, import_drizzle_migrations_sync.syncDrizzleMigrations)({ migrationsDir });
19
+ }
20
+ }), buildMigrations = (0, import_citty.defineCommand)({
21
+ meta: {
22
+ name: "build-migrations",
23
+ description: "Build migration bundle for deployment"
24
+ },
25
+ args: {
26
+ dir: {
27
+ type: "string",
28
+ description: "Migrations directory",
29
+ required: !1,
30
+ default: "./src/database/migrations"
31
+ },
32
+ out: {
33
+ type: "string",
34
+ description: "Output file name",
35
+ required: !1,
36
+ default: "migrate-dist.js"
37
+ }
38
+ },
39
+ async run({ args }) {
40
+ const migrationsDir = (0, import_node_path.join)(process.cwd(), args.dir);
41
+ await (0, import_build_migrations.buildMigrations)({
42
+ migrationsDir,
43
+ outFile: args.out
44
+ });
45
+ }
46
+ }), migrationAdd = (0, import_citty.defineCommand)({
47
+ meta: {
48
+ name: "migrate:add",
49
+ description: "Create a new custom TypeScript migration"
50
+ },
51
+ args: {
52
+ name: {
53
+ type: "positional",
54
+ description: "Migration name",
55
+ required: !1
56
+ },
57
+ dir: {
58
+ type: "string",
59
+ description: "Migrations directory",
60
+ required: !1,
61
+ default: "./src/database/migrations"
62
+ }
63
+ },
64
+ async run({ args }) {
65
+ const migrationsDir = (0, import_node_path.join)(process.cwd(), args.dir);
66
+ (0, import_migration_add.addMigration)({ migrationsDir, name: args.name });
67
+ }
68
+ }), psql = (0, import_citty.defineCommand)({
69
+ meta: {
70
+ name: "psql",
71
+ description: "Connect to PostgreSQL database with psql"
72
+ },
73
+ args: {
74
+ connectionString: {
75
+ type: "string",
76
+ description: "PostgreSQL connection string",
77
+ required: !1
78
+ },
79
+ query: {
80
+ type: "string",
81
+ description: "Query to execute",
82
+ required: !1
83
+ }
84
+ },
85
+ async run({ args }) {
86
+ const connectionString = args.connectionString || process.env.ZERO_UPSTREAM_DB;
87
+ connectionString || (console.error(
88
+ "No connection string provided. Set ZERO_UPSTREAM_DB or pass --connectionString"
89
+ ), process.exit(1));
90
+ const exitCode = (0, import_psql.runPsql)({ connectionString, query: args.query });
91
+ process.exit(exitCode || 0);
92
+ }
93
+ }), pgDump = (0, import_citty.defineCommand)({
94
+ meta: {
95
+ name: "pg_dump",
96
+ description: "Dump PostgreSQL database using pg_dump"
97
+ },
98
+ args: {
99
+ connectionString: {
100
+ type: "string",
101
+ description: "PostgreSQL connection string",
102
+ required: !1
103
+ }
104
+ },
105
+ async run({ args }) {
106
+ const connectionString = args.connectionString || process.env.ZERO_UPSTREAM_DB;
107
+ connectionString || (console.error(
108
+ "No connection string provided. Set ZERO_UPSTREAM_DB or pass --connectionString"
109
+ ), process.exit(1));
110
+ const cliArgs = process.argv.slice(3), exitCode = (0, import_pg_dump.runPgDump)({ connectionString, args: cliArgs });
111
+ process.exit(exitCode || 0);
112
+ }
113
+ }), main = (0, import_citty.defineCommand)({
114
+ meta: {
115
+ name: "postgres",
116
+ description: "PostgreSQL database utilities and migration tools",
117
+ version: "0.0.1"
118
+ },
119
+ subCommands: {
120
+ "sync-drizzle": syncDrizzle,
121
+ "build-migrations": buildMigrations,
122
+ "migrate:add": migrationAdd,
123
+ psql,
124
+ pg_dump: pgDump
125
+ }
126
+ });
127
+ (0, import_citty.runMain)(main);
128
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/cli.ts"],
4
+ "mappings": "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",
5
+ "names": ["buildMigrationsRun"]
6
+ }