budibase/packages/server/src/integrations/redis.ts

150 lines
3.3 KiB
TypeScript
Raw Normal View History

import { DatasourceFieldType, Integration, QueryType } from "@budibase/types"
2022-03-31 16:44:06 +02:00
import Redis from "ioredis"
2022-03-31 11:56:16 +02:00
module RedisModule {
interface RedisConfig {
host: string
port: number
username: string
password?: string
}
const SCHEMA: Integration = {
docs: "https://redis.io/docs/",
description: "",
friendlyName: "Redis",
2022-06-23 12:35:57 +02:00
type: "Non-relational",
2022-03-31 11:56:16 +02:00
datasource: {
host: {
type: "string",
required: true,
default: "localhost",
},
port: {
type: "number",
required: true,
default: 6379,
},
username: {
type: "string",
required: false,
},
password: {
type: "password",
required: false,
},
},
query: {
2022-03-31 16:44:06 +02:00
create: {
type: QueryType.FIELDS,
2022-03-31 16:44:06 +02:00
fields: {
key: {
type: DatasourceFieldType.STRING,
2022-03-31 16:44:06 +02:00
required: true,
},
value: {
type: DatasourceFieldType.STRING,
2022-03-31 16:44:06 +02:00
required: true,
},
2022-04-25 11:24:49 +02:00
ttl: {
type: DatasourceFieldType.NUMBER,
2022-04-25 11:24:49 +02:00
},
2022-03-31 16:44:06 +02:00
},
},
2022-03-31 11:56:16 +02:00
read: {
2022-03-31 16:44:06 +02:00
readable: true,
type: QueryType.FIELDS,
2022-03-31 16:44:06 +02:00
fields: {
key: {
type: DatasourceFieldType.STRING,
2022-03-31 16:44:06 +02:00
required: true,
},
},
},
delete: {
type: QueryType.FIELDS,
2022-03-31 16:44:06 +02:00
fields: {
key: {
type: DatasourceFieldType.STRING,
2022-03-31 16:44:06 +02:00
required: true,
},
},
},
command: {
readable: true,
displayName: "Redis Command",
type: QueryType.JSON,
2022-03-31 11:56:16 +02:00
},
},
}
2022-03-31 16:44:06 +02:00
class RedisIntegration {
2022-03-31 11:56:16 +02:00
private readonly config: RedisConfig
private client: any
constructor(config: RedisConfig) {
this.config = config
2022-03-31 16:44:06 +02:00
this.client = new Redis({
host: this.config.host,
port: this.config.port,
username: this.config.username,
password: this.config.password,
})
}
async disconnect() {
this.client.disconnect()
}
async redisContext(query: Function) {
try {
return await query()
} catch (err) {
throw new Error(`Redis error: ${err}`)
} finally {
this.disconnect()
}
2022-03-31 11:56:16 +02:00
}
2022-04-25 11:24:49 +02:00
async create(query: { key: string; value: string; ttl: number }) {
2022-03-31 16:44:06 +02:00
return this.redisContext(async () => {
const response = await this.client.set(query.key, query.value)
2022-04-25 11:24:49 +02:00
if (query.ttl) {
await this.client.expire(query.key, query.ttl)
}
2022-03-31 16:44:06 +02:00
return response
})
}
async read(query: { key: string }) {
return this.redisContext(async () => {
const response = await this.client.get(query.key)
return response
})
}
async delete(query: { key: string }) {
return this.redisContext(async () => {
const response = await this.client.del(query.key)
return response
})
}
async command(query: { json: string }) {
return this.redisContext(async () => {
const commands = query.json.trim().split(" ")
const pipeline = this.client.pipeline([commands])
const result = await pipeline.exec()
return {
response: result[0][1],
}
})
}
2022-03-31 11:56:16 +02:00
}
module.exports = {
schema: SCHEMA,
integration: RedisIntegration,
}
}