Skip to content

Commit dc239fe

Browse files
committed
Refactor MaterializedView and View to use TableReference and unknown
Extract a non-generic TableReference interface from OlapTable to avoid invariance issues with OlapTable<T> (T appears in both input and output positions). Deduplicate formatTableReference into a shared export. Replace { [key: string]: any } with { [key: string]: unknown } in MaterializedView and View metadata fields for stricter type safety.
1 parent d8a0c51 commit dc239fe

4 files changed

Lines changed: 41 additions & 31 deletions

File tree

packages/ts-moose-lib/src/dmv2/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ export type SimpleAggregated<
4646
_argType?: ArgType;
4747
};
4848

49-
export { OlapTable, OlapConfig, S3QueueTableSettings } from "./sdk/olapTable";
49+
export {
50+
OlapTable,
51+
OlapConfig,
52+
S3QueueTableSettings,
53+
TableReference,
54+
formatTableReference,
55+
} from "./sdk/olapTable";
5056
export { ClickHouseEngines } from "../dataModels/types";
5157
export {
5258
Stream,

packages/ts-moose-lib/src/dmv2/sdk/materializedView.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
import { ClickHouseEngines } from "../../dataModels/types";
22
import { Sql, toStaticQuery } from "../../sqlHelpers";
3-
import { OlapConfig, OlapTable } from "./olapTable";
3+
import {
4+
OlapConfig,
5+
OlapTable,
6+
TableReference,
7+
formatTableReference,
8+
} from "./olapTable";
49
import { View } from "./view";
510
import { IJsonSchemaCollection } from "typia";
611
import { Column } from "../../dataModels/dataModelTypes";
712
import { getMooseInternal, isClientOnlyMode } from "../internal";
813
import { getSourceFileFromStack } from "../utils/stackTrace";
914

10-
/**
11-
* Helper function to format a table reference as `database`.`table` or just `table`
12-
*/
13-
function formatTableReference(table: OlapTable<any> | View): string {
14-
const database =
15-
table instanceof OlapTable ? table.config.database : undefined;
16-
if (database) {
17-
return `\`${database}\`.\`${table.name}\``;
18-
}
19-
return `\`${table.name}\``;
20-
}
21-
2215
/**
2316
* Configuration options for creating a Materialized View.
2417
* @template T The data type of the records stored in the target table of the materialized view.
@@ -27,7 +20,7 @@ export interface MaterializedViewConfig<T> {
2720
/** The SQL SELECT statement or `Sql` object defining the data to be materialized. Dynamic SQL (with parameters) is not allowed here. */
2821
selectStatement: string | Sql;
2922
/** An array of OlapTable or View objects that the `selectStatement` reads from. */
30-
selectTables: (OlapTable<any> | View)[];
23+
selectTables: (TableReference | View)[];
3124

3225
/** @deprecated See {@link targetTable}
3326
* The name for the underlying target OlapTable that stores the materialized data. */

packages/ts-moose-lib/src/dmv2/sdk/olapTable.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,29 @@ type EngineConfig<T> =
625625
*/
626626
export type OlapConfig<T> = EngineConfig<T> | LegacyOlapConfig<T>;
627627

628+
/**
629+
* Non-generic interface for referencing an OlapTable or View by name,
630+
* without depending on the table's row type parameter.
631+
*
632+
* Use this in signatures that only need the table identity (name and
633+
* optional database), avoiding the invariance issues of OlapTable<T>.
634+
*/
635+
export interface TableReference {
636+
name: string;
637+
config?: { database?: string };
638+
}
639+
640+
/**
641+
* Format a table reference as `database`.`table` or just `table`.
642+
*/
643+
export function formatTableReference(table: TableReference): string {
644+
const database = table.config?.database;
645+
if (database) {
646+
return `\`${database}\`.\`${table.name}\``;
647+
}
648+
return `\`${table.name}\``;
649+
}
650+
628651
/**
629652
* Represents an OLAP (Online Analytical Processing) table, typically corresponding to a ClickHouse table.
630653
* Provides a typed interface for interacting with the table.

packages/ts-moose-lib/src/dmv2/sdk/view.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import { Sql, toStaticQuery } from "../../sqlHelpers";
2-
import { OlapTable } from "./olapTable";
2+
import { TableReference, formatTableReference } from "./olapTable";
33
import { getMooseInternal, isClientOnlyMode } from "../internal";
44
import { getSourceFileFromStack } from "../utils/stackTrace";
55

6-
/**
7-
* Helper function to format a table reference as `database`.`table` or just `table`
8-
*/
9-
function formatTableReference(table: OlapTable<any> | View): string {
10-
const database =
11-
table instanceof OlapTable ? table.config.database : undefined;
12-
if (database) {
13-
return `\`${database}\`.\`${table.name}\``;
14-
}
15-
return `\`${table.name}\``;
16-
}
17-
186
/**
197
* Represents a database View, defined by a SQL SELECT statement based on one or more base tables or other views.
208
* Emits structured data for the Moose infrastructure system.
@@ -33,7 +21,7 @@ export class View {
3321
sourceTables: string[];
3422

3523
/** Optional metadata for the view */
36-
metadata: { [key: string]: any };
24+
metadata: { [key: string]: unknown };
3725

3826
/**
3927
* Creates a new View instance.
@@ -45,8 +33,8 @@ export class View {
4533
constructor(
4634
name: string,
4735
selectStatement: string | Sql,
48-
baseTables: (OlapTable<any> | View)[],
49-
metadata?: { [key: string]: any },
36+
baseTables: (TableReference | View)[],
37+
metadata?: { [key: string]: unknown },
5038
) {
5139
if (typeof selectStatement !== "string") {
5240
selectStatement = toStaticQuery(selectStatement);

0 commit comments

Comments
 (0)