@xata.io/drizzle 0.0.0-alpha.vab97e27be6032a4c2bb926be93ba2d31c905cb75 → 0.0.0-alpha.vabd3a68e52765fc3f9486a19d65a304827b78002

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xata.io/drizzle",
3
- "version": "0.0.0-alpha.vab97e27be6032a4c2bb926be93ba2d31c905cb75",
3
+ "version": "0.0.0-alpha.vabd3a68e52765fc3f9486a19d65a304827b78002",
4
4
  "description": "",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -10,6 +10,11 @@
10
10
  "import": "./dist/index.mjs",
11
11
  "require": "./dist/index.cjs",
12
12
  "types": "./dist/index.d.ts"
13
+ },
14
+ "./pg": {
15
+ "import": "./dist/pg.mjs",
16
+ "require": "./dist/pg.cjs",
17
+ "types": "./dist/pg.d.ts"
13
18
  }
14
19
  },
15
20
  "author": "",
@@ -18,15 +23,15 @@
18
23
  "url": "https://github.com/xataio/client-ts/issues"
19
24
  },
20
25
  "dependencies": {
21
- "pg": "^8.11.3",
22
- "@xata.io/client": "0.0.0-alpha.vab97e27be6032a4c2bb926be93ba2d31c905cb75"
26
+ "@xata.io/client": "0.0.0-alpha.vabd3a68e52765fc3f9486a19d65a304827b78002"
23
27
  },
24
28
  "devDependencies": {
25
- "@types/pg": "^8.11.0",
26
- "drizzle-orm": "^0.29.3"
29
+ "@types/pg": "^8.11.6",
30
+ "drizzle-orm": "^0.31.2",
31
+ "pg": "^8.12.0"
27
32
  },
28
33
  "peerDependencies": {
29
- "drizzle-orm": "^0.28.5"
34
+ "drizzle-orm": "*"
30
35
  },
