]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/plugin.ts
WIP plugins: add storage manager
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
index 340d49f3b251f68bfd5bfd85dd1f01baa5e1db12..bd3d7a81ec46443ad6e11149a98febc55d9998e9 100644 (file)
@@ -9,7 +9,7 @@ import {
 } from '../../helpers/custom-validators/plugins'
 import { PluginType } from '../../../shared/models/plugins/plugin.type'
 import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
-import { FindAndCountOptions } from 'sequelize'
+import { FindAndCountOptions, json } from 'sequelize'
 
 @DefaultScope(() => ({
   attributes: {
@@ -142,6 +142,40 @@ export class PluginModel extends Model<PluginModel> {
       .then(() => undefined)
   }
 
+  static getData (pluginName: string, pluginType: PluginType, key: string) {
+    const query = {
+      raw: true,
+      attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
+      where: {
+        name: pluginName,
+        type: pluginType
+      }
+    }
+
+    return PluginModel.findOne(query)
+      .then((c: any) =>  {
+        if (!c) return undefined
+
+        return c.value
+      })
+  }
+
+  static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
+    const query = {
+      where: {
+        name: pluginName,
+        type: pluginType
+      }
+    }
+
+    const toSave = {
+      [`storage.${key}`]: data
+    }
+
+    return PluginModel.update(toSave, query)
+                      .then(() => undefined)
+  }
+
   static listForApi (options: {
     type?: PluginType,
     uninstalled?: boolean,