feat: add workspace

This commit is contained in:
moonrailgun 2023-09-03 03:49:20 +08:00
parent 2193ff4bcb
commit 84c663985a
5 changed files with 102 additions and 18 deletions

View File

@ -1,2 +1,9 @@
# tianji # tianji
Tianji must not be leaked Tianji must not be leaked
## Open Source
`Tianji` is open source with `Apache 2.0` license.
And its inspired by `umami` license which under `MIT` and `uptime-kuma` which under `MIT` license too

View File

@ -4,11 +4,11 @@ datasource db {
} }
generator client { generator client {
provider = "prisma-client-js" provider = "prisma-client-js"
} }
model User { model User {
id String @id @unique @db.Uuid @default(uuid()) id String @id @unique @default(uuid()) @db.Uuid
username String @unique @db.VarChar(255) username String @unique @db.VarChar(255)
password String @db.VarChar(60) password String @db.VarChar(60)
role String @db.VarChar(50) role String @db.VarChar(50)
@ -16,25 +16,50 @@ model User {
updatedAt DateTime? @updatedAt @db.Timestamptz(6) updatedAt DateTime? @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @db.Timestamptz(6) deletedAt DateTime? @db.Timestamptz(6)
workspaces WorkspacesOnUsers[]
}
model Workspace {
id String @id @unique @default(uuid()) @db.Uuid
name String @db.VarChar(100)
createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
users WorkspacesOnUsers[]
website Website[] website Website[]
} }
model Website { model WorkspacesOnUsers {
id String @id @unique @db.Uuid @default(uuid()) userId String @db.Uuid
name String @db.VarChar(100) workspaceId String @db.Uuid
domain String? @db.VarChar(500) role String @db.VarChar(100)
shareId String? @unique @db.VarChar(50) createdAt DateTime? @default(now()) @db.Timestamptz(6)
resetAt DateTime? @db.Timestamptz(6) updatedAt DateTime? @updatedAt @db.Timestamptz(6)
userId String? @db.Uuid
createdAt DateTime? @default(now()) @db.Timestamptz(6) user User @relation(fields: [userId], references: [id])
updatedAt DateTime? @updatedAt @db.Timestamptz(6) workspace Workspace @relation(fields: [workspaceId], references: [id])
deletedAt DateTime? @db.Timestamptz(6)
@@id([userId, workspaceId])
@@index([userId])
@@index([workspaceId])
}
model Website {
id String @id @unique @default(uuid()) @db.Uuid
name String @db.VarChar(100)
domain String? @db.VarChar(500)
shareId String? @unique @db.VarChar(50)
resetAt DateTime? @db.Timestamptz(6)
workspace Workspace @relation(fields: [workspaceId], references: [id])
workspaceId String @db.Uuid
createdAt DateTime? @default(now()) @db.Timestamptz(6)
updatedAt DateTime? @updatedAt @db.Timestamptz(6)
deletedAt DateTime? @db.Timestamptz(6)
user User? @relation(fields: [userId], references: [id])
eventData EventData[] eventData EventData[]
sessionData SessionData[] sessionData SessionData[]
@@index([userId]) @@index([workspaceId])
@@index([createdAt]) @@index([createdAt])
@@index([shareId]) @@index([shareId])
} }

View File

@ -1,5 +1,6 @@
import { prisma } from './_client'; import { prisma } from './_client';
import bcryptjs from 'bcryptjs'; import bcryptjs from 'bcryptjs';
import { ROLES } from '../utils/const';
async function hashPassword(password: string) { async function hashPassword(password: string) {
return await bcryptjs.hash(password, 10); return await bcryptjs.hash(password, 10);
@ -19,17 +20,51 @@ export async function createAdminUser(username: string, password: string) {
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
role: 'admin', role: ROLES.admin,
workspaces: {
create: [
{
role: ROLES.owner,
workspace: {
create: {
name: username,
},
},
},
],
},
}, },
}); });
} }
export async function createUser(username: string, password: string) { export async function createUser(username: string, password: string) {
const existCount = await prisma.user.count({
where: {
username,
},
});
if (existCount > 0) {
throw new Error('User already exists');
}
await prisma.user.create({ await prisma.user.create({
data: { data: {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
role: 'normal', role: ROLES.user,
workspaces: {
create: [
{
role: ROLES.owner,
workspace: {
create: {
name: username,
},
},
},
],
},
}, },
}); });
} }
@ -40,6 +75,14 @@ export async function authUser(username: string, password: string) {
username, username,
password: await hashPassword(password), password: await hashPassword(password),
}, },
select: {
id: true,
username: true,
role: true,
createdAt: true,
updatedAt: true,
deletedAt: true,
},
}); });
return user; return user;

View File

@ -0,0 +1,9 @@
export const ROLES = {
// System Role
admin: 'admin',
user: 'user',
// Workspace Role
owner: 'owner',
readOnly: 'readOnly',
} as const;

View File

@ -1,5 +1,5 @@
import { defineConfig } from "vite"; import { defineConfig } from 'vite';
import react from "@vitejs/plugin-react"; import react from '@vitejs/plugin-react';
// https://vitejs.dev/config/ // https://vitejs.dev/config/
export default defineConfig({ export default defineConfig({