Template
feat: Overhauled base template
This commit is contained in:
1 parent
04b9739853
commit
dabfef1458
37 files changed
+2679
-144
No files matched your search
@@ -0,0 +1,42 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `user` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`username` VARCHAR(191) NOT NULL,
|
||||
`password` VARCHAR(191) NOT NULL,
|
||||
`email` VARCHAR(191) NULL,
|
||||
`display_name` VARCHAR(191) NULL,
|
||||
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`active` BOOLEAN NOT NULL DEFAULT true,
|
||||
|
||||
UNIQUE INDEX `user_username_key`(`username`),
|
||||
UNIQUE INDEX `user_email_key`(`email`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `audit_type` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `audit` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`type_id` INTEGER NOT NULL,
|
||||
`account_id` INTEGER NOT NULL,
|
||||
`target_id` INTEGER NULL,
|
||||
`data` VARCHAR(191) NULL,
|
||||
`ip` VARCHAR(191) NULL,
|
||||
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `audit` ADD CONSTRAINT `audit_account_id_fkey` FOREIGN KEY (`account_id`) REFERENCES `user`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `audit` ADD CONSTRAINT `audit_type_id_fkey` FOREIGN KEY (`type_id`) REFERENCES `audit_type`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "mysql"
|
||||
@@ -0,0 +1,17 @@
|
||||
// This file was generated by Prisma, and assumes you have installed the following:
|
||||
// npm install --save-dev prisma dotenv
|
||||
import 'dotenv/config';
|
||||
import { defineConfig, env } from 'prisma/config';
|
||||
|
||||
const dbUrl = `mysql://${env('DB_USER')}:${env('DB_PASS')}@${env('DB_HOST')}:${env('DB_PORT')}/${env('DB_NAME')}`;
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'schema.prisma',
|
||||
migrations: {
|
||||
path: 'migrations',
|
||||
seed: 'tsx ./prisma/seed.ts',
|
||||
},
|
||||
datasource: {
|
||||
url: dbUrl,
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "./generated"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "mysql"
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
username String @unique
|
||||
password String
|
||||
email String? @unique
|
||||
displayName String? @map("display_name")
|
||||
createdAt DateTime @db.Timestamp @map("created_at") @default(now())
|
||||
updatedAt DateTime @db.Timestamp @updatedAt @map("updated_at") @default(now())
|
||||
active Boolean @default(true)
|
||||
|
||||
audits Audit[]
|
||||
|
||||
@@map("user")
|
||||
}
|
||||
|
||||
model AuditType {
|
||||
id Int @id @default(autoincrement())
|
||||
name String
|
||||
|
||||
audits Audit[]
|
||||
|
||||
@@map("audit_type")
|
||||
}
|
||||
|
||||
model Audit {
|
||||
id Int @id @default(autoincrement())
|
||||
account User @relation(fields: [accountId], references: [id])
|
||||
type AuditType @relation(fields: [typeId], references: [id])
|
||||
|
||||
typeId Int @map("type_id")
|
||||
accountId Int @map("account_id")
|
||||
targetId Int? @map("target_id")
|
||||
data String?
|
||||
ip String?
|
||||
|
||||
timestamp DateTime @db.Timestamp @default(now())
|
||||
|
||||
@@map("audit")
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import 'dotenv/config';
|
||||
import { AuditType } from '#/db/types/audit';
|
||||
import Environment from '#/util/Environment';
|
||||
import { PrismaMariaDb } from '@prisma/adapter-mariadb';
|
||||
import { PrismaClient } from './generated/client';
|
||||
|
||||
const adapter = new PrismaMariaDb({
|
||||
host: Environment.DB_HOST,
|
||||
port: Environment.DB_PORT,
|
||||
user: Environment.DB_USER,
|
||||
password: Environment.DB_PASS,
|
||||
database: Environment.DB_NAME,
|
||||
});
|
||||
const client = new PrismaClient({ adapter });
|
||||
|
||||
async function main() {
|
||||
const auditTypes: AuditType[] = [
|
||||
{ id: 1, name: 'Create User' },
|
||||
{ id: 2, name: 'Modify User' },
|
||||
{ id: 3, name: 'Delete User' },
|
||||
|
||||
{ id: 100, name: 'User Login' },
|
||||
{ id: 101, name: 'User Logout' },
|
||||
];
|
||||
|
||||
for (const auditType of auditTypes) {
|
||||
await client.auditType.upsert({
|
||||
where: { id: auditType.id },
|
||||
create: { id: auditType.id, name: auditType.name },
|
||||
update: { name: auditType.name },
|
||||
});
|
||||
}
|
||||
|
||||
await client.user.upsert({
|
||||
where: { id: 1 },
|
||||
create: {
|
||||
username: 'admin',
|
||||
displayName: 'System',
|
||||
password: '',
|
||||
active: false,
|
||||
},
|
||||
update: {
|
||||
username: 'admin',
|
||||
displayName: 'System',
|
||||
password: '',
|
||||
active: false,
|
||||
email: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
main()
|
||||
.then(async () => {
|
||||
await client.$disconnect();
|
||||
})
|
||||
.catch(async (e) => {
|
||||
console.error(e);
|
||||
await client.$disconnect();
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user