{
  "id": "library_configuration",
  "title": "Library configuration",
  "url": "https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/deprecated-features/triggers-and-functions/concepts/library_configuration/",
  "summary": "How to use configuration in JavaScript functions",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-01T08:10:08-05:00",
  "page_type": "content",
  "content_hash": "f3c29c5d17219bb629fc8b56d4455cde3b26dd78f683b2444a89038e07477954",
  "sections": [
    {
      "id": "content",
      "title": "Content",
      "role": "content",
      "text": "When writing a library, you may want to provide a loading configuration so that different users can use the same library with slightly different behaviour, without changing the base code. For example, assume you write a library that adds a `__last_updated__` field to a hash (you can see how it can also be done with [keyspace triggers]()), the code will look like this:\n\n[code example]\n\nRun example:\n\n[code example]\n\nThe problem with the above code is that the `__last_update__` field is hard coded. What if we want to allow the user to configure it at runtime? Triggers and functions provide for specifying a library configuration at load time using a [`CONFIG`]() argument that is passed to the `TFUNCTION LOAD` command. The configuration argument accepts a string representation of a JSON object. The JSON will be provided to the library as a JS object under the `redis.config` variable. We can change the above example to accept the `__last_update__` field name as a library configuration. The code will look like this:\n\n[code example]\n\nNotice that in the above example we first set `last_update_field_name` to `__last_update__`, the default value in cases where a value is not provided by the configuration. Then we check if we have `last_update_field_name` in our configuration and if we do we use it. We can now load our function with a [`CONFIG`]() argument:\n\n[code example]\n\nWe can see that the last update field name is `last_update`:\n\n[code example]\n\nNotice, triggers and functions only provides the library with the JSON configuration. **It's the library's responsibility to verify the correctness of the given configuration**."
    }
  ],
  "examples": [
    {
      "id": "content-ex0",
      "language": "js",
      "code": "#!js api_version=1.0 name=lib\n\nredis.registerFunction(\"hset\", function(client, key, field, val){\n    // get the current time in ms\n    var curr_time = client.call(\"time\")[0];\n    return client.call('hset', key, field, val, \"__last_update__\", curr_time);\n});",
      "section_id": "content"
    },
    {
      "id": "content-ex1",
      "language": "bash",
      "code": "127.0.0.1:6379> TFCALL lib.hset k a b 0\n(integer) 2\n127.0.0.1:6379> hgetall k\n1) \"foo\"\n2) \"bar\"\n3) \"__last_update__\"\n4) \"1658653125\"",
      "section_id": "content"
    },
    {
      "id": "content-ex2",
      "language": "js",
      "code": "#!js api_version=1.0 name=lib\n\nvar last_update_field_name = \"__last_update__\"\n\nif (redis.config.last_update_field_name !== undefined) {\n    if (typeof redis.config.last_update_field_name != 'string') {\n        throw \"last_update_field_name must be a string\";\n    }\n    last_update_field_name = redis.config.last_update_field_name\n}\n\nredis.registerFunction(\"hset\", function(client, key, field, val){\n    // get the current time in ms\n    var curr_time = client.call(\"time\")[0];\n    return client.call('hset', key, field, val, last_update_field_name, curr_time);\n});",
      "section_id": "content"
    },
    {
      "id": "content-ex3",
      "language": "bash",
      "code": "> redis-cli -x TFUNCTION LOAD REPLACE CONFIG '{\"last_update_field_name\":\"last_update\"}' < <path to code file>\nOK",
      "section_id": "content"
    },
    {
      "id": "content-ex4",
      "language": "bash",
      "code": "127.0.0.1:6379> TFCALL lib.hset h a b 0\n(integer) 2\n127.0.0.1:6379> hgetall h\n1) \"a\"\n2) \"b\"\n3) \"last_update\"\n4) \"1658654047\"",
      "section_id": "content"
    }
  ]
}
