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

Add PostgreSQL connection string support in client constructor #1607

Draft
wants to merge 1 commit into
base: feat/remove-data-api-sdk-base
Choose a base branch
from
Draft
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: 10 additions & 0 deletions packages/client/src/api/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ describe('API Proxy types', () => {
expect(xata.authentication.undefinedOperation).toBeUndefined();
});
});

describe('XataApiClient', () => {
test('accepts and uses postgresConnectionString', () => {
const postgresConnectionString = 'postgres://user:password@localhost:5432/mydb';
const client = new XataApiClient({ apiKey: 'fake-api-key', postgresConnectionString });

// @ts-ignore
expect(client.postgresConnectionString).toBe(postgresConnectionString);
});
});
5 changes: 4 additions & 1 deletion packages/client/src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface XataApiClientOptions {
trace?: TraceFunction;
clientName?: string;
xataAgentExtra?: Record<string, string>;
postgresConnectionString?: string; // Pe028
}

type UserProps = {
Expand Down Expand Up @@ -42,6 +43,7 @@ const buildApiClient = () =>
const apiKey = options.apiKey;
const trace = options.trace ?? defaultTrace;
const clientID = generateUUID();
const postgresConnectionString = options.postgresConnectionString; // Pbb35

if (!apiKey) {
throw new Error('Could not resolve a valid apiKey');
Expand All @@ -55,7 +57,8 @@ const buildApiClient = () =>
trace,
clientName: options.clientName,
xataAgentExtra: options.xataAgentExtra,
clientID
clientID,
postgresConnectionString // Pbb35
};

return new Proxy(this, {
Expand Down
17 changes: 17 additions & 0 deletions packages/client/src/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, test } from 'vitest';
import { BaseClient } from './client';
import { DatabaseSchema } from './schema';

const schema: DatabaseSchema = {
tables: []
};

describe('BaseClient', () => {
test('accepts and uses postgresConnectionString', () => {
const postgresConnectionString = 'postgres://user:password@localhost:5432/mydb';
const client = new BaseClient({ apiKey: 'fake-api-key', databaseURL: 'https://example.xata.sh/db', branch: 'main', postgresConnectionString }, schema);

// @ts-ignore
expect(client.#options.postgresConnectionString).toBe(postgresConnectionString);
});
});
11 changes: 8 additions & 3 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type BaseClientOptions = {
enableBrowser?: boolean;
clientName?: string;
xataAgentExtra?: Record<string, string>;
postgresConnectionString?: string;
};

type SafeOptions = AllRequired<Omit<BaseClientOptions, 'clientName' | 'xataAgentExtra'>> & {
Expand Down Expand Up @@ -95,6 +96,7 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu
const clientName = options?.clientName;
const host = options?.host ?? 'production';
const xataAgentExtra = options?.xataAgentExtra;
const postgresConnectionString = options?.postgresConnectionString;

if (!apiKey) {
throw new Error('Option apiKey is required');
Expand All @@ -118,7 +120,8 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu
clientID: generateUUID(),
enableBrowser,
clientName,
xataAgentExtra
xataAgentExtra,
postgresConnectionString
};
}

Expand All @@ -130,7 +133,8 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu
trace,
clientID,
clientName,
xataAgentExtra
xataAgentExtra,
postgresConnectionString
}: SafeOptions): ApiExtraProps {
return {
fetch,
Expand All @@ -145,7 +149,8 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu
trace,
clientID,
clientName,
xataAgentExtra
xataAgentExtra,
postgresConnectionString
};
}
} as unknown as ClientConstructor<Plugins>;
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/kysely/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class KyselyPlugin<Schemas extends Record<string, XataRecord>> extends Xa
const xata = { sql: new SQLPlugin().build(pluginOptions) };

return new Kysely<Model<Schemas>>({
dialect: new XataDialect({ xata })
dialect: new XataDialect({ xata, postgresConnectionString: pluginOptions.postgresConnectionString })
});
}
}
24 changes: 20 additions & 4 deletions packages/plugin-client-kysely/src/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ export type XataDialectConfig = {
* @default 'strong'
*/
consistency?: 'strong' | 'eventual';
postgresConnectionString?: string;
};

export class XataDialect implements Dialect {
constructor(private config: XataDialectConfig) {}

createAdapter() {
return new PostgresAdapter();
if (this.config.postgresConnectionString) {
return new PostgresAdapter();
}
return new XataAdapter();
}

createDriver(): Driver {
Expand Down Expand Up @@ -84,6 +88,21 @@ export class XataConnection implements DatabaseConnection {
const { sql } = this.#config.xata;
const { sql: statement, parameters } = compiledQuery;

if (this.#config.postgresConnectionString) {
const { Client } = require('pg');
const client = new Client({ connectionString: this.#config.postgresConnectionString });
await client.connect();
const res = await client.query(statement, parameters);
await client.end();

return {
rows: res.rows as O[],
columns: res.fields,
numAffectedRows: BigInt(res.rowCount),
numUpdatedOrDeletedRows: BigInt(res.rowCount)
};
}

const { records, warning, columns } = await sql({
statement,
params: parameters as any[],
Expand All @@ -97,11 +116,8 @@ export class XataConnection implements DatabaseConnection {

return {
rows: records as O[],
// @ts-ignore
columns,
// @ts-ignore replaces `QueryResult.numUpdatedOrDeletedRows` in kysely > 0.22
numAffectedRows,
// deprecated in kysely > 0.22, keep for backward compatibility.
numUpdatedOrDeletedRows: numAffectedRows
};
}
Expand Down
Loading