Fix plugin storeData
This commit is contained in:
parent
15b4bcdf04
commit
97b65ce58a
|
@ -1,5 +1,5 @@
|
||||||
import * as Bluebird from 'bluebird'
|
import * as Bluebird from 'bluebird'
|
||||||
import { FindAndCountOptions, json } from 'sequelize'
|
import { FindAndCountOptions, json, QueryTypes } from 'sequelize'
|
||||||
import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
|
||||||
import { MPlugin, MPluginFormattable } from '@server/typings/models'
|
import { MPlugin, MPluginFormattable } from '@server/typings/models'
|
||||||
import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
|
import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
|
||||||
|
@ -212,18 +212,17 @@ export class PluginModel extends Model<PluginModel> {
|
||||||
}
|
}
|
||||||
|
|
||||||
static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
|
static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
|
||||||
const query = {
|
const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' +
|
||||||
where: {
|
'WHERE "name" = :pluginName AND "type" = :pluginType'
|
||||||
name: pluginName,
|
|
||||||
type: pluginType
|
const jsonPath = '{' + key + '}'
|
||||||
}
|
|
||||||
|
const options = {
|
||||||
|
replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) },
|
||||||
|
type: QueryTypes.UPDATE
|
||||||
}
|
}
|
||||||
|
|
||||||
const toSave = {
|
return PluginModel.sequelize.query(query, options)
|
||||||
[`storage.${key}`]: data
|
|
||||||
}
|
|
||||||
|
|
||||||
return PluginModel.update(toSave, query)
|
|
||||||
.then(() => undefined)
|
.then(() => undefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
25
server/tests/fixtures/peertube-plugin-test-six/main.js
vendored
Normal file
25
server/tests/fixtures/peertube-plugin-test-six/main.js
vendored
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
async function register ({
|
||||||
|
storageManager,
|
||||||
|
peertubeHelpers
|
||||||
|
}) {
|
||||||
|
const { logger } = peertubeHelpers
|
||||||
|
|
||||||
|
{
|
||||||
|
await storageManager.storeData('superkey', { value: 'toto' })
|
||||||
|
await storageManager.storeData('anotherkey', { value: 'toto2' })
|
||||||
|
|
||||||
|
const result = await storageManager.getData('superkey')
|
||||||
|
logger.info('superkey stored value is %s', result.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function unregister () {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
register,
|
||||||
|
unregister
|
||||||
|
}
|
||||||
|
|
||||||
|
// ###########################################################################
|
20
server/tests/fixtures/peertube-plugin-test-six/package.json
vendored
Normal file
20
server/tests/fixtures/peertube-plugin-test-six/package.json
vendored
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"name": "peertube-plugin-test-six",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "Plugin test 6",
|
||||||
|
"engine": {
|
||||||
|
"peertube": ">=1.3.0"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"peertube",
|
||||||
|
"plugin"
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/Chocobozzz/PeerTube",
|
||||||
|
"author": "Chocobozzz",
|
||||||
|
"bugs": "https://github.com/Chocobozzz/PeerTube/issues",
|
||||||
|
"library": "./main.js",
|
||||||
|
"staticDirs": {},
|
||||||
|
"css": [],
|
||||||
|
"clientScripts": [],
|
||||||
|
"translations": {}
|
||||||
|
}
|
|
@ -6,3 +6,4 @@ import './translations'
|
||||||
import './video-constants'
|
import './video-constants'
|
||||||
import './plugin-helpers'
|
import './plugin-helpers'
|
||||||
import './plugin-router'
|
import './plugin-router'
|
||||||
|
import './plugin-storage'
|
||||||
|
|
30
server/tests/plugins/plugin-storage.ts
Normal file
30
server/tests/plugins/plugin-storage.ts
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
||||||
|
|
||||||
|
import 'mocha'
|
||||||
|
import { getPluginTestPath, installPlugin, setAccessTokensToServers } from '../../../shared/extra-utils'
|
||||||
|
import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers'
|
||||||
|
|
||||||
|
describe('Test plugin storage', function () {
|
||||||
|
let server: ServerInfo
|
||||||
|
|
||||||
|
before(async function () {
|
||||||
|
this.timeout(30000)
|
||||||
|
|
||||||
|
server = await flushAndRunServer(1)
|
||||||
|
await setAccessTokensToServers([ server ])
|
||||||
|
|
||||||
|
await installPlugin({
|
||||||
|
url: server.url,
|
||||||
|
accessToken: server.accessToken,
|
||||||
|
path: getPluginTestPath('-six')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Should correctly store a subkey', async function () {
|
||||||
|
await waitUntilLog(server, 'superkey stored value is toto')
|
||||||
|
})
|
||||||
|
|
||||||
|
after(async function () {
|
||||||
|
await cleanupTests([ server ])
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user