inferring the column information from the imported schema

is there anyway to get the column information from the imported schema information. When I dump the schema I can see that there is [Symbol(drizzle:Columns] property but not sure how I'd access it. Does drizzle provide some sort of API for extracting this data?
5 Replies
Angelelz
Angelelz14mo ago
What is the use case? What do you want that for? is it for the types or the values?
CharlesCharlieCharles
basically i am using it to build the query from the query string and would like try and automate setting the allowed conditionals and includes, so I don't have to type it out for every query. This would also have the benefit of knowing which type to coerce it too without manually checking. dialogRoutes.ts
const allowedConditionals = [
{ id: 'verbId', schema: dialog.verbId },
{ id: 'tenseId', schema: dialog.tenseId },
{ id: 'levelId', schema: dialog.levelId },
{ id: 'typeId', schema: dialog.typeId }
]

const allowedIncludes = ['verb', 'tense']

router.get('/', async (req: Request, res: Response) => {

const conditionalClause = parseConditionals(req, allowedConditionals);
const withs = parseIncludes(req, allowedIncludes);

const dialogs = await db.query.dialog.findMany({
where: and(...conditionalClause),
with: withs
})

return res.json({
success: true,
data: dialogs
})
})
const allowedConditionals = [
{ id: 'verbId', schema: dialog.verbId },
{ id: 'tenseId', schema: dialog.tenseId },
{ id: 'levelId', schema: dialog.levelId },
{ id: 'typeId', schema: dialog.typeId }
]

const allowedIncludes = ['verb', 'tense']

router.get('/', async (req: Request, res: Response) => {

const conditionalClause = parseConditionals(req, allowedConditionals);
const withs = parseIncludes(req, allowedIncludes);

const dialogs = await db.query.dialog.findMany({
where: and(...conditionalClause),
with: withs
})

return res.json({
success: true,
data: dialogs
})
})
parseConditionals.ts
import {eq, SQL} from "drizzle-orm";
import {MySqlColumn} from "drizzle-orm/mysql-core";
import {Request} from "express";

export const parseConditionals = (req: Request, allowedConditionals: { id: string, schema: MySqlColumn}[]) : SQL[] => {
let conditionalClause: SQL[] = [];
allowedConditionals.forEach( conditional => {
if(req.query[conditional.id]) {
conditionalClause.push( eq(conditional.schema, Number(req.query[conditional.id]) ) )
}
})
return conditionalClause;
}
import {eq, SQL} from "drizzle-orm";
import {MySqlColumn} from "drizzle-orm/mysql-core";
import {Request} from "express";

export const parseConditionals = (req: Request, allowedConditionals: { id: string, schema: MySqlColumn}[]) : SQL[] => {
let conditionalClause: SQL[] = [];
allowedConditionals.forEach( conditional => {
if(req.query[conditional.id]) {
conditionalClause.push( eq(conditional.schema, Number(req.query[conditional.id]) ) )
}
})
return conditionalClause;
}
parseIncludes.ts
import {Request} from "express";

export const parseIncludes = (req: Request, allowedIncludes: string[]): {} => {
const parsedIncludes: string[] = req.query._includes ? String(req.query._includes).split(',') : []
const includes: string[] = parsedIncludes.filter( include => allowedIncludes.includes(include.toLowerCase()) )
const withs: any = {};
includes.forEach( (include : string) => {
withs[include] = true;
})
return withs;
}
import {Request} from "express";

export const parseIncludes = (req: Request, allowedIncludes: string[]): {} => {
const parsedIncludes: string[] = req.query._includes ? String(req.query._includes).split(',') : []
const includes: string[] = parsedIncludes.filter( include => allowedIncludes.includes(include.toLowerCase()) )
const withs: any = {};
includes.forEach( (include : string) => {
withs[include] = true;
})
return withs;
}
Angelelz
Angelelz14mo ago
getTableColumns might be what you're looking for: https://orm.drizzle.team/docs/goodies#get-typed-table-columns
Goodies - DrizzleORM
Drizzle ORM | %s
Angelelz
Angelelz14mo ago
You can also get the table information, it will give you back a lot more: https://orm.drizzle.team/docs/goodies#get-table-information
Goodies - DrizzleORM
Drizzle ORM | %s
CharlesCharlieCharles
perfect, thanks. Didn't spot that.
Want results from more Discord servers?
Add your server