From c35e2f54b58d4ed91c31e4f40b473b11c4ce4592 Mon Sep 17 00:00:00 2001 From: kennethcheng Date: Mon, 27 Apr 2026 19:33:53 +0800 Subject: [PATCH] =?UTF-8?q?feat(db):=20=E6=96=B0=E5=A2=9E=E4=BA=A4?= =?UTF-8?q?=E6=98=93=E6=B5=81=E6=B0=B4=E8=A1=A8=20(transactions)=20?= =?UTF-8?q?=E5=B9=B6=E9=94=81=E5=AE=9A=E6=9E=81=E8=87=B4=E9=87=91=E8=9E=8D?= =?UTF-8?q?=E7=B2=BE=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drizzle/0002_rapid_invaders.sql | 15 +++ drizzle/meta/0002_snapshot.json | 232 ++++++++++++++++++++++++++++++++ drizzle/meta/_journal.json | 7 + src/db/schema.ts | 30 ++++- 4 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 drizzle/0002_rapid_invaders.sql create mode 100644 drizzle/meta/0002_snapshot.json diff --git a/drizzle/0002_rapid_invaders.sql b/drizzle/0002_rapid_invaders.sql new file mode 100644 index 0000000..4b7027f --- /dev/null +++ b/drizzle/0002_rapid_invaders.sql @@ -0,0 +1,15 @@ +CREATE TYPE "public"."transaction_type_enum" AS ENUM('BUY', 'SELL', 'DIVIDEND', 'AIRDROP', 'FEE');--> statement-breakpoint +CREATE TABLE "transactions" ( + "id" uuid PRIMARY KEY NOT NULL, + "asset_id" uuid NOT NULL, + "tx_type" "transaction_type_enum" NOT NULL, + "quantity" numeric(36, 18) NOT NULL, + "price" numeric(36, 18) NOT NULL, + "fee" numeric(36, 18) DEFAULT '0' NOT NULL, + "tx_currency" varchar(10) NOT NULL, + "exchange_rate" numeric(20, 8) DEFAULT '1' NOT NULL, + "executed_at" timestamp with time zone NOT NULL, + "created_at" timestamp with time zone DEFAULT now() +); +--> statement-breakpoint +ALTER TABLE "transactions" ADD CONSTRAINT "transactions_asset_id_assets_id_fk" FOREIGN KEY ("asset_id") REFERENCES "public"."assets"("id") ON DELETE no action ON UPDATE no action; \ No newline at end of file diff --git a/drizzle/meta/0002_snapshot.json b/drizzle/meta/0002_snapshot.json new file mode 100644 index 0000000..61687e2 --- /dev/null +++ b/drizzle/meta/0002_snapshot.json @@ -0,0 +1,232 @@ +{ + "id": "18d8b1e1-2700-4d54-adf8-493d526d5bf5", + "prevId": "efb1ef58-df53-4e36-b691-ee9811c3ab36", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.assets": { + "name": "assets", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "symbol": { + "name": "symbol", + "type": "varchar(20)", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "asset_type_enum", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "base_currency": { + "name": "base_currency", + "type": "varchar(10)", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "assets_symbol_unique": { + "name": "assets_symbol_unique", + "nullsNotDistinct": false, + "columns": [ + "symbol" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.transactions": { + "name": "transactions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true + }, + "asset_id": { + "name": "asset_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "tx_type": { + "name": "tx_type", + "type": "transaction_type_enum", + "typeSchema": "public", + "primaryKey": false, + "notNull": true + }, + "quantity": { + "name": "quantity", + "type": "numeric(36, 18)", + "primaryKey": false, + "notNull": true + }, + "price": { + "name": "price", + "type": "numeric(36, 18)", + "primaryKey": false, + "notNull": true + }, + "fee": { + "name": "fee", + "type": "numeric(36, 18)", + "primaryKey": false, + "notNull": true, + "default": "'0'" + }, + "tx_currency": { + "name": "tx_currency", + "type": "varchar(10)", + "primaryKey": false, + "notNull": true + }, + "exchange_rate": { + "name": "exchange_rate", + "type": "numeric(20, 8)", + "primaryKey": false, + "notNull": true, + "default": "'1'" + }, + "executed_at": { + "name": "executed_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "transactions_asset_id_assets_id_fk": { + "name": "transactions_asset_id_assets_id_fk", + "tableFrom": "transactions", + "tableTo": "assets", + "columnsFrom": [ + "asset_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "username": { + "name": "username", + "type": "varchar(50)", + "primaryKey": false, + "notNull": true + }, + "password_hash": { + "name": "password_hash", + "type": "varchar(255)", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "users_username_unique": { + "name": "users_username_unique", + "nullsNotDistinct": false, + "columns": [ + "username" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": { + "public.asset_type_enum": { + "name": "asset_type_enum", + "schema": "public", + "values": [ + "STOCK", + "CRYPTO", + "CASH" + ] + }, + "public.transaction_type_enum": { + "name": "transaction_type_enum", + "schema": "public", + "values": [ + "BUY", + "SELL", + "DIVIDEND", + "AIRDROP", + "FEE" + ] + } + }, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index fc2e9dd..423a1f8 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -15,6 +15,13 @@ "when": 1777288158234, "tag": "0001_brown_lucky_pierre", "breakpoints": true + }, + { + "idx": 2, + "version": "7", + "when": 1777289592183, + "tag": "0002_rapid_invaders", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/db/schema.ts b/src/db/schema.ts index f028a75..76d5d60 100644 --- a/src/db/schema.ts +++ b/src/db/schema.ts @@ -1,4 +1,4 @@ -import { pgTable, uuid, varchar, timestamp, pgEnum } from "drizzle-orm/pg-core"; +import { pgTable, uuid, varchar, timestamp, pgEnum, numeric } from "drizzle-orm/pg-core"; export const users = pgTable("users", { id: uuid("id").primaryKey().defaultRandom(), @@ -23,3 +23,31 @@ export const assets = pgTable("assets", { createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }) .defaultNow(), }); + +export const transactionTypeEnum = pgEnum("transaction_type_enum", [ + "BUY", + "SELL", + "DIVIDEND", + "AIRDROP", + "FEE", +]); + +export const transactions = pgTable("transactions", { + id: uuid("id").primaryKey(), + assetId: uuid("asset_id") + .notNull() + .references(() => assets.id), + txType: transactionTypeEnum("tx_type").notNull(), + quantity: numeric("quantity", { precision: 36, scale: 18 }).notNull(), + price: numeric("price", { precision: 36, scale: 18 }).notNull(), + fee: numeric("fee", { precision: 36, scale: 18 }) + .notNull() + .default("0"), + txCurrency: varchar("tx_currency", { length: 10 }).notNull(), + exchangeRate: numeric("exchange_rate", { precision: 20, scale: 8 }) + .notNull() + .default("1"), + executedAt: timestamp("executed_at", { withTimezone: true }).notNull(), + createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }) + .defaultNow(), +});