mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on the organization system.
This commit is contained in:
+53
-6
@@ -1,13 +1,60 @@
|
||||
import "dotenv/config";
|
||||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
import {drizzle} from "drizzle-orm/node-postgres";
|
||||
|
||||
import * as settings from "./schema/00_setting";
|
||||
import * as user from "./schema/01_user";
|
||||
import * as organisation from "./schema/02_organization";
|
||||
import * as invitation from "./schema/03_member";
|
||||
import * as member from "./schema/04_invitation";
|
||||
import * as project from "./schema/05_project";
|
||||
import * as agent from "./schema/07_agent";
|
||||
import * as database from "./schema/06_database";
|
||||
|
||||
|
||||
import {Pool} from "pg";
|
||||
|
||||
// Do not delete
|
||||
import dotenv from "dotenv";
|
||||
import { env } from "@/env.mjs";
|
||||
import * as schema from "./schema";
|
||||
import {migrate} from "drizzle-orm/node-postgres/migrator";
|
||||
|
||||
dotenv.config({
|
||||
path: ".env",
|
||||
});
|
||||
|
||||
export const db = drizzle(env.DATABASE_URL!, {
|
||||
schema,
|
||||
const pool = new Pool({
|
||||
connectionString: process.env.DATABASE_URL!,
|
||||
});
|
||||
|
||||
export const schemas = {
|
||||
...settings,
|
||||
...user,
|
||||
...organisation,
|
||||
...invitation,
|
||||
...member,
|
||||
...project,
|
||||
...agent,
|
||||
...database
|
||||
};
|
||||
|
||||
export const db = drizzle({
|
||||
client: pool,
|
||||
logger: true,
|
||||
schema: schemas,
|
||||
});
|
||||
|
||||
export async function makeMigration() {
|
||||
if (process.env.NODE_ENV != "development") {
|
||||
const pool = new Pool({
|
||||
connectionString: process.env.DATABASE_URL!,
|
||||
});
|
||||
|
||||
const database = drizzle({client: pool});
|
||||
|
||||
console.log("Running migrations...");
|
||||
try {
|
||||
await migrate(database, {migrationsFolder: "./src/db/migrations"});
|
||||
console.log("Migrations applied successfully.");
|
||||
} catch (error) {
|
||||
console.error("Error applying migrations:", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
export * from "./schema/00_setting";
|
||||
export { user, session, userRelations } from "./schema/01_user";
|
||||
export type { Organization } from "./schema/02_organization";
|
||||
export { organization, member as organizationMember, invitation as organizationInvitation } from "./schema/02_organization";
|
||||
export * from "./schema/03_project";
|
||||
export * from "./schema/04_agent";
|
||||
export * from "./schema/05_database";
|
||||
@@ -2,8 +2,10 @@ import { relations } from "drizzle-orm";
|
||||
import { boolean, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
|
||||
import { createSelectSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { project } from "./03_project";
|
||||
import { member, invitation, organization } from "./02_organization";
|
||||
import { project } from "./05_project";
|
||||
import {member} from "@/db/schema/03_member";
|
||||
import {invitation} from "@/db/schema/04_invitation";
|
||||
import {organization} from "@/db/schema/02_organization";
|
||||
|
||||
export const user = pgTable("user", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";
|
||||
import { user } from "./01_user";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { project } from "./03_project";
|
||||
import { project } from "./05_project";
|
||||
import { createSelectSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import {invitation} from "@/db/schema/04_invitation";
|
||||
import {member} from "@/db/schema/03_member";
|
||||
|
||||
export const organization = pgTable("organization", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
@@ -15,32 +16,6 @@ export const organization = pgTable("organization", {
|
||||
metadata: text("metadata"),
|
||||
});
|
||||
|
||||
export const member = pgTable("member", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
organizationId: uuid("organization_id")
|
||||
.notNull()
|
||||
.references(() => organization.id, { onDelete: "cascade" }),
|
||||
userId: uuid("user_id")
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: "cascade" }),
|
||||
role: text("role").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at"),
|
||||
});
|
||||
|
||||
export const invitation = pgTable("invitation", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
organizationId: uuid("organization_id")
|
||||
.notNull()
|
||||
.references(() => organization.id, { onDelete: "cascade" }),
|
||||
email: text("email").notNull(),
|
||||
role: text("role"),
|
||||
status: text("status").notNull(),
|
||||
expiresAt: timestamp("expires_at").notNull(),
|
||||
inviterId: uuid("inviter_id")
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: "cascade" }),
|
||||
});
|
||||
|
||||
export const organizationRelations = relations(organization, ({ many }) => ({
|
||||
members: many(member),
|
||||
@@ -48,33 +23,8 @@ export const organizationRelations = relations(organization, ({ many }) => ({
|
||||
projects: many(project),
|
||||
}));
|
||||
|
||||
export const memberRelations = relations(member, ({ one }) => ({
|
||||
user: one(user, {
|
||||
fields: [member.userId],
|
||||
references: [user.id],
|
||||
}),
|
||||
organization: one(organization, {
|
||||
fields: [member.organizationId],
|
||||
references: [organization.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const invitationRelations = relations(invitation, ({ one }) => ({
|
||||
organization: one(organization, {
|
||||
fields: [invitation.organizationId],
|
||||
references: [organization.id],
|
||||
}),
|
||||
inviter: one(user, {
|
||||
fields: [invitation.inviterId],
|
||||
references: [user.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
export const organizationSchema = createSelectSchema(organization);
|
||||
export type Organization = z.infer<typeof organizationSchema>;
|
||||
|
||||
export const organizationMemberSchema = createSelectSchema(member);
|
||||
export type OrganizationMember = z.infer<typeof organizationMemberSchema>;
|
||||
|
||||
export const organizationInvitationSchema = createSelectSchema(invitation);
|
||||
export type OrganizationInvitation = z.infer<typeof organizationInvitationSchema>;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import {pgTable, text, timestamp, uuid} from "drizzle-orm/pg-core";
|
||||
import {user} from "@/db/schema/01_user";
|
||||
import {organization} from "@/db/schema/02_organization";
|
||||
import {relations} from "drizzle-orm";
|
||||
import {createSelectSchema} from "drizzle-zod";
|
||||
import {z} from "zod";
|
||||
|
||||
|
||||
export const member = pgTable("member", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
organizationId: uuid("organization_id")
|
||||
.notNull()
|
||||
.references(() => organization.id, { onDelete: "cascade" }),
|
||||
userId: uuid("user_id")
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: "cascade" }),
|
||||
role: text("role").notNull(),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
updatedAt: timestamp("updated_at"),
|
||||
});
|
||||
|
||||
export const memberRelations = relations(member, ({ one }) => ({
|
||||
user: one(user, {
|
||||
fields: [member.userId],
|
||||
references: [user.id],
|
||||
}),
|
||||
organization: one(organization, {
|
||||
fields: [member.organizationId],
|
||||
references: [organization.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
|
||||
export const organizationMemberSchema = createSelectSchema(member);
|
||||
export type OrganizationMember = z.infer<typeof organizationMemberSchema>;
|
||||
@@ -0,0 +1,39 @@
|
||||
import {relations} from "drizzle-orm";
|
||||
import {user} from "@/db/schema/01_user";
|
||||
import {organization} from "@/db/schema/02_organization";
|
||||
import {createSelectSchema} from "drizzle-zod";
|
||||
import {z} from "zod";
|
||||
import {pgTable, text, timestamp, uuid} from "drizzle-orm/pg-core";
|
||||
|
||||
|
||||
export const invitation = pgTable("invitation", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
organizationId: uuid("organization_id")
|
||||
.notNull()
|
||||
.references(() => organization.id, { onDelete: "cascade" }),
|
||||
email: text("email").notNull(),
|
||||
role: text("role"),
|
||||
status: text("status").notNull(),
|
||||
expiresAt: timestamp("expires_at").notNull(),
|
||||
inviterId: uuid("inviter_id")
|
||||
.notNull()
|
||||
.references(() => user.id, { onDelete: "cascade" }),
|
||||
});
|
||||
|
||||
|
||||
|
||||
export const invitationRelations = relations(invitation, ({ one }) => ({
|
||||
organization: one(organization, {
|
||||
fields: [invitation.organizationId],
|
||||
references: [organization.id],
|
||||
}),
|
||||
inviter: one(user, {
|
||||
fields: [invitation.inviterId],
|
||||
references: [user.id],
|
||||
}),
|
||||
}));
|
||||
|
||||
|
||||
|
||||
export const organizationInvitationSchema = createSelectSchema(invitation);
|
||||
export type OrganizationInvitation = z.infer<typeof organizationInvitationSchema>;
|
||||
@@ -3,7 +3,7 @@ import { relations } from "drizzle-orm";
|
||||
import { Organization, organization } from "./02_organization";
|
||||
import { createSelectSchema } from "drizzle-zod";
|
||||
import { z } from "zod";
|
||||
import { Database, database } from "./05_database";
|
||||
import { Database, database } from "./06_database";
|
||||
|
||||
export const project = pgTable("projects", {
|
||||
id: uuid("id").primaryKey().defaultRandom(),
|
||||
@@ -1,6 +1,6 @@
|
||||
import { pgTable, text, boolean, timestamp, uuid, integer, pgEnum, uniqueIndex } from "drizzle-orm/pg-core";
|
||||
import { Agent, agent } from "./04_agent";
|
||||
import { Project, project } from "./03_project";
|
||||
import { Agent, agent } from "./07_agent";
|
||||
import { Project, project } from "./05_project";
|
||||
import { relations } from "drizzle-orm";
|
||||
import { dbmsEnum, statusEnum } from "./types";
|
||||
import { createSelectSchema } from "drizzle-zod";
|
||||
Reference in New Issue
Block a user