{"id":26975165,"url":"https://github.com/izure1/klaf","last_synced_at":"2025-04-03T11:19:22.257Z","repository":{"id":200035912,"uuid":"705021122","full_name":"izure1/klaf","owner":"izure1","description":"Very simple read/write database with a No-SQL. It's written in JavaScript using pure Node.js API and pretty easy and small.","archived":false,"fork":false,"pushed_at":"2024-10-18T23:35:15.000Z","size":583,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-10-19T02:43:33.472Z","etag":null,"topics":["database","db","document-database","document-oriented","javascript","key-value-database","key-value-store","nodejs","nosql-database","store","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/izure1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-14T20:07:11.000Z","updated_at":"2024-10-18T23:35:19.000Z","dependencies_parsed_at":"2023-10-15T11:33:22.646Z","dependency_job_id":"874ae6f0-b71c-4bc2-b603-92d7ec3adc46","html_url":"https://github.com/izure1/klaf","commit_stats":null,"previous_names":["izure1/tissue-roll","izure1/klaf"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izure1%2Fklaf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izure1%2Fklaf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izure1%2Fklaf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izure1%2Fklaf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izure1","download_url":"https://codeload.github.com/izure1/klaf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246989732,"owners_count":20865331,"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":["database","db","document-database","document-oriented","javascript","key-value-database","key-value-store","nodejs","nosql-database","store","typescript"],"created_at":"2025-04-03T11:19:21.632Z","updated_at":"2025-04-03T11:19:22.235Z","avatar_url":"https://github.com/izure1.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Klaf\r\n\r\n[![jsdelivr](https://data.jsdelivr.com/v1/package/npm/klaf.js/badge)](https://www.jsdelivr.com/package/npm/klaf.js)\r\n![node.js workflow](https://github.com/izure1/klaf/actions/workflows/node.js.yml/badge.svg)\r\n\r\nVery simple read/write database with a **NoSQL**.  \r\nIt's written in JavaScript using pure Node.js API and pretty easy and small.  \r\nThis database works seamlessly with [Node.js](https://nodejs.org/), [Bun](https://bun.sh/) [Deno](https://deno.com/), and Browser, and Web worker.\r\n\r\nChoose the [database](#database) and [engine](#engine) that best fit your needs to handle your data efficiently!\r\n\r\n## Database\r\n\r\n**klaf** comes in two flavors: **key-value** database and **document-oriented** database.  \r\nYou have the freedom to choose based on your needs, **but most users will likely prefer the *document-oriented* database.**\r\n\r\nFor details on how to use each database, please refer to the links below.\r\n\r\n* [**Document-oriented Database**](./docs/document/README.md)\r\n* [**Key-value Database**](./docs/core/README.md)\r\n\r\n## [Engine](./docs/engine/README.md)\r\n\r\nKlaf.js introduces the concept of an engine, which is an instance responsible for handling how data is stored. Currently, three types of engines are supported by default: **FileSystem**, **InMemory**, and **WebWorker**. If needed, you can also create your own custom engine.  \r\nChoose the engine that best fits your needs.\r\n\r\nFor a detailed list of the supported engines and more information, refer to [this link](./docs/engine/README.md).  \r\nIf you're unsure what to choose, select the **FileSystem** engine.\r\n\r\n## Example\r\n\r\nThis example shows how to build a database using the document-oriented database **KlafDocument** and the **FileSystem** engine.\r\n\r\n```typescript\r\nimport { KlafDocument, DataJournal } from 'klaf.js'\r\nimport { FileSystemEngine } from 'klaf.js/engine/FileSystem'\r\n\r\nconst db = await KlafDocument.Open({\r\n  path: 'my-database-path.db',\r\n  version: 0,\r\n  engine: new FileSystemEngine(),\r\n  journal: new DataJournal(new FileSystemEngine()),\r\n  scheme: {\r\n    id: {\r\n      default: () =\u003e crypto.randomUUID()\r\n    },\r\n    nickname: {\r\n      default: () =\u003e 'Anonymous',\r\n      validate: (v) =\u003e typeof v === 'string',\r\n    },\r\n    gender: {\r\n      default: () =\u003e 'other',\r\n      validate: (v) =\u003e ['male', 'female', 'other'].includes(v),\r\n    }\r\n  }\r\n})\r\n\r\nawait db.put({ nickname: 'faker', gender: 'male' })\r\nconst [err, documents] = await db.pick({ gender: 'male' })\r\n```\r\n\r\nThe following example demonstrates how to build a database using the key-value database **Klaf** and the **WebWorker** engine.\r\n\r\n```typescript\r\nimport { Klaf, DataJournal } from 'klaf.js'\r\nimport { WebWorkerEngine } from 'klaf.js/engine/WebWorker'\r\n\r\nconst db = await Klaf.Open({\r\n  path: 'my-database-path.db',\r\n  engine: new WebWorkerEngine(),\r\n  journal: new DataJournal(new WebWorkerEngine())\r\n})\r\n\r\nconst [errPut, key] = await db.put('Faker, GOAT.')\r\nconst [errPick, row] = await db.pick(key)\r\nrow.record.payload // \"Faker, GOAT.\"\r\n```\r\n\r\n## Install\r\n\r\n### Node.js (NPM)\r\n\r\n```bash\r\nnpm i klaf.js\r\n```\r\n\r\n### Deno (JSR)\r\n\r\n```bash\r\ndeno add jsr:@izure/klaf\r\n```\r\n\r\n### Browser (ESM)\r\n\r\n```javascript\r\nimport { Klaf, KlafDocument } from 'https://cdn.jsdelivr.net/npm/klaf.js/dist/esm/index.mjs'\r\n\r\n// engines\r\nimport { InMemoryEngine } from 'https://cdn.jsdelivr.net/npm/klaf.js/dist/esm/engine/InMemory.mjs'\r\nimport { WebWorkerEngine } from 'https://cdn.jsdelivr.net/npm/klaf.js/dist/esm/engine/WebWorker.mjs'\r\n```\r\n\r\n## Why\r\n\r\n### Why use **klaf**?\r\n\r\nJavaScript has numerous fantastic database libraries available, but at times, they can seem overly complex.  \r\nThis particular solution is ideal for situations where you need to store data for an extended period, making it well-suited for less critical data that doesn't require a rigid structure.\r\n\r\nSince it is implemented in pure JavaScript, there is no need for pre-builds or configuration based on the Node.js version. It is compatible with all versions!\r\n\r\n### Why should I use this instead of **JSON**?\r\n\r\nWhen the **JSON** files get large, quick data read and write operations can become challenging.  \r\n**klaf** handles data input and output in real-time, ensuring fast and lightweight performance. Check the performance tests below.\r\n\r\n## How\r\n\r\n### How does **klaf** work?\r\n\r\n**klaf** manages files by breaking them into logical blocks called pages. You can set the page size when creating the database.\r\n\r\nWhen you insert data, the ID you get back includes information about where the data is stored on the page. This makes it possible to work with large files quickly. This value could be seen by users, but it's encrypted to make it hard to predict. This way, it stops users from trying to steal data by requesting fake record IDs.\r\n\r\n### How many can I own data?\r\n\r\n**klaf** can make a unsigned 32bit range of page block. This is a **4,294,967,296**. And each page can own unsigned 32bit range of records also. So you can theoretically insert **4,294,967,295** * **4,294,967,295** records.\r\n\r\n## Performance Test\r\n\r\nThe test result is the average value from 10 attempts.  \r\nThe latest performance benchmarking was conducted based on version **2.0.6**. This test was conducted using the **FileSystem** engine, and it was tested without the **Journal** feature.\r\n\r\n**klaf** supports two databases, and this test tested the core functions of data reading/writing of the two databases. Therefore, it's not a perfect test result, but it's enough to show the time complexity.\r\n\r\nIf you're adding data to the database in real-time, the results would be as follows:\r\n\r\n### WRITE\r\n\r\nOverall, Klaf supports faster writes than JSON. As the size increases, this gap becomes even larger.\r\n\r\n|`WRITE`|JSON|KLAF|`RESULT`|\r\n|---|---|---|---|\r\n|1,000 times|1014ms|864ms|***+15% Faster***|\r\n|2,000 times|2200ms|1700ms|***+23% Faster***|\r\n|4,000 times|5674ms|3163ms|***+44% Faster***|\r\n|8,000 times|15332ms|5925ms|***+61% Faster***|\r\n\r\n### READ\r\n\r\n**klaf** maintains a steady reading speed no matter the database size. In contrast, JSON files slow down as they get bigger.\r\n\r\n|`READ`|JSON|KLAF|`RESULT`|\r\n|---|---|---|---|\r\n|from 8,000 records|1.8ms|2ms|*-10% Slower*|\r\n|from 16,000 records|4ms|2ms|***+100% Faster***|\r\n|from 32,000 records|5.4ms|2ms|***+170% Faster***|\r\n|from 64,000 records|11.4ms|2ms|***+470% Faster***|\r\n|from 128,000 records|26.4ms|2ms|***+1220% Faster***|\r\n\r\n### RESULT\r\n\r\n![WRITE](./docs/asset/image/svg_perf_write.svg)\r\n![READ](./docs/asset/image/svg_perf_read.svg)\r\n\r\n**NOTICE!**\r\n\r\n*This is the usual case, but the results can be different depending on programming optimizations. Please note that this test takes a square of the sample size to easily show the error with a small number of tests. Therefore, the graph appears to increase exponentially, but in terms of time complexity, JSON has **O(n)**, and klaf has a speed of **O(1)** or **O(log n)**.*\r\n\r\n*The Journal feature is designed to enhance database stability. However, it may slightly reduce performance, approximately by **25~30%**.*\r\n\r\n## Repository\r\n\r\n|Site|Link|\r\n|---|---|\r\n|**NPM**|[View](https://www.npmjs.com/package/klaf.js)|\r\n|**JSR**|[View](https://jsr.io/@izure/klaf)|\r\n|**jsdelivr**|[View](https://www.jsdelivr.com/package/npm/klaf.js)|\r\n|**Github**|[View](https://github.com/izure1/klaf)|\r\n\r\n## Migration\r\n\r\nThe Klaf library is the new name for the TissueRoll library.\r\n\r\n|     Version                         |     Link      |\r\n|-------------------------------------|---------------|\r\n| From Klaf 2.x to Klaf 3.x           |[Link](./docs/migration/8.md)|\r\n| From Klaf 1.x to Klaf 2.x           |[Link](./docs/migration/7.md)|\r\n| From TissueRoll 5.x.x to Klaf 1.x   |[Link](./docs/migration/6.md)|\r\n\r\n## License\r\n\r\nMIT LICENSE\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizure1%2Fklaf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizure1%2Fklaf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizure1%2Fklaf/lists"}