tzezar
tzezar
Explore posts from servers
DTDrizzle Team
Created by Suryansh on 8/7/2024 in #help
Extending Typescript Client Model(Table)
I will give you example, sorry for formatting i am writing from phone oop way:
class User {
id!: number
firstName!: string
lastName!: string

getFullName () {
return this.firstName + " " + this.lastName
}
}

const result = userRepo.findOne(id)
const user = new User(result)
console.log(user.getFullName())
class User {
id!: number
firstName!: string
lastName!: string

getFullName () {
return this.firstName + " " + this.lastName
}
}

const result = userRepo.findOne(id)
const user = new User(result)
console.log(user.getFullName())
fp way:
const getFullName(user) {
return user.firstName + " " + user.lastName
}

const result = userRepo.findOne(id)
console.log(getFullName(result))
const getFullName(user) {
return user.firstName + " " + user.lastName
}

const result = userRepo.findOne(id)
console.log(getFullName(result))
8 replies
DTDrizzle Team
Created by Suryansh on 8/7/2024 in #help
Extending Typescript Client Model(Table)
Second one: use concat and select builder or query builder with https://orm.drizzle.team/docs/rqb#include-custom-fields
8 replies
DTDrizzle Team
Created by Suryansh on 8/7/2024 in #help
Extending Typescript Client Model(Table)
First scenario: you either create class instance with described method and map results to it - each entry will have this method, I dont recommend creating classes in TS tho, but if this is helpfull for you then why not. Or: create function that takes results array / entry object and pass results to it constructing desired shape of data.
8 replies
DTDrizzle Team
Created by Suryansh on 8/7/2024 in #help
Extending Typescript Client Model(Table)
Do you want to have function attached to result that will return fullName or you want to return fullName constructed in db?
8 replies
DTDrizzle Team
Created by jrydberg on 8/3/2024 in #help
How to get the type of transaction from a database?
I did it like this:
export const dbDrizzle = drizzle(pool);

export type DbConn = typeof dbDrizzle;


const getUser = async (db: DbConn, id: number) => {
const [supplier] = await db.select().from(dostawca).where(eq(dostawca.id, id)).execute()
return supplier;
}


await dbDrizzle.transaction(async (db) => {
const user = await getUser(db, 1);
})

const user = await getUser(dbDrizzle, 1);
export const dbDrizzle = drizzle(pool);

export type DbConn = typeof dbDrizzle;


const getUser = async (db: DbConn, id: number) => {
const [supplier] = await db.select().from(dostawca).where(eq(dostawca.id, id)).execute()
return supplier;
}


await dbDrizzle.transaction(async (db) => {
const user = await getUser(db, 1);
})

const user = await getUser(dbDrizzle, 1);
it allows to pass regular db conn or transaction
6 replies
DTDrizzle Team
Created by jrydberg on 8/3/2024 in #help
How to get the type of transaction from a database?
import { type PostgresJsDatabase } from "drizzle-orm/postgres-js";
6 replies
DTDrizzle Team
Created by Liam on 7/31/2024 in #help
There is not enough information to infer relation
I am not sure since i have not used drizzle for so long. Is your relations syntax correct? As i remember i had to set references field
5 replies
DTDrizzle Team
Created by Timo on 7/14/2024 in #help
Overwriting Schema Defaults
are you sure error comes from setting values that have defauls? can you show ts error?
3 replies
DTDrizzle Team
Created by xxxxx on 7/14/2024 in #help
How to get good at Drizzle?
It will come with experience, at first many people get overwhelmed, usually when they learn too much stuff at once. Start small and improve your skills and it will be your second nature. Alternative is to use some 1st class ORM like mikro-orm, it has Unit Of Work pattern built in that simplifies business logic a loooot (simillar to ef core from c#). If your app is mainly CRUD I highly suggest give it a try or simmilar orm like TypeOrm etc. Drizzle is good, but it is not my go-to technology for all projects (specially when I know I will be making a lot of business logic; mapping is so time consuming). My tip is to make pseudo code with logic you would need to implement some feature without actually coding, I mean some transactional script with commands like: do this, then do this, take that and if this is something do this, then make something etc.
7 replies
DTDrizzle Team
Created by tzezar on 7/9/2024 in #help
Does 'query' support subqueries with filters?
Oh wow its so easy. Amazing
await db.query.posts.findMany({
where: (posts, { eq }) => (eq(posts.id, 1)),
with: {
comments: {
where: (comments, { lt }) => lt(comments.createdAt, new Date()),
},
},
});
await db.query.posts.findMany({
where: (posts, { eq }) => (eq(posts.id, 1)),
with: {
comments: {
where: (comments, { lt }) => lt(comments.createdAt, new Date()),
},
},
});
2 replies
DTDrizzle Team
Created by tzezar on 7/8/2024 in #help
applying migrations...error: unsafe use of new value "zewnetrzny"
We deleted migrations folder and with fresh db error does not raise. Weird. Btw nice playground, have not know it exists
3 replies
DTDrizzle Team
Created by tzezar on 7/7/2024 in #help
How to narrow type in select from enum?
My attempt was to use sql operator like this:
typ: sql<'produkt'>`${produkt.typ}`,
typ: sql<'produkt'>`${produkt.typ}`,
but I believe there might be better solution
2 replies
DTDrizzle Team
Created by textYash on 3/24/2024 in #help
How to check if an entry already exist?
Probably not the best solution, but I do
let [s] = await tx
.select()
.from(settlement)
.where(eq(settlement.id, settlementId))
if (!s) {
throw new Error("settlement not found")
let [s] = await tx
.select()
.from(settlement)
.where(eq(settlement.id, settlementId))
if (!s) {
throw new Error("settlement not found")
10 replies
DTDrizzle Team
Created by 34383432 on 3/3/2024 in #help
I need help with simple Table design 🥺
and about error: please provide error message
3 replies
DTDrizzle Team
Created by 34383432 on 3/3/2024 in #help
I need help with simple Table design 🥺
it is better practice to store foreign keys as integer not text
3 replies
DTDrizzle Team
Created by tzezar on 2/1/2024 in #help
How should locks be done with drizzle?
thanks
5 replies
DTDrizzle Team
Created by tzezar on 1/26/2024 in #help
Inserting into columns of type Enum
I changed column definition from pgEnum to text type (as below) and it worked just fine
type: text("type", { enum: ['IN', 'OUT'] }).notNull()
type: text("type", { enum: ['IN', 'OUT'] }).notNull()
7 replies
DTDrizzle Team
Created by tzezar on 1/26/2024 in #help
Inserting into columns of type Enum
Exactly; but I have no idea why this is a case. Migrations were done, table definition seems to be fine, insert statement also.
7 replies