- Abstract shared sql code to a new drizzle package - Adds sqlite package, not ready to publish until drizzle patches some issues - Add `transactionOptions` to allow customizing or disabling db transactions - Adds "experimental" label to the `schemaName` property until drizzle patches an issue
34 lines
838 B
TypeScript
34 lines
838 B
TypeScript
import type { ChainedMethods } from '@payloadcms/drizzle/types'
|
|
|
|
import { chainMethods } from '@payloadcms/drizzle'
|
|
import { sql } from 'drizzle-orm'
|
|
|
|
import type { CountDistinct, SQLiteAdapter } from './types.js'
|
|
|
|
export const countDistinct: CountDistinct = async function countDistinct(
|
|
this: SQLiteAdapter,
|
|
{ db, joins, tableName, where },
|
|
) {
|
|
const chainedMethods: ChainedMethods = []
|
|
|
|
joins.forEach(({ condition, table }) => {
|
|
chainedMethods.push({
|
|
args: [table, condition],
|
|
method: 'leftJoin',
|
|
})
|
|
})
|
|
|
|
const countResult = await chainMethods({
|
|
methods: chainedMethods,
|
|
query: db
|
|
.select({
|
|
count: sql<number>`count
|
|
(DISTINCT ${this.tables[tableName].id})`,
|
|
})
|
|
.from(this.tables[tableName])
|
|
.where(where),
|
|
})
|
|
|
|
return Number(countResult[0].count)
|
|
}
|