@rebasepro/codegen 0.30.1 → 0.31.0-canary.g59cd6fd
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/README.md +40 -38
- package/dist/generate-types.d.ts +23 -3
- package/dist/index.d.ts +24 -4
- package/dist/index.es.js +435 -380
- package/dist/index.es.js.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @rebasepro/codegen
|
|
2
2
|
|
|
3
|
-
Generates typed TypeScript definitions from Rebase
|
|
3
|
+
Generates typed TypeScript definitions from Rebase table descriptors — produces `Database` interface with `Row`, `Insert`, and `Update` types for each collection.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -14,12 +14,12 @@ of it resolves too: Node has supported `require(esm)` since 22.12.
|
|
|
14
14
|
|
|
15
15
|
### Dependencies
|
|
16
16
|
|
|
17
|
-
- `@rebasepro/common` — a runtime dependency (
|
|
17
|
+
- `@rebasepro/common` — a runtime dependency (naming helpers)
|
|
18
18
|
- `@rebasepro/types` — a peer dependency (types only)
|
|
19
19
|
|
|
20
20
|
## What This Package Does
|
|
21
21
|
|
|
22
|
-
`@rebasepro/codegen` takes
|
|
22
|
+
`@rebasepro/codegen` takes the compiled table descriptors — `TableDescriptor[]`, what `compileTables` from `@rebasepro/common` returns and `GET /api/schema/tables` serves — and produces TypeScript type files that provide full autocompletion when used with `@rebasepro/client`. It handles every Postgres column type, enums and `.oneOf()` unions, relations and junction pivots, JSON documents typed from their shapes, geopoints, vectors, and insert optionality from defaults, identities and stamps.
|
|
23
23
|
|
|
24
24
|
This is typically invoked via the CLI (`pnpm rebase generate-sdk`) rather than called directly.
|
|
25
25
|
|
|
@@ -31,9 +31,9 @@ This is typically invoked via the CLI (`pnpm rebase generate-sdk`) rather than c
|
|
|
31
31
|
| `generateTypedefs` | Function | Generates the `database.types.ts` content string |
|
|
32
32
|
| `GeneratedFile` | Interface | `{ path: string; content: string }` |
|
|
33
33
|
| `GenerateSDKOptions` | Interface | `{ includeReadme?: boolean }` (default: `true`) |
|
|
34
|
-
| `toPascalCase` | Function | `"
|
|
35
|
-
| `toCamelCase` | Function | `"
|
|
36
|
-
| `toSafeIdentifier` | Function | Converts a **slug** to a valid JS identifier (
|
|
34
|
+
| `toPascalCase` | Function | `"order_items"` → `"OrderItems"` |
|
|
35
|
+
| `toCamelCase` | Function | `"order_items"` → `"orderItems"` |
|
|
36
|
+
| `toSafeIdentifier` | Function | Converts a **slug** to a valid JS identifier (table accessors only — never column names) |
|
|
37
37
|
| `indent` | Function | Indent text by N spaces |
|
|
38
38
|
| `CodegenError` | Class | Thrown for a schema that cannot be expressed as valid TypeScript |
|
|
39
39
|
|
|
@@ -41,23 +41,25 @@ This is typically invoked via the CLI (`pnpm rebase generate-sdk`) rather than c
|
|
|
41
41
|
|
|
42
42
|
`generateSDK()` produces:
|
|
43
43
|
|
|
44
|
-
1. **`database.types.ts`** — A `Database` interface keyed by each
|
|
44
|
+
1. **`database.types.ts`** — A `Database` interface keyed by each table's
|
|
45
45
|
*accessor* (`my-notes` → `myNotes`, the property name on `client.data`),
|
|
46
|
-
alongside a `
|
|
47
|
-
slug the wire uses
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
alongside a `tablesDictionary` const mapping each accessor back to the
|
|
47
|
+
slug the wire uses, its `TablesDictionary` type and `TableName`, the union
|
|
48
|
+
of every slug. Each entry contains:
|
|
49
|
+
- `Row` — what a read serves, keyed by each column's **wire** key: a column
|
|
50
|
+
keyed `createdAt` is `row.createdAt` whatever its SQL name, and a
|
|
50
51
|
`belongsTo` named `author` gives `row.authorId`. Nullable columns are `T | null`,
|
|
51
52
|
the primary key is always present, relations appear only when `include`
|
|
52
|
-
names them, and `
|
|
53
|
-
- `Insert` — what `create()` accepts. Server-assigned ids
|
|
54
|
-
`belongsTo` target may be named either way
|
|
55
|
-
`{ authorId: 5 }`); `
|
|
53
|
+
names them, and `.private()` columns are absent.
|
|
54
|
+
- `Insert` — what `create()` accepts. Server-assigned ids, defaults and
|
|
55
|
+
stamps are optional; a `belongsTo` target may be named either way
|
|
56
|
+
(`{ author: 5 }` or `{ authorId: 5 }`); `.private()` columns are absent
|
|
57
|
+
here too. A view's is `never`.
|
|
56
58
|
- `Update` — what `update()` accepts. Everything optional, primary key
|
|
57
|
-
omitted, `
|
|
59
|
+
omitted, `.private()` columns absent. A view's is `never`.
|
|
58
60
|
|
|
59
|
-
`
|
|
60
|
-
the
|
|
61
|
+
`.private()` means one thing in all three: the API surface does not mention
|
|
62
|
+
the column, in either direction. The server refuses a write naming such a
|
|
61
63
|
field, with `VALIDATION_EXCLUDED_FIELDS`, and nothing generated names it, so a
|
|
62
64
|
generated client cannot offer a password
|
|
63
65
|
hash as something to read, filter or send.
|
|
@@ -66,16 +68,16 @@ hash as something to read, filter or send.
|
|
|
66
68
|
|
|
67
69
|
### Names
|
|
68
70
|
|
|
69
|
-
A
|
|
71
|
+
A column key you wrote is emitted verbatim, quoted when it is not a valid
|
|
70
72
|
identifier (`"user id"?: string | null`), because `where` and `orderBy` are keyed
|
|
71
73
|
off `Row` — a renamed key makes the correct filter fail to compile and the wrong
|
|
72
74
|
one fail at runtime. Only the two derived names are transformed, both to
|
|
73
|
-
camelCase: the
|
|
75
|
+
camelCase: the table accessor, and a foreign key that comes from a relation.
|
|
74
76
|
|
|
75
77
|
Generation **fails** rather than emitting a broken file when two slugs would
|
|
76
78
|
produce the same accessor: the interface would not compile, and
|
|
77
|
-
`
|
|
78
|
-
reads to another
|
|
79
|
+
`tablesDictionary` would silently keep only one, routing one table's
|
|
80
|
+
reads to another.
|
|
79
81
|
|
|
80
82
|
### Untrusted schemas
|
|
81
83
|
|
|
@@ -84,31 +86,31 @@ column names and enum values come from that server. Every one of them is emitted
|
|
|
84
86
|
as an escaped literal — untrusted input cannot add a declaration to the generated
|
|
85
87
|
file. There is a test that asserts exactly this.
|
|
86
88
|
|
|
87
|
-
###
|
|
89
|
+
### Column Type Mapping
|
|
88
90
|
|
|
89
|
-
|
|
|
91
|
+
| Column | TypeScript Type |
|
|
90
92
|
|---|---|
|
|
91
|
-
| `
|
|
92
|
-
| `
|
|
93
|
+
| `text`, `varchar`, `char`, `uuid` | `string` (or the union of its `.oneOf()` values) |
|
|
94
|
+
| `enum` | the union of its values |
|
|
95
|
+
| every integer, `real`, `doublePrecision`, `numeric` | `number` (or the union of its `.oneOf()` values) |
|
|
93
96
|
| `boolean` | `boolean` |
|
|
94
|
-
| `date` | `string` (ISO 8601) |
|
|
97
|
+
| `timestamptz`, `date`, `time` | `string` (ISO 8601) |
|
|
95
98
|
| `geopoint` | `{ latitude: number; longitude: number }` |
|
|
96
|
-
| `
|
|
97
|
-
| `
|
|
98
|
-
| `map` | Inline object type or `Record<string, unknown>` |
|
|
99
|
-
| `array` | `Array<T>` with inferred inner type |
|
|
99
|
+
| `jsonb` / `json` with a shape | the shape's type; without one `Record<string, unknown>` |
|
|
100
|
+
| `array(inner)` | `Array<T>` of the inner column's type |
|
|
100
101
|
| `vector` | `number[]` |
|
|
101
|
-
| `
|
|
102
|
+
| `bytea` | `string` (base64) |
|
|
103
|
+
| `custom` | `unknown` |
|
|
104
|
+
| a relation | the target's own `Row`, inlined (what `include` serves) |
|
|
102
105
|
|
|
103
106
|
## Quick Start
|
|
104
107
|
|
|
105
108
|
```typescript
|
|
106
109
|
import { generateSDK } from "@rebasepro/codegen";
|
|
107
|
-
import
|
|
110
|
+
import { compileTables } from "@rebasepro/common";
|
|
111
|
+
import { posts, users } from "./backend/schema";
|
|
108
112
|
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
const files = generateSDK(collections);
|
|
113
|
+
const files = generateSDK(compileTables([posts, users]));
|
|
112
114
|
// files = [
|
|
113
115
|
// { path: "database.types.ts", content: "..." },
|
|
114
116
|
// { path: "README.md", content: "..." }
|
|
@@ -122,5 +124,5 @@ for (const file of files) {
|
|
|
122
124
|
## Related Packages
|
|
123
125
|
|
|
124
126
|
- `@rebasepro/client` — Consumes the generated types for typed API calls
|
|
125
|
-
- `@rebasepro/types` — Provides `
|
|
126
|
-
- `@rebasepro/common` — Provides `
|
|
127
|
+
- `@rebasepro/types` — Provides `TableDescriptor` and the column and relation types it reads
|
|
128
|
+
- `@rebasepro/common` — Provides `compileTables`, which turns `defineTable` results into descriptors
|
package/dist/generate-types.d.ts
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { TableDescriptor } from "@rebasepro/types";
|
|
2
2
|
/**
|
|
3
3
|
* A schema that cannot be expressed as a valid TypeScript file.
|
|
4
4
|
*
|
|
5
5
|
* Thrown rather than emitted. The generator used to concatenate whatever it was
|
|
6
6
|
* given, so a slug that collided with another one, or that was not an
|
|
7
7
|
* identifier, produced a file that either failed to compile or — worse —
|
|
8
|
-
* compiled while quietly routing one
|
|
8
|
+
* compiled while quietly routing one table to another's slug.
|
|
9
|
+
*
|
|
10
|
+
* @group SDK generation
|
|
9
11
|
*/
|
|
10
12
|
export declare class CodegenError extends Error {
|
|
11
13
|
constructor(message: string);
|
|
12
14
|
}
|
|
13
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Generate the typed SDK's `database.types.ts` from table descriptors — what
|
|
17
|
+
* `compileTables` produces, `GET /api/schema/tables` serves and introspection
|
|
18
|
+
* reports.
|
|
19
|
+
*
|
|
20
|
+
* Emits `Database` (per table: `Row`, `Insert`, `Update`, `Relations`, `Slug`),
|
|
21
|
+
* `TableName`, `tablesDictionary`, `TablesDictionary` and the
|
|
22
|
+
* `include` helpers `RelationsOf`, `RelationKeys`, `RelationTarget`,
|
|
23
|
+
* `IncludeOptionsFor`, `IncludeFor` and `RowWith`.
|
|
24
|
+
*
|
|
25
|
+
* Deterministic: tables are emitted in slug order whatever order they arrive
|
|
26
|
+
* in, so `rebase doctor` can regenerate in memory and diff against the file.
|
|
27
|
+
*
|
|
28
|
+
* @throws {CodegenError} when two slugs produce one accessor, or a slug has
|
|
29
|
+
* nothing an accessor can be built from.
|
|
30
|
+
*
|
|
31
|
+
* @group SDK generation
|
|
32
|
+
*/
|
|
33
|
+
export declare function generateTypedefs(input: readonly TableDescriptor[]): string;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @rebasepro/codegen
|
|
3
3
|
*
|
|
4
|
-
* Generates a purely typed
|
|
4
|
+
* Generates the typed SDK — a purely typed TypeScript database definition —
|
|
5
|
+
* from table descriptors: what `compileTables` makes of `backend/schema`, what
|
|
6
|
+
* `GET /api/schema/tables` serves, and what introspection reports.
|
|
5
7
|
*/
|
|
6
|
-
import {
|
|
8
|
+
import type { TableDescriptor } from "@rebasepro/types";
|
|
7
9
|
export { generateTypedefs, CodegenError } from "./generate-types.js";
|
|
8
|
-
export { toPascalCase, toCamelCase, toSafeIdentifier,
|
|
10
|
+
export { toPascalCase, toCamelCase, toSafeIdentifier, toTableAccessor, indent } from "./utils.js";
|
|
11
|
+
/**
|
|
12
|
+
* One file of a generated SDK.
|
|
13
|
+
*
|
|
14
|
+
* @group SDK generation
|
|
15
|
+
*/
|
|
9
16
|
export interface GeneratedFile {
|
|
10
17
|
/** Relative file path within the output directory */
|
|
11
18
|
path: string;
|
|
12
19
|
/** File content */
|
|
13
20
|
content: string;
|
|
14
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Options of {@link generateSDK}.
|
|
24
|
+
*
|
|
25
|
+
* @group SDK generation
|
|
26
|
+
*/
|
|
15
27
|
export interface GenerateSDKOptions {
|
|
16
28
|
/** Whether to include a README file (default: true) */
|
|
17
29
|
includeReadme?: boolean;
|
|
@@ -23,4 +35,12 @@ export interface GenerateSDKOptions {
|
|
|
23
35
|
*/
|
|
24
36
|
baseUrl?: string;
|
|
25
37
|
}
|
|
26
|
-
|
|
38
|
+
/**
|
|
39
|
+
* The typed SDK for a set of tables: `database.types.ts` (see
|
|
40
|
+
* {@link generateTypedefs}) and, unless switched off, a README.
|
|
41
|
+
*
|
|
42
|
+
* @param tables The table descriptors, in any order.
|
|
43
|
+
*
|
|
44
|
+
* @group SDK generation
|
|
45
|
+
*/
|
|
46
|
+
export declare function generateSDK(tables: readonly TableDescriptor[], options?: GenerateSDKOptions): GeneratedFile[];
|