K
Kysely•16mo ago
NazCodeland

Unable to connect to DB

Thank God there is a Discord server xD I been trying to make this work for like 2 hours and I can't get it to work
import { Pool } from 'pg';
import {
Kysely,
PostgresDialect,
Generated,
ColumnType,
Selectable,
Insertable,
Updateable
} from 'kysely';

interface UserTable {
id: Generated<number>;
email: string;
firstName: string | null;
lastName: string | null;
}

interface Database {
User: UserTable;
}

const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({
user: 'postgres',
password: 'secretPassword',
host: 'localhost',
port: 5432,
database: 'Barbercate'
})
})
});

console.log(db);

export async function getUser() {
console.log('inside kysely.ts');
const person = await db.selectFrom('User').select('firstName').executeTakeFirst();
console.log('person:', person);
return person;
}

export async function getAllTables(): Promise<string[]> {
const tables = await db.select('*').from('pg_catalog.pg_tables').execute();
return tables;
}
import { Pool } from 'pg';
import {
Kysely,
PostgresDialect,
Generated,
ColumnType,
Selectable,
Insertable,
Updateable
} from 'kysely';

interface UserTable {
id: Generated<number>;
email: string;
firstName: string | null;
lastName: string | null;
}

interface Database {
User: UserTable;
}

const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({
user: 'postgres',
password: 'secretPassword',
host: 'localhost',
port: 5432,
database: 'Barbercate'
})
})
});

console.log(db);

export async function getUser() {
console.log('inside kysely.ts');
const person = await db.selectFrom('User').select('firstName').executeTakeFirst();
console.log('person:', person);
return person;
}

export async function getAllTables(): Promise<string[]> {
const tables = await db.select('*').from('pg_catalog.pg_tables').execute();
return tables;
}
Solution:
There are a few approaches: https://kysely.dev/docs/recipes/schemas 1. WithSchemaPlugin to apply schema globally to all queries. 2. withSchema method to apply to specific queries on the fly....
Working with schemas | Kysely
First of all, when we talk about schemas in this document, we mean custom
Jump to solution
22 Replies
Igal
Igal•16mo ago
Hey 👋 What errors are you getting?
NazCodeland
NazCodeland•16mo ago
my db variable returns an empty object so all the functions I run on it don't work cuz it's an empty object that doesn't contain those functions for example, selectFrom
Igal
Igal•16mo ago
its a class instance, without toString, toJSON, etc. implementation and no regard for enumarable keys. It doesn't mean the functions don't exist on it when you print it 🙂
NazCodeland
NazCodeland•16mo ago
ok my knowledge isn't too vast so excuse me if its an easy fix
Igal
Igal•16mo ago
All good, we all learn each day
NazCodeland
NazCodeland•16mo ago
xD
Igal
Igal•16mo ago
export async function getAllTables(): Promise<string[]> {
const tables = await db.select('*').from('pg_catalog.pg_tables').execute();
return tables;
}
export async function getAllTables(): Promise<string[]> {
const tables = await db.select('*').from('pg_catalog.pg_tables').execute();
return tables;
}
this shouldn't work. it's not kysely code. Also Kysely has an introspection module that returns all tables.
NazCodeland
NazCodeland•16mo ago
the reason I had this code was because I keep getting the error that
Igal
Igal•16mo ago
https://kysely-org.github.io/kysely/classes/Kysely.html#introspection this exists on db and has functionality to get all tables metadata
Kysely | kysely
Documentation for kysely
NazCodeland
NazCodeland•16mo ago
I keep getting this error
Uncaught (in promise) TRPCClientError: getIser is not defined
at TRPCClientError.from (transformResult-9a244fe7.mjs:4:20)
at httpBatchLink-e66c0674.mjs:187:60
from @ transformResult-9a244fe7.mjs:4
(anonymous) @ httpBatchLink-e66c0674.mjs:187
await in (anonymous) (async)
(anonymous) @ index.mjs:470
Show 3 more frames
Uncaught (in promise) TRPCClientError: getIser is not defined
at TRPCClientError.from (transformResult-9a244fe7.mjs:4:20)
at httpBatchLink-e66c0674.mjs:187:60
from @ transformResult-9a244fe7.mjs:4
(anonymous) @ httpBatchLink-e66c0674.mjs:187
await in (anonymous) (async)
(anonymous) @ index.mjs:470
Show 3 more frames
Igal
Igal•16mo ago
this is a TRPC error
NazCodeland
NazCodeland•16mo ago
oops one second I noticed a small mistake this is my TRPC endpoint code
relation "User" does not exist
relation "User" does not exist
I am making the api call using TRPC but it's this function that isn't running
Igal
Igal•16mo ago
relation User does not exist means that table is not in your database
NazCodeland
NazCodeland•16mo ago
right, but I do have it in my db
NazCodeland
NazCodeland•16mo ago
Want results from more Discord servers?
Add your server