@xata.io/drizzle 0.0.0-alpha.vcb602e5 → 0.0.0-alpha.vcb803592081f059abd106ee274c620282ecfd74c

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/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
+ }));