-
Notifications
You must be signed in to change notification settings - Fork 8
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
[Experimental] Support for prisma #1220
Draft
SferaDev
wants to merge
13
commits into
main
Choose a base branch
from
prisma
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8e3bb7e
Add experimental support for prisma
SferaDev 6c818dc
Forward xata tables
SferaDev a87dd43
Expose schema in client
SferaDev 0392c89
get basic queries working
eemmiillyy 935d819
int,float,bool
eemmiillyy 7f07a41
json,datetime
eemmiillyy ea5e878
queryable relation data
eemmiillyy 99a18d1
table name
eemmiillyy 0dc7893
Merge branch 'main' into prisma
SferaDev 17e0052
Use metadata from sql endpoint
SferaDev 6e56d2f
Merge remote-tracking branch 'origin/main' into prisma
SferaDev 93d6c31
Update packages/plugin-client-prisma/src/driver.ts
SferaDev 841feab
Merge branch 'main' into prisma
SferaDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
src | ||
tsconfig.json | ||
rollup.config.mjs | ||
.eslintrc.cjs | ||
.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @xata.io/prisma |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "@xata.io/prisma", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "rimraf dist && rollup -c", | ||
"tsc": "tsc --noEmit" | ||
}, | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/xataio/client-ts/issues" | ||
}, | ||
"dependencies": { | ||
"@prisma/driver-adapter-utils": "^5.4.2", | ||
"@xata.io/client": "workspace:*" | ||
}, | ||
"devDependencies": { | ||
"@prisma/client": "^5.6.0", | ||
"prisma": "^5.6.0", | ||
"superjson": "^2.2.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import dts from 'rollup-plugin-dts'; | ||
import esbuild from 'rollup-plugin-esbuild'; | ||
|
||
export default [ | ||
{ | ||
input: 'src/index.ts', | ||
plugins: [esbuild()], | ||
output: [ | ||
{ | ||
file: `dist/index.cjs`, | ||
format: 'cjs', | ||
sourcemap: true | ||
}, | ||
{ | ||
file: `dist/index.mjs`, | ||
format: 'es', | ||
sourcemap: true | ||
} | ||
] | ||
}, | ||
{ | ||
input: 'src/index.ts', | ||
plugins: [dts()], | ||
output: { | ||
file: `dist/index.d.ts`, | ||
format: 'es' | ||
} | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { type ColumnType, ColumnTypeEnum } from '@prisma/driver-adapter-utils'; | ||
|
||
export function fieldToColumnType(fieldType: string): ColumnType { | ||
switch (fieldType) { | ||
case 'bool': | ||
return ColumnTypeEnum.Boolean; | ||
case 'bytea': | ||
return ColumnTypeEnum.Bytes; | ||
case 'char': | ||
return ColumnTypeEnum.Text; | ||
case 'int8': | ||
return ColumnTypeEnum.Int64; | ||
case 'int2': | ||
return ColumnTypeEnum.Int32; | ||
case 'int4': | ||
return ColumnTypeEnum.Int32; | ||
case 'regproc': | ||
return ColumnTypeEnum.Text; | ||
case 'text': | ||
return ColumnTypeEnum.Text; | ||
case 'oid': | ||
return ColumnTypeEnum.Int64; | ||
case 'tid': | ||
return ColumnTypeEnum.Text; | ||
case 'xid': | ||
return ColumnTypeEnum.Text; | ||
case 'cid': | ||
return ColumnTypeEnum.Text; | ||
case 'json': | ||
return ColumnTypeEnum.Json; | ||
case 'xml': | ||
return ColumnTypeEnum.Text; | ||
case 'pg_node_tree': | ||
return ColumnTypeEnum.Text; | ||
case 'smgr': | ||
return ColumnTypeEnum.Text; | ||
case 'path': | ||
return ColumnTypeEnum.Text; | ||
case 'polygon': | ||
return ColumnTypeEnum.Text; | ||
case 'cidr': | ||
return ColumnTypeEnum.Text; | ||
case 'float4': | ||
return ColumnTypeEnum.Float; | ||
case 'float8': | ||
return ColumnTypeEnum.Double; | ||
case 'abstime': | ||
return ColumnTypeEnum.Text; | ||
case 'reltime': | ||
return ColumnTypeEnum.Text; | ||
case 'tinterval': | ||
return ColumnTypeEnum.Text; | ||
case 'circle': | ||
return ColumnTypeEnum.Text; | ||
case 'macaddr8': | ||
return ColumnTypeEnum.Text; | ||
case 'money': | ||
return ColumnTypeEnum.Numeric; | ||
case 'macaddr': | ||
return ColumnTypeEnum.Text; | ||
case 'inet': | ||
return ColumnTypeEnum.Text; | ||
case 'aclitem': | ||
return ColumnTypeEnum.Text; | ||
case 'bpchar': | ||
return ColumnTypeEnum.Text; | ||
case 'varchar': | ||
return ColumnTypeEnum.Text; | ||
case 'date': | ||
return ColumnTypeEnum.Date; | ||
case 'time': | ||
return ColumnTypeEnum.Time; | ||
case 'timestamp': | ||
return ColumnTypeEnum.DateTime; | ||
case 'timestamptz': | ||
return ColumnTypeEnum.DateTime; | ||
case 'interval': | ||
return ColumnTypeEnum.Text; | ||
case 'timetz': | ||
return ColumnTypeEnum.Time; | ||
case 'bit': | ||
return ColumnTypeEnum.Text; | ||
case 'varbit': | ||
return ColumnTypeEnum.Text; | ||
case 'numeric': | ||
return ColumnTypeEnum.Numeric; | ||
case 'refcursor': | ||
return ColumnTypeEnum.Text; | ||
case 'regprocedure': | ||
return ColumnTypeEnum.Text; | ||
case 'regoper': | ||
return ColumnTypeEnum.Text; | ||
case 'regoperator': | ||
return ColumnTypeEnum.Text; | ||
case 'regclass': | ||
return ColumnTypeEnum.Text; | ||
case 'regtype': | ||
return ColumnTypeEnum.Text; | ||
case 'uuid': | ||
return ColumnTypeEnum.Uuid; | ||
case 'txid_snapshot': | ||
return ColumnTypeEnum.Text; | ||
case 'pg_lsn': | ||
return ColumnTypeEnum.Text; | ||
case 'pg_ndistinct': | ||
return ColumnTypeEnum.Text; | ||
case 'pg_dependencies': | ||
return ColumnTypeEnum.Text; | ||
case 'tsvector': | ||
return ColumnTypeEnum.Text; | ||
case 'tsquery': | ||
return ColumnTypeEnum.Text; | ||
case 'gtsvector': | ||
return ColumnTypeEnum.Text; | ||
case 'regconfig': | ||
return ColumnTypeEnum.Text; | ||
case 'regdictionary': | ||
return ColumnTypeEnum.Text; | ||
case 'jsonb': | ||
return ColumnTypeEnum.Json; | ||
case 'regnamespace': | ||
return ColumnTypeEnum.Text; | ||
case 'regrole': | ||
return ColumnTypeEnum.Text; | ||
default: | ||
return ColumnTypeEnum.Text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* eslint-disable @typescript-eslint/require-await */ | ||
import type { | ||
ColumnType, | ||
DriverAdapter, | ||
Query, | ||
Queryable, | ||
Result, | ||
ResultSet, | ||
Transaction | ||
} from '@prisma/driver-adapter-utils'; | ||
import { Debug, err, ok } from '@prisma/driver-adapter-utils'; | ||
import { Responses, SQLPluginResult } from '@xata.io/client'; | ||
import { fieldToColumnType } from './conversion'; | ||
|
||
const debug = Debug('prisma:driver-adapter:xata'); | ||
|
||
type PerformIOResult = Responses.SQLResponse; | ||
|
||
abstract class XataQueryable implements Queryable { | ||
readonly flavour = 'postgres'; | ||
|
||
async queryRaw(query: Query): Promise<Result<ResultSet>> { | ||
const tag = '[js::query_raw]'; | ||
debug(`${tag} %O`, query); | ||
|
||
const res = await this.performIO(query); | ||
|
||
if (!res.ok) { | ||
return err(res.error); | ||
} | ||
|
||
const { records, columns = {}, warning } = res.value; | ||
if (warning) debug(`${tag} %O`, warning); | ||
|
||
const [columnNames, columnTypes] = Object.fromEntries(columns as any).reduce( | ||
([names, types]: [string[], ColumnType[]], [name, { type_name }]: [string, { type_name: string }]) => { | ||
names.push(name); | ||
types.push(fieldToColumnType(type_name)); | ||
return [names, types]; | ||
}, | ||
[[], []] as [string[], ColumnType[]] | ||
); | ||
|
||
return ok({ columnNames, columnTypes, rows: records as any[] }); | ||
} | ||
|
||
async executeRaw(query: Query): Promise<Result<number>> { | ||
const tag = '[js::execute_raw]'; | ||
debug(`${tag} %O`, query); | ||
|
||
return (await this.performIO(query)).map((r) => r.total ?? 0); | ||
} | ||
|
||
abstract performIO(query: Query): Promise<Result<PerformIOResult>>; | ||
} | ||
|
||
type XataClient = { sql: SQLPluginResult }; | ||
|
||
export class PrismaXataHTTP extends XataQueryable implements DriverAdapter { | ||
constructor(private xata: XataClient) { | ||
super(); | ||
} | ||
|
||
override async performIO(query: Query): Promise<Result<PerformIOResult>> { | ||
const { sql, args: values } = query; | ||
return ok(await this.xata.sql(sql, values)); | ||
} | ||
|
||
startTransaction(): Promise<Result<Transaction>> { | ||
return Promise.reject(new Error('Transactions are not supported in HTTP mode')); | ||
} | ||
|
||
async close() { | ||
return ok(undefined); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SQLPlugin, XataPlugin, XataPluginOptions } from '@xata.io/client'; | ||
import { PrismaXataHTTP } from './driver'; | ||
|
||
export class PrismaPlugin extends XataPlugin { | ||
build(pluginOptions: XataPluginOptions) { | ||
const xata = { schema: { tables: pluginOptions.tables }, sql: new SQLPlugin().build(pluginOptions) }; | ||
|
||
return new PrismaXataHTTP(xata, pluginOptions.tables); | ||
} | ||
} | ||
|
||
export * from './driver'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { describe, test } from 'vitest'; | ||
import { PrismaXataHTTP } from '../src/driver'; | ||
import { smokeTest } from './smoke'; | ||
import { BaseClient } from '../../client/src'; | ||
|
||
describe.skip('@xata.io/prisma plugin', () => { | ||
test('run smoke tests', async () => { | ||
const xata = new BaseClient(); | ||
const adapter = new PrismaXataHTTP(xata); | ||
|
||
await smokeTest(adapter); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Improve this