Skip to content

Commit

Permalink
Merge pull request #238 from aXenDeveloper/database/indexes
Browse files Browse the repository at this point in the history
feat(database): Add indexes to database
  • Loading branch information
aXenDeveloper authored Feb 29, 2024
2 parents 2194079 + 1e6a0e5 commit a9531cf
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 284 deletions.
65 changes: 42 additions & 23 deletions backend/src/apps/admin/core/database/schema/admins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
boolean,
index,
integer,
pgTable,
serial,
Expand All @@ -10,19 +11,28 @@ import { relations } from "drizzle-orm";
import { core_groups } from "./groups";
import { core_users } from "./users";

export const core_admin_permissions = pgTable("core_admin_permissions", {
id: serial("id").primaryKey(),
group_id: integer("group_id").references(() => core_groups.id, {
onDelete: "cascade"
}),
user_id: integer("user_id").references(() => core_users.id, {
onDelete: "cascade"
}),
unrestricted: boolean("unrestricted").notNull().default(false),
created: integer("created").notNull(),
updated: integer("updated").notNull(),
protected: boolean("protected").notNull().default(false)
});
export const core_admin_permissions = pgTable(
"core_admin_permissions",
{
id: serial("id").primaryKey(),
group_id: integer("group_id").references(() => core_groups.id, {
onDelete: "cascade"
}),
user_id: integer("user_id").references(() => core_users.id, {
onDelete: "cascade"
}),
unrestricted: boolean("unrestricted").notNull().default(false),
created: integer("created").notNull(),
updated: integer("updated").notNull(),
protected: boolean("protected").notNull().default(false)
},
table => ({
group_id_idx: index("core_admin_permissions_group_id_idx").on(
table.group_id
),
user_id_idx: index("core_admin_permissions_user_id_idx").on(table.user_id)
})
);

export const core_admin_permissions_relations = relations(
core_admin_permissions,
Expand All @@ -38,16 +48,25 @@ export const core_admin_permissions_relations = relations(
})
);

export const core_admin_sessions = pgTable("core_admin_sessions", {
login_token: varchar("login_token", { length: 255 }).primaryKey(),
user_id: integer("user_id")
.notNull()
.references(() => core_users.id, {
onDelete: "cascade"
}),
last_seen: integer("last_seen").notNull(),
expires: integer("expires").notNull()
});
export const core_admin_sessions = pgTable(
"core_admin_sessions",
{
login_token: varchar("login_token", { length: 255 }).primaryKey(),
user_id: integer("user_id")
.notNull()
.references(() => core_users.id, {
onDelete: "cascade"
}),
last_seen: integer("last_seen").notNull(),
expires: integer("expires").notNull()
},
table => ({
login_token_idx: index("core_admin_sessions_login_token_idx").on(
table.login_token
),
user_id_idx: index("core_admin_sessions_user_id_idx").on(table.user_id)
})
);

