Skip to content

[Experimental] Support for prisma #1220

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

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Forward xata tables
Signed-off-by: Alexis Rico <[email protected]>
  • Loading branch information
SferaDev committed Oct 12, 2023
commit 6c818dcad802ab07fd430e837e65f1580e3dd398
9 changes: 5 additions & 4 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ export const buildClient = <Plugins extends Record<string, XataPlugin> = {}>(plu
sql: SQLPluginResult;
files: FilesPluginResult<any>;

constructor(options: BaseClientOptions = {}, schemaTables?: Schemas.Table[]) {
constructor(options: BaseClientOptions = {}, schemaTables: Schemas.Table[]) {
const safeOptions = this.#parseOptions(options);
this.#options = safeOptions;

const pluginOptions: XataPluginOptions = {
...this.#getFetchProps(safeOptions),
cache: safeOptions.cache,
host: safeOptions.host
host: safeOptions.host,
tables: schemaTables ?? []
};

const db = new SchemaPlugin(schemaTables).build(pluginOptions);
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
const db = new SchemaPlugin().build(pluginOptions);
const search = new SearchPlugin(db).build(pluginOptions);
const transactions = new TransactionPlugin().build(pluginOptions);
const sql = new SQLPlugin().build(pluginOptions);
const files = new FilesPlugin().build(pluginOptions);
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiExtraProps, HostProvider } from './api';
import { ApiExtraProps, HostProvider, Schemas } from './api';
import { CacheImpl } from './schema/cache';

export abstract class XataPlugin {
Expand All @@ -8,4 +8,5 @@ export abstract class XataPlugin {
export type XataPluginOptions = ApiExtraProps & {
cache: CacheImpl;
host: HostProvider;
tables: Schemas.Table[];
};
6 changes: 3 additions & 3 deletions packages/client/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ export class SchemaPlugin<Schemas extends Record<string, XataRecord>> extends Xa
#tables: Record<string, Repository<any>> = {};
#schemaTables?: Table[];

constructor(schemaTables?: Table[]) {
constructor() {
super();

this.#schemaTables = schemaTables;
}

build(pluginOptions: XataPluginOptions): SchemaPluginResult<Schemas> {
this.#schemaTables = pluginOptions.tables;

const db: any = new Proxy(
{},
{
Expand Down
5 changes: 3 additions & 2 deletions packages/client/src/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ export type SearchPluginResult<Schemas extends Record<string, BaseData>> = {
export class SearchPlugin<Schemas extends Record<string, XataRecord>> extends XataPlugin {
#schemaTables?: Table[];

constructor(private db: SchemaPluginResult<Schemas>, schemaTables?: Table[]) {
constructor(private db: SchemaPluginResult<Schemas>) {
super();
this.#schemaTables = schemaTables;
}

build(pluginOptions: XataPluginOptions): SearchPluginResult<Schemas> {
this.#schemaTables = pluginOptions.tables;

return {
all: async <Tables extends StringKeys<Schemas>>(query: string, options: SearchOptions<Schemas, Tables> = {}) => {
const records = await this.#search(query, options, pluginOptions);
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-client-prisma/src/driver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DriverAdapter, Query, Queryable, Result, ResultSet, Transaction } from '@prisma/driver-adapter-utils';
import { Debug, ok } from '@prisma/driver-adapter-utils';
import { Responses, SQLPluginResult } from '@xata.io/client';
import { Schemas, Responses, SQLPluginResult } from '@xata.io/client';
import { fieldToColumnType } from './conversion';

const debug = Debug('prisma:driver-adapter:xata');
Expand Down Expand Up @@ -43,7 +43,7 @@ abstract class XataQueryable implements Queryable {
type XataClient = { sql: SQLPluginResult };

export class PrismaXataHTTP extends XataQueryable implements DriverAdapter {
constructor(private client: XataClient) {
constructor(private client: XataClient, _tables?: Schemas.Table[]) {
super();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-client-prisma/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class PrismaPlugin extends XataPlugin {
build(pluginOptions: XataPluginOptions) {
const xata = { sql: new SQLPlugin().build(pluginOptions) };

return new PrismaXataHTTP(xata);
return new PrismaXataHTTP(xata, pluginOptions.tables);
}
}

Expand Down