Skip to content

Commit

Permalink
chore: Add userEquipment table to database schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Perdolique committed Jul 27, 2024
1 parent 0175fa7 commit d0281ab
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 1 deletion.
8 changes: 8 additions & 0 deletions server/database/migrations/0005_chunky_speed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE `userEquipment` (
`userId` text NOT NULL,
`equipmentId` text NOT NULL,
`createdAt` integer DEFAULT (unixepoch()) NOT NULL,
PRIMARY KEY(`equipmentId`, `userId`),
FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON UPDATE cascade ON DELETE cascade,
FOREIGN KEY (`equipmentId`) REFERENCES `equipment`(`id`) ON UPDATE cascade ON DELETE cascade
);
160 changes: 160 additions & 0 deletions server/database/migrations/meta/0005_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
{
"version": "6",
"dialect": "sqlite",
"id": "94c132e8-597a-47d4-a863-4b967ff2737e",
"prevId": "8058baa2-8347-4b2d-8960-30bc2ced3ede",
"tables": {
"equipment": {
"name": "equipment",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"weight": {
"name": "weight",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"userEquipment": {
"name": "userEquipment",
"columns": {
"userId": {
"name": "userId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"equipmentId": {
"name": "equipmentId",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
}
},
"indexes": {},
"foreignKeys": {
"userEquipment_userId_users_id_fk": {
"name": "userEquipment_userId_users_id_fk",
"tableFrom": "userEquipment",
"tableTo": "users",
"columnsFrom": [
"userId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "cascade"
},
"userEquipment_equipmentId_equipment_id_fk": {
"name": "userEquipment_equipmentId_equipment_id_fk",
"tableFrom": "userEquipment",
"tableTo": "equipment",
"columnsFrom": [
"equipmentId"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "cascade"
}
},
"compositePrimaryKeys": {
"userEquipment_userId_equipmentId_pk": {
"columns": [
"equipmentId",
"userId"
],
"name": "userEquipment_userId_equipmentId_pk"
}
},
"uniqueConstraints": {}
},
"users": {
"name": "users",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"createdAt": {
"name": "createdAt",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "(unixepoch())"
},
"isAdmin": {
"name": "isAdmin",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}
7 changes: 7 additions & 0 deletions server/database/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
"when": 1720977690688,
"tag": "0004_funny_wolfsbane",
"breakpoints": true
},
{
"idx": 5,
"version": "6",
"when": 1721496479449,
"tag": "0005_chunky_speed",
"breakpoints": true
}
]
}
31 changes: 30 additions & 1 deletion server/database/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sql } from 'drizzle-orm'
import { sqliteTable, text, integer, } from 'drizzle-orm/sqlite-core'
import { sqliteTable, text, integer, primaryKey } from 'drizzle-orm/sqlite-core'
import { ulid } from 'ulid'

export const users = sqliteTable('users', {
Expand Down Expand Up @@ -43,3 +43,32 @@ export const equipment = sqliteTable('equipment', {
.notNull()
.default(sql`(unixepoch())`)
})

export const userEquipment = sqliteTable('userEquipment', {
userId:
text('userId')
.notNull()
.references(() => users.id, {
onDelete: 'cascade',
onUpdate: 'cascade'
}),

equipmentId:
text('equipmentId')
.notNull()
.references(() => equipment.id, {
onDelete: 'cascade',
onUpdate: 'cascade'
}),

createdAt:
integer('createdAt', {
mode: 'timestamp'
})
.notNull()
.default(sql`(unixepoch())`)
}, (table) => ({
primaryKey: primaryKey({
columns: [table.userId, table.equipmentId]
})
}))

0 comments on commit d0281ab

Please sign in to comment.