|
| 1 | +import {relations} from 'drizzle-orm' |
| 2 | +import {integer, sqliteTable, text} from 'drizzle-orm/sqlite-core' |
| 3 | + |
| 4 | +export const tagColors = [ |
| 5 | + 'red', |
| 6 | + 'green', |
| 7 | + 'blue', |
| 8 | + 'yellow', |
| 9 | + 'gray', |
| 10 | + 'orange', |
| 11 | + 'purple', |
| 12 | + 'pink', |
| 13 | +] as const |
| 14 | + |
| 15 | +export const tweet = sqliteTable('tweet', { |
| 16 | + id: integer('id', {mode: 'number'}) |
| 17 | + .primaryKey({autoIncrement: true}) |
| 18 | + .notNull(), |
| 19 | + description: text('description'), |
| 20 | + url: text('url', {length: 2083}).notNull(), |
| 21 | + createdAt: integer('created_at', {mode: 'timestamp'}).notNull(), |
| 22 | + userId: text('user_id', {length: 100}).notNull(), |
| 23 | +}) |
| 24 | + |
| 25 | +export const tweetRelations = relations(tweet, ({many}) => ({ |
| 26 | + tweetsToTags: many(tweetsToTags), |
| 27 | +})) |
| 28 | + |
| 29 | +export const tag = sqliteTable('tag', { |
| 30 | + id: integer('id', {mode: 'number'}) |
| 31 | + .primaryKey({autoIncrement: true}) |
| 32 | + .notNull(), |
| 33 | + name: text('name', {length: 255}).notNull(), |
| 34 | + color: text('color', {enum: tagColors}).notNull(), |
| 35 | + userId: text('user_id', {length: 100}).notNull(), |
| 36 | +}) |
| 37 | + |
| 38 | +export const tagsRelations = relations(tag, ({many}) => ({ |
| 39 | + tweetsToTags: many(tweetsToTags), |
| 40 | +})) |
| 41 | + |
| 42 | +export const tweetsToTags = sqliteTable('tweet_tag', { |
| 43 | + tweetId: integer('tweet_id', {mode: 'number'}) |
| 44 | + .notNull() |
| 45 | + .references(() => tweet.id), |
| 46 | + tagId: integer('tag_id', {mode: 'number'}) |
| 47 | + .notNull() |
| 48 | + .references(() => tag.id), |
| 49 | +}) |
| 50 | + |
| 51 | +export const tweetWithTagsRelations = relations(tweetsToTags, ({one}) => ({ |
| 52 | + tweet: one(tweet, { |
| 53 | + fields: [tweetsToTags.tweetId], |
| 54 | + references: [tweet.id], |
| 55 | + }), |
| 56 | + tag: one(tag, { |
| 57 | + fields: [tweetsToTags.tagId], |
| 58 | + references: [tag.id], |
| 59 | + }), |
| 60 | +})) |
0 commit comments