You can create a more TypeScript-friendly schema system using a builder pattern or a type-safe DSL. Here are a few approaches:
Approach 1: Builder Pattern with Fluent API
type GroupConfig<T> = {
type: 'group';
groupBy: keyof T;
alias: string;
fields: SchemaConfig<T>;
};
type FieldConfig<T> = {
type: 'field';
source: keyof T;
};
type SchemaConfig<T> = {
[K: string]: FieldConfig<T> | GroupConfig<T>;
};
class SchemaBuilder<T> {
private schema: SchemaConfig<T> = {};
field(name: string, source: keyof T): this {
this.schema[name] = { type: 'field', source };
return this;
}
group(alias: string, groupBy: keyof T, builder: (sub: SchemaBuilder<T>) => void): this {
const subBuilder = new SchemaBuilder<T>();
builder(subBuilder);
this.schema[alias] = {
type: 'group',
groupBy,
alias,
fields: subBuilder.build()
};
return this;
}
build(): SchemaConfig<T> {
return this.schema;
}
}
// Usage
interface SqlRow {
pid: number;
contributor: string;
projectID: number;
projectName: string;
}
const schema = new SchemaBuilder<SqlRow>()
.field('id', 'pid')
.field('name', 'contributor')
.group('contributors', 'pid', sub => sub
.field('id', 'projectID')
.field('name', 'projectName')
.group('projects', 'projectID', subsub => subsub
.field('id', 'projectID')
.field('name', 'projectName')
)
)
.build();
Approach 2: Function-Based DSL
type Schema<T> = {
[key: string]: FieldDef<T> | GroupDef<T>;
};
type FieldDef<T> = {
_type: 'field';
source: keyof T;
};
type GroupDef<T> = {
_type: 'group';
groupBy: keyof T;
schema: Schema<T>;
};
// Helper functions
function field<T>(source: keyof T): FieldDef<T> {
return { _type: 'field', source };
}
function group<T>(groupBy: keyof T, schema: Schema<T>): GroupDef<T> {
return { _type: 'group', groupBy, schema };
}
// Usage
const schema: Schema<SqlRow> = {
id: field('pid'),
name: field('contributor'),
contributors: group('pid', {
id: field('projectID'),
name: field('projectName'),
projects: group('projectID', {
id: field('projectID'),
name: field('projectName')
})
})
};
Approach 3: Proxy-Based Dynamic Schema
type SchemaDefinition<T> = {
[key: string]: {
source?: keyof T;
groupBy?: keyof T;
children?: SchemaDefinition<T>;
};
};
class DynamicSchema<T> {
private definition: SchemaDefinition<T> = {};
get proxy(): any {
return new Proxy(this, {
get(target, prop: string) {
if (prop === 'build') return () => target.definition;
if (prop === 'toJSON') return () => target.definition;
return {
field(source: keyof T) {
target.definition[prop] = { source };
return target.proxy;
},
group(groupBy: keyof T) {
const childSchema = new DynamicSchema<T>();
target.definition[prop] = { groupBy, children: childSchema.definition };
return {
define: (fn: (schema: any) => void) => {
fn(childSchema.proxy);
return target.proxy;
}
};
}
};
}
});
}
}
// Usage
const schema = new DynamicSchema<SqlRow>().proxy
.id.field('pid')
.name.field('contributor')
.contributors.group('pid').define(sub => sub
.id.field('projectID')
.name.field('projectName')
.projects.group('projectID').define(subsub => subsub
.id.field('projectID')
.name.field('projectName')
)
)
.build();
Converting to Your Format
To convert these type-safe schemas back to your string-based format for the JSON builder:
function convertToStringSchema<T>(schema: SchemaConfig<T>): any {
const result: any = {};
for (const [key, value] of Object.entries(schema)) {
if (value.type === 'field') {
result[key] = value.source;
} else if (value.type === 'group') {
const groupKey = `$group[${key}](${String(value.groupBy)})`;
result[groupKey] = convertToStringSchema(value.fields);
}
}
return result;
}
The builder pattern (Approach 1) is probably the most balanced solution - it provides good type safety, is easy to understand, and doesn't require complex TypeScript features. The function-based DSL (Approach 2) is more concise but slightly less discoverable. The proxy-based approach (Approach 3) gives you the most natural syntax but might be harder to debug.