All files / src/resp/command exec-command.ts

100% Statements 23/23
100% Branches 4/4
100% Functions 4/4
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56      1x 1x 1x                             1x 4x   4x   4x   4x   4x     4x       4x 1x 3x 1x 1x   2x 2x 4x 4x 4x       4x   2x      
import { Database } from "../data/database";
import { IRequest } from "../../server/request";
import { ICmdReq } from "../../server/session";
import { Logger } from "../../logger";
import { IRespCommand } from "./resp-command";
import { RedisToken } from "../protocol/redis-token";
 
/**
 * ### Available since 1.2.0.
 * ### EXEC
 * Executes all previously queued commands in a transaction and restores the connection state
 * to normal.
 *
 * When using [WATCH]{public link WatchCommand}, EXEC will execute commands only if the watched keys were not modified,
 * allowing for a check-and-set mechanism.
 * ### Return value
 * Array reply: each element being the reply to each of the commands in the atomic transaction.
 *
 * When using [WATCH]{public link WatchCommand}, EXEC can return a Null reply if the execution was aborted.
 */
export class ExecCommand extends IRespCommand {
    public blocking = true
 
    public minParams = 0
 
    public maxParams = 0
 
    public name = "exec"
 
    private logger: Logger = new Logger(module.id);
 
    public execSync(request: IRequest, db: Database): RedisToken {
        this.logger.debug(
            `${request.getCommand()}.execute(%s)`,
            ...request.getParams()
        );
        if (!request.getSession().inTransaction()) {
            return RedisToken.error("ERR EXEC without MULTI");
        } else if (request.getSession().isErrored()) {
            request.getSession().abortTransaction();
            return RedisToken.error("EXECABORT Transaction discarded because of previous errors.");
        }
        const commands: ICmdReq[] = request.getSession().getTransaction(),
            tokens: any[] = [];
        commands.map(async(cmdReq) => {
            this.logger.debug(`Executing transaction command ${cmdReq.request.getCommand()}`);
            const token = await cmdReq.command.execSync(
                cmdReq.request,
                db
            );
            tokens.push(token);
        });
        return RedisToken.array(tokens);
    }
}