Skip to content

Commit

Permalink
Adding and testing .raw({columnNames: true})
Browse files Browse the repository at this point in the history
  • Loading branch information
geelen committed Feb 1, 2024
1 parent 9310b39 commit 1bd2170
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
23 changes: 17 additions & 6 deletions src/cloudflare/internal/d1-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type D1Result<T = unknown> = D1Response & {
results: T[]
}

type D1RawOptions = {
columnNames?: boolean
}

type D1UpstreamFailure = {
results?: never
error: string
Expand Down Expand Up @@ -85,12 +89,12 @@ class D1Database {
public async batch<T = unknown>(
statements: D1PreparedStatement[]
): Promise<D1Result<T>[]> {
const exec = await this._sendOrThrow(
const exec = (await this._sendOrThrow(
'/query',
statements.map((s: D1PreparedStatement) => s.statement),
statements.map((s: D1PreparedStatement) => s.params),
'ROWS_AND_COLUMNS'
) as D1UpstreamSuccess<T>[]
)) as D1UpstreamSuccess<T>[]
return exec.map(toArrayOfObjects)
}

Expand Down Expand Up @@ -303,7 +307,7 @@ class D1PreparedStatement {
)
}

public async raw<T = unknown[]>(): Promise<T[]> {
public async raw<T = unknown[]>(options?: D1RawOptions): Promise<T[]> {
const s = firstIfArray(
await this.database._sendOrThrow<Record<string, unknown>>(
'/query',
Expand All @@ -319,6 +323,9 @@ class D1PreparedStatement {
if (Array.isArray(s.results)) {
const raw: T[] = []
for (const row of s.results) {
if (options?.columnNames && raw.length === 0) {
raw.push(Array.from(Object.keys(row)) as T)
}
const entry = Object.keys(row).map((k) => {
return row[k]
})
Expand All @@ -327,7 +334,10 @@ class D1PreparedStatement {
return raw
} else {
// Otherwise, data is already in the correct format
return s.results.rows as T[]
return [
...(options?.columnNames ? [s.results.columns as T] : []),
...(s.results.rows as T[]),
]
}
}
}
Expand All @@ -353,8 +363,9 @@ function toArrayOfObjects<T>(response: D1UpstreamSuccess<T>): D1Result<T> {
const { rows, columns } = results
return {
...response,
results: rows.map((row) =>
Object.fromEntries(row.map((cell, i) => [columns[i], cell]))
results: rows.map(
(row) =>
Object.fromEntries(row.map((cell, i) => [columns[i], cell])) as T
),
}
}
Expand Down
34 changes: 32 additions & 2 deletions src/cloudflare/internal/test/d1/d1-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export const test_d1_api = test(async (DB) => {
)

await itShould(
'not lose data for duplicate columns in a join using raw',
'not lose data for duplicate columns in a join using raw()',
() => DB.prepare(`SELECT * FROM abc, cde;`).raw(),
[
[1, 2, 3, 'A', 'B', 'C'],
Expand All @@ -345,9 +345,39 @@ export const test_d1_api = test(async (DB) => {
]
)

await itShould(
'add columns using .raw({ columnNames: true })',
() => DB.prepare(`SELECT * FROM abc, cde;`).raw({ columnNames: true }),
[
['a', 'b', 'c', 'c', 'd', 'e'],
[1, 2, 3, 'A', 'B', 'C'],
[1, 2, 3, 'D', 'E', 'F'],
[1, 2, 3, 'G', 'H', 'I'],
[4, 5, 6, 'A', 'B', 'C'],
[4, 5, 6, 'D', 'E', 'F'],
[4, 5, 6, 'G', 'H', 'I'],
]
)

await itShould(
'not add columns using .raw({ columnNames: false })',
() => DB.prepare(`SELECT * FROM abc, cde;`).raw({ columnNames: false }),
[
[1, 2, 3, 'A', 'B', 'C'],
[1, 2, 3, 'D', 'E', 'F'],
[1, 2, 3, 'G', 'H', 'I'],
[4, 5, 6, 'A', 'B', 'C'],
[4, 5, 6, 'D', 'E', 'F'],
[4, 5, 6, 'G', 'H', 'I'],
]
)

await itShould(
'return 0 rows_written for IN clauses',
() => DB.prepare(`SELECT * from cde WHERE c IN ('A','B','C','X','Y','Z')`).all(),
() =>
DB.prepare(
`SELECT * from cde WHERE c IN ('A','B','C','X','Y','Z')`
).all(),
{
success: true,
results: [{ c: 'A', d: 'B', e: 'C' }],
Expand Down

0 comments on commit 1bd2170

Please sign in to comment.