pato
pato
Explore posts from servers
DTDrizzle Team
Created by pato on 10/14/2024 in #help
Natively write SQL functions/triggers with Drizzle?
Is it possible to, for example, write this kind of logic directly in Drizzle's schema?
-- Checks if a user already has an active ban before inserting a new one.
CREATE OR REPLACE FUNCTION check_active_ban()
RETURNS TRIGGER AS $$
BEGIN
-- Check if there is an existing ban for the same user_id with expire NULL or in the future
IF EXISTS (
SELECT 1 FROM bans
WHERE "userId" = NEW."userId"
AND (expires IS NULL OR expires > CURRENT_TIMESTAMP)
) THEN
-- Raise an exception to prevent the insert
RAISE EXCEPTION 'A ban for this user already exists and is not expired';
END IF;
-- Allow the insert
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-------------------------------- Triggers --------------------------------

CREATE OR REPLACE TRIGGER trigger_before_bans_insert
BEFORE INSERT ON bans
FOR EACH ROW
EXECUTE FUNCTION check_active_ban();
-- Checks if a user already has an active ban before inserting a new one.
CREATE OR REPLACE FUNCTION check_active_ban()
RETURNS TRIGGER AS $$
BEGIN
-- Check if there is an existing ban for the same user_id with expire NULL or in the future
IF EXISTS (
SELECT 1 FROM bans
WHERE "userId" = NEW."userId"
AND (expires IS NULL OR expires > CURRENT_TIMESTAMP)
) THEN
-- Raise an exception to prevent the insert
RAISE EXCEPTION 'A ban for this user already exists and is not expired';
END IF;
-- Allow the insert
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-------------------------------- Triggers --------------------------------

CREATE OR REPLACE TRIGGER trigger_before_bans_insert
BEFORE INSERT ON bans
FOR EACH ROW
EXECUTE FUNCTION check_active_ban();
9 replies
DTDrizzle Team
Created by pato on 9/6/2024 in #help
Accumulating connections
No description
24 replies
DTDrizzle Team
Created by pato on 6/21/2024 in #help
When and how to perform transactions
I’m new to the concept of transactions on SQL, and was interested in understanding if there is a way to apply every change I make to my schema as a transaction in case anything goes wrong, so it can be rolled back. Or am I thinking this wrong? When should transactions be used?
4 replies
DTDrizzle Team
Created by pato on 6/4/2024 in #help
Queries require "await"?
I just tried to do a simple
db.update(usersTable)
.set({ [slot]: itemId })
.where(eq(usersTable.id, userId))
db.update(usersTable)
.set({ [slot]: itemId })
.where(eq(usersTable.id, userId))
without an await and the query wasn't sent. When I added the await, it updated the database. Is this normal behavior? If so, why?
3 replies
DTDrizzle Team
Created by pato on 5/17/2024 in #help
How to register triggers, events and functions
Since Drizzle doesn't offer support for defining triggers, events or functions, what is the best way to register these? For example, a developer is setting up locally the project's database. Drizzle can handle tables and other things thanks to the schema, but if the database needs to have certain triggers and events registered beforehand, what would be the best way to set it up?
2 replies
DTDrizzle Team
Created by pato on 5/17/2024 in #help
findMany
If no rows are found, does it return undefined or an empty array?
3 replies
DTDrizzle Team
Created by pato on 5/12/2024 in #help
help with count()
How can I count the amount of rows where userId column has a given value?
9 replies
DTDrizzle Team
Created by pato on 5/10/2024 in #help
pushing not allowed
I tried pushing my schema, but because there are tables in the database which I don’t own, since it’s a dev database hosted on a provider, drizzle doesn’t allow me to complete the operation, since it says it would affect them. There should be some way to select only certain options to push.
1 replies
DTDrizzle Team
Created by pato on 5/10/2024 in #help
drizzle-kit unknown command migrate
When I try running npx drizzle-kit migrate, it says error: unknown command 'migrate'
3 replies