export const core_admin_sessions_relations = relations(
core_admin_sessions,
Expand Down
51 changes: 32 additions & 19 deletions backend/src/apps/admin/core/database/schema/files.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import { integer, pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
import {
index,
integer,
pgTable,
serial,
text,
varchar
} from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

import { core_users } from "./users";

export const core_files = pgTable("core_files", {
id: serial("id").primaryKey(),
extension: varchar("extension", { length: 32 }).notNull(),
name: varchar("name", { length: 255 }).notNull(),
url: varchar("url", { length: 255 }).notNull(),
user_id: integer("user_id")
.notNull()
.references(() => core_users.id, {
onDelete: "cascade"
}),
created: integer("created").notNull(),
file_size: integer("file_size").notNull(),
position: integer("position").notNull().default(0),
description: text("description"),
module: varchar("module", { length: 255 }),
module_id: varchar("module_id", { length: 255 }),
mimetype: varchar("mimetype", { length: 255 })
});
export const core_files = pgTable(
"core_files",
{
id: serial("id").primaryKey(),
extension: varchar("extension", { length: 32 }).notNull(),
name: varchar("name", { length: 255 }).notNull(),
url: varchar("url", { length: 255 }).notNull(),
user_id: integer("user_id")
.notNull()
.references(() => core_users.id, {
onDelete: "cascade"
}),
created: integer("created").notNull(),
file_size: integer("file_size").notNull(),
position: integer("position").notNull().default(0),
description: text("description"),
module: varchar("module", { length: 255 }),
module_id: varchar("module_id", { length: 255 }),
mimetype: varchar("mimetype", { length: 255 })
},
table => ({
user_id_idx: index("core_files_user_id_idx").on(table.user_id)
})
);

export const core_files_relations = relations(core_files, ({ one }) => ({
user: one(core_users, {
Expand Down
38 changes: 24 additions & 14 deletions backend/src/apps/admin/core/database/schema/groups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { relations } from "drizzle-orm";
import {
boolean,
index,
integer,
pgTable,
serial,
Expand Down Expand Up @@ -29,20 +30,29 @@ export const core_members = pgTable("core_members", {
posts: integer("posts").notNull().default(0)
});

export const core_groups_names = pgTable("core_groups_names", {
id: serial("id").primaryKey(),
group_id: integer("group_id")
.notNull()
.references(() => core_groups.id, {
onDelete: "cascade"
}),
language_code: varchar("language_code")
.notNull()
.references(() => core_languages.code, {
onDelete: "cascade"
}),
value: varchar("value", { length: 255 }).notNull()
});
export const core_groups_names = pgTable(
"core_groups_names",
{
id: serial("id").primaryKey(),
group_id: integer("group_id")
.notNull()
.references(() => core_groups.id, {
onDelete: "cascade"
}),
language_code: varchar("language_code")
.notNull()
.references(() => core_languages.code, {
onDelete: "cascade"
}),
value: varchar("value", { length: 255 }).notNull()
},
table => ({
group_id_idx: index("core_groups_names_group_id_idx").on(table.group_id),
language_code_idx: index("core_groups_names_language_code_idx").on(
table.language_code
)
})
);

export const core_groups_names_relations = relations(
core_groups_names,
Expand Down
28 changes: 18 additions & 10 deletions backend/src/apps/admin/core/database/schema/languages.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import {
boolean,
index,
integer,
pgTable,
serial,
varchar
} from "drizzle-orm/pg-core";

export const core_languages = pgTable("core_languages", {
id: serial("id").primaryKey(),
code: varchar("code", { length: 32 }).notNull().unique(),
name: varchar("name", { length: 255 }).notNull(),
timezone: varchar("timezone", { length: 255 }).notNull().default("UTC"),
protected: boolean("protected").notNull().default(false),
default: boolean("default").notNull().default(false),
enabled: boolean("enabled").notNull().default(true),
created: integer("created").notNull()
});
export const core_languages = pgTable(
"core_languages",
{
id: serial("id").primaryKey(),
code: varchar("code", { length: 32 }).notNull().unique(),
name: varchar("name", { length: 255 }).notNull(),
timezone: varchar("timezone", { length: 255 }).notNull().default("UTC"),
protected: boolean("protected").notNull().default(false),
default: boolean("default").notNull().default(false),
enabled: boolean("enabled").notNull().default(true),
created: integer("created").notNull()
},
table => ({
code_idx: index("core_languages_code_idx").on(table.code),
name_idx: index("core_languages_name_idx").on(table.name)
})
);
12 changes: 10 additions & 2 deletions backend/src/apps/admin/core/database/schema/moderators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { boolean, integer, pgTable, serial } from "drizzle-orm/pg-core";
import { boolean, index, integer, pgTable, serial } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";

import { core_groups } from "./groups";
Expand All @@ -18,7 +18,15 @@ export const core_moderators_permissions = pgTable(
created: integer("created").notNull(),
updated: integer("updated").notNull(),
protected: boolean("protected").notNull().default(false)
}
},
table => ({
group_id_idx: index("core_moderators_permissions_group_id_idx").on(
table.group_id
),
user_id_idx: index("core_moderators_permissions_user_id_idx").on(
table.user_id
)
})
);

export const core_moderators_permissions_relations = relations(
Expand Down
97 changes: 61 additions & 36 deletions backend/src/apps/admin/core/database/schema/nav.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { relations } from "drizzle-orm";
import {
boolean,
index,
integer,
pgTable,
serial,
Expand All @@ -9,14 +10,20 @@ import {

import { core_languages } from "./languages";

export const core_nav = pgTable("core_nav", {
id: serial("id").primaryKey(),
href: varchar("href", { length: 255 }).notNull(),
external: boolean("external").notNull().default(false),
position: integer("position").notNull().default(0),
// ! Warning: this is a recursive relation. It's not supported by drizzle-orm yet.
parent_id: integer("parent_id")
});
export const core_nav = pgTable(
"core_nav",
{
id: serial("id").primaryKey(),
href: varchar("href", { length: 255 }).notNull(),
external: boolean("external").notNull().default(false),
position: integer("position").notNull().default(0),
// ! Warning: this is a recursive relation. It's not supported by drizzle-orm yet.
parent_id: integer("parent_id")
},
table => ({
parent_id_idx: index("core_nav_parent_id_idx").on(table.parent_id)
})
);

export const core_nav_relations = relations(core_nav, ({ many, one }) => ({
name: many(core_nav_name),
Expand All @@ -27,20 +34,29 @@ export const core_nav_relations = relations(core_nav, ({ many, one }) => ({
})
}));

export const core_nav_name = pgTable("core_nav_name", {
id: serial("id").primaryKey(),
nav_id: serial("nav_id")
.notNull()
.references(() => core_nav.id, {
onDelete: "cascade"
}),
language_code: varchar("language_code")
.notNull()
.references(() => core_languages.code, {
onDelete: "cascade"
}),
value: varchar("value", { length: 50 }).notNull()
});
export const core_nav_name = pgTable(
"core_nav_name",
{
id: serial("id").primaryKey(),
nav_id: serial("nav_id")
.notNull()
.references(() => core_nav.id, {
onDelete: "cascade"
}),
language_code: varchar("language_code")
.notNull()
.references(() => core_languages.code, {
onDelete: "cascade"
}),
value: varchar("value", { length: 50 }).notNull()
},
table => ({
nav_id_idx: index("core_nav_name_nav_id_idx").on(table.nav_id),
language_code_idx: index("core_nav_name_language_code_idx").on(
table.language_code
)
})
);

export const core_nav_name_relations = relations(core_nav_name, ({ one }) => ({
nav: one(core_nav, {
Expand All @@ -53,20 +69,29 @@ export const core_nav_name_relations = relations(core_nav_name, ({ one }) => ({
})
}));

export const core_nav_description = pgTable("core_nav_description", {
id: serial("id").primaryKey(),
nav_id: serial("nav_id")
.notNull()
.references(() => core_nav.id, {
onDelete: "cascade"
}),
language_code: varchar("language_code")
.notNull()
.references(() => core_languages.code, {
onDelete: "cascade"
}),
value: varchar("value", { length: 50 }).notNull()
});
export const core_nav_description = pgTable(
"core_nav_description",
{
id: serial("id").primaryKey(),
nav_id: serial("nav_id")
.notNull()
.references(() => core_nav.id, {
onDelete: "cascade"
}),
language_code: varchar("language_code")
.notNull()
.references(() => core_languages.code, {
onDelete: "cascade"
}),
value: varchar("value", { length: 50 }).notNull()
},
table => ({
nav_id_idx: index("core_nav_description_nav_id_idx").on(table.nav_id),
language_code_idx: index("core_nav_description_language_code_idx").on(
table.language_code
)
})
);

export const core_nav_description_relations = relations(
core_nav_description,
Expand Down
Loading

0 comments on commit a9531cf

Please sign in to comment.