Add support for SQLite and PostgreSQL. Also add/fix encryption...

...for a smaller number of fields
This commit is contained in:
Isaac
2022-07-23 01:44:03 +01:00
parent 08e757febf
commit 3de9cd8c3a
11 changed files with 859 additions and 9 deletions

View File

@@ -6,6 +6,8 @@ const I18n = require('@eartharoid/i18n');
const fs = require('fs');
const { join } = require('path');
const YAML = require('yaml');
const encryptionMiddleware = require('./lib/middleware/prisma-encryption');
const typesMiddleware = require('./lib/middleware/prisma-types');
module.exports = class Client extends FrameworkClient {
constructor(config, log) {
@@ -36,6 +38,8 @@ module.exports = class Client extends FrameworkClient {
async login(token) {
/** @type {PrismaClient} */
this.prisma = new PrismaClient();
this.prisma.$use(encryptionMiddleware);
this.prisma.$use(typesMiddleware);
this.keyv = new Keyv();
return super.login(token);
}

View File

@@ -0,0 +1,49 @@
const Cryptr = require('cryptr');
const cryptr = new Cryptr(process.env.ENCRYPTION_KEY);
const encryptedFields = [
// 'name',
'content',
'username',
'displayName',
// 'channelName',
// 'openingMessage',
// 'description',
'value',
// 'placeholder',
'closedReason',
'topic',
'comment',
// 'label',
// 'regex',
];
const encrypt = obj => {
for (const prop in obj) {
if (typeof obj[prop] === 'string' && obj[prop].length !== 0 && encryptedFields.includes(prop)) {
obj[prop] = cryptr.encrypt(obj[prop]);
} else if (typeof obj[prop] === 'object') {
obj[prop] = encrypt(obj[prop]);
}
}
return obj;
};
const decrypt = obj => {
for (const prop in obj) {
if (typeof obj[prop] === 'string' && obj[prop].length !== 0 && encryptedFields.includes(prop)) {
obj[prop] = cryptr.decrypt(obj[prop]);
} else if (typeof obj[prop] === 'object') {
obj[prop] = decrypt(obj[prop]);
}
}
return obj;
};
module.exports = async (params, next) => {
if (params.args.create) params.args.create = encrypt(params.args.create);
if (params.args.data) params.args.data = encrypt(params.args.data);
if (params.args.update) params.args.update = encrypt(params.args.update);
let result = await next(params);
if (result) result = decrypt(result);
return result;
};

View File

@@ -0,0 +1,35 @@
const jsonFields = [
'pingRoles',
'requiredRoles',
'staffRoles',
'autoTag',
'blocklist',
'workingHours',
'options',
'pinnedMessages',
];
const traverse = (obj, func) => {
for (const prop in obj) {
console.log(prop, typeof obj[prop], obj[prop]);
if (jsonFields.includes(prop) && obj[prop] !== null && obj[prop] !== undefined) {
obj[prop] = func(obj[prop]);
} else if (typeof obj[prop] === 'object') {
obj[prop] = traverse(obj[prop], func);
}
}
return obj;
};
module.exports = async (params, next) => {
if (process.env.DB_PROVIDER === 'sqlite') {
if (params.args.create) params.args.create = traverse(params.args.create, val => JSON.stringify(val));
if (params.args.data) params.args.data = traverse(params.args.data, val => JSON.stringify(val));
if (params.args.update) params.args.update = traverse(params.args.update, val => JSON.stringify(val));
let result = await next(params);
if (result) result = traverse(result, val => JSON.parse(val));
return result;
} else {
return await next(params);
}
};

View File

@@ -78,7 +78,8 @@ module.exports.post = fastify => ({
data: {
guild: { connect: { id: guild.id } },
...data,
questions: { createMany: { data: data.questions ?? [] } },
// questions: { createMany: { data: data.questions ?? [] } },
questions: { create: { data: data.questions ?? [] } }, // sqlite doesn't support createMany?
},
});