Browse Source

Standarize database calls to support sqlite as well as mysql/postgres

Pitu 5 years ago
parent
commit
ac36cdc143
6 changed files with 99 additions and 14 deletions
  1. 4 2
      knexfile.js
  2. 8 4
      nuxt.config.js
  3. 2 1
      package.json
  4. 33 2
      src/api/structures/Route.js
  5. 1 0
      src/wizard.js
  6. 51 5
      yarn.lock

+ 4 - 2
knexfile.js

@@ -6,7 +6,8 @@ module.exports = {
 		host: process.env.DB_HOST,
 		user: process.env.DB_USER,
 		password: process.env.DB_PASSWORD,
-		database: process.env.DB_DATABASE
+		database: process.env.DB_DATABASE,
+		filename: 'database.sqlite'
 	},
 	pool: {
 		min: process.env.DATABASE_POOL_MIN || 2,
@@ -17,5 +18,6 @@ module.exports = {
 	},
 	seeds: {
 		directory: 'src/api/database/seeds'
-	}
+	},
+	useNullAsDefault: process.env.DB_CLIENT === 'sqlite3' ? true : false
 };

+ 8 - 4
nuxt.config.js

@@ -8,6 +8,7 @@ export default {
 		port: process.env.WEBSITE_PORT
 	},
 	env: {
+		development: process.env.NODE_ENV !== 'production',
 		version: process.env.npm_package_version,
 		URL: process.env.DOMAIN,
 		baseURL: `${process.env.DOMAIN}${process.env.ROUTE_PREFIX}`,
@@ -66,15 +67,18 @@ export default {
 	],
 	css: [],
 	modules: [
-		'@nuxtjs/axios'
+		'@nuxtjs/axios',
+		'cookie-universal-nuxt'
 	],
 	axios: {
 		baseURL: `${process.env.DOMAIN}${process.env.ROUTE_PREFIX}`
 	},
 	build: {
 		extractCSS: true,
-		postcss: [
-			autoprefixer
-		]
+		postcss: {
+			preset: {
+				autoprefixer
+			}
+		}
 	}
 };

+ 2 - 1
package.json

@@ -39,7 +39,7 @@
 		"busboy": "^0.2.14",
 		"chalk": "^2.4.1",
 		"compression": "^1.7.2",
-		"cookieparser": "^0.1.0",
+		"cookie-universal-nuxt": "^2.0.14",
 		"cors": "^2.8.5",
 		"dotenv": "^6.2.0",
 		"dumper.js": "^1.3.1",
@@ -65,6 +65,7 @@
 		"randomstring": "^1.1.5",
 		"serve-static": "^1.13.2",
 		"sharp": "^0.21.3",
+		"sqlite3": "^4.0.6",
 		"uuid": "^3.3.2",
 		"v-clipboard": "^2.2.1",
 		"vue-axios": "^2.1.4",

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

@@ -1,3 +1,4 @@
+const nodePath = require('path');
 const JWT = require('jsonwebtoken');
 const db = require('knex')({
 	client: process.env.DB_CLIENT,
@@ -5,8 +6,38 @@ const db = require('knex')({
 		host: process.env.DB_HOST,
 		user: process.env.DB_USER,
 		password: process.env.DB_PASSWORD,
-		database: process.env.DB_DATABASE
-	}
+		database: process.env.DB_DATABASE,
+		filename: nodePath.join(__dirname, '..', '..', '..', 'database.sqlite')
+	},
+	postProcessResponse: result => {
+		/*
+			Fun fact: Depending on the database used by the user and given that I don't want
+			to force a specific database for everyone because of the nature of this project,
+			some things like different data types for booleans need to be considered like in
+			the implementation below where sqlite returns 1 and 0 instead of true and false.
+		*/
+		const booleanFields = [
+			'enabled',
+			'enableDownload',
+			'isAdmin'
+		];
+
+		const processResponse = row => {
+			Object.keys(row).forEach(key => {
+				if (booleanFields.includes(key)) {
+					row[key] = row[key] === 1 ? true : false;
+				}
+			});
+			return row;
+		};
+
+		if (Array.isArray(result)) {
+			return result.map(row => processResponse(row));
+		}
+
+		return processResponse(result);
+	},
+	useNullAsDefault: process.env.DB_CLIENT === 'sqlite3' ? true : false
 });
 const moment = require('moment');
 const log = require('../utils/Log');

+ 1 - 0
src/wizard.js

