Better DB handling and stuff

This commit is contained in:
Pitu 2019-02-23 00:45:45 +09:00
parent df90d3157a
commit fc95cb7b0f
9 changed files with 274 additions and 1032 deletions

View File

@ -18,5 +18,6 @@ module.exports = {
},
seeds: {
directory: 'src/api/database/seeds'
}
},
useNullAsDefault: process.env.DB_CLIENT === 'sqlite' ? true : false
};

View File

@ -3,8 +3,8 @@ exports.up = async knex => {
table.increments();
table.string('username');
table.string('password');
table.boolean('enabled').defaultTo(true);
table.boolean('isAdmin').defaultTo(false);
table.boolean('enabled');
table.boolean('isAdmin');
table.string('apiKey');
table.timestamp('passwordEditedAt');
table.timestamp('apiKeyEditedAt');
@ -39,9 +39,9 @@ exports.up = async knex => {
table.integer('userId');
table.integer('albumId');
table.string('identifier');
table.integer('views').defaultTo(0);
table.boolean('enabled').defaultTo(true);
table.boolean('enableDownload').defaultTo(true);
table.integer('views');
table.boolean('enabled');
table.boolean('enableDownload');
table.timestamp('expiresAt');
table.timestamp('createdAt');
table.timestamp('editedAt');

View File

@ -16,6 +16,7 @@ exports.seed = async db => {
apiKeyEditedAt: now,
createdAt: now,
editedAt: now,
enabled: true,
isAdmin: true
});
console.log();

View File

@ -37,7 +37,8 @@ class linkPOST extends Route {
albumId,
enabled: true,
enableDownload: true,
expiresAt: null
expiresAt: null,
views: 0
});
return res.json({

View File

@ -51,7 +51,9 @@ class registerPOST extends Route {
apiKey: randomstring.generate(64),
apiKeyEditedAt: now,
createdAt: now,
editedAt: now
editedAt: now,
enabled: true,
isAdmin: false
});
return res.json({ message: 'The account was created successfully' });
}

View File

@ -118,19 +118,37 @@ class uploadPOST extends Route {
store the details on the database.
*/
const now = moment.utc().toDate();
let inserted = null;
let insertedId = null;
try {
inserted = await db.table('files').insert({
userId: user ? user.id : null,
name: upload.filename,
original: upload.originalname,
type: upload.mimetype || '',
size: upload.size,
hash,
ip: req.ip,
createdAt: now,
editedAt: now
}, 'id');
/*
This is so fucking dumb
*/
if (process.env.DB_CLIENT === 'sqlite3') {
insertedId = await db.table('files').insert({
userId: user ? user.id : null,
name: upload.filename,
original: upload.originalname,
type: upload.mimetype || '',
size: upload.size,
hash,
ip: req.ip,
createdAt: now,
editedAt: now
});
} else {
insertedId = await db.table('files').insert({
userId: user ? user.id : null,
name: upload.filename,
original: upload.originalname,
type: upload.mimetype || '',
size: upload.size,
hash,
ip: req.ip,
createdAt: now,
editedAt: now
}, 'id');
}
/*
TODO: Something funny here, I'm not sure since I don't use MySQL but I think the argument id
on the insert function on top behaves differently on psql/mysql/sqlite. Needs testing.
@ -155,7 +173,7 @@ class uploadPOST extends Route {
*/
if (albumId) {
try {
await db.table('albumsFiles').insert({ albumId, fileId: inserted[0] });
await db.table('albumsFiles').insert({ albumId, fileId: insertedId[0] });
await db.table('albums').where('id', albumId).update('editedAt', now);
} catch (error) {
log.error('There was an error updating editedAt on an album');

View File

@ -8,7 +8,8 @@ const db = require('knex')({
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
filename: nodePath.join(__dirname, '..', '..', '..', 'database.sqlite')
}
},
useNullAsDefault: process.env.DB_CLIENT === 'sqlite' ? true : false
});
const moment = require('moment');
const log = require('../utils/Log');

View File

@ -10,7 +10,8 @@ const db = require('knex')({
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
filename: path.join(__dirname, '..', '..', '..', 'database.sqlite')
}
},
useNullAsDefault: process.env.DB_CLIENT === 'sqlite' ? true : false
});
const moment = require('moment');
const log = require('../utils/Log');

1235
yarn.lock

File diff suppressed because it is too large Load Diff