@xata.io/drizzle 0.0.0-alpha.vf501d195ec7907e696637eff52ae02941341dca1 → 0.0.0-alpha.vf58a3c229cee57bd4c4c22a72c3826618cdc9b94

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.
@@ -0,0 +1,89 @@
1
+ CREATE TABLE IF NOT EXISTS "comment_likes" (
2
+ "id" serial PRIMARY KEY NOT NULL,
3
+ "creator" integer,
4
+ "comment_id" integer,
5
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
6
+ );
7
+ --> statement-breakpoint
8
+ CREATE TABLE IF NOT EXISTS "comments" (
9
+ "id" serial PRIMARY KEY NOT NULL,
10
+ "content" text NOT NULL,
11
+ "creator" integer,
12
+ "post_id" integer,
13
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
14
+ );
15
+ --> statement-breakpoint
16
+ CREATE TABLE IF NOT EXISTS "groups" (
17
+ "id" serial PRIMARY KEY NOT NULL,
18
+ "name" text NOT NULL,
19
+ "description" text
20
+ );
21
+ --> statement-breakpoint
22
+ CREATE TABLE IF NOT EXISTS "posts" (
23
+ "id" serial PRIMARY KEY NOT NULL,
24
+ "content" text NOT NULL,
25
+ "owner_id" integer,
26
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
27
+ );
28
+ --> statement-breakpoint
29
+ CREATE TABLE IF NOT EXISTS "users" (
30
+ "id" serial PRIMARY KEY NOT NULL,
31
+ "name" text NOT NULL,
32
+ "verified" boolean DEFAULT false NOT NULL,
33
+ "invited_by" integer
34
+ );
35
+ --> statement-breakpoint
36
+ CREATE TABLE IF NOT EXISTS "users_to_groups" (
37
+ "id" serial PRIMARY KEY NOT NULL,
38
+ "user_id" integer NOT NULL,
39
+ "group_id" integer NOT NULL,
40
+ CONSTRAINT "users_to_groups_group_id_user_id_pk" PRIMARY KEY("group_id","user_id")
41
+ );
42
+ --> statement-breakpoint
43
+ DO $$ BEGIN
44
+ ALTER TABLE "comment_likes" ADD CONSTRAINT "comment_likes_creator_users_id_fk" FOREIGN KEY ("creator") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
45
+ EXCEPTION
46
+ WHEN duplicate_object THEN null;
47
+ END $$;
48
+ --> statement-breakpoint
49
+ DO $$ BEGIN
50
+ ALTER TABLE "comment_likes" ADD CONSTRAINT "comment_likes_comment_id_comments_id_fk" FOREIGN KEY ("comment_id") REFERENCES "comments"("id") ON DELETE no action ON UPDATE no action;
51
+ EXCEPTION
52
+ WHEN duplicate_object THEN null;
53
+ END $$;
54
+ --> statement-breakpoint
55
+ DO $$ BEGIN
56
+ ALTER TABLE "comments" ADD CONSTRAINT "comments_creator_users_id_fk" FOREIGN KEY ("creator") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
57
+ EXCEPTION
58
+ WHEN duplicate_object THEN null;
59
+ END $$;
60
+ --> statement-breakpoint
61
+ DO $$ BEGIN
62
+ ALTER TABLE "comments" ADD CONSTRAINT "comments_post_id_posts_id_fk" FOREIGN KEY ("post_id") REFERENCES "posts"("id") ON DELETE no action ON UPDATE no action;
63
+ EXCEPTION
64
+ WHEN duplicate_object THEN null;
65
+ END $$;
66
+ --> statement-breakpoint
67
+ DO $$ BEGIN
68
+ ALTER TABLE "posts" ADD CONSTRAINT "posts_owner_id_users_id_fk" FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
69
+ EXCEPTION
70
+ WHEN duplicate_object THEN null;
71
+ END $$;
72
+ --> statement-breakpoint
73
+ DO $$ BEGIN
74
+ ALTER TABLE "users" ADD CONSTRAINT "users_invited_by_users_id_fk" FOREIGN KEY ("invited_by") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
75
+ EXCEPTION
76
+ WHEN duplicate_object THEN null;
77
+ END $$;
78
+ --> statement-breakpoint
79
+ DO $$ BEGIN
80
+ ALTER TABLE "users_to_groups" ADD CONSTRAINT "users_to_groups_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
81
+ EXCEPTION
82
+ WHEN duplicate_object THEN null;
83
+ END $$;
84
+ --> statement-breakpoint
85
+ DO $$ BEGIN
86
+ ALTER TABLE "users_to_groups" ADD CONSTRAINT "users_to_groups_group_id_groups_id_fk" FOREIGN KEY ("group_id") REFERENCES "groups"("id") ON DELETE no action ON UPDATE no action;
87
+ EXCEPTION
88
+ WHEN duplicate_object THEN null;
89
+ END $$;
@@ -0,0 +1,299 @@
1
+ {
2
+ "id": "6d0ce1bb-56fc-4be8-b007-e5fdcf4ddc91",
3
+ "prevId": "00000000-0000-0000-0000-000000000000",
4
+ "version": "5",
5
+ "dialect": "pg",
6
+ "tables": {
7
+ "comment_likes": {
8
+ "name": "comment_likes",
9
+ "schema": "",
10
+ "columns": {
11
+ "id": {
12
+ "name": "id",
13
+ "type": "serial",
14
+ "primaryKey": true,
15
+ "notNull": true
16
+ },
17
+ "creator": {
18
+ "name": "creator",
19
+ "type": "integer",
20
+ "primaryKey": false,
21
+ "notNull": false
22
+ },
23
+ "comment_id": {
24
+ "name": "comment_id",
25
+ "type": "integer",
26
+ "primaryKey": false,
27
+ "notNull": false
28
+ },
29
+ "created_at": {
30
+ "name": "created_at",
31
+ "type": "timestamp with time zone",
32
+ "primaryKey": false,
33
+ "notNull": true,
34
+ "default": "now()"
35
+ }
36
+ },
37
+ "indexes": {},
38
+ "foreignKeys": {
39
+ "comment_likes_creator_users_id_fk": {
40
+ "name": "comment_likes_creator_users_id_fk",
41
+ "tableFrom": "comment_likes",
42
+ "tableTo": "users",
43
+ "columnsFrom": ["creator"],
44
+ "columnsTo": ["id"],
45
+ "onDelete": "no action",
46
+ "onUpdate": "no action"
47
+ },
48
+ "comment_likes_comment_id_comments_id_fk": {
49
+ "name": "comment_likes_comment_id_comments_id_fk",
50
+ "tableFrom": "comment_likes",
51
+ "tableTo": "comments",
52
+ "columnsFrom": ["comment_id"],
53
+ "columnsTo": ["id"],
54
+ "onDelete": "no action",
55
+ "onUpdate": "no action"
56
+ }
57
+ },
58
+ "compositePrimaryKeys": {},
59
+ "uniqueConstraints": {}
60
+ },
61
+ "comments": {
62
+ "name": "comments",
63
+ "schema": "",
64
+ "columns": {
65
+ "id": {
66
+ "name": "id",
67
+ "type": "serial",
68
+ "primaryKey": true,
69
+ "notNull": true
70
+ },
71
+ "content": {
72
+ "name": "content",
73
+ "type": "text",
74
+ "primaryKey": false,
75
+ "notNull": true
76
+ },
77
+ "creator": {
78
+ "name": "creator",
79
+ "type": "integer",
80
+ "primaryKey": false,
81
+ "notNull": false
82
+ },
83
+ "post_id": {
84
+ "name": "post_id",
85
+ "type": "integer",
86
+ "primaryKey": false,
87
+ "notNull": false
88
+ },
89
+ "created_at": {
90
+ "name": "created_at",
91
+ "type": "timestamp with time zone",
92
+ "primaryKey": false,
93
+ "notNull": true,
94
+ "default": "now()"
95
+ }
96
+ },
97
+ "indexes": {},
98
+ "foreignKeys": {
99
+ "comments_creator_users_id_fk": {
100
+ "name": "comments_creator_users_id_fk",
101
+ "tableFrom": "comments",
102
+ "tableTo": "users",
103
+ "columnsFrom": ["creator"],
104
+ "columnsTo": ["id"],
105
+ "onDelete": "no action",
106
+ "onUpdate": "no action"
107
+ },
108
+ "comments_post_id_posts_id_fk": {
109
+ "name": "comments_post_id_posts_id_fk",
110
+ "tableFrom": "comments",
111
+ "tableTo": "posts",
112
+ "columnsFrom": ["post_id"],
113
+ "columnsTo": ["id"],
114
+ "onDelete": "no action",
115
+ "onUpdate": "no action"
116
+ }
117
+ },
118
+ "compositePrimaryKeys": {},
119
+ "uniqueConstraints": {}
120
+ },
121
+ "groups": {
122
+ "name": "groups",
123
+ "schema": "",
124
+ "columns": {
125
+ "id": {
126
+ "name": "id",
127
+ "type": "serial",
128
+ "primaryKey": true,
129
+ "notNull": true
130
+ },
131
+ "name": {
132
+ "name": "name",
133
+ "type": "text",
134
+ "primaryKey": false,
135
+ "notNull": true
136
+ },
137
+ "description": {
138
+ "name": "description",
139
+ "type": "text",
140
+ "primaryKey": false,
141
+ "notNull": false
142
+ }
143
+ },
144
+ "indexes": {},
145
+ "foreignKeys": {},
146
+ "compositePrimaryKeys": {},
147
+ "uniqueConstraints": {}
148
+ },
149
+ "posts": {
150
+ "name": "posts",
151
+ "schema": "",
152
+ "columns": {
153
+ "id": {
154
+ "name": "id",
155
+ "type": "serial",
156
+ "primaryKey": true,
157
+ "notNull": true
158
+ },
159
+ "content": {
160
+ "name": "content",
161
+ "type": "text",
162
+ "primaryKey": false,
163
+ "notNull": true
164
+ },
165
+ "owner_id": {
166
+ "name": "owner_id",
167
+ "type": "integer",
168
+ "primaryKey": false,
169
+ "notNull": false
170
+ },
171
+ "created_at": {
172
+ "name": "created_at",
173
+ "type": "timestamp with time zone",
174
+ "primaryKey": false,
175
+ "notNull": true,
176
+ "default": "now()"
177
+ }
178
+ },
179
+ "indexes": {},
180
+ "foreignKeys": {
181
+ "posts_owner_id_users_id_fk": {
182
+ "name": "posts_owner_id_users_id_fk",
183
+ "tableFrom": "posts",
184
+ "tableTo": "users",
185
+ "columnsFrom": ["owner_id"],
186
+ "columnsTo": ["id"],
187
+ "onDelete": "no action",
188
+ "onUpdate": "no action"
189
+ }
190
+ },
191
+ "compositePrimaryKeys": {},
192
+ "uniqueConstraints": {}
193
+ },
194
+ "users": {
195
+ "name": "users",
196
+ "schema": "",
197
+ "columns": {
198
+ "id": {
199
+ "name": "id",
200
+ "type": "serial",
201
+ "primaryKey": true,
202
+ "notNull": true
203
+ },
204
+ "name": {
205
+ "name": "name",
206
+ "type": "text",
207
+ "primaryKey": false,
208
+ "notNull": true
209
+ },
210
+ "verified": {
211
+ "name": "verified",
212
+ "type": "boolean",
213
+ "primaryKey": false,
214
+ "notNull": true,
215
+ "default": false
216
+ },
217
+ "invited_by": {
218
+ "name": "invited_by",
219
+ "type": "integer",
220
+ "primaryKey": false,
221
+ "notNull": false
222
+ }
223
+ },
224
+ "indexes": {},
225
+ "foreignKeys": {
226
+ "users_invited_by_users_id_fk": {
227
+ "name": "users_invited_by_users_id_fk",
228
+ "tableFrom": "users",
229
+ "tableTo": "users",
230
+ "columnsFrom": ["invited_by"],
231
+ "columnsTo": ["id"],
232
+ "onDelete": "no action",
233
+ "onUpdate": "no action"
234
+ }
235
+ },
236
+ "compositePrimaryKeys": {},
237
+ "uniqueConstraints": {}
238
+ },
239
+ "users_to_groups": {
240
+ "name": "users_to_groups",
241
+ "schema": "",
242
+ "columns": {
243
+ "id": {
244
+ "name": "id",
245
+ "type": "serial",
246
+ "primaryKey": true,
247
+ "notNull": true
248
+ },
249
+ "user_id": {
250
+ "name": "user_id",
251
+ "type": "integer",
252
+ "primaryKey": false,
253
+ "notNull": true
254
+ },
255
+ "group_id": {
256
+ "name": "group_id",
257
+ "type": "integer",
258
+ "primaryKey": false,
259
+ "notNull": true
260
+ }
261
+ },
262
+ "indexes": {},
263
+ "foreignKeys": {
264
+ "users_to_groups_user_id_users_id_fk": {
265
+ "name": "users_to_groups_user_id_users_id_fk",
266
+ "tableFrom": "users_to_groups",
267
+ "tableTo": "users",
268
+ "columnsFrom": ["user_id"],
269
+ "columnsTo": ["id"],
270
+ "onDelete": "no action",
271
+ "onUpdate": "no action"
272
+ },
273
+ "users_to_groups_group_id_groups_id_fk": {
274
+ "name": "users_to_groups_group_id_groups_id_fk",
275
+ "tableFrom": "users_to_groups",
276
+ "tableTo": "groups",
277
+ "columnsFrom": ["group_id"],
278
+ "columnsTo": ["id"],
279
+ "onDelete": "no action",
280
+ "onUpdate": "no action"
281
+ }
282
+ },
283
+ "compositePrimaryKeys": {
284
+ "users_to_groups_group_id_user_id_pk": {
285
+ "name": "users_to_groups_group_id_user_id_pk",
286
+ "columns": ["group_id", "user_id"]
287
+ }
288
+ },
289
+ "uniqueConstraints": {}
290
+ }
291
+ },
292
+ "enums": {},
293
+ "schemas": {},
294
+ "_meta": {
295
+ "columns": {},
296
+ "schemas": {},
297
+ "tables": {}
298
+ }
299
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "version": "5",
3
+ "dialect": "pg",
4
+ "entries": [
5
+ {
6
+ "idx": 0,
7
+ "version": "5",
8
+ "when": 1707376384737,
9
+ "tag": "0000_clumsy_white_queen",
10
+ "breakpoints": true
11
+ }
12
+ ]
13
+ }
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
+ }));