Extending Typescript Client Model(Table)

I have a simple question that would really help me. Let's say I have table Users with columns, firstName, LastName. I define my table using drizzle. When I get the resultant from Users query. I want it to have a function that returns fullName. Is it possible to extend Model(Tables) with custom functions ?
5 Replies
tzezar
tzezar4mo ago
Do you want to have function attached to result that will return fullName or you want to return fullName constructed in db? 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.
tzezar
tzezar4mo ago
Second one: use concat and select builder or query builder with https://orm.drizzle.team/docs/rqb#include-custom-fields
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Suryansh
SuryanshOP4mo ago
I understood the second but first one I didn't
tzezar
tzezar4mo ago
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))
Suryansh
SuryanshOP4mo ago
oh Thnx
Want results from more Discord servers?
Add your server