Add basic knex methods to be used for /account/* pages
This commit is contained in:
parent
162f8fd9b5
commit
269aa6bfe6
2 changed files with 217 additions and 0 deletions
65
src/db/account.js
Normal file
65
src/db/account.js
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
const LOGGER = require('@calzoneman/jsli')('AccountDB');
|
||||
|
||||
class AccountDB {
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
getByName(name) {
|
||||
return this.db.runTransaction(async tx => {
|
||||
const rows = await tx.table('users').where({ name }).select();
|
||||
|
||||
if (rows.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.mapUser(rows[0]);
|
||||
});
|
||||
}
|
||||
|
||||
updateByName(name, changedFields) {
|
||||
return this.db.runTransaction(tx => {
|
||||
if (changedFields.profile) {
|
||||
changedFields.profile = JSON.stringify(changedFields.profile);
|
||||
}
|
||||
|
||||
return tx.table('users')
|
||||
.update(changedFields)
|
||||
.where({ name })
|
||||
.then(rowsUpdated => {
|
||||
if (rowsUpdated === 0) {
|
||||
throw new Error(`Cannot update: name "${name}" does not exist`);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
mapUser(user) {
|
||||
// Backwards compatibility
|
||||
// Maybe worth backfilling one day to be done with it?
|
||||
try {
|
||||
let profile;
|
||||
|
||||
if (!user.profile) {
|
||||
profile = { image: '', text: '' };
|
||||
} else {
|
||||
profile = JSON.parse(user.profile);
|
||||
}
|
||||
|
||||
if (!profile.image) profile.image = '';
|
||||
if (!profile.text) profile.text = '';
|
||||
|
||||
user.profile = profile;
|
||||
} catch (error) {
|
||||
// TODO: backfill erroneous records and remove this check
|
||||
LOGGER.warn('Invalid profile "%s": %s', user.profile, error);
|
||||
user.profile = { image: '', text: '' };
|
||||
}
|
||||
|
||||
user.time = new Date(user.time);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
export { AccountDB };
|
||||
Loading…
Add table
Add a link
Reference in a new issue