31
36
  "scripts": {
32
37
  "build": "rimraf dist && rollup -c",
@@ -1,6 +1,6 @@
1
1
  import { BaseClient, HostProvider, parseProviderString, XataApiClient } from '@xata.io/client';
2
2
  import 'dotenv/config';
3
- import { desc, DrizzleError, eq, gt, gte, or, placeholder, sql, TransactionRollbackError } from 'drizzle-orm';
3
+ import { desc, DrizzleError, eq, gt, gte, or, placeholder, sql } from 'drizzle-orm';
4
4
  import { Client } from 'pg';
5
5
  import { afterAll, afterEach, beforeAll, beforeEach, describe, expectTypeOf, test } from 'vitest';
6
6
  import { drizzle as drizzlePg, type XataDatabase } from '../src/pg';
@@ -9,7 +9,7 @@ import * as schema from './schema';
9
9
 
10
10
  const { usersTable, postsTable, commentsTable, usersToGroupsTable, groupsTable } = schema;
11
11
 
12
- const ENABLE_LOGGING = true;
12
+ const ENABLE_LOGGING = false;
13
13
 
14
14
  declare module 'vitest' {
15
15
  export interface TestContext {
@@ -51,25 +51,47 @@ function getDomain(host: HostProvider) {
51
51
  }
52
52
  }
53
53
 
54
- beforeAll(async () => {
55
- await api.database.createDatabase({
56
- workspace,
57
- database,
58
- data: { region, branchName: 'main' },
59
- headers: { 'X-Features': 'feat-pgroll-migrations=1' }
60
- });
54
+ function getDrizzleClient(type: string, branch: string) {
55
+ if (type === 'http') {
56
+ const xata = new BaseClient({
57
+ apiKey,
58
+ host,
59
+ clientName: 'sdk-tests',
60
+ databaseURL: `https://${workspace}.${region}.${getDomain(host)}/db/${database}`,
61
+ branch
62
+ });
61
63
 
62
- await waitForReplication();
64
+ return { db: drizzleHttp(xata, { schema, logger: ENABLE_LOGGING }) };
65
+ } else if (type === 'pg') {
66
+ const client = new Client({
67
+ connectionString: `postgresql://${workspace}:${apiKey}@${region}.sql.${getDomain(
68
+ host
69
+ )}:5432/${database}:${branch}`,
70
+ ssl: true
71
+ });
63
72
 
64
- const client = new Client({
65
- connectionString: `postgresql://${workspace}:${apiKey}@${region}.sql.${getDomain(host)}:5432/${database}:main`
66
- });
73
+ return { db: drizzlePg(client, { schema, logger: ENABLE_LOGGING }), client };
74
+ } else {
75
+ throw new Error(`Unknown type: ${type}`);
76
+ }
77
+ }
78
+
79
+ describe.concurrent.each([{ type: 'pg' }, { type: 'http' }])('Drizzle $type', ({ type }) => {
80
+ beforeAll(async () => {
81
+ await api.databases.createDatabase({
82
+ pathParams: { workspaceId: workspace, dbName: database },
83
+ body: { region, branchName: 'main' },
84
+ headers: { 'X-Features': 'feat-pgroll-migrations=1' }
85
+ });
67
86
 
68
- await client.connect();
69
- const db = drizzlePg(client, { schema, logger: ENABLE_LOGGING });
87
+ await waitForReplication();
70
88
 
71
- await db.execute(
72
- sql`
89
+ // For now, run the migrations via wire protocol
90
+ const { client, db } = getDrizzleClient('pg', 'main');
91
+ await client?.connect();
92
+
93
+ await db.execute(
94
+ sql`
73
95
  CREATE TABLE "users" (
74
96
  "id" serial PRIMARY KEY NOT NULL,
75
97
  "name" text NOT NULL,
@@ -77,27 +99,27 @@ beforeAll(async () => {
77
99
  "invited_by" int REFERENCES "users"("id")
78
100
  );
79
101
  `
80
- );
81
- await db.execute(
82
- sql`
102
+ );
103
+ await db.execute(
104
+ sql`
83
105
  CREATE TABLE IF NOT EXISTS "groups" (
84
106
  "id" serial PRIMARY KEY NOT NULL,
85
107
  "name" text NOT NULL,
86
108
  "description" text
87
109
  );
88
110
  `
89
- );
90
- await db.execute(
91
- sql`
111
+ );
112
+ await db.execute(
113
+ sql`
92
114
  CREATE TABLE IF NOT EXISTS "users_to_groups" (
93
115
  "id" serial PRIMARY KEY NOT NULL,
94
116
  "user_id" int REFERENCES "users"("id"),
95
117
  "group_id" int REFERENCES "groups"("id")
96
118
  );
97
119
  `
98
- );
99
- await db.execute(
100
- sql`
120
+ );
121
+ await db.execute(
122
+ sql`
101
123
  CREATE TABLE IF NOT EXISTS "posts" (
102
124
  "id" serial PRIMARY KEY NOT NULL,
103
125
  "content" text NOT NULL,
@@ -105,9 +127,9 @@ beforeAll(async () => {
105
127
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
106
128
  );
107
129
  `
108
- );
109
- await db.execute(
110
- sql`
130
+ );
131
+ await db.execute(
132
+ sql`
111
133
  CREATE TABLE IF NOT EXISTS "comments" (
112
134
  "id" serial PRIMARY KEY NOT NULL,
113
135
  "content" text NOT NULL,
@@ -116,9 +138,9 @@ beforeAll(async () => {
116
138
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
117
139
  );
118
140
  `
119
- );
120
- await db.execute(
121
- sql`
141
+ );
142
+ await db.execute(
143
+ sql`
122
144
  CREATE TABLE IF NOT EXISTS "comment_likes" (
123
145
  "id" serial PRIMARY KEY NOT NULL,
124
146
  "creator" int REFERENCES "users"("id"),
@@ -126,47 +148,32 @@ beforeAll(async () => {
126
148
  "created_at" timestamp with time zone DEFAULT now() NOT NULL
127
149
  );
128
150
  `
129
- );
151
+ );
130
152
 
131
- await client.end();
132
- });
153
+ await client?.end();
154
+ });
133
155
 
134
- afterAll(async () => {
135
- await api.database.deleteDatabase({ workspace, database });
136
- });
156
+ afterAll(async () => {
157
+ await api.databases.deleteDatabase({ pathParams: { workspaceId: workspace, dbName: database } });
158
+ });
137
159
 
138
- describe.concurrent.each([{ type: 'pg' } /**{ type: 'http' }**/])('Drizzle $type', ({ type }) => {
139
160
  beforeEach(async (ctx) => {
140
161
  ctx.branch = `test-${Math.random().toString(36).substring(7)}`;
141
- await api.branches.createBranch({ workspace, database, region, branch: ctx.branch, from: 'main' });
142
-
143
- if (type === 'http') {
144
- const xata = new BaseClient({
145
- apiKey,
146
- host,
147
- clientName: 'sdk-tests',
148
- databaseURL: `https://${workspace}.${region}.${getDomain(host)}/db/${database}`,
149
- branch: ctx.branch
150
- });
151
-
152
- ctx.db = drizzleHttp(xata, { schema, logger: ENABLE_LOGGING });
153
- } else if (type === 'pg') {
154
- ctx.client = new Client({
155
- connectionString: `postgresql://${workspace}:${apiKey}@${region}.sql.${getDomain(host)}:5432/${database}:${
156
- ctx.branch
157
- }`
158
- });
159
-
160
- await ctx.client.connect();
161
- ctx.db = drizzlePg(ctx.client, { schema, logger: ENABLE_LOGGING });
162
- } else {
163
- throw new Error(`Unknown type: ${type}`);
164
- }
162
+ await api.branch.createBranch({
163
+ pathParams: { workspace, region, dbBranchName: `${database}:${ctx.branch}` },
164
+ body: { from: 'main' }
165
+ });
166
+
167
+ const { db, client } = getDrizzleClient(type, ctx.branch);
168
+ await client?.connect();
169
+
170
+ ctx.db = db;
171
+ ctx.client = client;
165
172
  });
166
173
 
167
174
  afterEach(async (ctx) => {
168
175
  await ctx.client?.end();
169
- await api.branches.deleteBranch({ workspace, database, region, branch: ctx.branch });
176
+ await api.branch.deleteBranch({ pathParams: { workspace, region, dbBranchName: `${database}:${ctx.branch}` } });
170
177
  });
171
178
 
172
179
  /*
@@ -886,133 +893,6 @@ describe.concurrent.each([{ type: 'pg' } /**{ type: 'http' }**/])('Drizzle $type
886
893
  });
887
894
  });
888
895
 
889
- test('[Find Many] Get users with posts in transaction', async (ctx) => {
890
- let usersWithPosts: {
891
- id: number;
892
- name: string;
893
- verified: boolean;
894
- invitedBy: number | null;
895
- posts: {
896
- id: number;
897
- content: string;
898
- ownerId: number | null;
899
- createdAt: Date;
900
- }[];
901
- }[] = [];
902
-
903
- await ctx.db.transaction(async (tx) => {
904
- await tx.insert(usersTable).values([
905
- { id: 1, name: 'Dan' },
906
- { id: 2, name: 'Andrew' },
907
- { id: 3, name: 'Alex' }
908
- ]);
909
-
910
- await tx.insert(postsTable).values([
911
- { ownerId: 1, content: 'Post1' },
912
- { ownerId: 1, content: 'Post1.1' },
913
- { ownerId: 2, content: 'Post2' },
914
- { ownerId: 3, content: 'Post3' }
915
- ]);
916
-
917
- usersWithPosts = await tx.query.usersTable.findMany({
918
- where: ({ id }, { eq }) => eq(id, 1),
919
- with: {
920
- posts: {
921
- where: ({ id }, { eq }) => eq(id, 1)
922
- }
923
- }
924
- });
925
- });
926
-
927
- expectTypeOf(usersWithPosts).toEqualTypeOf<
928
- {
929
- id: number;
930
- name: string;
931
- verified: boolean;
932
- invitedBy: number | null;
933
- posts: {
934
- id: number;
935
- content: string;
936
- ownerId: number | null;
937
- createdAt: Date;
938
- }[];
939
- }[]
940
- >();
941
-
942
- ctx.expect(usersWithPosts.length).eq(1);
943
- ctx.expect(usersWithPosts[0]?.posts.length).eq(1);
944
-
945
- ctx.expect(usersWithPosts[0]).toEqual({
946
- id: 1,
947
- name: 'Dan',
948
- verified: false,
949
- invitedBy: null,
950
- posts: [{ id: 1, ownerId: 1, content: 'Post1', createdAt: usersWithPosts[0]?.posts[0]?.createdAt }]
951
- });
952
- });
953
-
954
- test('[Find Many] Get users with posts in rollbacked transaction', async (ctx) => {
955
- let usersWithPosts: {
956
- id: number;
957
- name: string;
958
- verified: boolean;
959
- invitedBy: number | null;
960
- posts: {
961
- id: number;
962
- content: string;
963
- ownerId: number | null;
964
- createdAt: Date;
965
- }[];
966
- }[] = [];
967
-
968
- await ctx
969
- .expect(
970
- ctx.db.transaction(async (tx) => {
971
- await tx.insert(usersTable).values([
972
- { id: 1, name: 'Dan' },
973
- { id: 2, name: 'Andrew' },
974
- { id: 3, name: 'Alex' }
975
- ]);
976
-
977
- await tx.insert(postsTable).values([
978
- { ownerId: 1, content: 'Post1' },
979
- { ownerId: 1, content: 'Post1.1' },
980
- { ownerId: 2, content: 'Post2' },
981
- { ownerId: 3, content: 'Post3' }
982
- ]);
983
-
984
- tx.rollback();
985
-
986
- usersWithPosts = await tx.query.usersTable.findMany({
987
- where: ({ id }, { eq }) => eq(id, 1),
988
- with: {
989
- posts: {
990
- where: ({ id }, { eq }) => eq(id, 1)
991
- }
992
- }
993
- });
994
- })
995
- )
996
- .rejects.toThrowError(new TransactionRollbackError());
997
-
998
- expectTypeOf(usersWithPosts).toEqualTypeOf<
999
- {
1000
- id: number;
1001
- name: string;
1002
- verified: boolean;
1003
- invitedBy: number | null;
1004
- posts: {
1005
- id: number;
1006
- content: string;
1007
- ownerId: number | null;
1008
- createdAt: Date;
1009
- }[];
1010
- }[]
1011
- >();
1012
-
1013
- ctx.expect(usersWithPosts.length).eq(0);
1014
- });
1015
-
1016
896
  // select only custom
1017
897
  test('[Find Many] Get only custom fields', async (ctx) => {
1018
898
  await ctx.db.insert(usersTable).values([
@@ -6407,7 +6287,7 @@ describe.concurrent.each([{ type: 'pg' } /**{ type: 'http' }**/])('Drizzle $type
6407
6287
  async function waitForReplication(): Promise<void> {
6408
6288
  try {
6409
6289
  await new Promise((resolve) => setTimeout(resolve, 2000));
6410
- await api.branches.getBranchList({ workspace, database, region });
6290
+ await api.branch.getBranchList({ pathParams: { workspace, dbName: database, region } });
6411
6291
  } catch (error) {
6412
6292
  console.log(`Replication not ready yet, retrying...`);
6413
6293
  return await waitForReplication();