import { Pool } from 'pg'
import { AnyColumn, ColumnType, Generated, Kysely, PostgresDialect } from 'kysely'
/**
* Interfaces for the table and the database
*/
interface UserTable {
id: Generated<number>
email: string
username: string
password: string
created_at: ColumnType<Date, string | undefined, never>
}
interface Database {
users: UserTable
}
/**
* PostgreSQL dialect and kysely instance
*/
const dialect = new PostgresDialect({ pool: new Pool(poolConfig) })
const db = new Kysely<Database>({ dialect })
/**
* Authenticator function that need kysely instance
* and an unknown table + columns
*/
function createAuthenticator<
DB,
TB extends keyof DB & string,
Columns extends AnyColumn<DB, TB>
>(config: {
db: Kysely<DB>
table: TB,
uids: Columns[]
}) {
config
.db
.selectFrom(config.table)
// The where clause gives an error
.where(config.uids[0], '=', 'foobar')
}
/**
* Using function to create authenticator
*/
createAuthenticator({
db: db,
table: 'users',
uids: ['email'],
})
import { Pool } from 'pg'
import { AnyColumn, ColumnType, Generated, Kysely, PostgresDialect } from 'kysely'
/**
* Interfaces for the table and the database
*/
interface UserTable {
id: Generated<number>
email: string
username: string
password: string
created_at: ColumnType<Date, string | undefined, never>
}
interface Database {
users: UserTable
}
/**
* PostgreSQL dialect and kysely instance
*/
const dialect = new PostgresDialect({ pool: new Pool(poolConfig) })
const db = new Kysely<Database>({ dialect })
/**
* Authenticator function that need kysely instance
* and an unknown table + columns
*/
function createAuthenticator<
DB,
TB extends keyof DB & string,
Columns extends AnyColumn<DB, TB>
>(config: {
db: Kysely<DB>
table: TB,
uids: Columns[]
}) {
config
.db
.selectFrom(config.table)
// The where clause gives an error
.where(config.uids[0], '=', 'foobar')
}
/**
* Using function to create authenticator
*/
createAuthenticator({
db: db,
table: 'users',
uids: ['email'],
})