CharlesCharlieCharles
CharlesCharlieCharles
DTDrizzle Team
Created by CharlesCharlieCharles on 11/11/2023 in #help
get last inserted id with mysql
thanks, I didn't spot it because thought it would be directly on the data object and it didn't show up on the types autocomplete. For anybody else looking for mySQl it's
data[0].insertId
data[0].insertId
4 replies
DTDrizzle Team
Created by CharlesCharlieCharles on 11/13/2023 in #help
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]
it was an incorrect import added by ide - import {sql} from "drizzle-orm/index" instead of import {sql} from "drizzle-orm"
2 replies
DTDrizzle Team
Created by CharlesCharlieCharles on 10/24/2023 in #help
inferring the column information from the imported schema
perfect, thanks. Didn't spot that.
6 replies
DTDrizzle Team
Created by CharlesCharlieCharles on 10/24/2023 in #help
inferring the column information from the imported schema
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;
}
6 replies