Multi-tenancy

Hey, we are trying to implement an use-case in our project and I need help on how to do it. We have an app that needs a environment for each user (a schema). And this schema has multiple (more than 20) tables. The authentication is made by an config schema that has a table that stores user credentials and his schema name. All user-enviroments are the same, so I need that every change that I do, will propagate to all of them. My problem is, how can I achieve this? We used to use Prisma, but it looks like it doesnt support this use-case, so we are studying to see if we can use Drizzle, but it seems like it cant support it either :/
1 Reply
GUIDAO2
GUIDAO2OP11mo ago
I think that it could be eazy to solve if we could just do
import { ColumnBuilderBaseConfig, ColumnDataType } from "drizzle-orm";
import { PgColumnBuilderBase, pgSchema, pgTable, serial, text, varchar } from "drizzle-orm/pg-core";

export const configSchema = pgSchema("config");
export const UserConfig = configSchema.table("user_config", {
id: serial("id").primaryKey(),

email: varchar("email", { length: 255 }).notNull().unique(),
password: varchar("password", { length: 255 }),

schema: text("schema"),
});

export function getNewSchema(schema: string) {
const modelSchema = pgSchema(schema);

const Users = modelSchema.table("user", {
id: serial("id").primaryKey(),

name: text("name"),
});

const Contracts = modelSchema.table("contract", {
id: serial("id").primaryKey(),
});

const exampleTable = modelSchema.table("example_table", {
id: serial("id").primaryKey(),
});

return { Users, Contracts, exampleTable };
// ^^^^^^^^^^ this.
}

export const schema1 = getNewSchema("schema_example1");
export const schema2 = getNewSchema("schema_example2");
import { ColumnBuilderBaseConfig, ColumnDataType } from "drizzle-orm";
import { PgColumnBuilderBase, pgSchema, pgTable, serial, text, varchar } from "drizzle-orm/pg-core";

export const configSchema = pgSchema("config");
export const UserConfig = configSchema.table("user_config", {
id: serial("id").primaryKey(),

email: varchar("email", { length: 255 }).notNull().unique(),
password: varchar("password", { length: 255 }),

schema: text("schema"),
});

export function getNewSchema(schema: string) {
const modelSchema = pgSchema(schema);

const Users = modelSchema.table("user", {
id: serial("id").primaryKey(),

name: text("name"),
});

const Contracts = modelSchema.table("contract", {
id: serial("id").primaryKey(),
});

const exampleTable = modelSchema.table("example_table", {
id: serial("id").primaryKey(),
});

return { Users, Contracts, exampleTable };
// ^^^^^^^^^^ this.
}

export const schema1 = getNewSchema("schema_example1");
export const schema2 = getNewSchema("schema_example2");
or return it as an array, but it doesnt seem like drizzle can understand it.
Want results from more Discord servers?
Add your server