dogmaql.driver.redis
Version:
Redis driver for DogmaQL (Dogma Query Language).
53 lines (51 loc) • 3.67 kB
JavaScript
"use strict";
var _dogmalang = require("dogmalang");
const time = _dogmalang.dogma.use(require("./time"));
const $SetTrans = class SetTrans {
constructor() {
{}
}
};
const SetTrans = new Proxy($SetTrans, { apply(receiver, self, args) {
return new $SetTrans(...args);
} });module.exports = exports = SetTrans;
SetTrans.prototype.transSetItem = function (stmt) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);{
return (0, _dogmalang.len)(stmt.values) == 1 ? [transSetItem(stmt, _dogmalang.dogma.getItem(stmt.values, 0).value)] : transSetItems(stmt);
}
};
function transSetItem(stmt, item) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);_dogmalang.dogma.paramExpected("item", item, null);{
return (_dogmalang.dogma.like(stmt.table, "^ts\\.") ? transSetTsItem : _dogmalang.dogma.like(stmt.table, "^fts\\.") ? transSetFtsItem : transSetKvItem)(stmt, item);
}
}
function transSetKvItem(stmt, item) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);_dogmalang.dogma.paramExpected("item", item, null);{
return ["eval", "local tabName, item = ARGV[1], ARGV[2]\nlocal key = tabName .. \":\" .. KEYS[1]\nlocal tab\n\n--(1) get table metadata\ntab = redis.call(\"json.get\", \"db.tables:\" .. tabName)\nif not tab then error(\"table '\" .. tabName .. \"' doesn't exist.\") end\ntab = cjson.decode(tab)\n\n--(2) set item\nredis.call(\"json.set\", key, \".\", item)\n\n--(3) set ttl if needed\nif tab.ttl and item.ttl ~= nil then\n redis.call(\"expireat\", key, item.ttl)\nend", 1, _dogmalang.dogma.getItem(item, _dogmalang.dogma.getItem((0, _dogmalang.keys)(item), 0)), stmt.table, JSON.stringify(item)];
}
}
function transSetTsItem(stmt, item) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);_dogmalang.dogma.paramExpected("item", item, null);{
return ["eval", "local tab, time, item = ARGV[1], ARGV[2], ARGV[3]\nlocal key = tab .. \":\" .. KEYS[1]\n\n--(1) check whether table exists\nif not redis.call(\"exists\", \"db.tables:\" .. tab) then\n error(\"table '\" .. tab .. \"' doesn't exist.\")\nend\n\n--(2) set item\nredis.call(\"zremrangebyscore\", key, time, time)\n\nif redis.call(\"zadd\", key, \"ch\", time, item) == 0 then\n error(\"key already exists: \" .. key .. \".\")\nend", 1, _dogmalang.dogma.getItem(item, _dogmalang.dogma.getItem((0, _dogmalang.keys)(item), 0)), stmt.table, time.formatTime(_dogmalang.dogma.getItem(item, _dogmalang.dogma.getItem((0, _dogmalang.keys)(item), 1))), JSON.stringify(item)];
}
}
function transSetFtsItem(stmt, item) {
_dogmalang.dogma.paramExpected("stmt", stmt, null);_dogmalang.dogma.paramExpected("item", item, null);{
return ["ft.add", stmt.table, stmt.table + ":" + _dogmalang.dogma.getItem(item, _dogmalang.dogma.getItem((0, _dogmalang.keys)(item), 0)), 1, "replace", "language", _dogmalang.dogma.getItem({ ["ar"]: "arabic", ["da"]: "danish", ["de"]: "german", ["en"]: "english", ["es"]: "spanish", ["fi"]: "finnish", ["fr"]: "french", ["hu"]: "hungarian", ["it"]: "italian", ["nl"]: "dutch", ["no"]: "norwegian", ["pt"]: "portuguese", ["ro"]: "romanian", ["ru"]: "russian", ["sv"]: "swedish", ["tr"]: "turkish", ["zh"]: "chinese" }, item.lang) || "english", "fields", ...(() => {
let fields = [];{
for (let [name, val] of Object.entries(item)) {
{
_dogmalang.dogma.lshift(fields, name);_dogmalang.dogma.lshift(fields, val);
}
}
}return fields;
})()];
}
}
function transSetItems(stmt) {
let cmds = [];_dogmalang.dogma.paramExpected("stmt", stmt, null);{
for (let item of stmt.values) {
_dogmalang.dogma.lshift(cmds, transSetItem(stmt, item.value));
}
}return cmds;
}