@kozou/introspect 0.0.1 → 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 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.
@@ -0,0 +1,4 @@
1
+ import type { Client } from 'pg';
2
+ import type { RawCheck } from '@kozou/core';
3
+ export declare function fetchChecks(client: Client, schemas: string[]): Promise<Map<string, RawCheck[]>>;
4
+ //# sourceMappingURL=checks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checks.d.ts","sourceRoot":"","sources":["../src/checks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AACjC,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAiB5C,wBAAsB,WAAW,CAC/B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EAAE,GAChB,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC,CAgClC"}
package/dist/checks.js ADDED
@@ -0,0 +1,34 @@
1
+ import { runQuery } from './errors.js';
2
+ const CHECK_SKIN_RE = /^CHECK \((.*)\)$/s;
3
+ function stripCheckSkin(def) {
4
+ const m = CHECK_SKIN_RE.exec(def);
5
+ return m ? m[1] : def;
6
+ }
7
+ export async function fetchChecks(client, schemas) {
8
+ const result = new Map();
9
+ if (schemas.length === 0)
10
+ return result;
11
+ const rows = await runQuery(client, `SELECT
12
+ n.nspname AS schema,
13
+ c.relname AS table,
14
+ con.conname AS name,
15
+ pg_get_constraintdef(con.oid, true) AS definition
16
+ FROM pg_constraint con
17
+ JOIN pg_class c ON c.oid = con.conrelid
18
+ JOIN pg_namespace n ON n.oid = c.relnamespace
19
+ WHERE con.contype = 'c'
20
+ AND con.conrelid <> 0
21
+ AND n.nspname = ANY($1)
22
+ ORDER BY n.nspname, c.relname, con.conname`, [schemas], 'fetchChecks');
23
+ for (const row of rows) {
24
+ const key = `${row.schema}.${row.table}`;
25
+ if (!result.has(key))
26
+ result.set(key, []);
27
+ result.get(key).push({
28
+ name: row.name,
29
+ expression: stripCheckSkin(row.definition),
30
+ });
31
+ }
32
+ return result;
33
+ }
34
+ //# sourceMappingURL=checks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checks.js","sourceRoot":"","sources":["../src/checks.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AASvC,MAAM,aAAa,GAAG,mBAAmB,CAAC;AAE1C,SAAS,cAAc,CAAC,GAAW;IACjC,MAAM,CAAC,GAAG,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAClC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAE,CAAC,CAAC,CAAC,GAAG,CAAC;AACzB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,MAAc,EACd,OAAiB;IAEjB,MAAM,MAAM,GAAG,IAAI,GAAG,EAAsB,CAAC;IAC7C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IAExC,MAAM,IAAI,GAAG,MAAM,QAAQ,CACzB,MAAM,EACN;;;;;;;;;;;gDAW4C,EAC5C,CAAC,OAAO,CAAC,EACT,aAAa,CACd,CAAC;IAEF,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,MAAM,GAAG,GAAG,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;QACzC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QAC1C,MAAM,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,IAAI,CAAC;YACpB,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,UAAU,EAAE,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC;SAC3C,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { Client } from 'pg';
2
+ import type { RawEnum } from '@kozou/core';
3
+ export declare function fetchEnums(client: Client, schemas: string[]): Promise<RawEnum[]>;
4
+ //# sourceMappingURL=enums.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enums.d.ts","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AACjC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAS3C,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAyBtF"}
package/dist/enums.js ADDED
@@ -0,0 +1,22 @@
1
+ import { runQuery } from './errors.js';
2
+ export async function fetchEnums(client, schemas) {
3
+ if (schemas.length === 0)
4
+ return [];
5
+ const rows = await runQuery(client, `SELECT
6
+ n.nspname AS schema,
7
+ t.typname AS name,
8
+ array_agg(e.enumlabel ORDER BY e.enumsortorder) AS values
9
+ FROM pg_type t
10
+ JOIN pg_namespace n ON n.oid = t.typnamespace
11
+ JOIN pg_enum e ON e.enumtypid = t.oid
12
+ WHERE t.typtype = 'e'
13
+ AND n.nspname = ANY($1)
14
+ GROUP BY n.nspname, t.typname
15
+ ORDER BY n.nspname, t.typname`, [schemas], 'fetchEnums');
16
+ return rows.map((row) => ({
17
+ schema: row.schema,
18
+ name: row.name,
19
+ values: row.values,
20
+ }));
21
+ }
22
+ //# sourceMappingURL=enums.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enums.js","sourceRoot":"","sources":["../src/enums.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAQvC,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,MAAc,EAAE,OAAiB;IAChE,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAEpC,MAAM,IAAI,GAAG,MAAM,QAAQ,CACzB,MAAM,EACN;;;;;;;;;;mCAU+B,EAC/B,CAAC,OAAO,CAAC,EACT,YAAY,CACb,CAAC;IAEF,OAAO,IAAI,CAAC,GAAG,CAAU,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACjC,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,MAAM,EAAE,GAAG,CAAC,MAAM;KACnB,CAAC,CAAC,CAAC;AACN,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { Client } from 'pg';
2
+ export type KozouIntrospectErrorOptions = {
3
+ /** The failed SQL body. Omitted on connection failure. */
4
+ query?: string;
5
+ /** PostgreSQL error code (e.g. "42501") */
6
+ pgErrorCode?: string;
7
+ /** The original error object (forwarded via the standard Error.cause field). */
8
+ cause?: unknown;
9
+ };
10
+ export declare class KozouIntrospectError extends Error {
11
+ readonly query?: string;
12
+ readonly pgErrorCode?: string;
13
+ constructor(message: string, options?: KozouIntrospectErrorOptions);
14
+ }
15
+ export declare function runQuery<R extends Record<string, unknown>>(client: Client, query: string, params: unknown[], context: string): Promise<R[]>;
16
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAEjC,MAAM,MAAM,2BAA2B,GAAG;IACxC,0DAA0D;IAC1D,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,2CAA2C;IAC3C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,gFAAgF;IAChF,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB,CAAC;AAEF,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;gBAClB,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,2BAAgC;CAMvE;AAQD,wBAAsB,QAAQ,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9D,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,OAAO,EAAE,EACjB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,EAAE,CAAC,CAYd"}
package/dist/errors.js ADDED
@@ -0,0 +1,28 @@
1
+ export class KozouIntrospectError extends Error {
2
+ query;
3
+ pgErrorCode;
4
+ constructor(message, options = {}) {
5
+ super(message, options.cause !== undefined ? { cause: options.cause } : undefined);
6
+ this.name = 'KozouIntrospectError';
7
+ this.query = options.query;
8
+ this.pgErrorCode = options.pgErrorCode;
9
+ }
10
+ }
11
+ function isPgErrorLike(value) {
12
+ return typeof value === 'object' && value !== null;
13
+ }
14
+ export async function runQuery(client, query, params, context) {
15
+ try {
16
+ const res = await client.query(query, params);
17
+ return res.rows;
18
+ }
19
+ catch (err) {
20
+ const pgErr = isPgErrorLike(err) ? err : {};
21
+ throw new KozouIntrospectError(`${context}: ${pgErr.message ?? String(err)}`, {
22
+ query,
23
+ pgErrorCode: pgErr.code,
24
+ cause: err,
25
+ });
26
+ }
27
+ }
28
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAWA,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IACpC,KAAK,CAAU;IACf,WAAW,CAAU;IAC9B,YAAY,OAAe,EAAE,UAAuC,EAAE;QACpE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;QACnF,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;QACnC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QAC3B,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IACzC,CAAC;CACF;AAID,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,CAAC;AACrD,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,MAAc,EACd,KAAa,EACb,MAAiB,EACjB,OAAe;IAEf,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,KAAK,CAAI,KAAK,EAAE,MAAM,CAAC,CAAC;QACjD,OAAO,GAAG,CAAC,IAAI,CAAC;IAClB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,KAAK,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,oBAAoB,CAAC,GAAG,OAAO,KAAK,KAAK,CAAC,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE;YAC5E,KAAK;YACL,WAAW,EAAE,KAAK,CAAC,IAAI;YACvB,KAAK,EAAE,GAAG;SACX,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
package/dist/fks.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Client } from 'pg';
2
+ import type { RawForeignKey } from '@kozou/core';
3
+ export declare function fetchForeignKeys(client: Client, schemas: string[]): Promise<Map<string, RawForeignKey[]>>;
4
+ //# sourceMappingURL=fks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fks.d.ts","sourceRoot":"","sources":["../src/fks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AACjC,OAAO,KAAK,EAAY,aAAa,EAAE,MAAM,aAAa,CAAC;AA4B3D,wBAAsB,gBAAgB,CACpC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EAAE,GAChB,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,aAAa,EAAE,CAAC,CAAC,CAsDvC"}
package/dist/fks.js ADDED
@@ -0,0 +1,61 @@
1
+ import { runQuery } from './errors.js';
2
+ const ACTION_MAP = {
3
+ a: 'NO ACTION',
4
+ r: 'RESTRICT',
5
+ c: 'CASCADE',
6
+ n: 'SET NULL',
7
+ d: 'SET DEFAULT',
8
+ };
9
+ function mapAction(code) {
10
+ return ACTION_MAP[code] ?? 'NO ACTION';
11
+ }
12
+ export async function fetchForeignKeys(client, schemas) {
13
+ const result = new Map();
14
+ if (schemas.length === 0)
15
+ return result;
16
+ const rows = await runQuery(client, `SELECT
17
+ n.nspname AS schema,
18
+ c.relname AS table,
19
+ con.conname AS name,
20
+ (
21
+ SELECT array_agg(a.attname ORDER BY ord.n)::text[]
22
+ FROM unnest(con.conkey) WITH ORDINALITY AS ord(attnum, n)
23
+ JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = ord.attnum
24
+ ) AS columns,
25
+ rn.nspname AS referenced_schema,
26
+ rc.relname AS referenced_table,
27
+ (
28
+ SELECT array_agg(a.attname ORDER BY ord.n)::text[]
29
+ FROM unnest(con.confkey) WITH ORDINALITY AS ord(attnum, n)
30
+ JOIN pg_attribute a ON a.attrelid = con.confrelid AND a.attnum = ord.attnum
31
+ ) AS referenced_columns,
32
+ con.confdeltype AS on_delete,
33
+ con.confupdtype AS on_update,
34
+ d.description AS comment
35
+ FROM pg_constraint con
36
+ JOIN pg_class c ON c.oid = con.conrelid
37
+ JOIN pg_namespace n ON n.oid = c.relnamespace
38
+ JOIN pg_class rc ON rc.oid = con.confrelid
39
+ JOIN pg_namespace rn ON rn.oid = rc.relnamespace
40
+ LEFT JOIN pg_description d ON d.objoid = con.oid AND d.classoid = 'pg_constraint'::regclass
41
+ WHERE con.contype = 'f'
42
+ AND n.nspname = ANY($1)
43
+ ORDER BY n.nspname, c.relname, con.conname`, [schemas], 'fetchForeignKeys');
44
+ for (const row of rows) {
45
+ const key = `${row.schema}.${row.table}`;
46
+ if (!result.has(key))
47
+ result.set(key, []);
48
+ result.get(key).push({
49
+ name: row.name,
50
+ columns: row.columns ?? [],
51
+ referencedSchema: row.referenced_schema,
52
+ referencedTable: row.referenced_table,
53
+ referencedColumns: row.referenced_columns ?? [],
54
+ onDelete: mapAction(row.on_delete),
55
+ onUpdate: mapAction(row.on_update),
56
+ comment: row.comment,
57
+ });
58
+ }
59
+ return result;
60
+ }
61
+ //# sourceMappingURL=fks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fks.js","sourceRoot":"","sources":["../src/fks.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAevC,MAAM,UAAU,GAA6B;IAC3C,CAAC,EAAE,WAAW;IACd,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,aAAa;CACjB,CAAC;AAEF,SAAS,SAAS,CAAC,IAAY;IAC7B,OAAO,UAAU,CAAC,IAAI,CAAC,IAAI,WAAW,CAAC;AACzC,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,MAAc,EACd,OAAiB;IAEjB,MAAM,MAAM,GAAG,IAAI,GAAG,EAA2B,CAAC;IAClD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IAExC,MAAM,IAAI,GAAG,MAAM,QAAQ,CACzB,MAAM,EACN;;;;;;;;;;;;;;;;;;;;;;;;;;;gDA2B4C,EAC5C,CAAC,OAAO,CAAC,EACT,kBAAkB,CACnB,CAAC;IAEF,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,MAAM,GAAG,GAAG,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,KAAK,EAAE,CAAC;QACzC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QAC1C,MAAM,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,IAAI,CAAC;YACpB,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,OAAO,EAAE,GAAG,CAAC,OAAO,IAAI,EAAE;YAC1B,gBAAgB,EAAE,GAAG,CAAC,iBAAiB;YACvC,eAAe,EAAE,GAAG,CAAC,gBAAgB;YACrC,iBAAiB,EAAE,GAAG,CAAC,kBAAkB,IAAI,EAAE;YAC/C,QAAQ,EAAE,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC;YAClC,QAAQ,EAAE,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC;YAClC,OAAO,EAAE,GAAG,CAAC,OAAO;SACrB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { ClientConfig } from 'pg';
2
+ import type { RawIntrospection } from '@kozou/core';
3
+ import { KozouIntrospectError } from './errors.js';
4
+ export type IntrospectOptions = {
5
+ connection: string | ClientConfig;
6
+ schemas?: string[];
7
+ include?: string[];
8
+ exclude?: string[];
9
+ timeoutMs?: number;
10
+ };
11
+ export { KozouIntrospectError };
12
+ export type { KozouIntrospectErrorOptions } from './errors.js';
13
+ export declare function introspect(opts: IntrospectOptions): Promise<RawIntrospection>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAU,YAAY,EAAE,MAAM,IAAI,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAMpD,OAAO,EAAE,oBAAoB,EAAY,MAAM,aAAa,CAAC;AAI7D,MAAM,MAAM,iBAAiB,GAAG;IAC9B,UAAU,EAAE,MAAM,GAAG,YAAY,CAAC;IAClC,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF,OAAO,EAAE,oBAAoB,EAAE,CAAC;AAChC,YAAY,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAC;AAkB/D,wBAAsB,UAAU,CAAC,IAAI,EAAE,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CA6EnF"}
package/dist/index.js ADDED
@@ -0,0 +1,78 @@
1
+ import pkg from 'pg';
2
+ import { fetchTables, mergeTableMetadata } from './tables.js';
3
+ import { fetchForeignKeys } from './fks.js';
4
+ import { fetchChecks } from './checks.js';
5
+ import { fetchViews } from './views.js';
6
+ import { fetchEnums } from './enums.js';
7
+ import { KozouIntrospectError, runQuery } from './errors.js';
8
+ const PgClient = pkg.Client;
9
+ export { KozouIntrospectError };
10
+ function isPgErrorLike(value) {
11
+ return typeof value === 'object' && value !== null;
12
+ }
13
+ async function fetchExistingSchemas(client, schemas) {
14
+ const rows = await runQuery(client, 'SELECT schema_name FROM information_schema.schemata WHERE schema_name = ANY($1)', [schemas], 'fetchExistingSchemas');
15
+ return rows.map((r) => r.schema_name);
16
+ }
17
+ export async function introspect(opts) {
18
+ const schemas = opts.schemas ?? ['public'];
19
+ const timeoutMs = opts.timeoutMs ?? 10_000;
20
+ if (opts.include?.length || opts.exclude?.length) {
21
+ console.warn('[@kozou/introspect] opts.include / opts.exclude are not implemented in v0.1 (warning only)');
22
+ }
23
+ const baseConfig = typeof opts.connection === 'string'
24
+ ? { connectionString: opts.connection }
25
+ : { ...opts.connection };
26
+ const clientConfig = { ...baseConfig, statement_timeout: timeoutMs };
27
+ const client = new PgClient(clientConfig);
28
+ try {
29
+ await client.connect();
30
+ }
31
+ catch (err) {
32
+ const pgErr = isPgErrorLike(err) ? err : {};
33
+ throw new KozouIntrospectError(`Failed to connect to PostgreSQL: ${pgErr.message ?? String(err)}`, { pgErrorCode: pgErr.code, cause: err });
34
+ }
35
+ try {
36
+ await client.query('BEGIN');
37
+ await client.query('SET TRANSACTION READ ONLY');
38
+ const serverRows = await runQuery(client, 'SHOW server_version', [], 'showServerVersion');
39
+ const serverVersion = serverRows[0]?.server_version ?? 'unknown';
40
+ const existing = await fetchExistingSchemas(client, schemas);
41
+ for (const s of schemas) {
42
+ if (!existing.includes(s)) {
43
+ console.warn(`[@kozou/introspect] schema "${s}" does not exist (skip)`);
44
+ }
45
+ }
46
+ const validSchemas = schemas.filter((s) => existing.includes(s));
47
+ const tables = await fetchTables(client, validSchemas);
48
+ const fks = await fetchForeignKeys(client, validSchemas);
49
+ const checks = await fetchChecks(client, validSchemas);
50
+ mergeTableMetadata(tables, fks, checks);
51
+ const views = await fetchViews(client, validSchemas);
52
+ const enums = await fetchEnums(client, validSchemas);
53
+ return {
54
+ serverVersion,
55
+ introspectedAt: new Date().toISOString(),
56
+ schemas,
57
+ tables,
58
+ views,
59
+ enums,
60
+ functions: [],
61
+ };
62
+ }
63
+ finally {
64
+ try {
65
+ await client.query('ROLLBACK');
66
+ }
67
+ catch (err) {
68
+ console.warn(`[@kozou/introspect] ROLLBACK failed (ignored): ${String(err)}`);
69
+ }
70
+ try {
71
+ await client.end();
72
+ }
73
+ catch (err) {
74
+ console.warn(`[@kozou/introspect] client.end failed (ignored): ${String(err)}`);
75
+ }
76
+ }
77
+ }
78
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,IAAI,CAAC;AAGrB,OA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OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QAEjE,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QACvD,MAAM,GAAG,GAAG,MAAM,gBAAgB,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QACzD,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QACvD,kBAAkB,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC;QAExC,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QACrD,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QAErD,OAAO;YACL,aAAa;YACb,cAAc,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YACxC,OAAO;YACP,MAAM;YACN,KAAK;YACL,KAAK;YACL,SAAS,EAAE,EAAE;SACd,CAAC;IACJ,CAAC;YAAS,CAAC;QACT,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACjC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CAAC,kDAAkD,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChF,CAAC;QACD,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,GAAG,EAAE,CAAC;QACrB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CAAC,oDAAoD,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAClF,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { Client } from 'pg';
2
+ import type { RawCheck, RawForeignKey, RawTable } from '@kozou/core';
3
+ export declare function fetchTables(client: Client, schemas: string[]): Promise<RawTable[]>;
4
+ export declare function mergeTableMetadata(tables: RawTable[], fks: Map<string, RawForeignKey[]>, checks: Map<string, RawCheck[]>): void;
5
+ //# sourceMappingURL=tables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tables.d.ts","sourceRoot":"","sources":["../src/tables.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AACjC,OAAO,KAAK,EAAE,QAAQ,EAAa,aAAa,EAAY,QAAQ,EAAE,MAAM,aAAa,CAAC;AAoC1F,wBAAsB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC,CA+IxF;AAED,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,QAAQ,EAAE,EAClB,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,aAAa,EAAE,CAAC,EACjC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAC,GAC9B,IAAI,CAMN"}
package/dist/tables.js ADDED
@@ -0,0 +1,128 @@
1
+ import { runQuery } from './errors.js';
2
+ export async function fetchTables(client, schemas) {
3
+ if (schemas.length === 0)
4
+ return [];
5
+ const tableRows = await runQuery(client, `SELECT
6
+ n.nspname AS schema,
7
+ c.relname AS name,
8
+ d.description AS comment
9
+ FROM pg_class c
10
+ JOIN pg_namespace n ON n.oid = c.relnamespace
11
+ LEFT JOIN pg_description d ON d.objoid = c.oid AND d.objsubid = 0
12
+ WHERE c.relkind = 'r'
13
+ AND n.nspname = ANY($1)
14
+ ORDER BY n.nspname, c.relname`, [schemas], 'fetchTables (table list)');
15
+ if (tableRows.length === 0) {
16
+ return [];
17
+ }
18
+ const tableNames = tableRows.map((r) => r.name);
19
+ const columnRows = await runQuery(client, `SELECT
20
+ n.nspname AS schema,
21
+ c.relname AS table,
22
+ a.attname AS name,
23
+ format_type(a.atttypid, a.atttypmod) AS data_type,
24
+ t.typname AS udt_name,
25
+ NOT a.attnotnull AS is_nullable,
26
+ pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
27
+ d.description AS comment,
28
+ a.attnum::int AS position
29
+ FROM pg_attribute a
30
+ JOIN pg_class c ON c.oid = a.attrelid
31
+ JOIN pg_namespace n ON n.oid = c.relnamespace
32
+ JOIN pg_type t ON t.oid = a.atttypid
33
+ LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
34
+ LEFT JOIN pg_description d ON d.objoid = a.attrelid AND d.objsubid = a.attnum
35
+ WHERE c.relkind = 'r'
36
+ AND n.nspname = ANY($1)
37
+ AND a.attnum > 0
38
+ AND NOT a.attisdropped
39
+ ORDER BY n.nspname, c.relname, a.attnum`, [schemas], 'fetchTables (columns)');
40
+ const pkRows = await runQuery(client, `SELECT
41
+ n.nspname AS schema,
42
+ c.relname AS table,
43
+ a.attname AS column,
44
+ ord.n::int AS ordinal
45
+ FROM pg_constraint con
46
+ JOIN pg_class c ON c.oid = con.conrelid
47
+ JOIN pg_namespace n ON n.oid = c.relnamespace
48
+ JOIN LATERAL unnest(con.conkey) WITH ORDINALITY AS ord(attnum, n) ON true
49
+ JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = ord.attnum
50
+ WHERE con.contype = 'p'
51
+ AND n.nspname = ANY($1)
52
+ ORDER BY n.nspname, c.relname, ord.n`, [schemas], 'fetchTables (primary keys)');
53
+ const indexRows = await runQuery(client, `SELECT
54
+ n.nspname AS schema,
55
+ t.relname AS table,
56
+ ic.relname AS name,
57
+ (
58
+ SELECT array_agg(a.attname ORDER BY ord.n)::text[]
59
+ FROM unnest(ix.indkey) WITH ORDINALITY AS ord(attnum, n)
60
+ JOIN pg_attribute a ON a.attrelid = ix.indrelid AND a.attnum = ord.attnum
61
+ ) AS columns,
62
+ ix.indisunique AS is_unique
63
+ FROM pg_index ix
64
+ JOIN pg_class ic ON ic.oid = ix.indexrelid
65
+ JOIN pg_class t ON t.oid = ix.indrelid
66
+ JOIN pg_namespace n ON n.oid = t.relnamespace
67
+ WHERE t.relkind = 'r'
68
+ AND n.nspname = ANY($1)
69
+ AND NOT ix.indisprimary
70
+ ORDER BY n.nspname, t.relname, ic.relname`, [schemas], 'fetchTables (indexes)');
71
+ const tableKey = (schema, name) => `${schema}.${name}`;
72
+ const columnsByTable = new Map();
73
+ for (const row of columnRows) {
74
+ if (!tableNames.includes(row.table))
75
+ continue;
76
+ const key = tableKey(row.schema, row.table);
77
+ if (!columnsByTable.has(key))
78
+ columnsByTable.set(key, []);
79
+ columnsByTable.get(key).push({
80
+ name: row.name,
81
+ dataType: row.data_type,
82
+ udtName: row.udt_name,
83
+ nullable: row.is_nullable,
84
+ defaultExpr: row.column_default,
85
+ comment: row.comment,
86
+ position: row.position,
87
+ });
88
+ }
89
+ const pkByTable = new Map();
90
+ for (const row of pkRows) {
91
+ const key = tableKey(row.schema, row.table);
92
+ if (!pkByTable.has(key))
93
+ pkByTable.set(key, []);
94
+ pkByTable.get(key).push(row.column);
95
+ }
96
+ const indexByTable = new Map();
97
+ for (const row of indexRows) {
98
+ const key = tableKey(row.schema, row.table);
99
+ if (!indexByTable.has(key))
100
+ indexByTable.set(key, []);
101
+ indexByTable.get(key).push({
102
+ name: row.name,
103
+ columns: row.columns ?? [],
104
+ unique: row.is_unique,
105
+ });
106
+ }
107
+ return tableRows.map((row) => {
108
+ const key = tableKey(row.schema, row.name);
109
+ return {
110
+ schema: row.schema,
111
+ name: row.name,
112
+ comment: row.comment,
113
+ columns: columnsByTable.get(key) ?? [],
114
+ primaryKey: pkByTable.get(key) ?? [],
115
+ foreignKeys: [],
116
+ checks: [],
117
+ indexes: indexByTable.get(key) ?? [],
118
+ };
119
+ });
120
+ }
121
+ export function mergeTableMetadata(tables, fks, checks) {
122
+ for (const table of tables) {
123
+ const key = `${table.schema}.${table.name}`;
124
+ table.foreignKeys = fks.get(key) ?? [];
125
+ table.checks = checks.get(key) ?? [];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AC,IAAI,EAAE,CAAC;IACvC,CAAC;AACH,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { Client } from 'pg';
2
+ import type { RawView } from '@kozou/core';
3
+ export declare function fetchViews(client: Client, schemas: string[]): Promise<RawView[]>;
4
+ //# sourceMappingURL=views.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"views.d.ts","sourceRoot":"","sources":["../src/views.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AACjC,OAAO,KAAK,EAAa,OAAO,EAAE,MAAM,aAAa,CAAC;AA6BtD,wBAAsB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CA0GtF"}
package/dist/views.js ADDED
@@ -0,0 +1,90 @@
1
+ import { runQuery } from './errors.js';
2
+ export async function fetchViews(client, schemas) {
3
+ if (schemas.length === 0)
4
+ return [];
5
+ const viewRows = await runQuery(client, `SELECT
6
+ n.nspname AS schema,
7
+ c.relname AS name,
8
+ d.description AS comment,
9
+ pg_get_viewdef(c.oid, true) AS definition
10
+ FROM pg_class c
11
+ JOIN pg_namespace n ON n.oid = c.relnamespace
12
+ LEFT JOIN pg_description d ON d.objoid = c.oid AND d.objsubid = 0
13
+ WHERE c.relkind = 'v'
14
+ AND n.nspname = ANY($1)
15
+ ORDER BY n.nspname, c.relname`, [schemas], 'fetchViews (view list)');
16
+ if (viewRows.length === 0)
17
+ return [];
18
+ const columnRows = await runQuery(client, `SELECT
19
+ n.nspname AS schema,
20
+ c.relname AS view,
21
+ a.attname AS name,
22
+ format_type(a.atttypid, a.atttypmod) AS data_type,
23
+ t.typname AS udt_name,
24
+ NOT a.attnotnull AS is_nullable,
25
+ pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
26
+ d.description AS comment,
27
+ a.attnum::int AS position
28
+ FROM pg_attribute a
29
+ JOIN pg_class c ON c.oid = a.attrelid
30
+ JOIN pg_namespace n ON n.oid = c.relnamespace
31
+ JOIN pg_type t ON t.oid = a.atttypid
32
+ LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
33
+ LEFT JOIN pg_description d ON d.objoid = a.attrelid AND d.objsubid = a.attnum
34
+ WHERE c.relkind = 'v'
35
+ AND n.nspname = ANY($1)
36
+ AND a.attnum > 0
37
+ AND NOT a.attisdropped
38
+ ORDER BY n.nspname, c.relname, a.attnum`, [schemas], 'fetchViews (columns)');
39
+ const depRows = await runQuery(client, `SELECT DISTINCT
40
+ view_ns.nspname AS view_schema,
41
+ view_cls.relname AS view_name,
42
+ dep_ns.nspname AS dep_schema,
43
+ dep_cls.relname AS dep_name
44
+ FROM pg_rewrite rw
45
+ JOIN pg_class view_cls ON view_cls.oid = rw.ev_class
46
+ JOIN pg_namespace view_ns ON view_ns.oid = view_cls.relnamespace
47
+ JOIN pg_depend d ON d.objid = rw.oid AND d.classid = 'pg_rewrite'::regclass
48
+ JOIN pg_class dep_cls ON dep_cls.oid = d.refobjid AND d.refclassid = 'pg_class'::regclass
49
+ JOIN pg_namespace dep_ns ON dep_ns.oid = dep_cls.relnamespace
50
+ WHERE view_ns.nspname = ANY($1)
51
+ AND view_cls.relkind = 'v'
52
+ AND dep_cls.relkind IN ('r','v','m')
53
+ AND dep_cls.oid <> view_cls.oid
54
+ ORDER BY view_ns.nspname, view_cls.relname, dep_ns.nspname, dep_cls.relname`, [schemas], 'fetchViews (dependencies)');
55
+ const viewKey = (schema, name) => `${schema}.${name}`;
56
+ const columnsByView = new Map();
57
+ for (const row of columnRows) {
58
+ const key = viewKey(row.schema, row.view);
59
+ if (!columnsByView.has(key))
60
+ columnsByView.set(key, []);
61
+ columnsByView.get(key).push({
62
+ name: row.name,
63
+ dataType: row.data_type,
64
+ udtName: row.udt_name,
65
+ nullable: row.is_nullable,
66
+ defaultExpr: row.column_default,
67
+ comment: row.comment,
68
+ position: row.position,
69
+ });
70
+ }
71
+ const depsByView = new Map();
72
+ for (const row of depRows) {
73
+ const key = viewKey(row.view_schema, row.view_name);
74
+ if (!depsByView.has(key))
75
+ depsByView.set(key, []);
76
+ depsByView.get(key).push({ schema: row.dep_schema, name: row.dep_name });
77
+ }
78
+ return viewRows.map((row) => {
79
+ const key = viewKey(row.schema, row.name);
80
+ return {
81
+ schema: row.schema,
82
+ name: row.name,
83
+ comment: row.comment,
84
+ columns: columnsByView.get(key) ?? [],
85
+ underlyingTables: depsByView.get(key) ?? [],
86
+ definition: row.definition,
87
+ };
88
+ });
89
+ }
90
+ //# sourceMappingURL=views.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"views.js","sourceRoot":"","sources":["../src/views.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AA4BvC,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,MAAc,EAAE,OAAiB;IAChE,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAEpC,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAC7B,MAAM,EACN;;;;;;;;;;mCAU+B,EAC/B,CAAC,OAAO,CAAC,EACT,wBAAwB,CACzB,CAAC;IAEF,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAErC,MAAM,UAAU,GAAG,MAAM,QAAQ,CAC/B,MAAM,EACN;;;;;;;;;;;;;;;;;;;;6CAoByC,EACzC,CAAC,OAAO,CAAC,EACT,sBAAsB,CACvB,CAAC;IAEF,MAAM,OAAO,GAAG,MAAM,QAAQ,CAC5B,MAAM,EACN;;;;;;;;;;;;;;;iFAe6E,EAC7E,CAAC,OAAO,CAAC,EACT,2BAA2B,CAC5B,CAAC;IAEF,MAAM,OAAO,GAAG,CAAC,MAAc,EAAE,IAAY,EAAE,EAAE,CAAC,GAAG,MAAM,IAAI,IAAI,EAAE,CAAC;IAEtE,MAAM,aAAa,GAAG,IA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CAAC,CAAC;QAC1C,OAAO;YACL,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,OAAO,EAAE,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE;YACrC,gBAAgB,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,EAAE;YAC3C,UAAU,EAAE,GAAG,CAAC,UAAU;SAC3B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,44 @@
1
1
  {
2
2
  "name": "@kozou/introspect",
3
- "version": "0.0.1",
4
- "description": "Placeholder; v0.1.0 release lands via CI shortly.",
3
+ "version": "0.1.0",
4
+ "description": "PostgreSQL DDL / COMMENT / VIEW introspection (Kozou v0.1 spec §5).",
5
5
  "license": "Apache-2.0",
6
- "publishConfig": { "access": "public" }
7
- }
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/kozou-dev/kozou.git",
9
+ "directory": "packages/introspect"
10
+ },
11
+ "homepage": "https://kozou.org",
12
+ "bugs": {
13
+ "url": "https://github.com/kozou-dev/kozou/issues"
14
+ },
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ },
23
+ "./package.json": "./package.json"
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "publishConfig": {
29
+ "access": "public",
30
+ "provenance": true
31
+ },
32
+ "dependencies": {
33
+ "pg": "^8.13.0",
34
+ "@kozou/core": "0.1.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/pg": "^8.11.0"
38
+ },
39
+ "scripts": {
40
+ "typecheck": "tsc --noEmit",
41
+ "build": "tsc",
42
+ "test": "vitest run --coverage"
43
+ }
44
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # @kozou/introspect
2
-
3
- Placeholder publish. The real v0.1.0 release lands via CI shortly. See https://github.com/kozou-dev/kozou for the source.