WhyDoThis
WhyDoThis
KKysely
Created by WhyDoThis on 9/10/2024 in #help
pg migration raw sql CREATE FUNCTION gets error: "TypeError: Cannot redefine property: then"
When I try:
await sql`
CREATE FUNCTION universal_history_trigger_function()
RETURNS trigger AS $$
BEGIN
IF TG_OP='INSERT' THEN
INSERT INTO universal_history (table_name, schema_name, operation, who_id, new_val)
VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, NEW.last_modified_by_id, row_to_json(NEW));
RETURN NEW;
ELSIF TG_OP='UPDATE' THEN
INSERT INTO universal_history (table_name, schema_name, operation, who_id, new_val, old_val)
VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, NEW.last_modified_by_id,
row_to_json(NEW), row_to_json(OLD));
RETURN NEW;
ELSIF TG_OP='DELETE' THEN
INSERT INTO universal_history (table_name, schema_name, operation, who_id, old_val)
VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, NEW.last_modified_by_id, row_to_json(OLD));
RETURN OLD;
END IF;
END;
$$ LANGUAGE plpgsql;
`.execute(db);
await sql`
CREATE FUNCTION universal_history_trigger_function()
RETURNS trigger AS $$
BEGIN
IF TG_OP='INSERT' THEN
INSERT INTO universal_history (table_name, schema_name, operation, who_id, new_val)
VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, NEW.last_modified_by_id, row_to_json(NEW));
RETURN NEW;
ELSIF TG_OP='UPDATE' THEN
INSERT INTO universal_history (table_name, schema_name, operation, who_id, new_val, old_val)
VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, NEW.last_modified_by_id,
row_to_json(NEW), row_to_json(OLD));
RETURN NEW;
ELSIF TG_OP='DELETE' THEN
INSERT INTO universal_history (table_name, schema_name, operation, who_id, old_val)
VALUES (TG_RELNAME, TG_TABLE_SCHEMA, TG_OP, NEW.last_modified_by_id, row_to_json(OLD));
RETURN OLD;
END IF;
END;
$$ LANGUAGE plpgsql;
`.execute(db);
in my migration "up" I get "✖ Migration failed with TypeError: Cannot redefine property: then" when migrating latest. When I try the same in pgadmin it works no issue. Any ideas/suggestions?
4 replies
KKysely
Created by WhyDoThis on 9/5/2024 in #help
Updateable primary key question & .set() type safety
Do you all wrap your Updateable versions of tables to omit id? Or am I doing something unconventional here. Also how do you get typesafety on set() calls to prevent it accepting an item with id (or whatever primary key) from being updated?
13 replies
KKysely
Created by WhyDoThis on 8/20/2024 in #help
What's the best way to use Pick with Selectable/Insertable/Updatable?
No description
7 replies
KKysely
Created by WhyDoThis on 6/28/2024 in #help
defineConfig using --environment type issue with seedFolder
No description
14 replies
KKysely
Created by WhyDoThis on 6/27/2024 in #help
Is there an easy tool to convert my existing knex migrations to kysely migrations?
I have a bunch of knex migrations in following format:
.createTable("state", (table: Knex.TableBuilder) => {
table.bigIncrements("id").unsigned().primary();
table.string("abbreviation", 2).notNullable();
table.string("name", 100).notNullable();
})
.createTable("state", (table: Knex.TableBuilder) => {
table.bigIncrements("id").unsigned().primary();
table.string("abbreviation", 2).notNullable();
table.string("name", 100).notNullable();
})
Copilot can't seem to do this for me so wondering if anyone knows of a tool? Maybe the only way is by hand.
25 replies
KKysely
Created by WhyDoThis on 6/25/2024 in #help
What am I not understanding about ColumnType?
(Apologize if this is very nooby I am new to kysely as of today) Used kysely-codegen and it spit out something like the following:
export type Int8 = ColumnType<
string,
bigint | number | string,
bigint | number | string
>;

export interface UserTable {
...
phoneOneCountryId: Generated<Int8>;
...
}
export type Int8 = ColumnType<
string,
bigint | number | string,
bigint | number | string
>;

export interface UserTable {
...
phoneOneCountryId: Generated<Int8>;
...
}
(Maybe it shouldn't be Generated? My current thought is code-gen marks that because the column has a default value? I'm struggling to find in kysely docs clear right/wrong here.) Which I then wrap:
export type NewUser = Insertable<UserTable>;
export type NewUser = Insertable<UserTable>;
However when I go to use like so:
const testNewUser: NewUser = {
...
phoneOneCountryId: 20,
...
};
const testNewUser: NewUser = {
...
phoneOneCountryId: 20,
...
};
I get compilation error:
Type 'number' is not assignable to type 'Int8'.ts(2322)
Type 'number' is not assignable to type 'Int8'.ts(2322)
---------------- If I understand how codegen set up Int8 correctly with ColumnType then it passed
bigint | number | string,
bigint | number | string,
as the InsertType (middle arg). So I wouldn't expect this to error - what am I missing here?
3 replies