@zeronsh/orbit 0.1.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 +201 -0
- package/dist/adapter-C-AhY9cw.d.ts +18 -0
- package/dist/chunk-2R6QPZNI.js +274 -0
- package/dist/chunk-2R6QPZNI.js.map +1 -0
- package/dist/chunk-7CMFTRDQ.js +253 -0
- package/dist/chunk-7CMFTRDQ.js.map +1 -0
- package/dist/chunk-N2NAKHMU.js +1951 -0
- package/dist/chunk-N2NAKHMU.js.map +1 -0
- package/dist/chunk-QR3NSGHJ.js +131 -0
- package/dist/chunk-QR3NSGHJ.js.map +1 -0
- package/dist/client.d.ts +349 -0
- package/dist/client.js +3 -0
- package/dist/client.js.map +1 -0
- package/dist/custom-DzMQ-nY2.d.ts +81 -0
- package/dist/drizzle/cli/bin.d.ts +1 -0
- package/dist/drizzle/cli/bin.js +51 -0
- package/dist/drizzle/cli/bin.js.map +1 -0
- package/dist/drizzle/cli.d.ts +63 -0
- package/dist/drizzle/cli.js +6 -0
- package/dist/drizzle/cli.js.map +1 -0
- package/dist/drizzle.d.ts +21 -0
- package/dist/drizzle.js +20 -0
- package/dist/drizzle.js.map +1 -0
- package/dist/introspect-zNCdXfuc.d.ts +31 -0
- package/dist/ir-DE_CZz0D.d.ts +49 -0
- package/dist/orm-core.d.ts +33 -0
- package/dist/orm-core.js +4 -0
- package/dist/orm-core.js.map +1 -0
- package/dist/query-BMK1cXAS.d.ts +296 -0
- package/dist/react.d.ts +16 -0
- package/dist/react.js +27 -0
- package/dist/react.js.map +1 -0
- package/dist/schema-BNM6bks7.d.ts +108 -0
- package/dist/server/pg.d.ts +18 -0
- package/dist/server/pg.js +35 -0
- package/dist/server/pg.js.map +1 -0
- package/dist/server.d.ts +41 -0
- package/dist/server.js +81 -0
- package/dist/server.js.map +1 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
95
|
+
Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Orbit authors
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { S as SchemaIR } from './ir-DE_CZz0D.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts an ORM's native schema (`Input`) into Orbit's normalized {@link SchemaIR}.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam Input the ORM's schema object (e.g. a Drizzle `* as schema` import).
|
|
7
|
+
* @typeParam Config adapter-specific options (table/column selection, casing, …).
|
|
8
|
+
*/
|
|
9
|
+
interface OrmAdapter<Input = unknown, Config = unknown> {
|
|
10
|
+
/** Stable adapter id, e.g. `'drizzle'`. Used in generated-file headers. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** Produce the normalized IR for the given schema + options. */
|
|
13
|
+
toIR(input: Input, config?: Config): SchemaIR;
|
|
14
|
+
}
|
|
15
|
+
/** Helper to define an adapter with inferred generics. */
|
|
16
|
+
declare function defineAdapter<Input, Config>(adapter: OrmAdapter<Input, Config>): OrmAdapter<Input, Config>;
|
|
17
|
+
|
|
18
|
+
export { type OrmAdapter as O, defineAdapter as d };
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { is, Table, getTableName, getTableColumns, One } from 'drizzle-orm';
|
|
2
|
+
import { getTableConfig } from 'drizzle-orm/pg-core';
|
|
3
|
+
|
|
4
|
+
// drizzle/src/introspect.ts
|
|
5
|
+
|
|
6
|
+
// drizzle/src/type-map.ts
|
|
7
|
+
var dataTypeToOrbit = {
|
|
8
|
+
number: "number",
|
|
9
|
+
bigint: "number",
|
|
10
|
+
boolean: "boolean",
|
|
11
|
+
date: "number"
|
|
12
|
+
};
|
|
13
|
+
var columnTypeToOrbit = {
|
|
14
|
+
PgText: "string",
|
|
15
|
+
PgChar: "string",
|
|
16
|
+
PgVarchar: "string",
|
|
17
|
+
PgUUID: "string",
|
|
18
|
+
PgEnumColumn: "string",
|
|
19
|
+
PgJsonb: "json",
|
|
20
|
+
PgJson: "json",
|
|
21
|
+
PgNumeric: "number",
|
|
22
|
+
PgDateString: "number",
|
|
23
|
+
PgTime: "number",
|
|
24
|
+
PgTimestampString: "number",
|
|
25
|
+
PgArray: "json",
|
|
26
|
+
// MySQL / SQLite analogues (best-effort, so non-pg dialects map too)
|
|
27
|
+
MySqlText: "string",
|
|
28
|
+
MySqlVarChar: "string",
|
|
29
|
+
MySqlChar: "string",
|
|
30
|
+
MySqlJson: "json",
|
|
31
|
+
MySqlInt: "number",
|
|
32
|
+
MySqlBigInt53: "number",
|
|
33
|
+
MySqlBoolean: "boolean",
|
|
34
|
+
SQLiteText: "string",
|
|
35
|
+
SQLiteInteger: "number",
|
|
36
|
+
SQLiteReal: "number",
|
|
37
|
+
SQLiteBlobJson: "json",
|
|
38
|
+
SQLiteBoolean: "boolean"
|
|
39
|
+
};
|
|
40
|
+
var sqlTypeToOrbit = {
|
|
41
|
+
text: "string",
|
|
42
|
+
char: "string",
|
|
43
|
+
character: "string",
|
|
44
|
+
varchar: "string",
|
|
45
|
+
"character varying": "string",
|
|
46
|
+
uuid: "string",
|
|
47
|
+
enum: "string",
|
|
48
|
+
jsonb: "json",
|
|
49
|
+
json: "json",
|
|
50
|
+
numeric: "number",
|
|
51
|
+
decimal: "number",
|
|
52
|
+
int: "number",
|
|
53
|
+
integer: "number",
|
|
54
|
+
smallint: "number",
|
|
55
|
+
bigint: "number",
|
|
56
|
+
int2: "number",
|
|
57
|
+
int4: "number",
|
|
58
|
+
int8: "number",
|
|
59
|
+
real: "number",
|
|
60
|
+
float4: "number",
|
|
61
|
+
float8: "number",
|
|
62
|
+
"double precision": "number",
|
|
63
|
+
serial: "number",
|
|
64
|
+
bigserial: "number",
|
|
65
|
+
date: "number",
|
|
66
|
+
time: "number",
|
|
67
|
+
"time without time zone": "number",
|
|
68
|
+
"time with time zone": "number",
|
|
69
|
+
timetz: "number",
|
|
70
|
+
timestamp: "number",
|
|
71
|
+
"timestamp without time zone": "number",
|
|
72
|
+
"timestamp with time zone": "number",
|
|
73
|
+
timestamptz: "number",
|
|
74
|
+
boolean: "boolean",
|
|
75
|
+
bool: "boolean"
|
|
76
|
+
};
|
|
77
|
+
function orbitTypeOf(column) {
|
|
78
|
+
const byColumnType = column.columnType && columnTypeToOrbit[column.columnType];
|
|
79
|
+
if (byColumnType) return byColumnType;
|
|
80
|
+
const byDataType = column.dataType && dataTypeToOrbit[column.dataType];
|
|
81
|
+
if (byDataType) return byDataType;
|
|
82
|
+
const sql = column.getSQLType?.();
|
|
83
|
+
if (sql) {
|
|
84
|
+
const base = sql.replace(/\(.*$/, "").replace(/\[\]$/, "").trim().toLowerCase();
|
|
85
|
+
if (sql.endsWith("[]")) return "json";
|
|
86
|
+
if (sqlTypeToOrbit[base]) return sqlTypeToOrbit[base];
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// drizzle/src/introspect.ts
|
|
92
|
+
function debugLog(on, ...args) {
|
|
93
|
+
if (on) console.error("[orbit-drizzle]", ...args);
|
|
94
|
+
}
|
|
95
|
+
function tableConfig(table) {
|
|
96
|
+
try {
|
|
97
|
+
const cfg = getTableConfig(table);
|
|
98
|
+
return {
|
|
99
|
+
name: cfg.name,
|
|
100
|
+
primaryKeyColumns: cfg.primaryKeys.map((pk) => pk.columns.map((c) => c.name)),
|
|
101
|
+
foreignKeys: cfg.foreignKeys.map((fk) => {
|
|
102
|
+
const ref = fk.reference();
|
|
103
|
+
return {
|
|
104
|
+
columns: ref.columns.map((c) => c.name),
|
|
105
|
+
foreignTable: getTableName(ref.foreignTable),
|
|
106
|
+
foreignColumns: ref.foreignColumns.map((c) => c.name)
|
|
107
|
+
};
|
|
108
|
+
})
|
|
109
|
+
};
|
|
110
|
+
} catch {
|
|
111
|
+
return { name: getTableName(table), primaryKeyColumns: [], foreignKeys: [] };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
function isTableIncluded(tableName, config) {
|
|
115
|
+
if (!config.tables) return true;
|
|
116
|
+
const entry = config.tables[tableName];
|
|
117
|
+
return entry !== false && entry !== void 0;
|
|
118
|
+
}
|
|
119
|
+
function isColumnIncluded(tableName, columnName, isPk, config) {
|
|
120
|
+
if (isPk) return true;
|
|
121
|
+
if (!config.tables) return true;
|
|
122
|
+
const entry = config.tables[tableName];
|
|
123
|
+
if (entry === void 0 || typeof entry === "boolean") return true;
|
|
124
|
+
return entry[columnName] !== false;
|
|
125
|
+
}
|
|
126
|
+
function buildColumn(tableName, dbName, column, isPk, config) {
|
|
127
|
+
const type = orbitTypeOf(column);
|
|
128
|
+
if (!type) {
|
|
129
|
+
debugLog(config.debug, `skipping ${tableName}.${dbName}: unsupported type ${column.columnType ?? column.dataType ?? column.getSQLType?.()}`);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const hasDefault = Boolean(column.hasDefault) || typeof column.defaultFn !== "undefined";
|
|
133
|
+
const optional = isPk ? false : hasDefault ? true : !column.notNull;
|
|
134
|
+
const cliCustom = config.customTypes?.[tableName]?.[dbName];
|
|
135
|
+
const enumCustom = column.enumValues && column.enumValues.length ? column.enumValues.map((v) => JSON.stringify(v)).join(" | ") : void 0;
|
|
136
|
+
return {
|
|
137
|
+
name: dbName,
|
|
138
|
+
type,
|
|
139
|
+
optional,
|
|
140
|
+
customType: cliCustom ?? enumCustom,
|
|
141
|
+
sourceType: column.columnType ?? column.dataType
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function findRelationsObject(input, config) {
|
|
145
|
+
if (config.relations) return config.relations;
|
|
146
|
+
for (const value of Object.values(input)) {
|
|
147
|
+
if (value && typeof value === "object" && !is(value, Table)) {
|
|
148
|
+
const vals = Object.values(value);
|
|
149
|
+
if (vals.length > 0 && vals.every((v) => v && typeof v === "object" && is(v.table, Table) && "relations" in v)) {
|
|
150
|
+
return value;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return void 0;
|
|
155
|
+
}
|
|
156
|
+
function throughColumnNames(side) {
|
|
157
|
+
if (!Array.isArray(side)) return [];
|
|
158
|
+
return side.map((w) => w?._).map((inner) => inner?.column?.name).filter(Boolean);
|
|
159
|
+
}
|
|
160
|
+
function relationToIR(sourceTableName, relName, rel, included) {
|
|
161
|
+
const cardinality = is(rel, One) ? "one" : "many";
|
|
162
|
+
const sourceFields = rel.sourceColumns?.map((c) => c.name) ?? [];
|
|
163
|
+
const targetFields = rel.targetColumns?.map((c) => c.name) ?? [];
|
|
164
|
+
const destName = rel.targetTable ? getTableName(rel.targetTable) : void 0;
|
|
165
|
+
if (!destName || !sourceFields.length || !targetFields.length) return null;
|
|
166
|
+
if (rel.throughTable) {
|
|
167
|
+
const junctionName = getTableName(rel.throughTable);
|
|
168
|
+
const through = rel.through;
|
|
169
|
+
const throughSrc = throughColumnNames(through?.source);
|
|
170
|
+
const throughTgt = throughColumnNames(through?.target);
|
|
171
|
+
if (!throughSrc.length || !throughTgt.length) return null;
|
|
172
|
+
if (!included.has(destName) || !included.has(junctionName)) return null;
|
|
173
|
+
const chain = [
|
|
174
|
+
{ sourceField: sourceFields, destField: throughSrc, destSchema: junctionName, cardinality: "many" },
|
|
175
|
+
{ sourceField: throughTgt, destField: targetFields, destSchema: destName, cardinality: "many" }
|
|
176
|
+
];
|
|
177
|
+
return { table: sourceTableName, name: relName, chain };
|
|
178
|
+
}
|
|
179
|
+
if (!included.has(destName)) return null;
|
|
180
|
+
return {
|
|
181
|
+
table: sourceTableName,
|
|
182
|
+
name: relName,
|
|
183
|
+
chain: [{ sourceField: sourceFields, destField: targetFields, destSchema: destName, cardinality }]
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function fkRelName(fkColumn, fallback) {
|
|
187
|
+
const m = fkColumn.replace(/(_id|Id)$/, "");
|
|
188
|
+
return m && m !== fkColumn ? m : fallback;
|
|
189
|
+
}
|
|
190
|
+
function drizzleToIR(input, config = {}) {
|
|
191
|
+
const tables = [];
|
|
192
|
+
const tableInstances = /* @__PURE__ */ new Map();
|
|
193
|
+
const included = /* @__PURE__ */ new Set();
|
|
194
|
+
for (const value of Object.values(input)) {
|
|
195
|
+
if (!is(value, Table)) continue;
|
|
196
|
+
const table = value;
|
|
197
|
+
const tableName = getTableName(table);
|
|
198
|
+
if (!isTableIncluded(tableName, config)) {
|
|
199
|
+
debugLog(config.debug, `skipping table ${tableName} (excluded by config)`);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
tableInstances.set(tableName, table);
|
|
203
|
+
included.add(tableName);
|
|
204
|
+
}
|
|
205
|
+
for (const [tableName, table] of tableInstances) {
|
|
206
|
+
const cfg = tableConfig(table);
|
|
207
|
+
const columns = getTableColumns(table);
|
|
208
|
+
const pk = /* @__PURE__ */ new Set();
|
|
209
|
+
for (const col of Object.values(columns)) if (col.primary) pk.add(col.name);
|
|
210
|
+
for (const pkCols of cfg.primaryKeyColumns) for (const c of pkCols) pk.add(c);
|
|
211
|
+
const irColumns = [];
|
|
212
|
+
for (const col of Object.values(columns)) {
|
|
213
|
+
const isPk = pk.has(col.name);
|
|
214
|
+
if (!isColumnIncluded(tableName, col.name, isPk, config)) continue;
|
|
215
|
+
const irCol = buildColumn(tableName, col.name, col, isPk, config);
|
|
216
|
+
if (irCol) irColumns.push(irCol);
|
|
217
|
+
}
|
|
218
|
+
if (pk.size === 0) {
|
|
219
|
+
throw new Error(`@orbit/drizzle: table "${tableName}" has no primary key \u2014 Orbit requires one. Add .primaryKey() or primaryKey({columns}).`);
|
|
220
|
+
}
|
|
221
|
+
tables.push({ name: tableName, columns: irColumns, primaryKey: [...pk] });
|
|
222
|
+
}
|
|
223
|
+
const relationships = [];
|
|
224
|
+
const relationsObj = findRelationsObject(input, config);
|
|
225
|
+
const seen = /* @__PURE__ */ new Set();
|
|
226
|
+
if (relationsObj) {
|
|
227
|
+
for (const node of Object.values(relationsObj)) {
|
|
228
|
+
const n = node;
|
|
229
|
+
if (!is(n.table, Table)) continue;
|
|
230
|
+
const sourceTableName = getTableName(n.table);
|
|
231
|
+
if (!included.has(sourceTableName)) continue;
|
|
232
|
+
const rels = n.relations;
|
|
233
|
+
for (const [relName, rel] of Object.entries(rels)) {
|
|
234
|
+
const ir = relationToIR(sourceTableName, relName, rel, included);
|
|
235
|
+
if (ir) {
|
|
236
|
+
relationships.push(ir);
|
|
237
|
+
seen.add(`${sourceTableName}.${relName}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
} else if (config.fkRelationships !== false) {
|
|
242
|
+
for (const [tableName, table] of tableInstances) {
|
|
243
|
+
const cfg = tableConfig(table);
|
|
244
|
+
for (const fk of cfg.foreignKeys) {
|
|
245
|
+
const parent = fk.foreignTable;
|
|
246
|
+
if (!included.has(parent)) continue;
|
|
247
|
+
const oneName = uniqueName(fkRelName(fk.columns[0] ?? parent, parent), tableName, seen);
|
|
248
|
+
relationships.push({
|
|
249
|
+
table: tableName,
|
|
250
|
+
name: oneName,
|
|
251
|
+
chain: [{ sourceField: fk.columns, destField: fk.foreignColumns, destSchema: parent, cardinality: "one" }]
|
|
252
|
+
});
|
|
253
|
+
const manyName = uniqueName(`${tableName}s`, parent, seen);
|
|
254
|
+
relationships.push({
|
|
255
|
+
table: parent,
|
|
256
|
+
name: manyName,
|
|
257
|
+
chain: [{ sourceField: fk.foreignColumns, destField: fk.columns, destSchema: tableName, cardinality: "many" }]
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return { tables, relationships };
|
|
263
|
+
}
|
|
264
|
+
function uniqueName(base, table, seen) {
|
|
265
|
+
let name = base;
|
|
266
|
+
let i = 2;
|
|
267
|
+
while (seen.has(`${table}.${name}`)) name = `${base}${i++}`;
|
|
268
|
+
seen.add(`${table}.${name}`);
|
|
269
|
+
return name;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export { drizzleToIR, orbitTypeOf };
|
|
273
|
+
//# sourceMappingURL=chunk-2R6QPZNI.js.map
|
|
274
|
+
//# sourceMappingURL=chunk-2R6QPZNI.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../drizzle/src/type-map.ts","../drizzle/src/introspect.ts"],"names":[],"mappings":";;;;;;AAUO,IAAM,eAAA,GAAkB;AAAA,EAC7B,MAAA,EAAQ,QAAA;AAAA,EACR,MAAA,EAAQ,QAAA;AAAA,EACR,OAAA,EAAS,SAAA;AAAA,EACT,IAAA,EAAM;AACR,CAAA;AAGO,IAAM,iBAAA,GAAoB;AAAA,EAC/B,MAAA,EAAQ,QAAA;AAAA,EACR,MAAA,EAAQ,QAAA;AAAA,EACR,SAAA,EAAW,QAAA;AAAA,EACX,MAAA,EAAQ,QAAA;AAAA,EACR,YAAA,EAAc,QAAA;AAAA,EACd,OAAA,EAAS,MAAA;AAAA,EACT,MAAA,EAAQ,MAAA;AAAA,EACR,SAAA,EAAW,QAAA;AAAA,EACX,YAAA,EAAc,QAAA;AAAA,EACd,MAAA,EAAQ,QAAA;AAAA,EACR,iBAAA,EAAmB,QAAA;AAAA,EACnB,OAAA,EAAS,MAAA;AAAA;AAAA,EAET,SAAA,EAAW,QAAA;AAAA,EACX,YAAA,EAAc,QAAA;AAAA,EACd,SAAA,EAAW,QAAA;AAAA,EACX,SAAA,EAAW,MAAA;AAAA,EACX,QAAA,EAAU,QAAA;AAAA,EACV,aAAA,EAAe,QAAA;AAAA,EACf,YAAA,EAAc,SAAA;AAAA,EACd,UAAA,EAAY,QAAA;AAAA,EACZ,aAAA,EAAe,QAAA;AAAA,EACf,UAAA,EAAY,QAAA;AAAA,EACZ,cAAA,EAAgB,MAAA;AAAA,EAChB,aAAA,EAAe;AAC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AAQ,IAAI,CAAA;AACzD,QAAA,aAAA,CAAc,IAAA,CAAK;AAAA,UACjB,KAAA,EAAO,MAAA;AAAA,UACP,IAAA,EAAM,QAAA;AAAA,UACN,KAAA,EAAO,CAAC,EAAE,WAAA,EAAa,EAAA,CAAG,cAAA,EAAgB,SAAA,EAAW,EAAA,CAAG,OAAA,EAAS,UAAA,EAAY,SAAA,EAAW,WAAA,EAAa,QAAQ;AAAA,SAC9G,CAAA;AAAA,MACH;AAAA,IACF;AAAA,EACF;AAEA,EAAA,OAAO,EAAE,QAAQ,aAAA,EAAc;AACjC;AAEA,SAAS,UAAA,CAAW,IAAA,EAAc,KAAA,EAAe,IAAA,EAA2B;AAC1E,EAAA,IAAI,IAAA,GAAO,IAAA;AACX,EAAA,IAAI,CAAA,GAAI,CAAA;AACR,EAAA,OAAO,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,KAAK,CAAA,CAAA,EAAI,IAAI,CAAA,CAAE,CAAA,EAAG,IAAA,GAAO,CAAA,EAAG,IAAI,CAAA,EAAG,CAAA,EAAG,CAAA,CAAA;AACzD,EAAA,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,KAAK,CAAA,CAAA,EAAI,IAAI,CAAA,CAAE,CAAA;AAC3B,EAAA,OAAO,IAAA;AACT","file":"chunk-2R6QPZNI.js","sourcesContent":["// Drizzle column type → Orbit value type. Ported from drizzle-zero's\n// `drizzle-to-zero.ts` (Orbit's value types are the same set Zero uses:\n// string / number / boolean / json). Resolution order, per column:\n// 1. by Drizzle `columnType` (e.g. `PgText`) — most specific\n// 2. by Drizzle `dataType` (e.g. `number`)\n// 3. by raw Postgres SQL type (`getSQLType()`) — broad fallback\n\nimport type { OrbitValueType } from '../../orm-core/src/index.ts';\n\n/** Drizzle `dataType` → Orbit type. */\nexport const dataTypeToOrbit = {\n number: 'number',\n bigint: 'number',\n boolean: 'boolean',\n date: 'number',\n} as const satisfies Record<string, OrbitValueType>;\n\n/** Drizzle `columnType` (dialect-specific) → Orbit type. */\nexport const columnTypeToOrbit = {\n PgText: 'string',\n PgChar: 'string',\n PgVarchar: 'string',\n PgUUID: 'string',\n PgEnumColumn: 'string',\n PgJsonb: 'json',\n PgJson: 'json',\n PgNumeric: 'number',\n PgDateString: 'number',\n PgTime: 'number',\n PgTimestampString: 'number',\n PgArray: 'json',\n // MySQL / SQLite analogues (best-effort, so non-pg dialects map too)\n MySqlText: 'string',\n MySqlVarChar: 'string',\n MySqlChar: 'string',\n MySqlJson: 'json',\n MySqlInt: 'number',\n MySqlBigInt53: 'number',\n MySqlBoolean: 'boolean',\n SQLiteText: 'string',\n SQLiteInteger: 'number',\n SQLiteReal: 'number',\n SQLiteBlobJson: 'json',\n SQLiteBoolean: 'boolean',\n} as const satisfies Record<string, OrbitValueType>;\n\n/** Raw Postgres SQL type name → Orbit type. */\nexport const sqlTypeToOrbit: Record<string, OrbitValueType> = {\n text: 'string',\n char: 'string',\n character: 'string',\n varchar: 'string',\n 'character varying': 'string',\n uuid: 'string',\n enum: 'string',\n jsonb: 'json',\n json: 'json',\n numeric: 'number',\n decimal: 'number',\n int: 'number',\n integer: 'number',\n smallint: 'number',\n bigint: 'number',\n int2: 'number',\n int4: 'number',\n int8: 'number',\n real: 'number',\n float4: 'number',\n float8: 'number',\n 'double precision': 'number',\n serial: 'number',\n bigserial: 'number',\n date: 'number',\n time: 'number',\n 'time without time zone': 'number',\n 'time with time zone': 'number',\n timetz: 'number',\n timestamp: 'number',\n 'timestamp without time zone': 'number',\n 'timestamp with time zone': 'number',\n timestamptz: 'number',\n boolean: 'boolean',\n bool: 'boolean',\n};\n\n/** Minimal shape of a Drizzle column we read for type resolution. */\nexport interface DrizzleColumnLike {\n readonly columnType?: string;\n readonly dataType?: string;\n getSQLType?(): string;\n}\n\n/** Resolve a Drizzle column to an Orbit value type, or `null` if unsupported. */\nexport function orbitTypeOf(column: DrizzleColumnLike): OrbitValueType | null {\n const byColumnType = column.columnType && (columnTypeToOrbit as Record<string, OrbitValueType>)[column.columnType];\n if (byColumnType) return byColumnType;\n const byDataType = column.dataType && (dataTypeToOrbit as Record<string, OrbitValueType>)[column.dataType];\n if (byDataType) return byDataType;\n const sql = column.getSQLType?.();\n if (sql) {\n const base = sql.replace(/\\(.*$/, '').replace(/\\[\\]$/, '').trim().toLowerCase();\n if (sql.endsWith('[]')) return 'json';\n if (sqlTypeToOrbit[base]) return sqlTypeToOrbit[base];\n }\n return null;\n}\n","// Read a Drizzle schema into Orbit's normalized {@link SchemaIR}.\n//\n// Relationships come from (in priority order):\n// 1. Drizzle Relations v2 — `defineRelations(...)` (the v1.0 RC API), including\n// many-to-many via `.through(...)` (emitted as a junction chain).\n// 2. Foreign keys (`.references(...)`) — used to synthesize `one`/`many`\n// relationships when no Relations object is supplied.\n//\n// Column names use the **database** column name (Orbit syncs raw Postgres rows),\n// so Orbit field names line up with what replication delivers.\n\nimport { getTableColumns, getTableName, is, Many, One, Table } from 'drizzle-orm';\nimport { getTableConfig } from 'drizzle-orm/pg-core';\nimport type { IRColumn, IRConnection, IRRelationship, IRTable, SchemaIR } from '../../orm-core/src/index.ts';\nimport { orbitTypeOf, type DrizzleColumnLike } from './type-map.ts';\n\nexport interface DrizzleAdapterConfig {\n /**\n * The Relations v2 object (`defineRelations(...)`), if it isn't one of the\n * exports in the schema you passed. When omitted, it is auto-detected among the\n * schema's exports; failing that, relationships are derived from foreign keys.\n */\n readonly relations?: unknown;\n /**\n * Per-table selection. Omit for \"all tables, all columns\". `false` excludes a\n * table; an object selects columns (`{ id: true, secret: false }`). Primary-key\n * columns are always included.\n */\n readonly tables?: Record<string, boolean | Record<string, boolean>>;\n /**\n * Custom TypeScript types per `table.column` (database names), supplied by the\n * CLI's type resolver so `$type<>()` survives into codegen. Runtime callers\n * can't read `$type` (it is type-level only), so this is normally CLI-only.\n */\n readonly customTypes?: Record<string, Record<string, string>>;\n /** Synthesize relationships from foreign keys when no Relations object exists. Default true. */\n readonly fkRelationships?: boolean;\n /** Print what was skipped / inferred. */\n readonly debug?: boolean;\n}\n\ntype AnyRecord = Record<string, unknown>;\n\nfunction debugLog(on: boolean | undefined, ...args: unknown[]): void {\n if (on) console.error('[orbit-drizzle]', ...args);\n}\n\n/** pg `getTableConfig`, with a fallback for non-pg tables. */\nfunction tableConfig(table: Table): { name: string; primaryKeyColumns: string[][]; foreignKeys: { columns: string[]; foreignTable: string; foreignColumns: string[] }[] } {\n try {\n const cfg = getTableConfig(table as Parameters<typeof getTableConfig>[0]);\n return {\n name: cfg.name,\n primaryKeyColumns: cfg.primaryKeys.map((pk) => pk.columns.map((c) => c.name)),\n foreignKeys: cfg.foreignKeys.map((fk) => {\n const ref = fk.reference();\n return {\n columns: ref.columns.map((c) => c.name),\n foreignTable: getTableName(ref.foreignTable),\n foreignColumns: ref.foreignColumns.map((c) => c.name),\n };\n }),\n };\n } catch {\n return { name: getTableName(table), primaryKeyColumns: [], foreignKeys: [] };\n }\n}\n\nfunction isTableIncluded(tableName: string, config: DrizzleAdapterConfig): boolean {\n if (!config.tables) return true;\n const entry = config.tables[tableName];\n return entry !== false && entry !== undefined;\n}\n\nfunction isColumnIncluded(tableName: string, columnName: string, isPk: boolean, config: DrizzleAdapterConfig): boolean {\n if (isPk) return true; // PKs are always needed\n if (!config.tables) return true;\n const entry = config.tables[tableName];\n if (entry === undefined || typeof entry === 'boolean') return true;\n return entry[columnName] !== false; // included unless explicitly excluded\n}\n\nfunction buildColumn(\n tableName: string,\n dbName: string,\n column: DrizzleColumnLike & { notNull?: boolean; primary?: boolean; hasDefault?: boolean; defaultFn?: unknown; enumValues?: readonly string[] },\n isPk: boolean,\n config: DrizzleAdapterConfig,\n): IRColumn | null {\n const type = orbitTypeOf(column);\n if (!type) {\n debugLog(config.debug, `skipping ${tableName}.${dbName}: unsupported type ${column.columnType ?? column.dataType ?? column.getSQLType?.()}`);\n return null;\n }\n const hasDefault = Boolean(column.hasDefault) || typeof column.defaultFn !== 'undefined';\n const optional = isPk ? false : hasDefault ? true : !column.notNull;\n\n // custom type: CLI-resolved $type<>() wins; otherwise enums become a string union.\n const cliCustom = config.customTypes?.[tableName]?.[dbName];\n const enumCustom = column.enumValues && column.enumValues.length\n ? column.enumValues.map((v) => JSON.stringify(v)).join(' | ')\n : undefined;\n\n return {\n name: dbName,\n type,\n optional,\n customType: cliCustom ?? enumCustom,\n sourceType: column.columnType ?? column.dataType,\n };\n}\n\n/** Find a Relations v2 (`defineRelations`) object: keyed by table → `{ table, relations }`. */\nfunction findRelationsObject(input: AnyRecord, config: DrizzleAdapterConfig): AnyRecord | undefined {\n if (config.relations) return config.relations as AnyRecord;\n for (const value of Object.values(input)) {\n if (value && typeof value === 'object' && !is(value, Table)) {\n const vals = Object.values(value as AnyRecord);\n if (vals.length > 0 && vals.every((v) => v && typeof v === 'object' && is((v as AnyRecord).table, Table) && 'relations' in (v as AnyRecord))) {\n return value as AnyRecord;\n }\n }\n }\n return undefined;\n}\n\nfunction throughColumnNames(side: unknown): string[] {\n // `through.source` / `through.target`: array of RelationsBuilderColumn wrappers\n // `{ _: { tableName, column, key } }` where `column` is the junction Drizzle column.\n if (!Array.isArray(side)) return [];\n return side.map((w) => (w as AnyRecord)?._ as AnyRecord).map((inner) => (inner?.column as AnyRecord)?.name as string).filter(Boolean);\n}\n\nfunction relationToIR(sourceTableName: string, relName: string, rel: AnyRecord, included: Set<string>): IRRelationship | null {\n const cardinality = is(rel, One) ? 'one' : 'many';\n const sourceFields = (rel.sourceColumns as { name: string }[] | undefined)?.map((c) => c.name) ?? [];\n const targetFields = (rel.targetColumns as { name: string }[] | undefined)?.map((c) => c.name) ?? [];\n const destName = rel.targetTable ? getTableName(rel.targetTable as Table) : undefined;\n if (!destName || !sourceFields.length || !targetFields.length) return null;\n\n // junction (many-to-many) via `.through(...)`\n if (rel.throughTable) {\n const junctionName = getTableName(rel.throughTable as Table);\n const through = rel.through as AnyRecord | undefined;\n const throughSrc = throughColumnNames(through?.source);\n const throughTgt = throughColumnNames(through?.target);\n if (!throughSrc.length || !throughTgt.length) return null;\n if (!included.has(destName) || !included.has(junctionName)) return null;\n const chain: IRConnection[] = [\n { sourceField: sourceFields, destField: throughSrc, destSchema: junctionName, cardinality: 'many' },\n { sourceField: throughTgt, destField: targetFields, destSchema: destName, cardinality: 'many' },\n ];\n return { table: sourceTableName, name: relName, chain };\n }\n\n if (!included.has(destName)) return null;\n return {\n table: sourceTableName,\n name: relName,\n chain: [{ sourceField: sourceFields, destField: targetFields, destSchema: destName, cardinality }],\n };\n}\n\n/** Heuristic relationship name from an FK column (drops a trailing `Id`/`_id`). */\nfunction fkRelName(fkColumn: string, fallback: string): string {\n const m = fkColumn.replace(/(_id|Id)$/, '');\n return m && m !== fkColumn ? m : fallback;\n}\n\n/** Convert a Drizzle schema (`import * as schema`) into Orbit's {@link SchemaIR}. */\nexport function drizzleToIR(input: AnyRecord, config: DrizzleAdapterConfig = {}): SchemaIR {\n // 1. Tables + columns.\n const tables: IRTable[] = [];\n const tableInstances = new Map<string, Table>();\n const included = new Set<string>();\n\n for (const value of Object.values(input)) {\n if (!is(value, Table)) continue;\n const table = value as Table;\n const tableName = getTableName(table);\n if (!isTableIncluded(tableName, config)) {\n debugLog(config.debug, `skipping table ${tableName} (excluded by config)`);\n continue;\n }\n tableInstances.set(tableName, table);\n included.add(tableName);\n }\n\n for (const [tableName, table] of tableInstances) {\n const cfg = tableConfig(table);\n const columns = getTableColumns(table) as Record<string, DrizzleColumnLike & { name: string; primary?: boolean }>;\n\n const pk = new Set<string>();\n for (const col of Object.values(columns)) if ((col as { primary?: boolean }).primary) pk.add(col.name);\n for (const pkCols of cfg.primaryKeyColumns) for (const c of pkCols) pk.add(c);\n\n const irColumns: IRColumn[] = [];\n for (const col of Object.values(columns)) {\n const isPk = pk.has(col.name);\n if (!isColumnIncluded(tableName, col.name, isPk, config)) continue;\n const irCol = buildColumn(tableName, col.name, col as never, isPk, config);\n if (irCol) irColumns.push(irCol);\n }\n\n if (pk.size === 0) {\n throw new Error(`@orbit/drizzle: table \"${tableName}\" has no primary key — Orbit requires one. Add .primaryKey() or primaryKey({columns}).`);\n }\n tables.push({ name: tableName, columns: irColumns, primaryKey: [...pk] });\n }\n\n // 2. Relationships — Relations v2 first, else foreign keys.\n const relationships: IRRelationship[] = [];\n const relationsObj = findRelationsObject(input, config);\n const seen = new Set<string>(); // `${table}.${name}` dedupe\n\n if (relationsObj) {\n for (const node of Object.values(relationsObj)) {\n const n = node as AnyRecord;\n if (!is(n.table, Table)) continue;\n const sourceTableName = getTableName(n.table as Table);\n if (!included.has(sourceTableName)) continue;\n const rels = n.relations as AnyRecord;\n for (const [relName, rel] of Object.entries(rels)) {\n const ir = relationToIR(sourceTableName, relName, rel as AnyRecord, included);\n if (ir) {\n relationships.push(ir);\n seen.add(`${sourceTableName}.${relName}`);\n }\n }\n }\n } else if (config.fkRelationships !== false) {\n // Derive from foreign keys: a `one` on the child + a `many` back on the parent.\n for (const [tableName, table] of tableInstances) {\n const cfg = tableConfig(table);\n for (const fk of cfg.foreignKeys) {\n const parent = fk.foreignTable;\n if (!included.has(parent)) continue;\n const oneName = uniqueName(fkRelName(fk.columns[0] ?? parent, parent), tableName, seen);\n relationships.push({\n table: tableName,\n name: oneName,\n chain: [{ sourceField: fk.columns, destField: fk.foreignColumns, destSchema: parent, cardinality: 'one' }],\n });\n const manyName = uniqueName(`${tableName}s`, parent, seen);\n relationships.push({\n table: parent,\n name: manyName,\n chain: [{ sourceField: fk.foreignColumns, destField: fk.columns, destSchema: tableName, cardinality: 'many' }],\n });\n }\n }\n }\n\n return { tables, relationships };\n}\n\nfunction uniqueName(base: string, table: string, seen: Set<string>): string {\n let name = base;\n let i = 2;\n while (seen.has(`${table}.${name}`)) name = `${base}${i++}`;\n seen.add(`${table}.${name}`);\n return name;\n}\n"]}
|