Skip to content

Commit

Permalink
removed hash_id.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgul committed May 20, 2024
1 parent f8d7b68 commit 1ac07f4
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 122 deletions.
521 changes: 521 additions & 0 deletions src/server/knex/migrations/20240519053307_hash_primary_key.ts

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions src/server/knex/seeds/admin_role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ exports.seed = async(knex) =>
// Check for admin records, and add them if they're not already there.
const admins = [
{
account_id: 1,
hash_id: '3VAAgA',
account_id: '3VAAgA',
email: '[email protected]',
name: 'Morgul',
avatar: 'https://lh3.googleusercontent.com/-r8fmbWdlFvg/AAAAAAAAAAI/AAAAAAAAA9g/oWyh8pnmDSY/s96-c/photo.jpg?sz=512',
Expand All @@ -28,8 +27,7 @@ exports.seed = async(knex) =>
created: knex.fn.now()
},
{
account_id: 2,
hash_id: '3VzoXi',
account_id: '3VzoXi',
email: '[email protected]',
name: 'Lord Null',
avatar: 'https://lh6.googleusercontent.com/-uCa4jsA-_i0/AAAAAAAAAAI/AAAAAAAAAB4/chnr3xPv3_c/s96-c/photo.jpg?sz=512',
Expand All @@ -41,15 +39,13 @@ exports.seed = async(knex) =>

const mods = [
{
account_id: 3,
hash_id: '3Ks11R',
account_id: '3Ks11R',
email: '[email protected]',
name: 'Burstaholic',
avatar: 'https://lh6.googleusercontent.com/-Cm7eBAJV2gQ/AAAAAAAAAAI/AAAAAAAAAXE/KLFM6YmcWm8/s96-c/photo.jpg?sz=512',
permissions: '[]',
settings: '{}',
created: knex.fn.now()

}
];

Expand Down
27 changes: 10 additions & 17 deletions src/server/managers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function list(filters : AccountFilters) : Promise<Account[]>
const db = await getDB();
const query = db('account')
.select(
'hash_id as id',
'account_id as id',
'email',
'name',
'avatar',
Expand All @@ -37,7 +37,7 @@ export async function list(filters : AccountFilters) : Promise<Account[]>

if(filters.id)
{
query.where({ hash_id: filters.id });
query.where({ account_id: filters.id });
}

if(filters.email)
Expand All @@ -61,27 +61,26 @@ export async function getGroups(accountID : string) : Promise<string[]>
.join('account_role as ar', 'ac.account_id', '=', 'ar.account_id')
.join('role as r', 'ar.role_id', '=', 'r.role_id')
.where({
'ac.hash_id': accountID
'ac.account_id': accountID
});

return roles.map((role) => role.name);
}

export async function getRaw(accountID : string) : Promise<Record<string, unknown>>
export async function get(accountID : string) : Promise<Account>
{
const db = await getDB();
const accounts = await db('account')
.select(
'account_id',
'hash_id as id',
'account_id as id',
'email',
'name',
'avatar',
'permissions',
'settings'
)
.where({
hash_id: accountID
account_id: accountID
});

if(accounts.length > 1)
Expand All @@ -95,22 +94,16 @@ export async function getRaw(accountID : string) : Promise<Record<string, unknow
else
{
const groups = await getGroups(accountID);
return { ...accounts[0], groups };
return Account.fromDB({ ...accounts[0], groups });
}
}

export async function get(accountID : string) : Promise<Account>
{
const { account_id, ...restAccount } = await getRaw(accountID);
return Account.fromDB(restAccount);
}

export async function getByEmail(email : string) : Promise<Account>
{
const db = await getDB();
const accounts = await db('account')
.select(
'hash_id as id',
'account_id as id',
'email',
'name',
'avatar',
Expand Down Expand Up @@ -164,7 +157,7 @@ export async function update(accountID : string, accountUpdate : Record<string,
const db = await getDB();
await db('account')
.update(newAccount.toDB())
.where({ hash_id: accountID });
.where({ account_id: accountID });

// Return the updated record
return get(accountID);
Expand All @@ -174,7 +167,7 @@ export async function remove(accountID : string) : Promise<{ status : 'ok' }>
{
const db = await getDB();
await db('account')
.where({ hash_id: accountID })
.where({ account_id: accountID })
.delete();

return { status: 'ok' };
Expand Down
33 changes: 12 additions & 21 deletions src/server/managers/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//----------------------------------------------------------------------------------------------------------------------

// Managers
import * as accountMan from './account';
import * as notebookMan from './notebook';
import systemMan from './system';

Expand All @@ -25,7 +24,7 @@ export async function get(id : string) : Promise<Character>
const db = await getDB();
const characters = await db('character as char')
.select(
'char.hash_id as id',
'char.character_id as id',
'char.system',
'char.name',
'char.description',
Expand All @@ -34,12 +33,12 @@ export async function get(id : string) : Promise<Character>
'char.color',
'char.campaign',
'char.details',
'acc.hash_id as accountID',
'note.hash_id as noteID'
'acc.account_id as accountID',
'note.note_id as noteID'
)
.join('note', 'note.note_id', '=', 'char.note_id')
.join('account as acc', 'acc.account_id', '=', 'char.account_id')
.where({ 'char.hash_id': id });
.where({ 'char.character_id': id });

if(characters.length > 1)
{
Expand All @@ -54,14 +53,14 @@ export async function get(id : string) : Promise<Character>
const char = Character.fromDB(characters[0]);
return systemMan.validateCharacterDetails(char);
}
} // get
}

export async function list(filters : Record<string, FilterToken> = {}) : Promise<Character[]>
{
const db = await getDB();
let query = db('character as char')
.select(
'char.hash_id as id',
'char.character_id as id',
'char.system',
'char.name',
'char.description',
Expand All @@ -70,8 +69,8 @@ export async function list(filters : Record<string, FilterToken> = {}) : Promise
'char.color',
'char.campaign',
'char.details',
'acc.hash_id as accountID',
'note.hash_id as noteID'
'acc.account_id as accountID',
'note.note_id as noteID'
)
.join('note', 'note.note_id', '=', 'char.note_id')
.join('account as acc', 'acc.account_id', '=', 'char.account_id');
Expand All @@ -90,13 +89,9 @@ export async function add(accountID : string, newCharacter : Record<string, unkn

const char = Character.fromJSON({ ...newCharacter, id: shortID(), noteID: notebook.id, accountID });

// FIXME: These hacks should be removed, and `hash_id` should be the foreign_key
const { account_id } = await accountMan.getRaw(char.accountID);
const { note_id } = await notebookMan.getRaw(char.noteID);

const db = await getDB();
await db('character')
.insert({ ...char.toDB(), account_id, note_id });
.insert(char.toDB());

// We know this is a string since it's set above.
return get(char.id as string);
Expand All @@ -121,15 +116,11 @@ export async function update(charID : string, updateChar : Record<string, unknow
// Make a new character object
const newCharacter = Character.fromJSON(allowedUpdate);

// FIXME: These hacks should be removed, and `hash_id` should be the foreign_key
const { account_id } = await accountMan.getRaw(char.accountID);
const { note_id } = await notebookMan.getRaw(char.noteID);

// Update the database
const db = await getDB();
await db('character')
.update({ ...newCharacter.toDB(), account_id, note_id })
.where({ hash_id: charID });
.update(newCharacter.toDB())
.where({ character_id: charID });

const newChar = await get(charID);

Expand All @@ -148,7 +139,7 @@ export async function remove(charID : string) : Promise<{ status : 'ok' }>
{
const db = await getDB();
await db('character')
.where({ hash_id: charID })
.where({ character_id: charID })
.delete();

// Broadcast the update
Expand Down
50 changes: 10 additions & 40 deletions src/server/managers/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export async function get(notebookID : string) : Promise<Notebook>
const pages = (await db('note_page as np')
.select(
'page_id as id',
'n.hash_id as notebookID',
'n.note_id as notebookID',
'content',
'title'
)
.join('note as n', 'n.note_id', '=', 'np.note_id')
.where({ 'n.hash_id': notebookID }))
.where({ 'n.note_id': notebookID }))
.map(NotebookPage.fromDB);

return Notebook.fromDB({ id: notebookID, pages });
Expand All @@ -41,15 +41,15 @@ export async function list(filters : NoteFilters) : Promise<Notebook[]>
{
const db = await getDB();
const query = db('note as n')
.select('n.hash_id as notebookID')
.distinct('n.hash_id')
.select('n.note_id as notebookID')
.distinct('n.note_id')
.leftJoin('note_page as np', 'np.note_id', '=', 'n.note_id')
.join('character as c', 'c.note_id', '=', 'n.note_id')
.join('account as a', 'a.account_id', '=', 'c.account_id');

if(filters.id)
{
query.where({ 'n.hash_id': filters.id });
query.where({ 'n.note_id': filters.id });
}

if(filters.email)
Expand All @@ -69,34 +69,13 @@ export async function list(filters : NoteFilters) : Promise<Notebook[]>
}));
}

export async function getRaw(notebookID : string) : Promise<Record<string, unknown>>
{
const db = await getDB();
const notebooks = await db('note')
.select()
.where({ hash_id: notebookID });

if(notebooks.length > 1)
{
throw new MultipleResultsError('notebook');
}
else if(notebooks.length === 0)
{
throw new NotFoundError(`No notebook record found for notebook '${ notebookID }'.`);
}
else
{
return notebooks[0];
}
}

export async function getPage(pageID : string | number) : Promise<NotebookPage>
{
const db = await getDB();
const pages = await db('note_page as np')
.select(
'page_id as id',
'n.hash_id as notebookID',
'n.note_id as notebookID',
'content',
'title'
)
Expand All @@ -122,16 +101,8 @@ export async function addPage(notebookID : string, page : Record<string, unknown
const db = await getDB();
const notePage = NotebookPage.fromJSON({ ...page, notebookID });

const [ notebook ] = await db('note')
.select('note_id as id')
.where({ hash_id: notebookID })
.catch(() =>
{
throw new NotFoundError(`No notebook record found for id '${ notebookID }'`);
});

const [ pageID ] = await db('note_page')
.insert({ ...notePage.toDB(), note_id: notebook.id });
.insert(notePage.toDB());

// Return the notebook page
return getPage(pageID);
Expand All @@ -143,7 +114,7 @@ export async function add(pages : Record<string, unknown>[] = []) : Promise<Note
const newNoteID = shortID();

await db('note')
.insert({ hash_id: newNoteID });
.insert({ note_id: newNoteID });

// Add any pages that were specified
await Promise.all(pages.map(async(page) =>
Expand All @@ -170,8 +141,7 @@ export async function updatePage(pageID : string | number, pageUpdate : Record<s
// Make a new page object
const newPage = NotebookPage.fromJSON(allowedUpdate);

// FIXME: We have to drop this, since we don't want to update it. This will be fixed if we switch to having a
// foreign key on the hash_id.
// Drop the note_id from the update
const { note_id, ...dbRest } = newPage.toDB();

// Update the database
Expand All @@ -198,7 +168,7 @@ export async function remove(notebookID : string) : Promise<{ status : 'ok' }>
{
const db = await getDB();
await db('note')
.where({ hash_id: notebookID })
.where({ note_id: notebookID })
.delete();

return { status: 'ok' };
Expand Down
Loading

0 comments on commit 1ac07f4

Please sign in to comment.