chore: establish workspace and initial asset data model
This commit is contained in:
commit
67220e38ed
20 files changed
+2853
No files matched your search
@@ -0,0 +1,97 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `User` (
|
||||
`id` CHAR(36) NOT NULL,
|
||||
`username` VARCHAR(64) NOT NULL,
|
||||
`passwordHash` VARCHAR(255) NOT NULL,
|
||||
`baseCurrency` CHAR(3) NOT NULL DEFAULT 'CNY',
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `User_username_key`(`username`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Session` (
|
||||
`id` CHAR(64) NOT NULL,
|
||||
`userId` CHAR(36) NOT NULL,
|
||||
`expiresAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `Session_userId_idx`(`userId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Position` (
|
||||
`id` CHAR(36) NOT NULL,
|
||||
`userId` CHAR(36) NOT NULL,
|
||||
`kind` VARCHAR(16) NOT NULL,
|
||||
`side` VARCHAR(16) NOT NULL,
|
||||
`name` VARCHAR(100) NOT NULL,
|
||||
`category` VARCHAR(40) NOT NULL,
|
||||
`currency` CHAR(3) NOT NULL,
|
||||
`notes` TEXT NOT NULL,
|
||||
`archived` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `Position_userId_kind_idx`(`userId`, `kind`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Revision` (
|
||||
`id` CHAR(36) NOT NULL,
|
||||
`positionId` CHAR(36) NOT NULL,
|
||||
`amount` DECIMAL(24, 8) NOT NULL,
|
||||
`effectiveDate` DATE NOT NULL,
|
||||
`notes` TEXT NOT NULL,
|
||||
`reason` VARCHAR(20) NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Revision_positionId_effectiveDate_key`(`positionId`, `effectiveDate`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `PositionLink` (
|
||||
`id` CHAR(36) NOT NULL,
|
||||
`sourceId` CHAR(36) NOT NULL,
|
||||
`targetId` CHAR(36) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `PositionLink_sourceId_targetId_key`(`sourceId`, `targetId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ExchangeRate` (
|
||||
`id` CHAR(36) NOT NULL,
|
||||
`userId` CHAR(36) NOT NULL,
|
||||
`currency` CHAR(3) NOT NULL,
|
||||
`baseCurrency` CHAR(3) NOT NULL,
|
||||
`date` DATE NOT NULL,
|
||||
`rate` DECIMAL(24, 12) NOT NULL,
|
||||
`source` VARCHAR(30) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `ExchangeRate_userId_currency_baseCurrency_date_key`(`userId`, `currency`, `baseCurrency`, `date`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Session` ADD CONSTRAINT `Session_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Position` ADD CONSTRAINT `Position_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Revision` ADD CONSTRAINT `Revision_positionId_fkey` FOREIGN KEY (`positionId`) REFERENCES `Position`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `PositionLink` ADD CONSTRAINT `PositionLink_sourceId_fkey` FOREIGN KEY (`sourceId`) REFERENCES `Position`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `PositionLink` ADD CONSTRAINT `PositionLink_targetId_fkey` FOREIGN KEY (`targetId`) REFERENCES `Position`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ExchangeRate` ADD CONSTRAINT `ExchangeRate_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1 @@
|
||||
provider = "mysql"
|
||||
@@ -0,0 +1,74 @@
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
datasource db {
|
||||
provider = "mysql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
model User {
|
||||
id String @id @default(uuid()) @db.Char(36)
|
||||
username String @unique @db.VarChar(64)
|
||||
passwordHash String @db.VarChar(255)
|
||||
baseCurrency String @default("CNY") @db.Char(3)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
positions Position[]
|
||||
rates ExchangeRate[]
|
||||
sessions Session[]
|
||||
}
|
||||
model Session {
|
||||
id String @id @db.Char(64)
|
||||
userId String @db.Char(36)
|
||||
user User @relation(fields:[userId],references:[id],onDelete:Cascade)
|
||||
expiresAt DateTime
|
||||
@@index([userId])
|
||||
}
|
||||
model Position {
|
||||
id String @id @default(uuid()) @db.Char(36)
|
||||
userId String @db.Char(36)
|
||||
user User @relation(fields:[userId],references:[id],onDelete:Cascade)
|
||||
kind String @db.VarChar(16)
|
||||
side String @db.VarChar(16)
|
||||
name String @db.VarChar(100)
|
||||
category String @db.VarChar(40)
|
||||
currency String @db.Char(3)
|
||||
notes String @db.Text
|
||||
archived Boolean @default(false)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
revisions Revision[]
|
||||
outgoing PositionLink[] @relation("Source")
|
||||
incoming PositionLink[] @relation("Target")
|
||||
@@index([userId,kind])
|
||||
}
|
||||
model Revision {
|
||||
id String @id @default(uuid()) @db.Char(36)
|
||||
positionId String @db.Char(36)
|
||||
position Position @relation(fields:[positionId],references:[id],onDelete:Cascade)
|
||||
amount Decimal @db.Decimal(24,8)
|
||||
effectiveDate DateTime @db.Date
|
||||
notes String @db.Text
|
||||
reason String @db.VarChar(20)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
@@unique([positionId,effectiveDate])
|
||||
}
|
||||
model PositionLink {
|
||||
id String @id @default(uuid()) @db.Char(36)
|
||||
sourceId String @db.Char(36)
|
||||
targetId String @db.Char(36)
|
||||
source Position @relation("Source",fields:[sourceId],references:[id],onDelete:Cascade)
|
||||
target Position @relation("Target",fields:[targetId],references:[id],onDelete:Cascade)
|
||||
@@unique([sourceId,targetId])
|
||||
}
|
||||
model ExchangeRate {
|
||||
id String @id @default(uuid()) @db.Char(36)
|
||||
userId String @db.Char(36)
|
||||
user User @relation(fields:[userId],references:[id],onDelete:Cascade)
|
||||
currency String @db.Char(3)
|
||||
baseCurrency String @db.Char(3)
|
||||
date DateTime @db.Date
|
||||
rate Decimal @db.Decimal(24,12)
|
||||
source String @db.VarChar(30)
|
||||
@@unique([userId,currency,baseCurrency,date])
|
||||
}
|
||||
Reference in new issue
Block a user