Aaroned
Aaroned
DTDrizzle Team
Created by Titan on 4/8/2024 in #help
Insert returning api to match select columns
@Titan Here is a possible conversion function
type TrueKeys<T extends Record<string, boolean>> = {
[K in keyof T]: T[K] extends true ? K : never
}[keyof T]

function getSelectColumns<
TQueryColumns extends Record<string, boolean>,
TKeys extends TrueKeys<TQueryColumns>,
TTableColumns extends Record<TKeys, unknown>
>(queryColumns: TQueryColumns, tableColumns: TTableColumns) {
const keys = Object.keys(tableColumns) as TKeys[]
return keys
.filter(key => queryColumns[key])
.reduce(
(acc, key) => ({ ...acc, [key]: tableColumns[key] }),
{} as { -readonly [Key in TKeys]: TTableColumns[Key] }
)
}

const queryColumns = {
id: true,
} as const

const selectColumns = getSelectColumns(
queryColumns,
getTableColumns(tables.organisation)
)

const selectResult = await db
.select(selectColumns)
.from(tables.organisation)
const queryResult = await db.query.organisation.findMany({
columns: queryColumns,
})
type TrueKeys<T extends Record<string, boolean>> = {
[K in keyof T]: T[K] extends true ? K : never
}[keyof T]

function getSelectColumns<
TQueryColumns extends Record<string, boolean>,
TKeys extends TrueKeys<TQueryColumns>,
TTableColumns extends Record<TKeys, unknown>
>(queryColumns: TQueryColumns, tableColumns: TTableColumns) {
const keys = Object.keys(tableColumns) as TKeys[]
return keys
.filter(key => queryColumns[key])
.reduce(
(acc, key) => ({ ...acc, [key]: tableColumns[key] }),
{} as { -readonly [Key in TKeys]: TTableColumns[Key] }
)
}

const queryColumns = {
id: true,
} as const

const selectColumns = getSelectColumns(
queryColumns,
getTableColumns(tables.organisation)
)

const selectResult = await db
.select(selectColumns)
.from(tables.organisation)
const queryResult = await db.query.organisation.findMany({
columns: queryColumns,
})
8 replies
DTDrizzle Team
Created by AuroPick on 4/7/2024 in #help
Query API relation null type
Yes chat media relations is correct
4 replies
DTDrizzle Team
Created by Titan on 4/8/2024 in #help
Insert returning api to match select columns
I think you will need to write an automatic mapping function SelectColumnsToReturningColumns, but I think you should be able to get the values needed for returning columns from getTableColumns
8 replies
DTDrizzle Team
Created by Titan on 4/8/2024 in #help
Insert returning api to match select columns
@Titan you could possibly create a conversion function that utilises both getTableColumns(table) (https://orm.drizzle.team/docs/goodies#get-typed-table-columns) and maybe also InferSelectModel<typeof table> (https://orm.drizzle.team/docs/goodies#type-api)
8 replies
DTDrizzle Team
Created by Martin Carlsson on 4/9/2024 in #help
Is relationships only for .findMany() and .findFirst()
@Martin Carlsson yes that's correct
3 replies
DTDrizzle Team
Created by DiamondDragon on 4/7/2024 in #help
Missing nested where clause
@DiamondDragon https://www.sqlite.org/optoverview.html this is a good page for understanding SQLite (ie. Turso libsql) query optimization and indexes. SQLite does NOT automatically create indexes on foreign keys, so if you are not sure, that is generally a good place to start. Another helpful tool for optimisation is EXPLAIN QUERY PLAN https://www.sqlite.org/eqp.html this can be run through the Turso CLI db shell command. (You will need to first obtain the SQL of the Drizzle query by using .toSQL() at the end of the query) You really want to avoid TABLE SCANS as each row is read in a table scan and counts to the Turso Rows Read quota. By adding appropriate indexes that SQLite can use for your query, the query engine will use INDEX SCANS and avoid TABLE SCANS. See also https://turso.tech/blog/tips-for-maximizing-your-turso-billing-allowances-48a0fca163e9
23 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
you can use serial if you want an autoincrementing integer, or uuid with .defaultRandom() for a string uuid
15 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
id: uuid('id').defaultRandom()
id: uuid('id').defaultRandom()
15 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
15 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
15 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
because you are not using serial (ie. autoincrement integer), but rather a string, yes you need to supply a value or a defaultFn
15 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
actually there is two error messages there, I think the first is the issue... you are using a string (varchar) as primary key.... are you supplying a value?
15 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
what is the type of newMediaId?
15 replies
DTDrizzle Team
Created by B33fb0n3 on 4/8/2024 in #help
No overload matches this call
@B33fb0n3 can you share your exclusiveItems schema?
15 replies
DTDrizzle Team
Created by andreas_444 on 4/4/2024 in #help
Object literal may only specify known properties, and photo does not exist in type
That's fair enough. But if you want you are welcome to define Drizzle ORM level relations (and hence use relation queries) without having to define database level foreign keys 😊
17 replies
DTDrizzle Team
Created by andreas_444 on 4/4/2024 in #help
Object literal may only specify known properties, and photo does not exist in type
FYI I just noticed this comment here https://orm.drizzle.team/docs/rqb#foreign-keys
What this means is relations and foreign keys can be used together, but they are not dependent on each other. You can define relations without using foreign keys (and vice versa), which allows them to be used with databases that do not support foreign keys.
17 replies
DTDrizzle Team
Created by DiamondDragon on 4/7/2024 in #help
Missing nested where clause
yes the 1st parameter fields is the table you are querying, and the 2nd parameter operators is just a shorthand to using operators without needing imports
23 replies
DTDrizzle Team
Created by DiamondDragon on 4/7/2024 in #help
Missing nested where clause
sorry @DiamondDragon I just realised it's likely not a bug, you probably just need to use the callback syntax:
const query = await db.query.rawCards.findFirst({
with: {
rawDetails: {
columns: {
zId: true,
rawData: true,
lastFetchedDate: true,
},
},
},
where: (rawCard, { notExists }) => notExists(
db.select().from(rawDetails).where(eq(rawDetails.rawCardsId, rawCard.id)),
),
})
const query = await db.query.rawCards.findFirst({
with: {
rawDetails: {
columns: {
zId: true,
rawData: true,
lastFetchedDate: true,
},
},
},
where: (rawCard, { notExists }) => notExists(
db.select().from(rawDetails).where(eq(rawDetails.rawCardsId, rawCard.id)),
),
})
23 replies
DTDrizzle Team
Created by DiamondDragon on 4/7/2024 in #help
Missing nested where clause
yeah left join will work well 👍
23 replies
DTDrizzle Team
Created by DiamondDragon on 4/7/2024 in #help
Missing nested where clause
In the mean time, if you want to return the first rawCard that doesn't have a rawDetail, then yes you can use left join and test for null
23 replies