Add basic knex methods for channel data needed for /account/*

This commit is contained in:
Calvin Montgomery 2017-08-29 21:23:04 -07:00
parent 269aa6bfe6
commit 33b2bc2d30
3 changed files with 344 additions and 13 deletions

View file

@ -7,30 +7,27 @@ class AccountDB {
getByName(name) {
return this.db.runTransaction(async tx => {
const rows = await tx.table('users').where({ name }).select();
const user = await tx.table('users').where({ name }).first();
if (rows.length === 0) {
return null;
}
if (!user) return null;
return this.mapUser(rows[0]);
return this.mapUser(user);
});
}
updateByName(name, changedFields) {
return this.db.runTransaction(tx => {
return this.db.runTransaction(async tx => {
if (changedFields.profile) {
changedFields.profile = JSON.stringify(changedFields.profile);
}
return tx.table('users')
const rowsUpdated = await tx.table('users')
.update(changedFields)
.where({ name })
.then(rowsUpdated => {
if (rowsUpdated === 0) {
throw new Error(`Cannot update: name "${name}" does not exist`);
}
});
.where({ name });
if (rowsUpdated === 0) {
throw new Error(`Cannot update: name "${name}" does not exist`);
}
});
}

99
src/db/channel.js Normal file
View file

@ -0,0 +1,99 @@
import fs from 'fs';
import path from 'path';
import Promise from 'bluebird';
const unlinkAsync = Promise.promisify(fs.unlink);
class ChannelDB {
constructor(db) {
this.db = db;
}
getByName(name) {
return this.db.runTransaction(async tx => {
const channel = await tx.table('channels')
.where({ name })
.first();
if (!channel) return null;
return this.mapChannel(channel);
});
}
listByOwner(owner) {
return this.db.runTransaction(async tx => {
const rows = await tx.table('channels')
.where({ owner })
.select();
return rows.map(row => this.mapChannel(row));
});
}
insert(params) {
const { name, owner } = params;
return this.db.runTransaction(async tx => {
const existing = await tx.table('channels')
.where({ name })
.forUpdate()
.first();
if (existing) {
throw new Error(`Channel "${name}" is already registered.`);
}
await tx.table('channels')
.insert({
name,
owner,
time: Date.now(), // Old column, does not use datetime type
last_loaded: new Date(),
owner_last_seen: new Date()
});
await tx.table('channel_ranks')
.insert({
name: owner,
rank: 5,
channel: name
});
});
}
// TODO: should this be a soft-delete?
deleteByName(name) {
return this.db.runTransaction(async tx => {
const channel = await tx.table('channels')
.where({ name })
.forUpdate()
.first();
if (!channel) return;
await tx.table('channel_ranks').where({ channel: name }).del();
await tx.table('channel_bans').where({ channel: name }).del();
await tx.table('channel_libraries').where({ channel: name }).del();
await tx.table('channel_data').where({ channel_id: channel.id }).del();
await tx.table('channels').where({ name }).del();
// TODO: deprecate and remove flatfile chandumps
const chandump = path.resolve(__dirname, '..', '..', 'chandump', name);
try {
await unlinkAsync(chandump);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
});
}
mapChannel(channel) {
// TODO: fix to datetime column?
channel.time = new Date(channel.time);
return channel;
}
}
export { ChannelDB };