Skip to content

Parameters in SQL and TypeScript share the same implementation; only their declaration methods differ.

Declaration

There is only one parameter namespace in SQL, meaning parameters are shared across different queries.

sql
-- @param uid / uint / label=User ID; default=11
select * from `user` where id = ${uid};

delete from `user` where id = ${uid};

In TypeScript, parameters can be passed as variables:

typescript
redisshell((cli) => {
  const params_key = params.string("key", { default: "hello" });
  const params_value = params.string("value", { default: "world" });

  // Note: Param instances cannot be directly used in operations. Use `valmap` if needed.
  // For example, the following code is incorrect:
  params_value.trim().toUpperCase();
  // Correct approach: Use `valmap` for operations:
  const params_value1 = params.string("value", {
    default: "world",
    valmap: (v) => v.trim().toUpperCase(),
  });

  cli.get(params_key);

  cli.set(params_key, params_value);
});

Declaring multiple parameters in SQL:

sql
-- @param uid / uint
-- @param name
-- @param info / string / kind=json

Referencing another parameter with <- name in SQL:

sql
-- @param uid / uint / label=User ID; default=34
select * from `user` where id = ${uid};

-- @param delete_uid / <- uid /  label=User ID to delete
delete from `user` where id = ${delete_uid};

String Parameters

String is the default type for parameters.

Two additional types are available for string parameters: json and textarea, to facilitate input of long content.

sql
-- @param txt / string / kind=json
select ${txt};

Time Parameters

Time parameters support specifying format templates and timezones.

sql
-- @param timev / datetime / tz=Asia/Shanghai
select ${timev};

The timezone defaults to the local timezone of the computer. When set to db, it will attempt to query and use the database's timezone. For other values (e.g., utc, Asia/Shanghai), refer to mdn