Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Data API arrays in where clauses #4241

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions drizzle-orm/src/aws-data-api/pg/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ export class AwsDataApiPgDatabase<
export class AwsPgDialect extends PgDialect {
static override readonly [entityKind]: string = 'AwsPgDialect';

override escapeParam(num: number): string {
return `:${num + 1}`;
override escapeParam(num: number, _value: unknown, encoder: unknown | undefined): string {
const param = `:${num + 1}`;

if (is(encoder, PgArray)) {
return `${param}::${encoder.getSQLType()}`;
}

return param;
}

override buildInsertQuery(
Expand Down
3 changes: 2 additions & 1 deletion drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { entityKind, is } from '~/entity.ts';
import { DrizzleError } from '~/errors.ts';
import type { MigrationConfig, MigrationMeta } from '~/migrator.ts';
import {
PgArray,
PgColumn,
PgDate,
PgDateString,
Expand Down Expand Up @@ -115,7 +116,7 @@ export class PgDialect {
return `"${name}"`;
}

escapeParam(num: number): string {
escapeParam(num: number, _value: unknown, _encoder: unknown | undefined): string {
return `$${num + 1}`;
}

Expand Down
18 changes: 13 additions & 5 deletions drizzle-orm/src/sql/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type Chunk =
export interface BuildQueryConfig {
casing: CasingCache;
escapeName(name: string): string;
escapeParam(num: number, value: unknown): string;
escapeParam(num: number, value: unknown, encoder: DriverValueEncoder<unknown, unknown> | undefined): string;
escapeString(str: string): string;
prepareTyping?: (encoder: DriverValueEncoder<unknown, unknown>) => QueryTypingsValue;
paramStartIndex?: { value: number };
Expand Down Expand Up @@ -217,7 +217,11 @@ export class SQL<T = unknown> implements SQLWrapper {

if (is(chunk, Param)) {
if (is(chunk.value, Placeholder)) {
return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ['none'] };
return {
sql: escapeParam(paramStartIndex.value++, chunk, chunk.encoder),
params: [chunk],
typings: ['none'],
};
}

const mappedValue = chunk.value === null ? null : chunk.encoder.mapToDriverValue(chunk.value);
Expand All @@ -235,11 +239,15 @@ export class SQL<T = unknown> implements SQLWrapper {
typings = [prepareTyping(chunk.encoder)];
}

return { sql: escapeParam(paramStartIndex.value++, mappedValue), params: [mappedValue], typings };
return {
sql: escapeParam(paramStartIndex.value++, mappedValue, chunk.encoder),
params: [mappedValue],
typings,
};
}

if (is(chunk, Placeholder)) {
return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ['none'] };
return { sql: escapeParam(paramStartIndex.value++, chunk, undefined), params: [chunk], typings: ['none'] };
}

if (is(chunk, SQL.Aliased) && chunk.fieldAlias !== undefined) {
Expand Down Expand Up @@ -280,7 +288,7 @@ export class SQL<T = unknown> implements SQLWrapper {
return { sql: this.mapInlineParam(chunk, config), params: [] };
}

return { sql: escapeParam(paramStartIndex.value++, chunk), params: [chunk], typings: ['none'] };
return { sql: escapeParam(paramStartIndex.value++, chunk, undefined), params: [chunk], typings: ['none'] };
}));
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"source-map-support": "^0.5.21",
"sql.js": "^1.8.0",
"sqlite3": "^5.1.4",
"sst": "^3.0.4",
"sst": "^3.9.28",
"uuid": "^9.0.0",
"uvu": "^0.5.6",
"vitest": "^2.1.2",
Expand Down
29 changes: 27 additions & 2 deletions integration-tests/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="./.sst/types.generated.ts" />
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
/* deno-fmt-ignore-file */

import 'sst';
declare module 'sst' {
export interface Resource {
MyVpc: {
type: 'sst.aws.Vpc';
};
Postgres: {
clusterArn: string;
database: string;
host: string;
password: string;
port: number;
reader: string;
secretArn: string;
type: 'sst.aws.Aurora';
username: string;
};
}
}

import 'sst';
export {};
13 changes: 7 additions & 6 deletions integration-tests/sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
app(_input) {
return {
name: 'awsdataapi',
removal: 'remove',
home: 'aws',
providers: { aws: '6.70.1' },
};
},
async run() {
new sst.aws.Postgres('Postgres', {
scaling: {
min: '0.5 ACU',
max: '1 ACU',
},
const vpc = new sst.aws.Vpc('MyVpc');

new sst.aws.Aurora('Postgres', {
dataApi: true,
engine: 'postgres',
vpc,
});
},
});
42 changes: 41 additions & 1 deletion integration-tests/tests/pg/awsdatapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dotenv/config';

import { RDSDataClient } from '@aws-sdk/client-rds-data';
import * as dotenv from 'dotenv';
import { asc, eq, inArray, notInArray, relations, sql, TransactionRollbackError } from 'drizzle-orm';
import { arrayContains, asc, eq, inArray, notInArray, relations, sql, TransactionRollbackError } from 'drizzle-orm';
import type { AwsDataApiPgDatabase } from 'drizzle-orm/aws-data-api/pg';
import { drizzle } from 'drizzle-orm/aws-data-api/pg';
import { migrate } from 'drizzle-orm/aws-data-api/pg/migrator';
Expand Down Expand Up @@ -1286,6 +1286,46 @@ test('update with array values works', async () => {
expect(insertResult?.bestTexts).toEqual(bestTexts);
});

test('select with arrayContains works', async () => {
const [newUser] = await db
.insert(usersTable)
.values({ name: 'John' })
.returning();

const bestTexts = ['text4', 'text5', 'text6'];
await db
.update(usersTable)
.set({
bestTexts,
})
.where(eq(usersTable.id, newUser!.id));

const [result] = await db.select().from(usersTable)
.where(arrayContains(usersTable.bestTexts, ['text4']));

expect(result?.bestTexts).toEqual(bestTexts);
});

test('select with array eq works', async () => {
const [newUser] = await db
.insert(usersTable)
.values({ name: 'John' })
.returning();

const bestTexts = ['text4', 'text5', 'text6'];
await db
.update(usersTable)
.set({
bestTexts,
})
.where(eq(usersTable.id, newUser!.id));

const [result] = await db.select().from(usersTable)
.where(eq(usersTable.bestTexts, bestTexts));

expect(result?.bestTexts).toEqual(bestTexts);
});

test('all date and time columns', async () => {
const table = pgTable('all_columns', {
id: serial('id').primaryKey(),
Expand Down
1 change: 0 additions & 1 deletion integration-tests/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export default defineConfig({
'js-tests/driver-init/module/vercel.test.mjs',
]
: []),
'tests/pg/awsdatapi.test.ts',
'tests/awsdatapi.alltypes.test.ts',
'tests/pg/vercel-pg.test.ts',
'tests/relational/vercel.test.ts',
Expand Down
Loading