From aadda310693b0789216945f2ef1b1510826c907d Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 12 Oct 2020 19:57:35 +0300 Subject: [PATCH] Add metadata field for threads Useful for e.g. storing plugin-specific data about a thread. --- src/data/Thread.js | 31 +++++++++++++++++++ ...012183935_add_metadata_field_to_threads.js | 11 +++++++ 2 files changed, 42 insertions(+) create mode 100644 src/data/migrations/20201012183935_add_metadata_field_to_threads.js diff --git a/src/data/Thread.js b/src/data/Thread.js index ac32c9d..ef1373d 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -28,6 +28,7 @@ const {THREAD_MESSAGE_TYPE, THREAD_STATUS, DISCORD_MESSAGE_ACTIVITY_TYPES} = req * @property {String} log_storage_type * @property {Object} log_storage_data * @property {String} created_at + * @property {String} metadata */ class Thread { constructor(props) { @@ -38,6 +39,12 @@ class Thread { this.log_storage_data = JSON.parse(props.log_storage_data); } } + + if (props.metadata) { + if (typeof props.metadata === "string") { + this.metadata = JSON.parse(props.metadata); + } + } } getSQLProps() { @@ -767,6 +774,30 @@ class Thread { log_storage_data, }); } + + /** + * @param {string} key + * @param {*} value + * @return {Promise} + */ + async setMetadataValue(key, value) { + this.metadata = this.metadata || {}; + this.metadata[key] = value; + + await knex("threads") + .where("id", this.id) + .update({ + metadata: this.getSQLProps().metadata, + }); + } + + /** + * @param {string} key + * @returns {*} + */ + getMetadataValue(key) { + return this.metadata ? this.metadata[key] : null; + } } module.exports = Thread; diff --git a/src/data/migrations/20201012183935_add_metadata_field_to_threads.js b/src/data/migrations/20201012183935_add_metadata_field_to_threads.js new file mode 100644 index 0000000..d567f20 --- /dev/null +++ b/src/data/migrations/20201012183935_add_metadata_field_to_threads.js @@ -0,0 +1,11 @@ +exports.up = async function(knex) { + await knex.schema.table("threads", table => { + table.text("metadata").nullable().defaultTo(null); + }); +}; + +exports.down = async function(knex) { + await knex.schema.table("threads", table => { + table.dropColumn("metadata"); + }); +};