43 lines
1.8 KiB
TypeScript
43 lines
1.8 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable, inject } from '@angular/core';
|
|
import { Observable, map } from 'rxjs';
|
|
import { PreviewResponse, SourceColumn, SqlConnectionRef } from './api.types';
|
|
|
|
/** POST /imports/sources/sql/*. Stateless: every call carries the connection. */
|
|
@Injectable({ providedIn: 'root' })
|
|
export class SqlSourceApi {
|
|
private readonly http = inject(HttpClient);
|
|
|
|
testConnection(connection: SqlConnectionRef): Observable<{ ok: true; dialect: string }> {
|
|
return this.http.post<{ ok: true; dialect: string }>('/imports/sources/sql/test-connection', connection);
|
|
}
|
|
|
|
schemas(connection: SqlConnectionRef): Observable<string[]> {
|
|
return this.http.post<{ schemas: string[] }>('/imports/sources/sql/schemas', connection).pipe(map((response) => response.schemas));
|
|
}
|
|
|
|
tables(connection: SqlConnectionRef, schema?: string): Observable<string[]> {
|
|
return this.http
|
|
.post<{ tables: string[] }>('/imports/sources/sql/tables', { ...connection, ...(schema ? { schema } : {}) })
|
|
.pipe(map((response) => response.tables));
|
|
}
|
|
|
|
columns(connection: SqlConnectionRef, table: string, schema?: string): Observable<SourceColumn[]> {
|
|
return this.http
|
|
.post<{ columns: SourceColumn[] }>('/imports/sources/sql/columns', { ...connection, table, ...(schema ? { schema } : {}) })
|
|
.pipe(map((response) => response.columns));
|
|
}
|
|
|
|
preview(
|
|
connection: SqlConnectionRef,
|
|
source: { table?: string; schema?: string; query?: string },
|
|
target: string | null,
|
|
): Observable<PreviewResponse> {
|
|
return this.http.post<PreviewResponse>('/imports/sources/sql/preview', {
|
|
...connection,
|
|
...(source.query ? { query: source.query } : { table: source.table, ...(source.schema ? { schema: source.schema } : {}) }),
|
|
...(target ? { target } : {}),
|
|
});
|
|
}
|
|
}
|