@bockit/sequel 0.0.0-stage → 0.2.0
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.
- package/LICENSE +202 -0
- package/README.md +227 -2
- package/THIRD_PARTY_NOTICES.md +268 -0
- package/binaries.json +106 -0
- package/dist/binaries/cache.d.ts +6 -0
- package/dist/binaries/cache.js +36 -0
- package/dist/binaries/ensure-binary.d.ts +10 -0
- package/dist/binaries/ensure-binary.js +85 -0
- package/dist/binaries/manifest.d.ts +30 -0
- package/dist/binaries/manifest.js +112 -0
- package/dist/binaries/platform.d.ts +3 -0
- package/dist/binaries/platform.js +10 -0
- package/dist/binaries/unpack.d.ts +2 -0
- package/dist/binaries/unpack.js +35 -0
- package/dist/binaries/verify.d.ts +2 -0
- package/dist/binaries/verify.js +16 -0
- package/dist/check.d.ts +9 -0
- package/dist/check.js +178 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +339 -0
- package/dist/config/define-config.d.ts +24 -0
- package/dist/config/define-config.js +4 -0
- package/dist/config/load-config.d.ts +6 -0
- package/dist/config/load-config.js +34 -0
- package/dist/config/resolve-config.d.ts +35 -0
- package/dist/config/resolve-config.js +159 -0
- package/dist/db/connection.d.ts +6 -0
- package/dist/db/connection.js +38 -0
- package/dist/db/shadow.d.ts +11 -0
- package/dist/db/shadow.js +68 -0
- package/dist/engine/client.d.ts +24 -0
- package/dist/engine/client.js +123 -0
- package/dist/engine/diff.d.ts +12 -0
- package/dist/engine/diff.js +51 -0
- package/dist/errors.d.ts +3 -0
- package/dist/errors.js +5 -0
- package/dist/extras/snapshot.d.ts +3 -0
- package/dist/extras/snapshot.js +99 -0
- package/dist/files.d.ts +1 -0
- package/dist/files.js +15 -0
- package/dist/generate/generate-modules.d.ts +17 -0
- package/dist/generate/generate-modules.js +97 -0
- package/dist/generate/modules.d.ts +16 -0
- package/dist/generate/modules.js +118 -0
- package/dist/generate/pg-dump.d.ts +8 -0
- package/dist/generate/pg-dump.js +66 -0
- package/dist/generate/plugin-wrapper.d.ts +3 -0
- package/dist/generate/plugin-wrapper.js +20 -0
- package/dist/generate/sqlc.d.ts +27 -0
- package/dist/generate/sqlc.js +113 -0
- package/dist/generate/watch.d.ts +16 -0
- package/dist/generate/watch.js +73 -0
- package/dist/generator/generate.d.ts +21 -0
- package/dist/generator/generate.js +440 -0
- package/dist/generator/keys.d.ts +4 -0
- package/dist/generator/keys.js +24 -0
- package/dist/generator/lexer.d.ts +12 -0
- package/dist/generator/lexer.js +210 -0
- package/dist/generator/main.d.ts +1 -0
- package/dist/generator/main.js +12 -0
- package/dist/generator/pg-array-helper.d.ts +2 -0
- package/dist/generator/pg-array-helper.js +53 -0
- package/dist/generator/plugin.d.ts +6 -0
- package/dist/generator/plugin.js +26 -0
- package/dist/generator/request.d.ts +57 -0
- package/dist/generator/request.js +96 -0
- package/dist/generator/select-list.d.ts +27 -0
- package/dist/generator/select-list.js +580 -0
- package/dist/generator/strings.d.ts +2 -0
- package/dist/generator/strings.js +34 -0
- package/dist/generator/types.d.ts +23 -0
- package/dist/generator/types.js +216 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/migrate/adopt-prisma.d.ts +23 -0
- package/dist/migrate/adopt-prisma.js +119 -0
- package/dist/migrate/deploy.d.ts +7 -0
- package/dist/migrate/deploy.js +23 -0
- package/dist/migrate/drift.d.ts +10 -0
- package/dist/migrate/drift.js +36 -0
- package/dist/migrate/files.d.ts +19 -0
- package/dist/migrate/files.js +97 -0
- package/dist/migrate/history.d.ts +20 -0
- package/dist/migrate/history.js +54 -0
- package/dist/migrate/lint.d.ts +21 -0
- package/dist/migrate/lint.js +153 -0
- package/dist/migrate/new.d.ts +13 -0
- package/dist/migrate/new.js +41 -0
- package/dist/migrate/runner.d.ts +28 -0
- package/dist/migrate/runner.js +124 -0
- package/dist/migrate/sql-text.d.ts +33 -0
- package/dist/migrate/sql-text.js +233 -0
- package/dist/schema/schema.d.ts +16 -0
- package/dist/schema/schema.js +111 -0
- package/dist/schema/unsupported.d.ts +3 -0
- package/dist/schema/unsupported.js +36 -0
- package/dist/schema/wasm.d.ts +8 -0
- package/dist/schema/wasm.js +32 -0
- package/package.json +50 -4
- package/vendor/prisma-schema-wasm/LICENSE +201 -0
- package/vendor/prisma-schema-wasm/README.md +32 -0
- package/vendor/prisma-schema-wasm/package.json +3 -0
- package/vendor/prisma-schema-wasm/prisma_schema_build.js +539 -0
- package/vendor/prisma-schema-wasm/prisma_schema_build_bg.wasm +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,228 @@
|
|
|
1
|
-
#
|
|
1
|
+
# sequel
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
One CLI for a TypeScript app's Postgres data layer:
|
|
4
|
+
|
|
5
|
+
- **Schema:** keep Prisma's schema language as the source of truth, as a folder of `.prisma` files.
|
|
6
|
+
- **Migrations:** reviewable SQL that is safe to run in production. sequel generates it without prompts, lints it, and applies it with its own runner.
|
|
7
|
+
- **Queries:** real SQL in `.sql` files, with typed TypeScript results, in the style of Kotlin's SQLDelight.
|
|
8
|
+
|
|
9
|
+
Nothing from sequel runs in your app except the generated query code, which calls [`pg`](https://node-postgres.com). Your app drops the `prisma` and `@prisma/client` packages and every install hook.
|
|
10
|
+
|
|
11
|
+
> **Status:** early development. Every command works, but sequel hasn't been adopted by an app yet, and commands and config may change before the first release. See [Status](#status).
|
|
12
|
+
|
|
13
|
+
## Why
|
|
14
|
+
|
|
15
|
+
Prisma is three tools sold as one: a schema language, migration tooling, and a query client. The schema language is good. In two production apps the other two caused most of a year's database incidents:
|
|
16
|
+
|
|
17
|
+
- **The generated client** comes with a `postinstall` that can regenerate the wrong project's client. It is held in memory by long-lived workers, so they reject new enum values until restarted. It bakes in column defaults, and it selects every column it knows about, so dropping one before a restart breaks every insert.
|
|
18
|
+
- **`migrate dev`** blocks without a terminal on any data-loss warning, and asks for resets that would wipe a dev database. It also keeps trying to drop what the schema language can't express: vector columns, collations, CHECKs, partial indexes, triggers and views.
|
|
19
|
+
- **The query DSL** can't express row locks, `ON CONFLICT … GREATEST`, recursive CTEs, window functions, `DISTINCT ON`, vector search or advisory locks. So real queries end up as raw SQL with hand-written types.
|
|
20
|
+
|
|
21
|
+
sequel keeps the schema language and replaces the other two:
|
|
22
|
+
|
|
23
|
+
- **Migrations** are SQL you read and commit. The runner gives each file its own transaction and sets lock and statement timeouts.
|
|
24
|
+
- **Queries** are SQL you write. sqlc and the database itself type them, including outer-join nullability, which tools that only ask Postgres to describe the query get wrong.
|
|
25
|
+
|
|
26
|
+
## How it works
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
prisma/*.prisma ─(schema WASM: validate, format)
|
|
30
|
+
│
|
|
31
|
+
├─(schema engine: diff)─► migrations/**/<ts>_<name>.sql ─(Squawk)─► sequel runner ─► Postgres
|
|
32
|
+
│ │
|
|
33
|
+
<module>/queries/*.sql ─(sqlc + database analyzer, against the migrated test DB)─► sequel's generator ─► *.ts
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
sequel is built on:
|
|
37
|
+
- Prisma's schema parser (vendored WASM);
|
|
38
|
+
- Prisma's native schema engine;
|
|
39
|
+
- [sqlc](https://sqlc.dev) with its database analyzer;
|
|
40
|
+
- [Squawk](https://squawkhq.com).
|
|
41
|
+
|
|
42
|
+
It fetches each tool as a pinned binary on first use and checks it against a sha256 recorded in sequel. Nothing downloads at install time.
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
Requirements:
|
|
47
|
+
- Node 24 or newer;
|
|
48
|
+
- Postgres (developed against 18);
|
|
49
|
+
- `pg_dump` on `PATH`, at your server's major version or newer;
|
|
50
|
+
- macOS on Apple silicon, Linux x64 or Linux arm64.
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
yarn add --dev @bockit/sequel
|
|
54
|
+
yarn add pg
|
|
55
|
+
yarn add --dev @types/pg
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Quick start
|
|
59
|
+
|
|
60
|
+
**1. Configure** `sequel.config.ts` at your app's root (illustrative):
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { defineConfig } from '@bockit/sequel'
|
|
64
|
+
|
|
65
|
+
export default defineConfig({
|
|
66
|
+
schema: 'prisma',
|
|
67
|
+
migrations: 'prisma/migrations',
|
|
68
|
+
queries: 'src/**/queries',
|
|
69
|
+
snapshot: 'prisma/snapshot.sql',
|
|
70
|
+
databases: {
|
|
71
|
+
target: process.env.DATABASE_URL,
|
|
72
|
+
test: process.env.TEST_DATABASE_URL,
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The schema's `datasource` block has no `url`: sequel supplies the connection.
|
|
78
|
+
|
|
79
|
+
**2. Change the schema, then make a migration:**
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
sequel format
|
|
83
|
+
sequel migrate new add_rate_limits
|
|
84
|
+
# review and edit prisma/migrations/<timestamp>_add_rate_limits.sql
|
|
85
|
+
sequel migrate deploy
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**3. Write a query** next to the module that uses it, in `src/data/queries/rate-limits.sql`:
|
|
89
|
+
|
|
90
|
+
```sql
|
|
91
|
+
-- name: LatestSnapshots :many
|
|
92
|
+
SELECT DISTINCT ON (account_id, bucket) *
|
|
93
|
+
FROM rate_limit_snapshots
|
|
94
|
+
WHERE account_id = @account_id
|
|
95
|
+
ORDER BY account_id, bucket, hour_utc DESC;
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**4. Generate, and use it:**
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
sequel generate # writes src/data/queries/rate-limits.ts; commit it
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { latestSnapshots } from './queries/rate-limits.ts'
|
|
106
|
+
|
|
107
|
+
const rows = await latestSnapshots(pool, { accountId })
|
|
108
|
+
// rows: { accountId: string; bucket: 'five_hour' | 'seven_day'; hourUtc: Date; … }[]
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**5. Add `sequel check` to your test script and CI.**
|
|
112
|
+
|
|
113
|
+
## Commands
|
|
114
|
+
|
|
115
|
+
| Command | What it does |
|
|
116
|
+
|---|---|
|
|
117
|
+
| `sequel validate` | Check the schema folder, with Prisma's own error messages. |
|
|
118
|
+
| `sequel format` | Format the schema folder. |
|
|
119
|
+
| `sequel migrate new <name>` | Diff the migration history against the schema and write `<timestamp>_<name>.sql`. Never prompts, never resets, never touches your target database. Lints the result. |
|
|
120
|
+
| `sequel migrate lint [file…]` | Lint migrations: Squawk's locking and data-loss rules, plus sequel's own. |
|
|
121
|
+
| `sequel migrate adopt-prisma` | Rename Prisma's `<ts>_<name>/migration.sql` files to `<ts>_<name>.sql`, and create sequel's history from `_prisma_migrations` on the target database. Safe to rerun. |
|
|
122
|
+
| `sequel migrate deploy [--allow-out-of-order]` | Apply pending migrations. |
|
|
123
|
+
| `sequel generate [--watch]` | Migrate the test database, then turn every query file into a typed module beside it. |
|
|
124
|
+
| `sequel extras` | Rewrite the snapshot of Postgres features the schema language can't express. |
|
|
125
|
+
| `sequel check` | Fail on stale generated code, schema drift, lint findings or a stale extras snapshot. |
|
|
126
|
+
|
|
127
|
+
Every command is non-interactive and exits non-zero with a message that names the fix.
|
|
128
|
+
|
|
129
|
+
### Migrations you can run in production
|
|
130
|
+
|
|
131
|
+
- **Files, not folders.** A migration is any `<timestamp>_<name>.sql` file under `migrations/`, in folders of any depth. The timestamp in the file name sets the order, so folders are only for organising.
|
|
132
|
+
- **One transaction per file.** A failed file leaves nothing behind, and an enum value added in one file can be used in the next.
|
|
133
|
+
- **`CREATE INDEX CONCURRENTLY`** goes alone in a file that starts with `-- sequel:no-transaction`.
|
|
134
|
+
- **Every file runs with `lock_timeout` and `statement_timeout`** (5 s and 60 s by default). A file can raise them with its own `SET LOCAL`.
|
|
135
|
+
- **One deploy at a time,** under an advisory lock.
|
|
136
|
+
- **Refused before anything runs:**
|
|
137
|
+
- a changed file, since each applied file's sha256 is recorded;
|
|
138
|
+
- a deleted file;
|
|
139
|
+
- a pending file older than the newest applied one, unless `--allow-out-of-order` is given.
|
|
140
|
+
- **No down migrations.** Expand, then contract: a column drop is two deploys. First stop using the column in your queries, with its field still in the schema. Then remove the field, in the same commit as the `DROP COLUMN` migration. Generated queries name every column they read, so a drop can't break code that no longer uses the column.
|
|
141
|
+
- **Lint findings** can be silenced with Squawk's `-- squawk-ignore <rule> -- <reason>`, or with the reason on the line above.
|
|
142
|
+
|
|
143
|
+
### Generated code
|
|
144
|
+
|
|
145
|
+
- One function per query, taking any `pg` client: a `Pool`, a `PoolClient`, or a client inside your transaction.
|
|
146
|
+
- Keys are camelCase, so the SQL needs no aliases.
|
|
147
|
+
- Enums are string-literal unions.
|
|
148
|
+
- Nullability follows the query, outer joins included.
|
|
149
|
+
- Anything sequel can't map is `unknown`, never a guess.
|
|
150
|
+
- `bigint` and `numeric` are strings, as `pg` returns them.
|
|
151
|
+
- Output compiles under `verbatimModuleSyntax` and `erasableSyntaxOnly`.
|
|
152
|
+
- Generated files are committed, so your build needs no database. Exclude them from your formatter.
|
|
153
|
+
- **Don't cast a nullable expression:** sqlc treats every cast as never null ([sqlc #3710](https://github.com/sqlc-dev/sqlc/issues/3710)).
|
|
154
|
+
|
|
155
|
+
### Postgres-only features
|
|
156
|
+
|
|
157
|
+
Collations, CHECKs, partial and expression indexes, vector and trigram indexes, generated columns, triggers and views:
|
|
158
|
+
- live in hand-written migration SQL, noted in a comment on their model;
|
|
159
|
+
- appear in the committed extras snapshot, so a change to them shows in review.
|
|
160
|
+
|
|
161
|
+
### Migrations managed by another tool
|
|
162
|
+
|
|
163
|
+
If another tool manages your schema and migrations, set `externalMigrations: true`. sequel then only types your queries:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
export default defineConfig({
|
|
167
|
+
externalMigrations: true,
|
|
168
|
+
queries: 'src/**/queries',
|
|
169
|
+
databases: { test: process.env.TEST_DATABASE_URL },
|
|
170
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
- `generate` types queries against the test database as your other tool left it. It never migrates it, and that tool's history table there, such as `_prisma_migrations`, is fine. Migrate the test database with your tool first.
|
|
174
|
+
- `check` checks only the generated modules: stale, missing or orphaned. It prints a line saying it skipped drift, lint, the extras snapshot and schema validation.
|
|
175
|
+
- `generate --watch` watches only the query folders.
|
|
176
|
+
- No schema folder or migrations are read, so `schema`, `migrations` and `snapshot` aren't needed, and are ignored if set. Only sqlc is fetched; `pg_dump` is still needed.
|
|
177
|
+
- `validate`, `format`, `migrate …` and `extras` refuse, naming the setting.
|
|
178
|
+
|
|
179
|
+
Remove the setting to have sequel manage the schema and migrations.
|
|
180
|
+
|
|
181
|
+
## Adopting sequel in a Prisma app
|
|
182
|
+
|
|
183
|
+
Move queries first, and decide about migrations last. Prisma keeps the schema, the migrations and `@prisma/client` while you port, and the app runs on both in the meantime.
|
|
184
|
+
|
|
185
|
+
### Queries first
|
|
186
|
+
|
|
187
|
+
1. Write `sequel.config.ts` with `externalMigrations: true` (see [above](#migrations-managed-by-another-tool)). The schema stays as it is, `url` included, and Prisma keeps migrating every database, the test database too.
|
|
188
|
+
2. Move queries to `.sql` files module by module, porting modules that share a transaction together. Run `sequel generate`, and add `sequel check` to your test script and CI.
|
|
189
|
+
3. Hand-written SQL now does what Prisma Client did for you:
|
|
190
|
+
- **`@updatedAt`:** Prisma Client sets it on every create and update, and the column has no database default. Set `updated_at = now()` in every `INSERT` and `UPDATE`.
|
|
191
|
+
- **`@default(cuid())` and `@default(uuid())`:** Prisma Client makes the id, and the column has no database default. Your app supplies it as a parameter.
|
|
192
|
+
- **A missing row:** Prisma's `update` and `delete` throw `P2025`. A `:one` query returns `null`, and an `:execrows` query returns `0`, so check for them.
|
|
193
|
+
- **Unique violations:** Prisma's `P2002` is now `pg`'s error with `code` `'23505'`.
|
|
194
|
+
- **Nested writes and `$transaction([...])`:** Prisma ran these in a transaction implicitly. Now take a client from the pool, run `BEGIN`, pass that client to every query in the transaction, and `COMMIT` (or `ROLLBACK` on error).
|
|
195
|
+
4. When the last query is ported, remove `@prisma/client` and the schema's `generator` block. Prisma's CLI still runs the migrations.
|
|
196
|
+
|
|
197
|
+
### Then migrations, if sequel is to run them
|
|
198
|
+
|
|
199
|
+
1. Split `schema.prisma` into a folder, and remove `url` from the `datasource` block.
|
|
200
|
+
2. In `sequel.config.ts`, remove `externalMigrations`, and set `schema`, `migrations`, `snapshot` and the target database. Set the lint cut-over to your newest existing migration.
|
|
201
|
+
3. Run `sequel migrate adopt-prisma` and commit the renamed files. Their contents don't change, so their checksums still match.
|
|
202
|
+
4. Run `sequel migrate adopt-prisma` once against every database: production, each developer's, and the test database. It creates sequel's history from `_prisma_migrations`, checking every checksum, and leaves that table untouched. Until then, `deploy` refuses that database.
|
|
203
|
+
5. Run `sequel extras`, and commit the snapshot.
|
|
204
|
+
6. Remove `prisma` and its install hooks.
|
|
205
|
+
7. In VS Code's Prisma extension, which still formats and validates offline, turn off the "Generate" CodeLens and the Prisma Client file watcher.
|
|
206
|
+
|
|
207
|
+
## Status
|
|
208
|
+
|
|
209
|
+
The order of work:
|
|
210
|
+
|
|
211
|
+
1. Repo skeleton: licence layout, vendored schema WASM, binary fetching. Done.
|
|
212
|
+
2. The generator and its eight-query test suite, proved by porting real queries. Done.
|
|
213
|
+
3. The runner, the schema-engine client, `migrate` and `check`. Done.
|
|
214
|
+
4. Adopt sequel in a first app, module by module, then drop `@prisma/client`.
|
|
215
|
+
5. A second app: split its schema into files, then the same steps.
|
|
216
|
+
|
|
217
|
+
Other open work is tracked in [`to-do.md`](to-do.md).
|
|
218
|
+
|
|
219
|
+
## Licence
|
|
220
|
+
|
|
221
|
+
sequel is licensed under the [Apache License 2.0](LICENSE).
|
|
222
|
+
|
|
223
|
+
It uses, without modification:
|
|
224
|
+
- Prisma's schema WASM, vendored in `vendor/prisma-schema-wasm/` under Apache-2.0;
|
|
225
|
+
- Prisma's schema engine (Apache-2.0), sqlc (MIT) and Squawk (Apache-2.0), fetched as pinned binaries and mirrored in this repository's releases;
|
|
226
|
+
- `pg` (MIT), as an npm dependency.
|
|
227
|
+
|
|
228
|
+
See [`THIRD_PARTY_NOTICES.md`](THIRD_PARTY_NOTICES.md).
|