163 lines
4.7 KiB
TypeScript
163 lines
4.7 KiB
TypeScript
|
|
import {
|
||
|
|
pgTable,
|
||
|
|
uuid,
|
||
|
|
varchar,
|
||
|
|
timestamp,
|
||
|
|
index,
|
||
|
|
uniqueIndex,
|
||
|
|
} from "drizzle-orm/pg-core";
|
||
|
|
import { relations } from "drizzle-orm";
|
||
|
|
import { objects, objectAssignees, workspaceMembers } from "./objects";
|
||
|
|
import { users, accounts, sessions } from "./users";
|
||
|
|
import { propertyDefinitions } from "./properties";
|
||
|
|
import { propertyValues } from "./values";
|
||
|
|
import { views } from "./views";
|
||
|
|
import { templates } from "./templates";
|
||
|
|
|
||
|
|
export const objectRelations = pgTable(
|
||
|
|
"object_relations",
|
||
|
|
{
|
||
|
|
id: uuid("id").primaryKey().defaultRandom(),
|
||
|
|
sourceId: uuid("source_id")
|
||
|
|
.notNull()
|
||
|
|
.references(() => objects.id, { onDelete: "cascade" }),
|
||
|
|
targetId: uuid("target_id")
|
||
|
|
.notNull()
|
||
|
|
.references(() => objects.id, { onDelete: "cascade" }),
|
||
|
|
relationType: varchar("relation_type", { length: 50 }).notNull(),
|
||
|
|
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
|
||
|
|
},
|
||
|
|
(table) => ({
|
||
|
|
sourceIdIdx: index("object_relations_source_id_idx").on(table.sourceId),
|
||
|
|
targetIdIdx: index("object_relations_target_id_idx").on(table.targetId),
|
||
|
|
relationTypeIdx: index("object_relations_relation_type_idx").on(table.relationType),
|
||
|
|
sourceTargetTypeUnique: uniqueIndex("object_relations_source_target_type_unique").on(
|
||
|
|
table.sourceId,
|
||
|
|
table.targetId,
|
||
|
|
table.relationType,
|
||
|
|
),
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
// --- Drizzle ORM relations ---
|
||
|
|
|
||
|
|
export const usersRelations = relations(users, ({ many }) => ({
|
||
|
|
objectsCreated: many(objects),
|
||
|
|
workspaceMemberships: many(workspaceMembers),
|
||
|
|
objectAssignees: many(objectAssignees),
|
||
|
|
accounts: many(accounts),
|
||
|
|
sessions: many(sessions),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const objectsRelations = relations(objects, ({ one, many }) => ({
|
||
|
|
parent: one(objects, {
|
||
|
|
fields: [objects.parentId],
|
||
|
|
references: [objects.id],
|
||
|
|
relationName: "objectHierarchy",
|
||
|
|
}),
|
||
|
|
children: many(objects, { relationName: "objectHierarchy" }),
|
||
|
|
workspace: one(objects, {
|
||
|
|
fields: [objects.workspaceId],
|
||
|
|
references: [objects.id],
|
||
|
|
relationName: "workspaceRoot",
|
||
|
|
}),
|
||
|
|
workspaceContents: many(objects, { relationName: "workspaceRoot" }),
|
||
|
|
template: one(templates, {
|
||
|
|
fields: [objects.templateId],
|
||
|
|
references: [templates.id],
|
||
|
|
}),
|
||
|
|
creator: one(users, {
|
||
|
|
fields: [objects.createdBy],
|
||
|
|
references: [users.id],
|
||
|
|
}),
|
||
|
|
propertyValues: many(propertyValues),
|
||
|
|
views: many(views),
|
||
|
|
assignees: many(objectAssignees),
|
||
|
|
workspaceMembers: many(workspaceMembers),
|
||
|
|
outgoingRelations: many(objectRelations, { relationName: "relationSource" }),
|
||
|
|
incomingRelations: many(objectRelations, { relationName: "relationTarget" }),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const objectAssigneesRelations = relations(objectAssignees, ({ one }) => ({
|
||
|
|
object: one(objects, {
|
||
|
|
fields: [objectAssignees.objectId],
|
||
|
|
references: [objects.id],
|
||
|
|
}),
|
||
|
|
user: one(users, {
|
||
|
|
fields: [objectAssignees.userId],
|
||
|
|
references: [users.id],
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const workspaceMembersRelations = relations(workspaceMembers, ({ one }) => ({
|
||
|
|
workspace: one(objects, {
|
||
|
|
fields: [workspaceMembers.workspaceId],
|
||
|
|
references: [objects.id],
|
||
|
|
}),
|
||
|
|
user: one(users, {
|
||
|
|
fields: [workspaceMembers.userId],
|
||
|
|
references: [users.id],
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const propertyDefinitionsRelations = relations(propertyDefinitions, ({ one, many }) => ({
|
||
|
|
workspace: one(objects, {
|
||
|
|
fields: [propertyDefinitions.workspaceId],
|
||
|
|
references: [objects.id],
|
||
|
|
}),
|
||
|
|
values: many(propertyValues),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const propertyValuesRelations = relations(propertyValues, ({ one }) => ({
|
||
|
|
object: one(objects, {
|
||
|
|
fields: [propertyValues.objectId],
|
||
|
|
references: [objects.id],
|
||
|
|
}),
|
||
|
|
propertyDefinition: one(propertyDefinitions, {
|
||
|
|
fields: [propertyValues.propertyDefId],
|
||
|
|
references: [propertyDefinitions.id],
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const viewsRelations = relations(views, ({ one }) => ({
|
||
|
|
object: one(objects, {
|
||
|
|
fields: [views.objectId],
|
||
|
|
references: [objects.id],
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const templatesRelations = relations(templates, ({ one, many }) => ({
|
||
|
|
workspace: one(objects, {
|
||
|
|
fields: [templates.workspaceId],
|
||
|
|
references: [objects.id],
|
||
|
|
}),
|
||
|
|
objects: many(objects),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const accountsRelations = relations(accounts, ({ one }) => ({
|
||
|
|
user: one(users, {
|
||
|
|
fields: [accounts.userId],
|
||
|
|
references: [users.id],
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const sessionsRelations = relations(sessions, ({ one }) => ({
|
||
|
|
user: one(users, {
|
||
|
|
fields: [sessions.userId],
|
||
|
|
references: [users.id],
|
||
|
|
}),
|
||
|
|
}));
|
||
|
|
|
||
|
|
export const objectRelationsRelations = relations(objectRelations, ({ one }) => ({
|
||
|
|
source: one(objects, {
|
||
|
|
fields: [objectRelations.sourceId],
|
||
|
|
references: [objects.id],
|
||
|
|
relationName: "relationSource",
|
||
|
|
}),
|
||
|
|
target: one(objects, {
|
||
|
|
fields: [objectRelations.targetId],
|
||
|
|
references: [objects.id],
|
||
|
|
relationName: "relationTarget",
|
||
|
|
}),
|
||
|
|
}));
|