Bläddra i källkod

Better DB handling and stuff

Pitu 6 år sedan
förälder
incheckning
fc95cb7b0f

+ 2 - 1
knexfile.js

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

+ 5 - 5
src/api/database/migrations/20190221225812_initialMigration.js

@@ -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');

+ 1 - 0
src/api/database/seeds/initial.js

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

+ 2 - 1
src/api/routes/albums/link/linkPOST.js

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

+ 3 - 1
src/api/routes/auth/registerPOST.js

@@ -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' });
 	}

+ 31 - 13
src/api/routes/files/uploadPOST.js

@@ -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');

+ 2 - 1
src/api/structures/Route.js

@@ -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');

+ 2 - 1
src/api/utils/Util.js

@@ -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');

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 226 - 1009
yarn.lock