Skip to content

Commit

Permalink
feat: add Model::params::<field>::as(<alias>) support
Browse files Browse the repository at this point in the history
  • Loading branch information
andykais committed Jul 7, 2024
1 parent 23161ae commit 632a1b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ abstract class ModelBase implements ModelInstance {
}
}

protected build_param_sql(schema_field: BuiltSchemaField<string, any>): string {
return `:${schema_field.field_name}`
protected build_param_sql(schema_field: ParamsField<any>): string {
if (schema_field.alias_of) {
console.debug(schema_field.alias_of)
return `:${schema_field.field_name}`
} else {
return `:${schema_field.field_name}`
}
}

protected build_result_sql(schema_field: ResultField<any>): string {
const table_name = schema_field.table_name ? `${schema_field.table_name}.` : ''
if (schema_field.alias_of) {
Expand Down
17 changes: 10 additions & 7 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Field<T extends SchemaField> implements SchemaField {
field_name: T['field_name']
data_transformers: T['data_transformers']

public constructor(public schema_field: T) {
public constructor(public schema_field: T, public alias_of?: string) {
this.table_name = schema_field.table_name
this.field_name = schema_field.field_name
this.data_transformers = schema_field.data_transformers
Expand All @@ -44,17 +44,20 @@ export class Field<T extends SchemaField> implements SchemaField {
// so it cant tell the difference between ParamsField and ResultField if there are no different properties
export class ParamsField<T extends SchemaField> extends Field<T> {
type = 'params' as const


public as<A extends string>(alias: A): AliasParamsField<T, A> {
return new ParamsField({
table_name: this.table_name,
field_name: alias,
data_transformers: this.data_transformers,
}, this.field_name)
}
}

export class ResultField<T extends SchemaField> extends Field<T> {
type = 'result' as const

alias_of: string | undefined

public constructor(schema_field: T, alias_of?: string) {
super(schema_field)
this.alias_of = alias_of
}

public as<A extends string>(alias: A): AliasResultField<T, A> {
return new ResultField({
Expand Down
11 changes: 11 additions & 0 deletions test/alias.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class Tag extends Model('tag', {
INNER JOIN tag_group ON tag_group_id = tag_group.id
WHERE tag.name LIKE ${Tag.params.name} || '%'`.all

select_by__name__group = this.query`SELECT ${Tag.result['*']} FROM tag
INNER JOIN tag_group ON tag_group_id = tag_group.id
WHERE tag.name = ${Tag.params.name} AND tag_group.name = ${TagGroup.params.name.as('group')}`

expose_prepare = this.prepare
}

Expand Down Expand Up @@ -71,6 +75,13 @@ test('field alias names', async (ctx) => {
expect_type<{ id: number; name: string; tag_group_id: number } | undefined>(hardcoded_query)
assert_equals(hardcoded_query, { id: picasso_tag_id, name: 'picasso', tag_group_id: artist_tag_group_id })

// test params aliases
assert_equals({
id: 1,
tag_group_id: 1,
name: 'picasso',
}, db.tag.select_by__name__group.one({group: 'artist', name: 'picasso'}))

db.close()
})

0 comments on commit 632a1b8

Please sign in to comment.