@xata.io/drizzle 0.0.0-alpha.vd6818b42285037f707a613bf0c3235b056223e62 → 0.0.0-alpha.vd6e1fe5a7d12401a419a8a1c53b3377ad595ef5c
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/.turbo/turbo-build.log +14 -3
- package/CHANGELOG.md +41 -3
- package/dist/index.cjs +54 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +22 -13
- package/dist/index.mjs +55 -34
- package/dist/index.mjs.map +1 -1
- package/dist/pg.cjs +215 -0
- package/dist/pg.cjs.map +1 -0
- package/dist/pg.d.ts +61 -0
- package/dist/pg.mjs +209 -0
- package/dist/pg.mjs.map +1 -0
- package/package.json +11 -6
- package/test/drizzle.test.ts +6295 -0
- package/test/schema.ts +85 -0
- package/test/pg.test.ts +0 -145
package/test/schema.ts
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
import { boolean, integer, type PgColumn, pgTable, primaryKey, serial, text, timestamp } from 'drizzle-orm/pg-core';
|
2
|
+
|
3
|
+
import { relations } from 'drizzle-orm';
|
4
|
+
|
5
|
+
export const usersTable = pgTable('users', {
|
6
|
+
id: serial('id').primaryKey(),
|
7
|
+
name: text('name').notNull(),
|
8
|
+
verified: boolean('verified').notNull().default(false),
|
9
|
+
invitedBy: integer('invited_by').references((): PgColumn => usersTable.id)
|
10
|
+
});
|
11
|
+
|
12
|
+
export const usersConfig = relations(usersTable, ({ one, many }) => ({
|
13
|
+
invitee: one(usersTable, { fields: [usersTable.invitedBy], references: [usersTable.id] }),
|
14
|
+
usersToGroups: many(usersToGroupsTable),
|
15
|
+
posts: many(postsTable)
|
16
|
+
}));
|
17
|
+
|
18
|
+
export const groupsTable = pgTable('groups', {
|
19
|
+
id: serial('id').primaryKey(),
|
20
|
+
name: text('name').notNull(),
|
21
|
+
description: text('description')
|
22
|
+
});
|
23
|
+
|
24
|
+
export const groupsConfig = relations(groupsTable, ({ many }) => ({
|
25
|
+
usersToGroups: many(usersToGroupsTable)
|
26
|
+
}));
|
27
|
+
|
28
|
+
export const usersToGroupsTable = pgTable(
|
29
|
+
'users_to_groups',
|
30
|
+
{
|
31
|
+
id: serial('id').primaryKey(),
|
32
|
+
userId: integer('user_id')
|
33
|
+
.notNull()
|
34
|
+
.references(() => usersTable.id),
|
35
|
+
groupId: integer('group_id')
|
36
|
+
.notNull()
|
37
|
+
.references(() => groupsTable.id)
|
38
|
+
},
|
39
|
+
(t) => ({
|
40
|
+
pk: primaryKey(t.groupId, t.userId)
|
41
|
+
})
|
42
|
+
);
|
43
|
+
|
44
|
+
export const usersToGroupsConfig = relations(usersToGroupsTable, ({ one }) => ({
|
45
|
+
group: one(groupsTable, { fields: [usersToGroupsTable.groupId], references: [groupsTable.id] }),
|
46
|
+
user: one(usersTable, { fields: [usersToGroupsTable.userId], references: [usersTable.id] })
|
47
|
+
}));
|
48
|
+
|
49
|
+
export const postsTable = pgTable('posts', {
|
50
|
+
id: serial('id').primaryKey(),
|
51
|
+
content: text('content').notNull(),
|
52
|
+
ownerId: integer('owner_id').references(() => usersTable.id),
|
53
|
+
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow()
|
54
|
+
});
|
55
|
+
|
56
|
+
export const postsConfig = relations(postsTable, ({ one, many }) => ({
|
57
|
+
author: one(usersTable, { fields: [postsTable.ownerId], references: [usersTable.id] }),
|
58
|
+
comments: many(commentsTable)
|
59
|
+
}));
|
60
|
+
|
61
|
+
export const commentsTable = pgTable('comments', {
|
62
|
+
id: serial('id').primaryKey(),
|
63
|
+
content: text('content').notNull(),
|
64
|
+
creator: integer('creator').references(() => usersTable.id),
|
65
|
+
postId: integer('post_id').references(() => postsTable.id),
|
66
|
+
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow()
|
67
|
+
});
|
68
|
+
|
69
|
+
export const commentsConfig = relations(commentsTable, ({ one, many }) => ({
|
70
|
+
post: one(postsTable, { fields: [commentsTable.postId], references: [postsTable.id] }),
|
71
|
+
author: one(usersTable, { fields: [commentsTable.creator], references: [usersTable.id] }),
|
72
|
+
likes: many(commentLikesTable)
|
73
|
+
}));
|
74
|
+
|
75
|
+
export const commentLikesTable = pgTable('comment_likes', {
|
76
|
+
id: serial('id').primaryKey(),
|
77
|
+
creator: integer('creator').references(() => usersTable.id),
|
78
|
+
commentId: integer('comment_id').references(() => commentsTable.id),
|
79
|
+
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow()
|
80
|
+
});
|
81
|
+
|
82
|
+
export const commentLikesConfig = relations(commentLikesTable, ({ one }) => ({
|
83
|
+
comment: one(commentsTable, { fields: [commentLikesTable.commentId], references: [commentsTable.id] }),
|
84
|
+
author: one(usersTable, { fields: [commentLikesTable.creator], references: [usersTable.id] })
|
85
|
+
}));
|
package/test/pg.test.ts
DELETED
@@ -1,145 +0,0 @@
|
|
1
|
-
import { sql } from 'drizzle-orm';
|
2
|
-
import { XataDatabase, drizzle } from '../src/pg';
|
3
|
-
import { boolean, jsonb, pgTable, serial, text, timestamp } from 'drizzle-orm/pg-core';
|
4
|
-
import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'vitest';
|
5
|
-
import { XataApiClient } from '../../client/dist';
|
6
|
-
import { Client } from 'pg';
|
7
|
-
|
8
|
-
pgTable('users', {
|
9
|
-
id: serial('id').primaryKey(),
|
10
|
-
name: text('name').notNull(),
|
11
|
-
verified: boolean('verified').notNull().default(false),
|
12
|
-
jsonb: jsonb('jsonb').$type<string[]>(),
|
13
|
-
createdAt: timestamp('created_at', { withTimezone: true }).notNull().defaultNow()
|
14
|
-
});
|
15
|
-
|
16
|
-
const api = new XataApiClient({
|
17
|
-
apiKey: process.env['XATA_API_KEY'],
|
18
|
-
host: 'staging'
|
19
|
-
});
|
20
|
-
|
21
|
-
const workspace = (process.env['XATA_WORKSPACE'] ?? '')?.split('-').pop() ?? '';
|
22
|
-
const database = `drizzle-test-${Math.random().toString(36).substr(2, 9)}`;
|
23
|
-
const region = 'eu-west-1';
|
24
|
-
const branch = 'main';
|
25
|
-
|
26
|
-
let db: XataDatabase<Record<string, never>>;
|
27
|
-
|
28
|
-
beforeAll(async () => {
|
29
|
-
await api.database.createDatabase({
|
30
|
-
workspace,
|
31
|
-
database,
|
32
|
-
data: { region },
|
33
|
-
headers: { 'X-Features': 'feat-pgroll-migrations=1' }
|
34
|
-
});
|
35
|
-
|
36
|
-
console.log('Created branch', branch, ' in database', database);
|
37
|
-
|
38
|
-
const client = new Client({
|
39
|
-
connectionString: `postgresql://${workspace}:${process.env['XATA_API_KEY']}@${region}.sql.staging-xata.dev:5432/${database}:${branch}`
|
40
|
-
});
|
41
|
-
|
42
|
-
db = drizzle(client);
|
43
|
-
|
44
|
-
await db.execute(
|
45
|
-
sql`
|
46
|
-
create table users (
|
47
|
-
id serial primary key,
|
48
|
-
name text not null,
|
49
|
-
verified boolean not null default false,
|
50
|
-
jsonb jsonb,
|
51
|
-
created_at timestamptz not null default now()
|
52
|
-
)
|
53
|
-
`
|
54
|
-
);
|
55
|
-
await db.execute(
|
56
|
-
sql`
|
57
|
-
create table cities (
|
58
|
-
id serial primary key,
|
59
|
-
name text not null,
|
60
|
-
state char(2)
|
61
|
-
)
|
62
|
-
`
|
63
|
-
);
|
64
|
-
await db.execute(
|
65
|
-
sql`
|
66
|
-
create table users2 (
|
67
|
-
id serial primary key,
|
68
|
-
name text not null,
|
69
|
-
city_id integer references cities(id)
|
70
|
-
)
|
71
|
-
`
|
72
|
-
);
|
73
|
-
await db.execute(
|
74
|
-
sql`
|
75
|
-
create table course_categories (
|
76
|
-
id serial primary key,
|
77
|
-
name text not null
|
78
|
-
)
|
79
|
-
`
|
80
|
-
);
|
81
|
-
await db.execute(
|
82
|
-
sql`
|
83
|
-
create table courses (
|
84
|
-
id serial primary key,
|
85
|
-
name text not null,
|
86
|
-
category_id integer references course_categories(id)
|
87
|
-
)
|
88
|
-
`
|
89
|
-
);
|
90
|
-
await db.execute(
|
91
|
-
sql`
|
92
|
-
create table orders (
|
93
|
-
id serial primary key,
|
94
|
-
region text not null,
|
95
|
-
product text not null,
|
96
|
-
amount integer not null,
|
97
|
-
quantity integer not null
|
98
|
-
)
|
99
|
-
`
|
100
|
-
);
|
101
|
-
await db.execute(
|
102
|
-
sql`
|
103
|
-
create table network_table (
|
104
|
-
inet inet not null,
|
105
|
-
cidr cidr not null,
|
106
|
-
macaddr macaddr not null,
|
107
|
-
macaddr8 macaddr8 not null
|
108
|
-
)
|
109
|
-
`
|
110
|
-
);
|
111
|
-
await db.execute(
|
112
|
-
sql`
|
113
|
-
create table sal_emp (
|
114
|
-
name text not null,
|
115
|
-
pay_by_quarter integer[] not null,
|
116
|
-
schedule text[][] not null
|
117
|
-
)
|
118
|
-
`
|
119
|
-
);
|
120
|
-
await db.execute(
|
121
|
-
sql`
|
122
|
-
create table tictactoe (
|
123
|
-
squares integer[3][3] not null
|
124
|
-
)
|
125
|
-
`
|
126
|
-
);
|
127
|
-
});
|
128
|
-
|
129
|
-
afterAll(async () => {
|
130
|
-
//await api.database.deleteDatabase({ workspace, database });
|
131
|
-
});
|
132
|
-
|
133
|
-
beforeEach(async () => {
|
134
|
-
const { schema } = await api.migrations.getSchema({ workspace, region, database, branch });
|
135
|
-
for (const table of Object.keys(schema.tables)) {
|
136
|
-
await db.execute(sql`delete from ${table}`);
|
137
|
-
}
|
138
|
-
});
|
139
|
-
|
140
|
-
describe.sequential('Drizzle', () => {
|
141
|
-
test('Create table', async () => {
|
142
|
-
const { schema } = await api.migrations.getSchema({ workspace, region, database, branch });
|
143
|
-
expect(Object.keys(schema.tables)).toEqual(['users']);
|
144
|
-
});
|
145
|
-
});
|