code refactor
This commit is contained in:
@@ -89,7 +89,7 @@ async function createTables() {
|
||||
conversation_id INT NOT NULL REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
|
||||
user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
joined_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
PRIMARY KEY (conversation_id, user_id)
|
||||
PRIMARY KEY (conversation_id, user_id) -- This composite primary key ensures uniqueness, but make sure that these fields are frequently queried together to avoid unnecessary performance overhead.
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_memberships_conversation_id ON Memberships (conversation_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_memberships_user_id ON Memberships (user_id);
|
||||
@@ -100,6 +100,7 @@ async function createTables() {
|
||||
}
|
||||
|
||||
try {
|
||||
// Create Contacts Table
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS Contacts (
|
||||
contact_id SERIAL PRIMARY KEY,
|
||||
@@ -113,10 +114,35 @@ async function createTables() {
|
||||
CREATE INDEX IF NOT EXISTS idx_contacts_conversation_id ON Contacts (conversation_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_contacts_contact_id ON Contacts (contact_id);
|
||||
`);
|
||||
console.log("Contacts table created successfully.");
|
||||
} catch (e) {
|
||||
console.error("Failed to create Contacts table: ", e);
|
||||
}
|
||||
|
||||
try {
|
||||
// Create GroupAdmins Table
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS GroupAdmins (
|
||||
conversation_id INT NOT NULL REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
|
||||
user_id INT NOT NULL REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
granted_by INT NOT NULL REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
granted_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
PRIMARY KEY (conversation_id, user_id),
|
||||
CONSTRAINT group_conversation_only CHECK (conversation_type = 'group'),
|
||||
CONSTRAINT admin_grants_admin CHECK (
|
||||
granted_by = user_id OR
|
||||
EXISTS (
|
||||
SELECT 1 FROM GroupAdmins ga
|
||||
WHERE ga.conversation_id = GroupAdmins.conversation_id
|
||||
AND ga.user_id = GroupAdmins.granted_by
|
||||
)
|
||||
)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_group_admins_conversation_id ON GroupAdmins (conversation_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_group_admins_user_id ON GroupAdmins (user_id);
|
||||
`);
|
||||
} catch (e) {
|
||||
console.error("Failed to create GroupAdmins table: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function insertUser(username, passwordHash) {
|
||||
|
||||
Reference in New Issue
Block a user