Skip to content

Commit

Permalink
Interim
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Apr 26, 2024
1 parent b969d3e commit 86a698d
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/core/Emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export abstract class Emitter {
.join([
sql`create table`,
ifNotExists ? sql`if not exists` : undefined,
sql.identifier(tableApi.name),
tableApi.target(),
sql`(${tableApi.createColumns()})`
])
.emit(this)
Expand Down
2 changes: 1 addition & 1 deletion src/core/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface FieldData {
}

export class Field<Value, Table extends string> implements HasSql<Value> {
private declare keep?: [Table]
private declare brand?: [Table]
readonly [internalField]: FieldData
readonly [internalSql]: Sql<Value>
constructor(
Expand Down
6 changes: 3 additions & 3 deletions src/core/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {getSelection, hasSelection, type HasQuery} from './Internal.ts'
import type {QueryMeta} from './MetaData.ts'

export class Resolver<Meta extends QueryMeta = QueryMeta> {
private declare keep?: [Meta]
private declare brand?: [Meta]
#driver: Driver
#dialect: Dialect

Expand All @@ -18,15 +18,15 @@ export class Resolver<Meta extends QueryMeta = QueryMeta> {
const isSelection = hasSelection(query)
const mapRow = isSelection ? getSelection(query).mapRow : undefined
const emitter = this.#dialect(query)
const stmt = this.#driver.prepare(emitter.sql)
const stmt = this.#driver.prepare(emitter.sql, name)
return new PreparedStatement<Meta>(emitter, stmt, mapRow)
}
}

type RowMapper = ((values: Array<unknown>) => unknown) | undefined

export class PreparedStatement<Meta extends QueryMeta> {
private declare keep?: [Meta]
private declare brand?: [Meta]
#emitter: Emitter
#stmt: Statement
#mapRow: RowMapper
Expand Down
14 changes: 14 additions & 0 deletions src/core/Schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {table, type Table, type TableDefinition} from './Table.ts'

export function schema<SchemaName extends string>(schemaName: SchemaName) {
return {
table<Definition extends TableDefinition, TableName extends string>(
tableName: TableName,
columns: Definition
) {
return <Table<Definition, `${SchemaName}.${TableName}`>>(
table(tableName, columns, schemaName)
)
}
}
}
2 changes: 1 addition & 1 deletion src/core/Sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type Decoder<T> =
| {mapFromDriverValue?(value: unknown): T}

export class Sql<Value = unknown> implements HasSql<Value> {
private declare keep?: [Value]
private declare brand?: [Value]
alias?: string
mapFromDriverValue?: (input: unknown) => Value
readonly [internalSql] = this
Expand Down
27 changes: 18 additions & 9 deletions src/core/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type {Column, JsonColumn, RequiredColumn} from './Column.ts'
import {jsonExpr, type Input, type JsonExpr} from './Expr.ts'
import {Field} from './Field.ts'
import {
type HasTarget,
getColumn,
getTable,
internalTable,
internalTarget,
type HasSql,
type HasTable
type HasTable,
type HasTarget
} from './Internal.ts'
import {sql, type Sql} from './Sql.ts'

Expand All @@ -21,22 +21,30 @@ export type TableDefinition = {
class TableData {
name!: string
alias?: string
schemaName?: string
columns!: TableDefinition
}

export class TableApi<
Definition extends TableDefinition = TableDefinition,
Name extends string = string
> extends TableData {
private declare keep?: [Definition, Name]
private declare brand?: [Definition, Name]

get aliased() {
return this.alias ?? this.name
}

from(): Sql {
target(): Sql {
const name = sql.join(
[
this.schemaName ? sql.identifier(this.schemaName) : undefined,
sql.identifier(this.name)
],
sql`.`
)
return sql.join([
sql.identifier(this.name),
name,
this.alias ? sql`as ${sql.identifier(this.alias)}` : undefined
])
}
Expand Down Expand Up @@ -124,12 +132,13 @@ export type TableUpdate<Definition extends TableDefinition> = {

export function table<Definition extends TableDefinition, Name extends string>(
name: Name,
columns: Definition
columns: Definition,
schemaName?: string
) {
const api = assign(new TableApi(), {name, columns})
const api = assign(new TableApi(), {name, schemaName, columns})
return <Table<Definition, Name>>{
[internalTable]: api,
[internalTarget]: api.from(),
[internalTarget]: api.target(),
...api.fields()
}
}
Expand All @@ -141,7 +150,7 @@ export function alias<Definition extends TableDefinition, Alias extends string>(
const api = assign(new TableApi(), {...getTable(table), alias})
return <Table<Definition, Alias>>{
[internalTable]: api,
[internalTarget]: api.from(),
[internalTarget]: api.target(),
...api.fields()
}
}
2 changes: 1 addition & 1 deletion src/core/query/Select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class Select<Input, Meta extends QueryMeta = QueryMeta>
from: sql.join([
from,
sql.unsafe(`${operator} join`),
rightTable.from(),
rightTable.target(),
sql`on ${on}`
])
})
Expand Down
1 change: 0 additions & 1 deletion src/postgres/PostgresDialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class PostgresEmitter extends Emitter {
emitDefaultValue() {
this.sql += 'default'
}

emitIdColumn() {
this.sql += 'int generated always as identity'
}
Expand Down
1 change: 1 addition & 0 deletions src/postgres/PostgresSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {schema as pgSchema} from '../core/Schema.ts'
1 change: 1 addition & 0 deletions src/postgres/PostgresTable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {table as pgTable} from '../core/Table.ts'
14 changes: 14 additions & 0 deletions test/query/Create.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Assert, Test} from '@sinclair/carbon'
import {schema} from '../../src/core/Schema.ts'
import {table} from '../../src/core/Table.ts'
import {integer} from '../../src/sqlite/SqliteColumns.ts'
import {builder, emit} from '../TestUtils.ts'
Expand All @@ -23,4 +24,17 @@ Test.describe('Create', () => {
'create table if not exists "Node" ("id" integer primary key)'
)
})

const testSchema = schema('test')
const testNode = testSchema.table('Node', {
id: integer().primaryKey()
})

Test.it('create table with schema', () => {
const query = builder.create(testNode)
Assert.isEqual(
emit(query),
'create table "test"."Node" ("id" integer primary key)'
)
})
})

0 comments on commit 86a698d

Please sign in to comment.