feat: integrate Drizzle ORM and SQLite for authentication

This commit is contained in:
2025-06-18 16:15:49 +08:00
parent 6652953b1a
commit bc81e4d6fe
13 changed files with 1024 additions and 37 deletions

10
server/db/index.ts Normal file
View File

@ -0,0 +1,10 @@
import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
import * as schema from './schema';
const client = createClient({
url: 'file:./server/db/local.db',
});
export const db = drizzle(client, { schema });
export * from './schema';

BIN
server/db/local.db Normal file

Binary file not shown.

View File

@ -0,0 +1,35 @@
CREATE TABLE `customers` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`gender` text NOT NULL,
`contact` text NOT NULL,
`id_card` text NOT NULL,
`password` text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `customers_contact_unique` ON `customers` (`contact`);--> statement-breakpoint
CREATE UNIQUE INDEX `customers_id_card_unique` ON `customers` (`id_card`);--> statement-breakpoint
CREATE TABLE `reservations` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`customer_id` integer NOT NULL,
`room_id` integer NOT NULL,
`check_in_time` integer NOT NULL,
`stay_days` integer NOT NULL,
FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`room_id`) REFERENCES `rooms`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `room_types` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`type_name` text NOT NULL,
`star_rating` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `rooms` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`type_id` integer NOT NULL,
`price` real NOT NULL,
`feature` text,
`available_count` integer DEFAULT 0 NOT NULL,
FOREIGN KEY (`type_id`) REFERENCES `room_types`(`id`) ON UPDATE no action ON DELETE no action
);

View File

@ -0,0 +1,248 @@
{
"version": "6",
"dialect": "sqlite",
"id": "4e2b0752-859c-462f-81a8-d794f837ca5b",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"customers": {
"name": "customers",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"gender": {
"name": "gender",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"contact": {
"name": "contact",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"id_card": {
"name": "id_card",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"password": {
"name": "password",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"customers_contact_unique": {
"name": "customers_contact_unique",
"columns": [
"contact"
],
"isUnique": true
},
"customers_id_card_unique": {
"name": "customers_id_card_unique",
"columns": [
"id_card"
],
"isUnique": true
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"reservations": {
"name": "reservations",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"customer_id": {
"name": "customer_id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"room_id": {
"name": "room_id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"check_in_time": {
"name": "check_in_time",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"stay_days": {
"name": "stay_days",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {
"reservations_customer_id_customers_id_fk": {
"name": "reservations_customer_id_customers_id_fk",
"tableFrom": "reservations",
"tableTo": "customers",
"columnsFrom": [
"customer_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
},
"reservations_room_id_rooms_id_fk": {
"name": "reservations_room_id_rooms_id_fk",
"tableFrom": "reservations",
"tableTo": "rooms",
"columnsFrom": [
"room_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"room_types": {
"name": "room_types",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"type_name": {
"name": "type_name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"star_rating": {
"name": "star_rating",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
},
"rooms": {
"name": "rooms",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"type_id": {
"name": "type_id",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"price": {
"name": "price",
"type": "real",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"feature": {
"name": "feature",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"available_count": {
"name": "available_count",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": 0
}
},
"indexes": {},
"foreignKeys": {
"rooms_type_id_room_types_id_fk": {
"name": "rooms_type_id_room_types_id_fk",
"tableFrom": "rooms",
"tableTo": "room_types",
"columnsFrom": [
"type_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"checkConstraints": {}
}
},
"views": {},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"indexes": {}
}
}

View File

@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "6",
"when": 1750234000729,
"tag": "0000_gifted_agent_brand",
"breakpoints": true
}
]
}

32
server/db/schema.ts Normal file
View File

@ -0,0 +1,32 @@
import { sqliteTable, text, integer, real } from 'drizzle-orm/sqlite-core';
export const customers = sqliteTable('customers', {
id: integer('id').primaryKey({ autoIncrement: true }),
name: text('name').notNull(),
gender: text('gender', { enum: ['male', 'female'] }).notNull(),
contact: text('contact').notNull().unique(),
idCard: text('id_card').notNull().unique(),
password: text('password').notNull(),
});
export const roomTypes = sqliteTable('room_types', {
id: integer('id').primaryKey({ autoIncrement: true }),
typeName: text('type_name').notNull(),
starRating: integer('star_rating').notNull(),
});
export const rooms = sqliteTable('rooms', {
id: integer('id').primaryKey({ autoIncrement: true }),
typeId: integer('type_id').notNull().references(() => roomTypes.id),
price: real('price').notNull(),
feature: text('feature'),
availableCount: integer('available_count').notNull().default(0),
});
export const reservations = sqliteTable('reservations', {
id: integer('id').primaryKey({ autoIncrement: true }),
customerId: integer('customer_id').notNull().references(() => customers.id),
roomId: integer('room_id').notNull().references(() => rooms.id),
checkInTime: integer('check_in_time', { mode: 'timestamp' }).notNull(),
stayDays: integer('stay_days').notNull(),
});