Jam
Jam
KKysely
Created by Jam on 9/18/2023 in #help
executeTakeFirst does not add limit(1). Why?
If my understanding is correct, there is never any reason not to limit(1) when you do an executeTakeFirst.
4 replies
KKysely
Created by Jam on 4/20/2023 in #help
Why does kysely-codegen generate the schema in node_modules?
Like probably many people here, I use https://github.com/RobinBlomberg/kysely-codegen . By default, the DB schema file is generated inside the node_modules. It can be changed of course, but I wonder why that is the default? I was expecting that file to be something you keep in versioning.
6 replies
KKysely
Created by Jam on 4/20/2023 in #help
Any plan to complete the Postgres dialect?
Hey there, Is there any plan (or a tracking issue) to complete the Postgres dialect with its specific words like CREATE/ENABLE POLICY, SET, SET LOCAL..?
5 replies
KKysely
Created by Jam on 4/20/2023 in #help
Convert date to timestamptz
Hello. I'm trying to convert the following query from TypeORM querybuilder to Kysely: itemRepository .createQueryBuilder() .select() .where({ id }) .andWhere('sys_period @> :timestamp::timestamptz', { timestamp }) .getOne(); My attempt: db.selectFrom('item') .selectAll() .where('id', '=', id) .where('sys_period', '@>', [timestamp]); However type is mismatched, I need to somehow convert the Date to a timestamptz. Any idea where I could define such mapping?
7 replies
KKysely
Created by Jam on 4/18/2023 in #help
Raw query for RLS not working
I am trying to migrate a raw query from TypeORM's builtin query builder to Kysely. It's required to be raw as Kysely unfortunately does not fully implement Postgres's SQL language. Original query:
const query = `
DROP POLICY IF EXISTS tenant_isolation_policy ON ${tableName};
CREATE POLICY tenant_isolation_policy ON ${tableName} USING ("tenantId"::uuid = current_setting('project.current_tenant')::uuid);
ALTER TABLE ${tableName} ENABLE ROW LEVEL SECURITY;
ALTER TABLE ${tableName} FORCE ROW LEVEL SECURITY;';
return await repository.query(query);
const query = `
DROP POLICY IF EXISTS tenant_isolation_policy ON ${tableName};
CREATE POLICY tenant_isolation_policy ON ${tableName} USING ("tenantId"::uuid = current_setting('project.current_tenant')::uuid);
ALTER TABLE ${tableName} ENABLE ROW LEVEL SECURITY;
ALTER TABLE ${tableName} FORCE ROW LEVEL SECURITY;';
return await repository.query(query);
What I wrote:
const query = sql`
DROP POLICY IF EXISTS tenant_isolation_policy ON ${tableName};
CREATE POLICY tenant_isolation_policy ON ${tableName} USING ("tenantId"::uuid = current_setting('project.current_tenant')::uuid);
ALTER TABLE ${tableName} ENABLE ROW LEVEL SECURITY;
ALTER TABLE ${tableName} FORCE ROW LEVEL SECURITY;
`.compile(this.db);
return await this.db.executeQuery(query);
const query = sql`
DROP POLICY IF EXISTS tenant_isolation_policy ON ${tableName};
CREATE POLICY tenant_isolation_policy ON ${tableName} USING ("tenantId"::uuid = current_setting('project.current_tenant')::uuid);
ALTER TABLE ${tableName} ENABLE ROW LEVEL SECURITY;
ALTER TABLE ${tableName} FORCE ROW LEVEL SECURITY;
`.compile(this.db);
return await this.db.executeQuery(query);
Error:
backend:dev: /Users/jam/tmp_proj/node_modules/pg-protocol/src/parser.ts:369
backend:dev: name === 'notice' ? new NoticeMessage(length, messageValue) : new DatabaseError(messageValue, length, name)
backend:dev: ^
backend:dev: error: syntax error at or near "$1"
backend:dev: at Parser.parseErrorMessage (/Users/jam/tmp_proj/node_modules/pg-protocol/src/parser.ts:369:69)
backend:dev: /Users/jam/tmp_proj/node_modules/pg-protocol/src/parser.ts:369
backend:dev: name === 'notice' ? new NoticeMessage(length, messageValue) : new DatabaseError(messageValue, length, name)
backend:dev: ^
backend:dev: error: syntax error at or near "$1"
backend:dev: at Parser.parseErrorMessage (/Users/jam/tmp_proj/node_modules/pg-protocol/src/parser.ts:369:69)
Any idea?
7 replies