@macss/modular-api 0.4.5 → 0.4.8
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 +70 -8
- package/dist/core/body_parser_error_handler.d.ts +15 -0
- package/dist/core/body_parser_error_handler.js +35 -0
- package/dist/core/logger/logger.d.ts +1 -0
- package/dist/core/logger/logger.js +1 -0
- package/dist/core/logger/logging_middleware.js +11 -0
- package/dist/core/modular_api.d.ts +17 -0
- package/dist/core/modular_api.js +107 -43
- package/dist/core/official_plugins.d.ts +35 -0
- package/dist/core/official_plugins.js +213 -0
- package/dist/core/plugin.d.ts +163 -0
- package/dist/core/plugin.js +304 -0
- package/dist/core/request_pipeline_audit.d.ts +15 -0
- package/dist/core/request_pipeline_audit.js +29 -0
- package/dist/core/schema/field.d.ts +2 -1
- package/dist/core/schema/field.js +4 -0
- package/dist/core/unhandled_request_error_handler.d.ts +2 -0
- package/dist/core/unhandled_request_error_handler.js +17 -0
- package/dist/core/usecase.js +1 -0
- package/dist/core/usecase_handler.js +3 -2
- package/dist/graphql/catalog/graphql_catalog_builder.d.ts +149 -0
- package/dist/graphql/catalog/graphql_catalog_builder.js +564 -0
- package/dist/graphql/metadata/graphql_metadata_parser.d.ts +53 -0
- package/dist/graphql/metadata/graphql_metadata_parser.js +373 -0
- package/dist/graphql/read/sql_read_contract.d.ts +155 -0
- package/dist/graphql/read/sql_read_contract.js +194 -0
- package/dist/graphql/read/sqlserver_read_compiler.d.ts +58 -0
- package/dist/graphql/read/sqlserver_read_compiler.js +279 -0
- package/dist/graphql/runtime/graphql_artifacts.d.ts +43 -0
- package/dist/graphql/runtime/graphql_artifacts.js +512 -0
- package/dist/graphql/runtime/graphql_runtime_options.d.ts +52 -0
- package/dist/graphql/runtime/graphql_runtime_options.js +60 -0
- package/dist/graphql/runtime/graphql_runtime_plugin.d.ts +18 -0
- package/dist/graphql/runtime/graphql_runtime_plugin.js +946 -0
- package/dist/graphql/schema/graphql_schema_sdl_generator.d.ts +17 -0
- package/dist/graphql/schema/graphql_schema_sdl_generator.js +221 -0
- package/dist/graphql/sqlserver/physical_model.d.ts +28 -0
- package/dist/graphql/sqlserver/physical_model.js +8 -0
- package/dist/graphql/sqlserver/sql_server_connection_settings.d.ts +15 -0
- package/dist/graphql/sqlserver/sql_server_connection_settings.js +28 -0
- package/dist/graphql/sqlserver/sql_server_metadata_reader.d.ts +39 -0
- package/dist/graphql/sqlserver/sql_server_metadata_reader.js +294 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +58 -1
- package/dist/openapi/swagger_docs.d.ts +11 -6
- package/dist/openapi/swagger_docs.js +16 -193
- package/package.json +9 -6
- package/dist/core/usecase_test_handler.d.ts +0 -27
- package/dist/core/usecase_test_handler.js +0 -60
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type GraphqlCatalog } from '../catalog/graphql_catalog_builder';
|
|
2
|
+
export declare class GraphqlSchemaSdlGenerator {
|
|
3
|
+
generate(catalog: GraphqlCatalog): string;
|
|
4
|
+
private collectCustomScalars;
|
|
5
|
+
private buildQueryType;
|
|
6
|
+
private buildObjectType;
|
|
7
|
+
private buildListEnvelope;
|
|
8
|
+
private buildKeyInput;
|
|
9
|
+
private buildFilterInput;
|
|
10
|
+
private buildOrderByInput;
|
|
11
|
+
private buildOrderFieldEnum;
|
|
12
|
+
private collectUsedFilterFamilies;
|
|
13
|
+
private buildScalarFilterInput;
|
|
14
|
+
private isCustomScalar;
|
|
15
|
+
private graphqlFieldType;
|
|
16
|
+
private enumValueForFieldName;
|
|
17
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GraphqlSchemaSdlGenerator = void 0;
|
|
4
|
+
const graphql_catalog_builder_1 = require("../catalog/graphql_catalog_builder");
|
|
5
|
+
class GraphqlSchemaSdlGenerator {
|
|
6
|
+
generate(catalog) {
|
|
7
|
+
const objects = [...catalog.objects].sort((left, right) => left.id.localeCompare(right.id));
|
|
8
|
+
const objectById = new Map(objects.map((object) => [object.id, object]));
|
|
9
|
+
const blocks = [];
|
|
10
|
+
const customScalars = this.collectCustomScalars(objects);
|
|
11
|
+
if (customScalars.length > 0) {
|
|
12
|
+
blocks.push(customScalars.map((scalar) => `scalar ${scalar}`).join('\n'));
|
|
13
|
+
}
|
|
14
|
+
blocks.push(this.buildQueryType(objects));
|
|
15
|
+
for (const object of objects) {
|
|
16
|
+
blocks.push(this.buildObjectType(object, objectById));
|
|
17
|
+
blocks.push(this.buildListEnvelope(object));
|
|
18
|
+
if (object.capabilities.item && object.graphql.itemField) {
|
|
19
|
+
blocks.push(this.buildKeyInput(object));
|
|
20
|
+
}
|
|
21
|
+
blocks.push(this.buildFilterInput(object));
|
|
22
|
+
blocks.push(this.buildOrderByInput(object));
|
|
23
|
+
blocks.push(this.buildOrderFieldEnum(object));
|
|
24
|
+
}
|
|
25
|
+
for (const family of this.collectUsedFilterFamilies(objects)) {
|
|
26
|
+
blocks.push(this.buildScalarFilterInput(family));
|
|
27
|
+
}
|
|
28
|
+
blocks.push(`enum SortDirection {
|
|
29
|
+
ASC
|
|
30
|
+
DESC
|
|
31
|
+
}`);
|
|
32
|
+
blocks.push(`input OffsetPageInput {
|
|
33
|
+
limit: Int
|
|
34
|
+
offset: Int
|
|
35
|
+
}`);
|
|
36
|
+
return blocks.join('\n\n');
|
|
37
|
+
}
|
|
38
|
+
collectCustomScalars(objects) {
|
|
39
|
+
const scalars = new Set();
|
|
40
|
+
for (const object of objects) {
|
|
41
|
+
for (const field of object.fields) {
|
|
42
|
+
if (this.isCustomScalar(field.type)) {
|
|
43
|
+
scalars.add(field.type);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return [...scalars].sort((left, right) => left.localeCompare(right));
|
|
48
|
+
}
|
|
49
|
+
buildQueryType(objects) {
|
|
50
|
+
const lines = ['type Query {'];
|
|
51
|
+
for (const object of objects) {
|
|
52
|
+
if (object.capabilities.item && object.graphql.itemField) {
|
|
53
|
+
lines.push(` ${object.graphql.itemField}(key: ${object.graphql.typeName}KeyInput!): ${object.graphql.typeName}`);
|
|
54
|
+
}
|
|
55
|
+
if (object.capabilities.collection) {
|
|
56
|
+
lines.push(` ${object.graphql.collectionField}(`);
|
|
57
|
+
lines.push(` filter: ${object.graphql.typeName}FilterInput`);
|
|
58
|
+
lines.push(` orderBy: [${object.graphql.typeName}OrderByInput!]`);
|
|
59
|
+
lines.push(' page: OffsetPageInput');
|
|
60
|
+
lines.push(` ): ${object.graphql.typeName}List!`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
lines.push('}');
|
|
64
|
+
return lines.join('\n');
|
|
65
|
+
}
|
|
66
|
+
buildObjectType(object, objectById) {
|
|
67
|
+
const lines = [`type ${object.graphql.typeName} {`];
|
|
68
|
+
for (const field of object.fields) {
|
|
69
|
+
if (field.visibility !== graphql_catalog_builder_1.GraphqlCatalogFieldVisibility.Public) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
lines.push(` ${field.publicName}: ${this.graphqlFieldType(field.type, field.nullable)}`);
|
|
73
|
+
}
|
|
74
|
+
for (const relation of object.relations) {
|
|
75
|
+
const target = objectById.get(relation.target);
|
|
76
|
+
if (!target) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const relationType = relation.cardinality === graphql_catalog_builder_1.GraphqlCatalogRelationCardinality.Many
|
|
80
|
+
? `[${target.graphql.typeName}!]!`
|
|
81
|
+
: target.graphql.typeName;
|
|
82
|
+
lines.push(` ${relation.name}: ${relationType}`);
|
|
83
|
+
}
|
|
84
|
+
lines.push('}');
|
|
85
|
+
return lines.join('\n');
|
|
86
|
+
}
|
|
87
|
+
buildListEnvelope(object) {
|
|
88
|
+
return `type ${object.graphql.typeName}List {
|
|
89
|
+
items: [${object.graphql.typeName}!]!
|
|
90
|
+
totalCount: Int!
|
|
91
|
+
}`;
|
|
92
|
+
}
|
|
93
|
+
buildKeyInput(object) {
|
|
94
|
+
const fieldByColumn = new Map(object.fields.map((field) => [field.column, field]));
|
|
95
|
+
const lines = [`input ${object.graphql.typeName}KeyInput {`];
|
|
96
|
+
for (const column of object.identity.fields) {
|
|
97
|
+
const field = fieldByColumn.get(column);
|
|
98
|
+
if (!field) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
lines.push(` ${field.publicName}: ${field.type}!`);
|
|
102
|
+
}
|
|
103
|
+
lines.push('}');
|
|
104
|
+
return lines.join('\n');
|
|
105
|
+
}
|
|
106
|
+
buildFilterInput(object) {
|
|
107
|
+
const lines = [`input ${object.graphql.typeName}FilterInput {`];
|
|
108
|
+
lines.push(` and: [${object.graphql.typeName}FilterInput!]`);
|
|
109
|
+
lines.push(` or: [${object.graphql.typeName}FilterInput!]`);
|
|
110
|
+
lines.push(` not: ${object.graphql.typeName}FilterInput`);
|
|
111
|
+
for (const field of object.fields) {
|
|
112
|
+
if (field.visibility !== graphql_catalog_builder_1.GraphqlCatalogFieldVisibility.Public ||
|
|
113
|
+
!field.filterable ||
|
|
114
|
+
field.type === 'Json') {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
lines.push(` ${field.publicName}: ${field.type}FilterInput`);
|
|
118
|
+
}
|
|
119
|
+
lines.push('}');
|
|
120
|
+
return lines.join('\n');
|
|
121
|
+
}
|
|
122
|
+
buildOrderByInput(object) {
|
|
123
|
+
return `input ${object.graphql.typeName}OrderByInput {
|
|
124
|
+
field: ${object.graphql.typeName}OrderField!
|
|
125
|
+
direction: SortDirection!
|
|
126
|
+
}`;
|
|
127
|
+
}
|
|
128
|
+
buildOrderFieldEnum(object) {
|
|
129
|
+
const lines = [`enum ${object.graphql.typeName}OrderField {`];
|
|
130
|
+
for (const field of object.fields) {
|
|
131
|
+
if (field.visibility !== graphql_catalog_builder_1.GraphqlCatalogFieldVisibility.Public || !field.sortable) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
lines.push(` ${this.enumValueForFieldName(field.publicName)}`);
|
|
135
|
+
}
|
|
136
|
+
lines.push('}');
|
|
137
|
+
return lines.join('\n');
|
|
138
|
+
}
|
|
139
|
+
collectUsedFilterFamilies(objects) {
|
|
140
|
+
const families = new Set();
|
|
141
|
+
for (const object of objects) {
|
|
142
|
+
for (const field of object.fields) {
|
|
143
|
+
if (field.visibility === graphql_catalog_builder_1.GraphqlCatalogFieldVisibility.Public &&
|
|
144
|
+
field.filterable &&
|
|
145
|
+
field.type !== 'Json') {
|
|
146
|
+
families.add(field.type);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return [...families].sort((left, right) => left.localeCompare(right));
|
|
151
|
+
}
|
|
152
|
+
buildScalarFilterInput(scalar) {
|
|
153
|
+
switch (scalar) {
|
|
154
|
+
case 'String':
|
|
155
|
+
return `input StringFilterInput {
|
|
156
|
+
eq: String
|
|
157
|
+
ne: String
|
|
158
|
+
in: [String!]
|
|
159
|
+
contains: String
|
|
160
|
+
startsWith: String
|
|
161
|
+
endsWith: String
|
|
162
|
+
isNull: Boolean
|
|
163
|
+
}`;
|
|
164
|
+
case 'Boolean':
|
|
165
|
+
return `input BooleanFilterInput {
|
|
166
|
+
eq: Boolean
|
|
167
|
+
ne: Boolean
|
|
168
|
+
isNull: Boolean
|
|
169
|
+
}`;
|
|
170
|
+
case 'Uuid':
|
|
171
|
+
return `input UuidFilterInput {
|
|
172
|
+
eq: Uuid
|
|
173
|
+
ne: Uuid
|
|
174
|
+
in: [Uuid!]
|
|
175
|
+
isNull: Boolean
|
|
176
|
+
}`;
|
|
177
|
+
case 'Int':
|
|
178
|
+
case 'Long':
|
|
179
|
+
case 'Float':
|
|
180
|
+
case 'Decimal':
|
|
181
|
+
case 'Date':
|
|
182
|
+
case 'DateTime':
|
|
183
|
+
return `input ${scalar}FilterInput {
|
|
184
|
+
eq: ${scalar}
|
|
185
|
+
ne: ${scalar}
|
|
186
|
+
in: [${scalar}!]
|
|
187
|
+
lt: ${scalar}
|
|
188
|
+
lte: ${scalar}
|
|
189
|
+
gt: ${scalar}
|
|
190
|
+
gte: ${scalar}
|
|
191
|
+
isNull: Boolean
|
|
192
|
+
}`;
|
|
193
|
+
case 'Json':
|
|
194
|
+
throw new Error('Json fields do not expose scalar filter operators in v1.');
|
|
195
|
+
default:
|
|
196
|
+
throw new Error(`Unsupported scalar family ${scalar} for filter input generation.`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
isCustomScalar(scalar) {
|
|
200
|
+
return ['Long', 'Decimal', 'Date', 'DateTime', 'Uuid', 'Json'].includes(scalar);
|
|
201
|
+
}
|
|
202
|
+
graphqlFieldType(scalar, nullable) {
|
|
203
|
+
return nullable ? scalar : `${scalar}!`;
|
|
204
|
+
}
|
|
205
|
+
enumValueForFieldName(publicName) {
|
|
206
|
+
const words = [];
|
|
207
|
+
const segmentPattern = /[A-Za-z0-9]+/g;
|
|
208
|
+
const wordPattern = /[A-Z]+(?:\d+)?(?=[A-Z][a-z]|$)|[A-Z]?[a-z]+\d*|\d+/g;
|
|
209
|
+
const segments = publicName.match(segmentPattern) ?? [];
|
|
210
|
+
for (const segment of segments) {
|
|
211
|
+
const matches = segment.match(wordPattern) ?? [];
|
|
212
|
+
for (const match of matches) {
|
|
213
|
+
if (match) {
|
|
214
|
+
words.push(match.toUpperCase());
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return words.join('_');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
exports.GraphqlSchemaSdlGenerator = GraphqlSchemaSdlGenerator;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export declare enum PhysicalObjectKind {
|
|
2
|
+
Table = "table",
|
|
3
|
+
View = "view"
|
|
4
|
+
}
|
|
5
|
+
export interface PhysicalCatalog {
|
|
6
|
+
readonly objects: readonly PhysicalObject[];
|
|
7
|
+
}
|
|
8
|
+
export interface PhysicalObject {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
readonly kind: PhysicalObjectKind;
|
|
11
|
+
readonly schemaName: string;
|
|
12
|
+
readonly objectName: string;
|
|
13
|
+
readonly identityFields: readonly string[];
|
|
14
|
+
readonly fields: readonly PhysicalField[];
|
|
15
|
+
readonly relations: readonly PhysicalRelationSeed[];
|
|
16
|
+
}
|
|
17
|
+
export interface PhysicalField {
|
|
18
|
+
readonly column: string;
|
|
19
|
+
readonly nativeType: string;
|
|
20
|
+
readonly nullable: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface PhysicalRelationSeed {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly sourceObjectId: string;
|
|
25
|
+
readonly targetObjectId: string;
|
|
26
|
+
readonly sourceFields: readonly string[];
|
|
27
|
+
readonly targetFields: readonly string[];
|
|
28
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PhysicalObjectKind = void 0;
|
|
4
|
+
var PhysicalObjectKind;
|
|
5
|
+
(function (PhysicalObjectKind) {
|
|
6
|
+
PhysicalObjectKind["Table"] = "table";
|
|
7
|
+
PhysicalObjectKind["View"] = "view";
|
|
8
|
+
})(PhysicalObjectKind || (exports.PhysicalObjectKind = PhysicalObjectKind = {}));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare class SqlServerConnectionSettings {
|
|
2
|
+
readonly host: string;
|
|
3
|
+
readonly port: number;
|
|
4
|
+
readonly database: string;
|
|
5
|
+
readonly username: string;
|
|
6
|
+
readonly password: string;
|
|
7
|
+
constructor(options: {
|
|
8
|
+
host: string;
|
|
9
|
+
port: number;
|
|
10
|
+
database: string;
|
|
11
|
+
username: string;
|
|
12
|
+
password: string;
|
|
13
|
+
});
|
|
14
|
+
static fromEnvironment(environment?: NodeJS.ProcessEnv): SqlServerConnectionSettings;
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlServerConnectionSettings = void 0;
|
|
4
|
+
class SqlServerConnectionSettings {
|
|
5
|
+
host;
|
|
6
|
+
port;
|
|
7
|
+
database;
|
|
8
|
+
username;
|
|
9
|
+
password;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.host = options.host;
|
|
12
|
+
this.port = options.port;
|
|
13
|
+
this.database = options.database;
|
|
14
|
+
this.username = options.username;
|
|
15
|
+
this.password = options.password;
|
|
16
|
+
}
|
|
17
|
+
static fromEnvironment(environment = process.env) {
|
|
18
|
+
const parsedPort = Number.parseInt(environment.MODULAR_API_SQLSERVER_PORT ?? '', 10);
|
|
19
|
+
return new SqlServerConnectionSettings({
|
|
20
|
+
host: environment.MODULAR_API_SQLSERVER_HOST ?? '127.0.0.1',
|
|
21
|
+
port: Number.isNaN(parsedPort) ? 14333 : parsedPort,
|
|
22
|
+
database: environment.MODULAR_API_SQLSERVER_DATABASE ?? 'modular_api_graphql_v1',
|
|
23
|
+
username: environment.MODULAR_API_SQLSERVER_USERNAME ?? 'sa',
|
|
24
|
+
password: environment.MODULAR_API_SQLSERVER_PASSWORD ?? 'ModularApi_dev_StrongPass1',
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.SqlServerConnectionSettings = SqlServerConnectionSettings;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type PhysicalCatalog } from './physical_model';
|
|
2
|
+
import { SqlServerConnectionSettings } from './sql_server_connection_settings';
|
|
3
|
+
type SqlServerConnectionConfig = {
|
|
4
|
+
user: string;
|
|
5
|
+
password: string;
|
|
6
|
+
server: string;
|
|
7
|
+
port: number;
|
|
8
|
+
database: string;
|
|
9
|
+
options: {
|
|
10
|
+
encrypt: boolean;
|
|
11
|
+
trustServerCertificate: boolean;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
type SqlServerRequestLike = {
|
|
15
|
+
query<Row extends object>(query: string): Promise<{
|
|
16
|
+
recordset: Row[];
|
|
17
|
+
}>;
|
|
18
|
+
};
|
|
19
|
+
type SqlServerConnectionPoolLike = {
|
|
20
|
+
connect(): Promise<void>;
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
request(): SqlServerRequestLike;
|
|
23
|
+
};
|
|
24
|
+
type SqlServerModuleLike = {
|
|
25
|
+
ConnectionPool: new (config: SqlServerConnectionConfig) => SqlServerConnectionPoolLike;
|
|
26
|
+
};
|
|
27
|
+
export declare class SqlServerMetadataReader {
|
|
28
|
+
private readonly connection;
|
|
29
|
+
private readonly sqlModuleLoader;
|
|
30
|
+
constructor(options: {
|
|
31
|
+
connection: SqlServerConnectionSettings;
|
|
32
|
+
sqlModule?: SqlServerModuleLike;
|
|
33
|
+
sqlModuleLoader?: () => SqlServerModuleLike;
|
|
34
|
+
});
|
|
35
|
+
introspect(options?: {
|
|
36
|
+
schemaNames?: Iterable<string>;
|
|
37
|
+
}): Promise<PhysicalCatalog>;
|
|
38
|
+
}
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlServerMetadataReader = void 0;
|
|
4
|
+
const physical_model_1 = require("./physical_model");
|
|
5
|
+
class SqlServerMetadataReader {
|
|
6
|
+
connection;
|
|
7
|
+
sqlModuleLoader;
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.connection = options.connection;
|
|
10
|
+
this.sqlModuleLoader =
|
|
11
|
+
options.sqlModule == null
|
|
12
|
+
? (options.sqlModuleLoader ?? defaultSqlModuleLoader)
|
|
13
|
+
: () => options.sqlModule;
|
|
14
|
+
}
|
|
15
|
+
async introspect(options = {}) {
|
|
16
|
+
const normalizedSchemaNames = Array.from(options.schemaNames ?? []).sort();
|
|
17
|
+
const sqlModule = loadSqlModule(this.sqlModuleLoader);
|
|
18
|
+
const pool = new sqlModule.ConnectionPool(buildConnectionConfig(this.connection));
|
|
19
|
+
await pool.connect();
|
|
20
|
+
try {
|
|
21
|
+
const objectsById = await loadObjects(pool, normalizedSchemaNames);
|
|
22
|
+
await loadFields(pool, normalizedSchemaNames, objectsById);
|
|
23
|
+
await loadIdentityFields(pool, normalizedSchemaNames, objectsById);
|
|
24
|
+
await loadRelations(pool, normalizedSchemaNames, objectsById);
|
|
25
|
+
return {
|
|
26
|
+
objects: Array.from(objectsById.values())
|
|
27
|
+
.map(buildPhysicalObject)
|
|
28
|
+
.sort((left, right) => left.id.localeCompare(right.id)),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
await pool.close();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.SqlServerMetadataReader = SqlServerMetadataReader;
|
|
37
|
+
async function loadObjects(pool, schemaNames) {
|
|
38
|
+
const rows = await runMetadataQuery(pool, 'SQL Server objects', `
|
|
39
|
+
SELECT
|
|
40
|
+
s.name AS schema_name,
|
|
41
|
+
o.name AS object_name,
|
|
42
|
+
CASE o.type
|
|
43
|
+
WHEN 'U' THEN 'table'
|
|
44
|
+
WHEN 'V' THEN 'view'
|
|
45
|
+
END AS object_kind
|
|
46
|
+
FROM sys.objects AS o
|
|
47
|
+
INNER JOIN sys.schemas AS s
|
|
48
|
+
ON s.schema_id = o.schema_id
|
|
49
|
+
WHERE o.type IN ('U', 'V')${schemaFilterClause('s.name', schemaNames)}
|
|
50
|
+
ORDER BY s.name, o.name;
|
|
51
|
+
`);
|
|
52
|
+
const objectsById = new Map();
|
|
53
|
+
for (const row of rows) {
|
|
54
|
+
const id = `${row.schema_name}.${row.object_name}`;
|
|
55
|
+
objectsById.set(id, {
|
|
56
|
+
id,
|
|
57
|
+
kind: parseObjectKind(row.object_kind),
|
|
58
|
+
schemaName: row.schema_name,
|
|
59
|
+
objectName: row.object_name,
|
|
60
|
+
identityFields: [],
|
|
61
|
+
fields: [],
|
|
62
|
+
relations: [],
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return objectsById;
|
|
66
|
+
}
|
|
67
|
+
async function loadFields(pool, schemaNames, objectsById) {
|
|
68
|
+
const rows = await runMetadataQuery(pool, 'SQL Server columns', `
|
|
69
|
+
SELECT
|
|
70
|
+
s.name AS schema_name,
|
|
71
|
+
o.name AS object_name,
|
|
72
|
+
c.name AS column_name,
|
|
73
|
+
TYPE_NAME(c.user_type_id) AS type_name,
|
|
74
|
+
CAST(c.max_length AS INT) AS max_length,
|
|
75
|
+
CAST(c.precision AS INT) AS precision,
|
|
76
|
+
CAST(c.scale AS INT) AS scale,
|
|
77
|
+
CAST(c.is_nullable AS INT) AS is_nullable
|
|
78
|
+
FROM sys.objects AS o
|
|
79
|
+
INNER JOIN sys.schemas AS s
|
|
80
|
+
ON s.schema_id = o.schema_id
|
|
81
|
+
INNER JOIN sys.columns AS c
|
|
82
|
+
ON c.object_id = o.object_id
|
|
83
|
+
WHERE o.type IN ('U', 'V')${schemaFilterClause('s.name', schemaNames)}
|
|
84
|
+
ORDER BY s.name, o.name, c.column_id;
|
|
85
|
+
`);
|
|
86
|
+
for (const row of rows) {
|
|
87
|
+
const object = requireObject(objectsById, row.schema_name, row.object_name);
|
|
88
|
+
object.fields.push({
|
|
89
|
+
column: row.column_name,
|
|
90
|
+
nativeType: formatNativeType({
|
|
91
|
+
typeName: row.type_name,
|
|
92
|
+
maxLength: row.max_length,
|
|
93
|
+
precision: row.precision,
|
|
94
|
+
scale: row.scale,
|
|
95
|
+
}),
|
|
96
|
+
nullable: Boolean(row.is_nullable),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function loadIdentityFields(pool, schemaNames, objectsById) {
|
|
101
|
+
const rows = await runMetadataQuery(pool, 'SQL Server identity fields', `
|
|
102
|
+
SELECT
|
|
103
|
+
s.name AS schema_name,
|
|
104
|
+
o.name AS object_name,
|
|
105
|
+
c.name AS column_name
|
|
106
|
+
FROM sys.objects AS o
|
|
107
|
+
INNER JOIN sys.schemas AS s
|
|
108
|
+
ON s.schema_id = o.schema_id
|
|
109
|
+
INNER JOIN sys.key_constraints AS kc
|
|
110
|
+
ON kc.parent_object_id = o.object_id
|
|
111
|
+
AND kc.type = 'PK'
|
|
112
|
+
INNER JOIN sys.index_columns AS ic
|
|
113
|
+
ON ic.object_id = kc.parent_object_id
|
|
114
|
+
AND ic.index_id = kc.unique_index_id
|
|
115
|
+
INNER JOIN sys.columns AS c
|
|
116
|
+
ON c.object_id = ic.object_id
|
|
117
|
+
AND c.column_id = ic.column_id
|
|
118
|
+
WHERE o.type = 'U'${schemaFilterClause('s.name', schemaNames)}
|
|
119
|
+
ORDER BY s.name, o.name, ic.key_ordinal;
|
|
120
|
+
`);
|
|
121
|
+
for (const row of rows) {
|
|
122
|
+
const object = requireObject(objectsById, row.schema_name, row.object_name);
|
|
123
|
+
object.identityFields.push(row.column_name);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
async function loadRelations(pool, schemaNames, objectsById) {
|
|
127
|
+
const rows = await runMetadataQuery(pool, 'SQL Server foreign keys', `
|
|
128
|
+
SELECT
|
|
129
|
+
source_schema.name AS source_schema_name,
|
|
130
|
+
source_object.name AS source_object_name,
|
|
131
|
+
fk.name AS constraint_name,
|
|
132
|
+
source_column.name AS source_column_name,
|
|
133
|
+
target_schema.name AS target_schema_name,
|
|
134
|
+
target_object.name AS target_object_name,
|
|
135
|
+
target_column.name AS target_column_name
|
|
136
|
+
FROM sys.foreign_keys AS fk
|
|
137
|
+
INNER JOIN sys.foreign_key_columns AS fkc
|
|
138
|
+
ON fkc.constraint_object_id = fk.object_id
|
|
139
|
+
INNER JOIN sys.objects AS source_object
|
|
140
|
+
ON source_object.object_id = fk.parent_object_id
|
|
141
|
+
INNER JOIN sys.schemas AS source_schema
|
|
142
|
+
ON source_schema.schema_id = source_object.schema_id
|
|
143
|
+
INNER JOIN sys.columns AS source_column
|
|
144
|
+
ON source_column.object_id = source_object.object_id
|
|
145
|
+
AND source_column.column_id = fkc.parent_column_id
|
|
146
|
+
INNER JOIN sys.objects AS target_object
|
|
147
|
+
ON target_object.object_id = fk.referenced_object_id
|
|
148
|
+
INNER JOIN sys.schemas AS target_schema
|
|
149
|
+
ON target_schema.schema_id = target_object.schema_id
|
|
150
|
+
INNER JOIN sys.columns AS target_column
|
|
151
|
+
ON target_column.object_id = target_object.object_id
|
|
152
|
+
AND target_column.column_id = fkc.referenced_column_id
|
|
153
|
+
WHERE source_object.type = 'U'${schemaFilterClause('source_schema.name', schemaNames)}
|
|
154
|
+
ORDER BY source_schema.name, source_object.name, fk.name, fkc.constraint_column_id;
|
|
155
|
+
`);
|
|
156
|
+
const relationsByKey = new Map();
|
|
157
|
+
for (const row of rows) {
|
|
158
|
+
const sourceObjectId = `${row.source_schema_name}.${row.source_object_name}`;
|
|
159
|
+
const targetObjectId = `${row.target_schema_name}.${row.target_object_name}`;
|
|
160
|
+
const relationKey = `${sourceObjectId}|${row.constraint_name}|${targetObjectId}`;
|
|
161
|
+
const relation = relationsByKey.get(relationKey) ?? {
|
|
162
|
+
name: row.constraint_name,
|
|
163
|
+
sourceObjectId,
|
|
164
|
+
targetObjectId,
|
|
165
|
+
sourceFields: [],
|
|
166
|
+
targetFields: [],
|
|
167
|
+
};
|
|
168
|
+
relation.sourceFields.push(row.source_column_name);
|
|
169
|
+
relation.targetFields.push(row.target_column_name);
|
|
170
|
+
relationsByKey.set(relationKey, relation);
|
|
171
|
+
}
|
|
172
|
+
for (const relation of relationsByKey.values()) {
|
|
173
|
+
const sourceObject = objectsById.get(relation.sourceObjectId);
|
|
174
|
+
if (!sourceObject) {
|
|
175
|
+
throw new Error(`Missing source object for relation ${relation.name}: ${relation.sourceObjectId}`);
|
|
176
|
+
}
|
|
177
|
+
sourceObject.relations.push({
|
|
178
|
+
name: relation.name,
|
|
179
|
+
sourceObjectId: relation.sourceObjectId,
|
|
180
|
+
targetObjectId: relation.targetObjectId,
|
|
181
|
+
sourceFields: Object.freeze([...relation.sourceFields]),
|
|
182
|
+
targetFields: Object.freeze([...relation.targetFields]),
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function runMetadataQuery(pool, label, query) {
|
|
187
|
+
try {
|
|
188
|
+
const result = await pool.request().query(query);
|
|
189
|
+
return result.recordset;
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
193
|
+
throw new Error(`Failed to load ${label}: ${message}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function buildConnectionConfig(connection) {
|
|
197
|
+
return {
|
|
198
|
+
user: connection.username,
|
|
199
|
+
password: connection.password,
|
|
200
|
+
server: connection.host,
|
|
201
|
+
port: connection.port,
|
|
202
|
+
database: connection.database,
|
|
203
|
+
options: {
|
|
204
|
+
encrypt: false,
|
|
205
|
+
trustServerCertificate: true,
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function defaultSqlModuleLoader() {
|
|
210
|
+
try {
|
|
211
|
+
const loaded = require('mssql');
|
|
212
|
+
return loaded.default ?? loaded;
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
throw toMissingSqlServerDriverError(error);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function loadSqlModule(loader) {
|
|
219
|
+
try {
|
|
220
|
+
return loader();
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
throw toMissingSqlServerDriverError(error);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
function toMissingSqlServerDriverError(error) {
|
|
227
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
228
|
+
if (message.includes('SqlServerMetadataReader requires the optional "mssql" package.')) {
|
|
229
|
+
return error instanceof Error ? error : new Error(message);
|
|
230
|
+
}
|
|
231
|
+
return new Error('SqlServerMetadataReader requires the optional "mssql" package. Install it to use SQL Server introspection. ' +
|
|
232
|
+
`Original error: ${message}`);
|
|
233
|
+
}
|
|
234
|
+
function buildPhysicalObject(object) {
|
|
235
|
+
return {
|
|
236
|
+
id: object.id,
|
|
237
|
+
kind: object.kind,
|
|
238
|
+
schemaName: object.schemaName,
|
|
239
|
+
objectName: object.objectName,
|
|
240
|
+
identityFields: Object.freeze([...object.identityFields]),
|
|
241
|
+
fields: Object.freeze([...object.fields]),
|
|
242
|
+
relations: Object.freeze([...object.relations]),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
function requireObject(objectsById, schemaName, objectName) {
|
|
246
|
+
const objectId = `${schemaName}.${objectName}`;
|
|
247
|
+
const object = objectsById.get(objectId);
|
|
248
|
+
if (!object) {
|
|
249
|
+
throw new Error(`Object not loaded before metadata expansion: ${objectId}`);
|
|
250
|
+
}
|
|
251
|
+
return object;
|
|
252
|
+
}
|
|
253
|
+
function parseObjectKind(value) {
|
|
254
|
+
switch (value) {
|
|
255
|
+
case physical_model_1.PhysicalObjectKind.Table:
|
|
256
|
+
return physical_model_1.PhysicalObjectKind.Table;
|
|
257
|
+
case physical_model_1.PhysicalObjectKind.View:
|
|
258
|
+
return physical_model_1.PhysicalObjectKind.View;
|
|
259
|
+
default:
|
|
260
|
+
throw new Error(`Unsupported SQL Server object kind: ${value}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function schemaFilterClause(column, schemaNames) {
|
|
264
|
+
if (schemaNames.length === 0) {
|
|
265
|
+
return '';
|
|
266
|
+
}
|
|
267
|
+
const values = schemaNames.map((schemaName) => `N'${schemaName.replace(/'/g, "''")}'`).join(', ');
|
|
268
|
+
return ` AND ${column} IN (${values})`;
|
|
269
|
+
}
|
|
270
|
+
function formatNativeType(options) {
|
|
271
|
+
switch (options.typeName.toLowerCase()) {
|
|
272
|
+
case 'nvarchar':
|
|
273
|
+
case 'nchar': {
|
|
274
|
+
const length = options.maxLength === -1 ? 'max' : String(options.maxLength / 2);
|
|
275
|
+
return `${options.typeName}(${length})`;
|
|
276
|
+
}
|
|
277
|
+
case 'varchar':
|
|
278
|
+
case 'char':
|
|
279
|
+
case 'varbinary':
|
|
280
|
+
case 'binary': {
|
|
281
|
+
const length = options.maxLength === -1 ? 'max' : String(options.maxLength);
|
|
282
|
+
return `${options.typeName}(${length})`;
|
|
283
|
+
}
|
|
284
|
+
case 'decimal':
|
|
285
|
+
case 'numeric':
|
|
286
|
+
return `${options.typeName}(${options.precision},${options.scale})`;
|
|
287
|
+
case 'datetime2':
|
|
288
|
+
case 'datetimeoffset':
|
|
289
|
+
case 'time':
|
|
290
|
+
return `${options.typeName}(${options.scale})`;
|
|
291
|
+
default:
|
|
292
|
+
return options.typeName;
|
|
293
|
+
}
|
|
294
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export { UseCaseException } from './core/use_case_exception';
|
|
|
6
6
|
export { InputValidationError } from './core/input_validation_error';
|
|
7
7
|
export { ModularApi } from './core/modular_api';
|
|
8
8
|
export type { ModularApiOptions } from './core/modular_api';
|
|
9
|
+
export { HOST_API_VERSION, PluginHostError } from './core/plugin';
|
|
10
|
+
export type { Capability, CapabilityHandle, HostMetadata, HttpMethod, MiddlewareSlot, ModuleExtensionContribution, ModuleExtensionPoint, Plugin, PluginHost, PluginManifest, PluginMiddleware, PluginRequestContext, PluginRequirement, PluginResponse, PluginRoute, PluginRouteVisibility, PluginValidationResult, RegisteredModuleView, RegisteredUseCaseView, } from './core/plugin';
|
|
9
11
|
export { ModuleBuilder } from './core/module_builder';
|
|
10
12
|
export type { UseCaseOptions } from './core/module_builder';
|
|
11
13
|
export { cors } from './middlewares/cors';
|
|
@@ -26,3 +28,18 @@ export { loggingMiddleware, LOGGER_LOCALS_KEY } from './core/logger/logging_midd
|
|
|
26
28
|
export type { LoggingMiddlewareOptions } from './core/logger/logging_middleware';
|
|
27
29
|
export { buildOpenApiSpec, jsonToYaml, openApiJsonHandler, openApiYamlHandler, } from './openapi/openapi';
|
|
28
30
|
export { swaggerDocsHandler } from './openapi/swagger_docs';
|
|
31
|
+
export { PhysicalObjectKind } from './graphql/sqlserver/physical_model';
|
|
32
|
+
export type { PhysicalCatalog, PhysicalField, PhysicalObject, PhysicalRelationSeed, } from './graphql/sqlserver/physical_model';
|
|
33
|
+
export { SqlServerConnectionSettings } from './graphql/sqlserver/sql_server_connection_settings';
|
|
34
|
+
export { SqlServerMetadataReader } from './graphql/sqlserver/sql_server_metadata_reader';
|
|
35
|
+
export { GraphqlMetadataParser, GraphqlMetadataSeverity } from './graphql/metadata/graphql_metadata_parser';
|
|
36
|
+
export type { GraphqlFieldMetadata, GraphqlMetadataDiagnostic, GraphqlMetadataFile, GraphqlMetadataLimit, GraphqlMetadataParseResult, GraphqlObjectMetadata, GraphqlRelationMetadata, } from './graphql/metadata/graphql_metadata_parser';
|
|
37
|
+
export { GraphqlCatalogBuildMode, GraphqlCatalogBuilder, GraphqlCatalogDiagnosticSeverity, GraphqlCatalogFieldVisibility, GraphqlCatalogIdentityMode, GraphqlCatalogNaming, GraphqlCatalogOrigin, GraphqlCatalogPaginationMode, GraphqlCatalogRelationCardinality, } from './graphql/catalog/graphql_catalog_builder';
|
|
38
|
+
export type { GraphqlCatalog, GraphqlCatalogBuild, GraphqlCatalogCapabilities, GraphqlCatalogDiagnostic, GraphqlCatalogField, GraphqlCatalogGraphqlNames, GraphqlCatalogIdentity, GraphqlCatalogPagination, GraphqlCatalogProvider, GraphqlCatalogRelation, GraphqlCatalogSource, GraphqlPublishedObject, } from './graphql/catalog/graphql_catalog_builder';
|
|
39
|
+
export { GraphqlSchemaSdlGenerator } from './graphql/schema/graphql_schema_sdl_generator';
|
|
40
|
+
export { GraphqlOptions, GraphqlRequestEvent, GraphqlRequestPhase, graphqlDefaultReadExecutorCapabilityId, } from './graphql/runtime/graphql_runtime_options';
|
|
41
|
+
export type { GraphqlEventSink, GraphqlSourceDigestFactory } from './graphql/runtime/graphql_runtime_options';
|
|
42
|
+
export { GraphqlArtifactBundle, GraphqlArtifactCompileError, GraphqlArtifactCompiler, GraphqlArtifactLoadError, tryLoadGraphqlCatalogArtifacts, } from './graphql/runtime/graphql_artifacts';
|
|
43
|
+
export { ReadExecutionContext, RowSet, SqlCollectionSelection, SqlCountSelection, SqlFilterCondition, SqlFilterGroup, SqlFilterGroupKind, SqlFilterOperator, SqlItemSelection, SqlOrderByClause, SqlPage, SqlParameter, SqlReadCommand, SqlReadCommandPurpose, SqlRelationBatchSelection, SqlSortDirection, } from './graphql/read/sql_read_contract';
|
|
44
|
+
export type { ReadExecutor } from './graphql/read/sql_read_contract';
|
|
45
|
+
export { SqlCatalogReadDispatcher, SqlServerReadCompiler } from './graphql/read/sqlserver_read_compiler';
|