{"id":18687308,"url":"https://github.com/junosuarez/sawtooth","last_synced_at":"2025-07-04T01:33:06.229Z","repository":{"id":6718194,"uuid":"7963948","full_name":"junosuarez/sawtooth","owner":"junosuarez","description":"node module: data access pattern for tiered-locality data access","archived":false,"fork":false,"pushed_at":"2013-02-03T21:04:20.000Z","size":106,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-06-21T01:01:12.048Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/junosuarez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-01T18:44:57.000Z","updated_at":"2017-04-25T16:24:23.000Z","dependencies_parsed_at":"2022-08-31T20:11:35.727Z","dependency_job_id":null,"html_url":"https://github.com/junosuarez/sawtooth","commit_stats":null,"previous_names":["agilediagnosis/sawtooth"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/junosuarez/sawtooth","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junosuarez%2Fsawtooth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junosuarez%2Fsawtooth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junosuarez%2Fsawtooth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junosuarez%2Fsawtooth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/junosuarez","download_url":"https://codeload.github.com/junosuarez/sawtooth/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/junosuarez%2Fsawtooth/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261320429,"owners_count":23140938,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T10:32:21.802Z","updated_at":"2025-07-04T01:33:06.186Z","avatar_url":"https://github.com/junosuarez.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sawtooth\ndata access pattern for tiered-locality data access\n\n## about\n\nWhen you go to access some data in your application, where does it come from? It could be in memory, but maybe you also have an on-disk cache, and you might have a remote location, say a REST api. Now what you've got is a problem of [spatial locality](http://en.wikipedia.org/wiki/Locality_of_reference), and it might be a good idea to separate the concern of tracking down where the data is away from the rest of your application.\n\nIn the above example, you would have 3 tiers of data access, sorted by latency:\n\na) in memory\nb) on disk\nc) remote service\n\nSawtooth is a caching data access pattern for `get`ing data identified by a key from each tier in turn, stopping at the first source which has the information availble. Then, on the way back up the tiers, it will call `set` and `get` successively, in a sawtooth pattern, for example:\n\n     | ^\n     v |\n    a.get \u003c------ a.set\n     |           7|\n     |          /\n     |         /\n     |        /\n     |       /\n     v      /\n    b.get \u003c------ b.set\n     |           7|\n     |          /\n     |         /\n     |        /\n     |       /\n     v      /\n    c.get  /\n\nSawtooth lets you represent this logic in a matrix:\n\n    sawtooth([\n      [a.get, a.set],\n      [b.get, b.set],\n      [c.get]\n    ])\n\nwhich returns a function roughly equivalent to the following call order:\n\n  or the following call order\n  a.get(x)\n  b.get(x)\n  c.get(x)\n  =\u003e yC\n  b.set(x, yC)\n  b.get(x)\n  =\u003e yB\n  a.set(x, yB)\n  a.get(x)\n  =\u003e yA\n\nNote that `sawtooth` supports mixing sync and async getters and setters through [Promises/A+ compliant promises](https://github.com/promises-aplus/promises-spec).\n\nYou can also pipeline mapping functions to transform values before a setter\n\n  eg:\n    sawtooth([\n      [a.get, a.set],\n      [null, bToa]\n      [b.get]\n    ])\n\n  represents:\n\n     | ^\n     v |\n    a.get \u003c---- a.set\n     |            ^\n     |            |\n     |          bToa\n     |           7|\n     |          /\n     v         /\n    b.get  ---/\n\n## installation\n\n    $ npm install sawtooth\n\n## usage\n\n`sawtooth` takes a matrix of functions and returns a function with the configured pipeline. Each row represents another tier.\n\n    sawtooth (matrix) =\u003e pipeline\n\nwhere\n\n    pipeline(key) =\u003e Promise\u003cvalue\u003e\n\nThe first type of tier is a getter/setter pair. The last tier should be a single getter.\n\n    [\n      [a.get, a.set],\n      [b.get, b.set],\n      [c.get]\n    ]\n\nTiers can also support optional string labels, used for logging and debugging:\n\n    [\n      [a.get, a.set, 'source A'],\n      [b.get, b.set, 'source B'],\n      [c.get, 'source C']\n    ]\n\nGetters are functions with the interface\n\n    function (key) =\u003e value\nor\n    function (key) =\u003e Promise\u003cvalue\u003e\n\nSetters are functions with the interface\n\n    function (key, value) =\u003e void\n\nCurrently `sawtooth` only supports a read-only access pattern.\n\nThe other type of tier is a scalar mapping function to transform the value. These may only be used in column 2, and are called in serial on the way back up from bottom to top with the value returned by the previous getter. This can be useful, for example, for deserializing JSON into an object, or calling a constructor. These tiers can also take a label.\n\n    [\n      [a.get, a.set, 'source A'],\n      [null, deserializeFoo, 'construct a new Foo'],\n      [c.get, 'source C']\n    ]\n\nMappers are functions with the interface\n\n    function (value, key) =\u003e valuePrime\nor\n    function (value, key) =\u003e Promise\u003cvaluePrime\u003e\n\nThe key is passed as the second argument so that plain scalar functions (eg (x) =\u003e y ) will work fine if the key is not necessary.\n\n## logging and debugging\n\n`sawtooth` returns a pipeline, which is also an EventEmitter which emits `log` events.\n\n    event `log`, values [level, message, stepLabelOrNumber, key, val]\n\n\n## running the tests\n\n    $ npm test\n\n## contributors\n\njden \u003cjason@denizac.org\u003e\n\nplease open an issue or pull request!\n\n## license\n\nMIT, (c) 2013 Agile Diagnosis, Inc. See LICENSE.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjunosuarez%2Fsawtooth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjunosuarez%2Fsawtooth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjunosuarez%2Fsawtooth/lists"}