-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexternal-datasource-service.ts
More file actions
481 lines (433 loc) · 16.6 KB
/
Copy pathexternal-datasource-service.ts
File metadata and controls
481 lines (433 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* ExternalDatasourceService — implements {@link IExternalDatasourceService}
* (ADR-0015 §6) on top of driver introspection.
*
* The service is intentionally decoupled from the kernel: all I/O
* (introspection, metadata reads) is injected via
* {@link ExternalDatasourceServiceConfig}, so the introspection/draft/validate
* logic is pure and unit-testable. The kernel plugin wires the real
* `IDataEngine` + `IMetadataService` callbacks in.
*/
import type {
IExternalDatasourceService,
RemoteTable,
GenerateDraftOpts,
ObjectDraft,
ImportObjectOpts,
ImportObjectResult,
SchemaValidationResult,
SchemaValidationReport,
IntrospectedSchema,
IntrospectedTable,
} from '@objectstack/spec/contracts';
import type { SchemaDiffEntry } from '@objectstack/spec/shared';
import {
suggestFieldType,
isCompatible,
ExternalCatalogSchema,
type ExternalCatalog,
type SqlDialect,
type FieldType,
} from '@objectstack/spec/data';
/** Minimal datasource shape the service reads (subset of `Datasource`). */
export interface DatasourceLike {
name: string;
schemaMode?: 'managed' | 'external' | 'validate-only';
external?: {
allowedSchemas?: string[];
validation?: { onMismatch?: 'fail' | 'warn' | 'ignore' };
};
}
/** Minimal object shape the service reads (subset of `ServiceObject`). */
export interface ObjectLike {
name: string;
label?: string;
datasource?: string;
external?: {
remoteName?: string;
remoteSchema?: string;
columnMap?: Record<string, string>;
ignoreColumns?: string[];
};
fields?: Record<string, { type?: string; required?: boolean }>;
}
export interface Logger {
warn: (message: string, meta?: unknown) => void;
info?: (message: string, meta?: unknown) => void;
}
/**
* Injected dependencies. The plugin supplies real implementations backed by
* the driver registry and `IMetadataService`; tests supply fakes.
*/
export interface ExternalDatasourceServiceConfig {
/** Introspect a datasource's live schema via its driver. */
introspect: (datasource: string) => Promise<IntrospectedSchema>;
/** Resolve a datasource definition by name. */
getDatasource: (name: string) => Promise<DatasourceLike | undefined>;
/** Resolve one object definition by name. */
getObject: (name: string) => Promise<ObjectLike | undefined>;
/** List all object definitions (for `validateAll`). */
listObjects: () => Promise<ObjectLike[]>;
/**
* Persist a refreshed catalog snapshot as an `external_catalog` metadata
* record. Optional: when absent, `refreshCatalog` still returns the snapshot
* but does not cache it (e.g. dev runs without a writable metadata store).
*/
persistCatalog?: (catalog: ExternalCatalog) => Promise<void>;
/**
* Persist an imported object definition as a live (runtime-origin) `object`
* metadata record. Optional: when absent, {@link ExternalDatasourceService.importObject}
* throws (the deployment is GitOps-only / has no writable metadata store).
*/
persistObject?: (name: string, definition: Record<string, unknown>) => Promise<void>;
logger?: Logger;
}
/** Columns ObjectStack manages itself — never validated against the remote. */
const BUILTIN_COLUMNS = new Set(['id', 'created_at', 'updated_at']);
/** Split a possibly schema-qualified name (`mart.fact_orders`). */
function parseQualified(raw: string): { schema?: string; name: string } {
const idx = raw.indexOf('.');
if (idx === -1) return { name: raw };
return { schema: raw.slice(0, idx), name: raw.slice(idx + 1) };
}
/** Normalise a remote table name into a snake_case object name. */
function toObjectName(remoteName: string): string {
const { name } = parseQualified(remoteName);
return name
.replace(/[^a-zA-Z0-9_]/g, '_')
.replace(/^[^a-z_]/, (c) => `_${c.toLowerCase()}`)
.toLowerCase();
}
/** snake_case → Title Case label. */
function toLabel(name: string): string {
return name
.split('_')
.filter(Boolean)
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join(' ');
}
export class ExternalDatasourceService implements IExternalDatasourceService {
constructor(private readonly config: ExternalDatasourceServiceConfig) {}
private get logger(): Logger | undefined {
return this.config.logger;
}
private findTable(schema: IntrospectedSchema, remoteName: string): IntrospectedTable | undefined {
const want = parseQualified(remoteName).name;
for (const table of Object.values(schema.tables)) {
if (table.name === remoteName) return table;
if (parseQualified(table.name).name === want) return table;
}
return undefined;
}
async listRemoteTables(
datasource: string,
opts?: { schema?: string },
): Promise<RemoteTable[]> {
const [schema, ds] = await Promise.all([
this.config.introspect(datasource),
this.config.getDatasource(datasource),
]);
const allowed = ds?.external?.allowedSchemas;
const tables: RemoteTable[] = [];
for (const table of Object.values(schema.tables)) {
const { schema: tableSchema, name } = parseQualified(table.name);
if (opts?.schema && tableSchema && tableSchema !== opts.schema) continue;
// allowedSchemas only filters tables we can attribute to a schema.
if (allowed && tableSchema && !allowed.includes(tableSchema)) continue;
tables.push({ schema: tableSchema, name, columnCount: table.columns.length });
}
return tables;
}
/**
* Probe a *saved* datasource by name with a live round-trip. Reuses the
* introspection path (driver connect + schema read) as a cheap connectivity
* check, so the secret is resolved through the same wired pool as the rest of
* the introspection surface — the caller never handles cleartext. Returns a
* structured result rather than throwing so the route can render ok/error
* uniformly. This backs the `datasource` `test_connection` action
* (`POST /datasources/:name/test`).
*/
async testConnection(
datasource: string,
): Promise<{ ok: boolean; latencyMs?: number; tableCount?: number; error?: string }> {
const started = Date.now();
try {
const schema = await this.config.introspect(datasource);
return {
ok: true,
latencyMs: Date.now() - started,
tableCount: Object.keys(schema.tables).length,
};
} catch (err) {
return { ok: false, error: err instanceof Error ? err.message : String(err) };
}
}
async generateObjectDraft(
datasource: string,
remoteName: string,
opts: GenerateDraftOpts = {},
): Promise<ObjectDraft> {
const schema = await this.config.introspect(datasource);
const table = this.findTable(schema, remoteName);
if (!table) {
throw new Error(
`Remote table '${remoteName}' not found on datasource '${datasource}'.`,
);
}
const dialect = schema.dialect as SqlDialect | undefined;
// Derive the remote schema from the matched table's qualified name (the
// caller may pass an unqualified `remoteName`).
const matched = parseQualified(table.name);
const remoteSchema = opts.remoteSchema ?? matched.schema;
const resolvedRemoteName = matched.name;
const include = opts.includeColumns ? new Set(opts.includeColumns) : undefined;
const exclude = opts.excludeColumns ? new Set(opts.excludeColumns) : new Set<string>();
const pkOverride = opts.primaryKey ? new Set(opts.primaryKey) : undefined;
const fields: Record<string, { type: FieldType; primaryKey?: boolean }> = {};
const review: ObjectDraft['review'] = [];
for (const col of table.columns) {
if (include && !include.has(col.name)) continue;
if (exclude.has(col.name)) continue;
const fieldName = opts.rename?.[col.name] ?? col.name;
const suggested = suggestFieldType(col.type, dialect);
const fieldType: FieldType = suggested ?? 'text';
if (!suggested) {
review.push({
column: col.name,
remoteType: col.type,
note: `unrecognised remote type — defaulted to 'text', verify`,
});
} else if (isCompatible(col.type, fieldType, dialect) === 'lossy') {
review.push({
column: col.name,
remoteType: col.type,
note: `mapped lossy to '${fieldType}'`,
});
}
const isPk = pkOverride ? pkOverride.has(col.name) : col.primaryKey;
fields[fieldName] = isPk ? { type: fieldType, primaryKey: true } : { type: fieldType };
}
const name = toObjectName(resolvedRemoteName);
const definition: Record<string, unknown> = {
name,
label: toLabel(name),
datasource,
external: {
...(remoteSchema ? { remoteSchema } : {}),
remoteName: resolvedRemoteName,
},
fields,
};
return {
name,
datasource,
definition,
source: renderObjectSource(definition, fields, review),
review,
};
}
async importObject(
datasource: string,
remoteName: string,
opts: ImportObjectOpts = {},
): Promise<ImportObjectResult> {
if (!this.config.persistObject) {
throw new Error(
`importObject requires a writable metadata store, but none is wired ` +
`(datasource '${datasource}'). This deployment may be GitOps-only — ` +
`use 'os datasource introspect' and commit the generated *.object.ts instead.`,
);
}
// Reuse the draft pipeline (type mapping, review notes, external binding).
const draft = await this.generateObjectDraft(datasource, remoteName, opts);
// Apply the runtime-persona overrides on top of the draft definition.
const name = opts.name ?? draft.name;
const external = {
...(draft.definition.external as Record<string, unknown>),
...(opts.writable ? { writable: true } : {}),
};
const definition: Record<string, unknown> = {
...draft.definition,
name,
label: toLabel(name),
external,
};
await this.config.persistObject(name, definition);
this.logger?.info?.(`importObject: persisted '${name}' from ${datasource}.${remoteName}`, {
writable: opts.writable === true,
review: draft.review.length,
});
return { name, definition, review: draft.review };
}
async refreshCatalog(datasource: string): Promise<ExternalCatalog> {
const schema = await this.config.introspect(datasource);
// Parse through the Zod schema so the persisted record is canonical
// (defaults applied, shape validated) and matches the `external_catalog`
// metadata type the boot gate + Studio read back.
const catalog = ExternalCatalogSchema.parse({
name: `${datasource}_catalog`,
datasource,
snapshotAt: new Date().toISOString(),
dialect: schema.dialect,
tables: Object.values(schema.tables).map((t) => {
const { schema: s, name } = parseQualified(t.name);
return {
remoteSchema: s,
remoteName: name,
columns: t.columns.map((c) => ({
name: c.name,
sqlType: c.type,
nullable: c.nullable,
primaryKey: c.primaryKey,
suggestedFieldType: suggestFieldType(c.type, schema.dialect as SqlDialect),
})),
};
}),
}) as ExternalCatalog;
// Best-effort cache: a failure to persist must not fail the refresh — the
// caller still gets the live snapshot back.
if (this.config.persistCatalog) {
try {
await this.config.persistCatalog(catalog);
} catch (err) {
this.logger?.warn?.(`refreshCatalog: failed to persist '${catalog.name}'`, err);
}
}
return catalog;
}
async validateObject(objectName: string): Promise<SchemaValidationResult> {
const obj = await this.config.getObject(objectName);
if (!obj) {
throw new Error(`Object '${objectName}' not found.`);
}
const datasource = obj.datasource ?? 'default';
const ds = await this.config.getDatasource(datasource);
// Not a federated object → nothing to validate.
if (!ds || !ds.schemaMode || ds.schemaMode === 'managed') {
return { ok: true, datasource, object: objectName, diffs: [] };
}
const schema = await this.config.introspect(datasource);
const dialect = schema.dialect as SqlDialect | undefined;
const remoteName = obj.external?.remoteName ?? obj.name;
const table = this.findTable(schema, remoteName);
const diffs: SchemaDiffEntry[] = [];
if (!table) {
diffs.push({
kind: 'missing_table',
remoteSchema: obj.external?.remoteSchema,
remoteName,
severity: 'error',
});
return { ok: false, datasource, object: objectName, diffs };
}
const columnsByName = new Map(table.columns.map((c) => [c.name, c]));
const ignore = new Set(obj.external?.ignoreColumns ?? []);
// columnMap is remoteColumn → fieldName; invert for field → remoteColumn.
const fieldToRemote = new Map<string, string>();
for (const [remoteCol, fieldName] of Object.entries(obj.external?.columnMap ?? {})) {
fieldToRemote.set(fieldName, remoteCol);
}
for (const [fieldName, field] of Object.entries(obj.fields ?? {})) {
if (BUILTIN_COLUMNS.has(fieldName)) continue;
const remoteCol = fieldToRemote.get(fieldName) ?? fieldName;
if (ignore.has(remoteCol)) continue;
const col = columnsByName.get(remoteCol);
if (!col) {
diffs.push({
kind: 'missing_column',
remoteName,
column: remoteCol,
severity: 'error',
});
continue;
}
const fieldType = (field.type ?? 'text') as FieldType;
const compat = isCompatible(col.type, fieldType, dialect);
if (compat === false) {
diffs.push({
kind: 'type_mismatch',
remoteName,
column: remoteCol,
expected: fieldType,
actual: col.type,
severity: 'error',
});
} else if (compat === 'lossy') {
diffs.push({
kind: 'type_mismatch',
remoteName,
column: remoteCol,
expected: fieldType,
actual: col.type,
severity: 'warning',
});
}
}
const ok = !diffs.some((d) => d.severity === 'error');
return { ok, datasource, object: objectName, diffs };
}
async validateAll(): Promise<SchemaValidationReport> {
const objects = await this.config.listObjects();
const federated = objects.filter(
(o) => o.external !== undefined || (o.datasource && o.datasource !== 'default'),
);
const results = await Promise.all(
federated.map((o) =>
this.validateObject(o.name).catch((err): SchemaValidationResult => {
this.logger?.warn(`validateObject('${o.name}') failed`, err);
return {
ok: false,
datasource: o.datasource ?? 'default',
object: o.name,
diffs: [
{
kind: 'missing_table',
remoteName: o.external?.remoteName ?? o.name,
actual: err instanceof Error ? err.message : String(err),
severity: 'error',
},
],
};
}),
),
);
const ok = results.every((r) => r.ok);
return { ok, results };
}
}
/** Render a reviewable `*.object.ts` source string for an object draft. */
function renderObjectSource(
definition: Record<string, unknown>,
fields: Record<string, { type: FieldType; primaryKey?: boolean }>,
review: ObjectDraft['review'],
): string {
const reviewByColumn = new Map(review.map((r) => [r.column, r.note]));
const external = definition.external as { remoteSchema?: string; remoteName?: string };
const fieldLines = Object.entries(fields).map(([fieldName, f]) => {
const note = reviewByColumn.get(fieldName);
const pk = f.primaryKey ? ', primaryKey: true' : '';
const comment = note ? ` // REVIEW: ${note}` : '';
return ` ${fieldName}: { type: '${f.type}'${pk} },${comment}`;
});
const externalLine = external.remoteSchema
? ` external: { remoteSchema: '${external.remoteSchema}', remoteName: '${external.remoteName}' },`
: ` external: { remoteName: '${external.remoteName}' },`;
return [
`// Generated by \`os datasource introspect\` (ADR-0015). Review before committing.`,
`import type { ServiceObjectInput } from '@objectstack/spec/data';`,
``,
`const ${definition.name as string}: ServiceObjectInput = {`,
` name: '${definition.name as string}',`,
` label: '${definition.label as string}',`,
` datasource: '${definition.datasource as string}',`,
externalLine,
` fields: {`,
...fieldLines,
` },`,
`};`,
``,
`export default ${definition.name as string};`,
``,
].join('\n');
}