71 lines
3.3 KiB
TypeScript
71 lines
3.3 KiB
TypeScript
import { relations } from 'drizzle-orm';
|
|
import { int, text, sqliteTable as table, primaryKey, blob } from 'drizzle-orm/sqlite-core';
|
|
|
|
export const usersTable = table("users", {
|
|
id: int().primaryKey({ autoIncrement: true }),
|
|
username: text().notNull().unique(),
|
|
email: text().notNull().unique(),
|
|
hash: text().notNull().unique(),
|
|
state: int().notNull().default(0),
|
|
});
|
|
|
|
export const usersDataTable = table("users_data", {
|
|
id: int().primaryKey().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
|
signin: int({ mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
|
|
lastTimestamp: int({ mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
|
|
});
|
|
|
|
export const userSessionsTable = table("user_sessions", {
|
|
id: int().notNull(),
|
|
user_id: int().notNull().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
|
timestamp: int({ mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
|
|
}, (table) => [ primaryKey({ columns: [table.id, table.user_id] }) ]);
|
|
|
|
export const userPermissionsTable = table("user_permissions", {
|
|
id: int().notNull().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
|
permission: text().notNull(),
|
|
}, (table) => [ primaryKey({ columns: [table.id, table.permission] }) ]);
|
|
|
|
export const projectFilesTable = table("project_files", {
|
|
id: text().primaryKey(),
|
|
path: text().notNull().unique(),
|
|
owner: int().notNull().references(() => usersTable.id, { onDelete: 'cascade', onUpdate: 'cascade' }),
|
|
title: text().notNull(),
|
|
type: text({ enum: ['file', 'folder', 'markdown', 'canvas', 'map'] }).notNull(),
|
|
navigable: int({ mode: 'boolean' }).notNull().default(true),
|
|
private: int({ mode: 'boolean' }).notNull().default(false),
|
|
order: int().notNull(),
|
|
timestamp: int({ mode: 'timestamp' }).notNull().$defaultFn(() => new Date()),
|
|
});
|
|
|
|
export const projectContentTable = table("project_content", {
|
|
id: text().primaryKey(),
|
|
content: blob({ mode: 'buffer' }),
|
|
});
|
|
|
|
export const emailValidationTable = table("email_validation", {
|
|
id: text().primaryKey(),
|
|
timestamp: int({ mode: 'timestamp' }).notNull(),
|
|
})
|
|
|
|
export const usersRelation = relations(usersTable, ({ one, many }) => ({
|
|
data: one(usersDataTable, { fields: [usersTable.id], references: [usersDataTable.id], }),
|
|
session: many(userSessionsTable),
|
|
permission: many(userPermissionsTable),
|
|
files: many(projectFilesTable),
|
|
}));
|
|
export const usersDataRelation = relations(usersDataTable, ({ one }) => ({
|
|
users: one(usersTable, { fields: [usersDataTable.id], references: [usersTable.id], }),
|
|
}));
|
|
export const userSessionsRelation = relations(userSessionsTable, ({ one }) => ({
|
|
users: one(usersTable, { fields: [userSessionsTable.user_id], references: [usersTable.id], }),
|
|
}));
|
|
export const userPermissionsRelation = relations(userPermissionsTable, ({ one }) => ({
|
|
users: one(usersTable, { fields: [userPermissionsTable.id], references: [usersTable.id], }),
|
|
}));
|
|
export const projectFilesRelation = relations(projectFilesTable, ({ one }) => ({
|
|
users: one(usersTable, { fields: [projectFilesTable.owner], references: [usersTable.id], }),
|
|
}));
|
|
export const projectContentRelation = relations(projectContentTable, ({ one }) => ({
|
|
files: one(projectFilesTable, { fields: [projectContentTable.id], references: [projectFilesTable.id], }),
|
|
})); |