@@ -105,6 +105,7 @@ async function start() {
 			handle: 'DB_CLIENT',
 			symbol: '>',
 			menu: [
+				'sqlite3',
 				'pg',
 				'mysql'
 			]

+ 51 - 5
yarn.lock

@@ -922,6 +922,11 @@
   resolved "https://registry.yarnpkg.com/@types/bluebird/-/bluebird-3.5.25.tgz#59188b871208092e37767e4b3d80c3b3eaae43bd"
   integrity sha512-yfhIBix+AIFTmYGtkC0Bi+XGjSkOINykqKvO/Wqdz/DuXlAKK7HmhLAXdPIGsV4xzKcL3ev/zYc4yLNo+OvGaw==
 
+"@types/cookie@^0.3.1":
+  version "0.3.2"
+  resolved "https://registry.yarnpkg.com/@types/cookie/-/cookie-0.3.2.tgz#453f4b14b25da6a8ea4494842dedcbf0151deef9"
+  integrity sha512-aHQA072E10/8iUQsPH7mQU/KUyQBZAGzTVRCUvnSz8mSvbrYsP4xEO2RSA0Pjltolzi0j8+8ixrm//Hr4umPzw==
+
 "@types/q@^1.5.1":
   version "1.5.1"
   resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.1.tgz#48fd98c1561fe718b61733daed46ff115b496e18"
@@ -2287,16 +2292,27 @@ cookie-signature@1.0.6:
   resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c"
   integrity sha1-4wOogrNCzD7oylE6eZmXNNqzriw=
 
+cookie-universal-nuxt@^2.0.14:
+  version "2.0.14"
+  resolved "https://registry.yarnpkg.com/cookie-universal-nuxt/-/cookie-universal-nuxt-2.0.14.tgz#6fdf8e928eadd7611c04a57614fe2e29b60eb971"
+  integrity sha512-ih9Z0Z2K6eLaugTttGCVN85nogKseIFF/dqup3klvYC4mQS3+1IloqBqzTL/N7degBBAols2oppwYNDmaRtVig==
+  dependencies:
+    "@types/cookie" "^0.3.1"
+    cookie-universal "^2.0.14"
+
+cookie-universal@^2.0.14:
+  version "2.0.14"
+  resolved "https://registry.yarnpkg.com/cookie-universal/-/cookie-universal-2.0.14.tgz#1b4f27cffccfc2e47703fa235c1f67f931213041"
+  integrity sha512-m6J0DQa4/RQvXhzUG37EY1ynK3Uq1BKzp5hotST9olrzjrRx+B0vNPx7azg0/X0XrYQvL7MMbPXwou8m0BNDwg==
+  dependencies:
+    "@types/cookie" "^0.3.1"
+    cookie "^0.3.1"
+
 cookie@0.3.1, cookie@^0.3.1:
   version "0.3.1"
   resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb"
   integrity sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=
 
-cookieparser@^0.1.0:
-  version "0.1.0"
-  resolved "https://registry.yarnpkg.com/cookieparser/-/cookieparser-0.1.0.tgz#ea12cb1085c174f3167faeaf7985f79abe671d0e"
-  integrity sha1-6hLLEIXBdPMWf66veYX3mr5nHQ4=
-
 copy-concurrently@^1.0.0:
   version "1.0.5"
   resolved "https://registry.yarnpkg.com/copy-concurrently/-/copy-concurrently-1.0.5.tgz#92297398cae34937fcafd6ec8139c18051f0b5e0"
@@ -5421,6 +5437,11 @@ nan@2.12.1, nan@^2.10.0, nan@^2.12.1, nan@^2.9.2:
   resolved "https://registry.yarnpkg.com/nan/-/nan-2.12.1.tgz#7b1aa193e9aa86057e3c7bbd0ac448e770925552"
   integrity sha512-JY7V6lRkStKcKTvHO5NVSQRv+RV+FIL5pvDoLiAtSL9pKlC5x9PKQcZDsq7m4FO4d57mkhC6Z+QhAh3Jdk5JFw==
 
+nan@~2.10.0:
+  version "2.10.0"
+  resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f"
+  integrity sha512-bAdJv7fBLhWC+/Bls0Oza+mvTaNQtP+1RyhhhvD95pgUJz6XM5IzgmxOkItJ9tkoCiplvAnXI1tNmmUD/eScyA==
+
 nanomatch@^1.2.9:
   version "1.2.13"
   resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
@@ -5580,6 +5601,22 @@ node-pre-gyp@^0.10.0:
     semver "^5.3.0"
     tar "^4"
 
+node-pre-gyp@^0.11.0:
+  version "0.11.0"
+  resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.11.0.tgz#db1f33215272f692cd38f03238e3e9b47c5dd054"
+  integrity sha512-TwWAOZb0j7e9eGaf9esRx3ZcLaE5tQ2lvYy1pb5IAaG1a2e2Kv5Lms1Y4hpj+ciXJRofIxxlt5haeQ/2ANeE0Q==
+  dependencies:
+    detect-libc "^1.0.2"
+    mkdirp "^0.5.1"
+    needle "^2.2.1"
+    nopt "^4.0.1"
+    npm-packlist "^1.1.6"
+    npmlog "^4.0.2"
+    rc "^1.2.7"
+    rimraf "^2.6.1"
+    semver "^5.3.0"
+    tar "^4"
+
 node-releases@^1.1.3:
   version "1.1.7"
   resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.7.tgz#b09a10394d0ed8f7778f72bb861dde68b146303b"
@@ -7868,6 +7905,15 @@ sprintf-js@~1.0.2:
   resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
   integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=
 
+sqlite3@^4.0.6:
+  version "4.0.6"
+  resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-4.0.6.tgz#e587b583b5acc6cb38d4437dedb2572359c080ad"
+  integrity sha512-EqBXxHdKiwvNMRCgml86VTL5TK1i0IKiumnfxykX0gh6H6jaKijAXvE9O1N7+omfNSawR2fOmIyJZcfe8HYWpw==
+  dependencies:
+    nan "~2.10.0"
+    node-pre-gyp "^0.11.0"
+    request "^2.87.0"
+
 sqlstring@2.3.1:
   version "2.3.1"
   resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.1.tgz#475393ff9e91479aea62dcaf0ca3d14983a7fb40"