{"id":22190910,"url":"https://github.com/timsusa/ketchup-db","last_synced_at":"2025-03-24T20:41:06.550Z","repository":{"id":263410543,"uuid":"890289140","full_name":"TimSusa/ketchup-db","owner":"TimSusa","description":"Super Simple JSON Database, you can just work with","archived":false,"fork":false,"pushed_at":"2024-11-18T21:20:32.000Z","size":52,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-19T06:44:15.267Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/TimSusa.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":"2024-11-18T10:15:58.000Z","updated_at":"2024-11-18T21:20:35.000Z","dependencies_parsed_at":"2024-11-18T12:20:22.236Z","dependency_job_id":"7b357e38-8292-490e-a709-28d6e7c9d3fb","html_url":"https://github.com/TimSusa/ketchup-db","commit_stats":null,"previous_names":["timsusa/ketchup-db"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimSusa%2Fketchup-db","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimSusa%2Fketchup-db/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimSusa%2Fketchup-db/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TimSusa%2Fketchup-db/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TimSusa","download_url":"https://codeload.github.com/TimSusa/ketchup-db/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245351757,"owners_count":20601087,"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-12-02T12:13:20.549Z","updated_at":"2025-03-24T20:41:06.524Z","avatar_url":"https://github.com/TimSusa.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Ketchup-DB\nSuper simple JSON database for Deno in TypeScript, which uses a `Set` to store the items.\n\n### Loading Multiple Items\nReads multiple items from a JSON file at once and adds them to the `Set`. All items are validated before being added in a single operation.\n\n### Bulk Saving\nWrites all items in the `Set` to the JSON file in one operation, making it more efficient than individual saves.\n\n### Implementation with Bulk Operations\n\n```typescript\n// Create an initializer object for User (no need to define User type separately)\nconst userInitializer = { name: \"\", email: \"\" };  // Note: no id required!\n\n// Derive the User type from the initializer\ntype User = typeof userInitializer;  // Automatically infers the User type\n\n// Automatically extract the keys from the initializer\nconst userKeys = Object.keys(userInitializer) as (keyof User)[];\n\n// Create a validator using the extracted keys\nconst validateUser: Validator\u003cUser\u003e = (item: any): item is User =\u003e\n  validateItem\u003cUser\u003e(item, userKeys);\n\n// Usage example\nasync function main() {\n  const db = createDb\u003cUser\u003e({\n    _filePath: \"users.json\",\n    validateItem: validateUser\n  });\n\n  // Load existing data from file in batch\n  await db.loadBatch();\n  console.log(\"Loaded Users:\", db.getAllItems());\n\n  // Add new users - IDs are optional!\n  await db.addItems([\n    { name: \"Alice\", email: \"alice@example.com\" },\n    { name: \"Bob\", email: \"bob@example.com\" }\n  ]);\n\n  console.log(\"After Adding Users:\", db.getAllItems());\n\n  await db.saveBatch();\n}\n\nawait main();\n```\n\n### Example Output\n```typescript\n{\n  {\n    id: \"9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d\",\n    name: \"Alice\",\n    email: \"alice@example.com\"\n  },\n  {\n    id: \"1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed\",\n    name: \"Bob\",\n    email: \"bob@example.com\"\n  }\n}\n```\n\n### Key Features\n\n1. **Efficient Loading**:\n   - Loads all items from the JSON file and validates them at once\n   - Valid items are added to the `Set` in a single operation\n\n2. **Optimized Saving**:\n   - Saves the entire `Set` to the JSON file in one operation\n   - Converts the `Set` to an array and serializes to JSON\n\n3. **Bulk Item Addition**:\n   - Handles multiple items simultaneously\n   - Validates all items before adding to the `Set`\n   - Single file write after processing\n\n### Benefits\n- **Performance**: Processing multiple items at once is more efficient\n- **Reduced File Access**: Minimizes disk operations by combining writes\n- **Ease of Use**: Simple API for handling large datasets\n\n### Running the Code\n```bash\ndeno run --allow-read --allow-write your_script.ts\n```\n\nThese optimizations make your `SetDatabase` class efficient for handling large datasets!\n\n# Deep Search Functionality\n\nThe database includes powerful deep search capabilities that can traverse through nested objects, arrays, and recursive structures.\n\n### Example User Data Structure\n```typescript\n// Example data from users.json\n{\n  \"users\": [\n    {\n      \"id\": 1,\n      \"name\": \"Alice Smith\",\n      \"email\": \"alice@example.com\",\n      \"tel\": \"+1-555-123-4567\",\n      \"address\": \"123 Main St\",\n      \"events\": [\n        {\n          \"id\": 101,\n          \"title\": \"Team Meetup\",\n          \"date\": \"2024-03-20\",\n          \"type\": \"work\"\n        },\n        {\n          \"id\": 102,\n          \"title\": \"Birthday Party\",\n          \"date\": \"2024-04-15\",\n          \"type\": \"personal\"\n        }\n      ]\n    }\n  ]\n}\n```\n\n### Deep Search Examples\n\n```typescript\n// Find users with work events\nconst workEvents = db.deepSearch(user =\u003e \n  user.events.some(event =\u003e event.type === \"work\")\n);\n\n// Search through nested event titles\nconst birthdayEvents = db.deepSearch(user =\u003e\n  user.events.some(event =\u003e \n    event.title.toLowerCase().includes(\"birthday\")\n  )\n);\n\n// Complex search combining multiple criteria\nconst complexSearch = db.deepSearch(user =\u003e \n  user.address.includes(\"Main St\") \u0026\u0026\n  user.events.some(event =\u003e \n    new Date(event.date) \u003e new Date(\"2024-03-01\")\n  )\n);\n```\n\n### Search Capabilities\n\n- **Nested Objects**: Searches through all object properties at any depth\n- **Arrays**: Traverses through array elements\n- **Type Safe**: Full TypeScript support with type inference\n- **Flexible Matching**: Supports custom predicates for complex search conditions\n\n### Complete Example with Results\n\n```typescript\n// Initial setup\nconst db = createDb\u003cUser\u003e({\n  _filePath: \"users.json\",\n  validateItem: validateUser\n});\n\n// Add sample users with events\nawait db.addItems([\n  {\n    name: \"Alice Smith\",\n    email: \"alice@example.com\",\n    tel: \"+1-555-123-4567\",\n    address: \"123 Main St\",\n    events: [\n      {\n        id: 101,\n        title: \"Team Meetup\",\n        date: \"2024-03-20\",\n        type: \"work\"\n      },\n      {\n        id: 102,\n        title: \"Birthday Party\",\n        date: \"2024-04-15\",\n        type: \"personal\"\n      }\n    ]\n  }\n]);\n\n// Search examples with results\nconst workEvents = db.deepSearch(user =\u003e \n  user.events.some(event =\u003e event.type === \"work\")\n);\nconsole.log(\"Users with work events:\", workEvents);\n// Output: Set(1) { \n//   { \n//     name: \"Alice Smith\",\n//     events: [{ title: \"Team Meetup\", type: \"work\", ... }],\n//     ...\n//   } \n// }\n\nconst birthdayEvents = db.deepSearch(user =\u003e\n  user.events.some(event =\u003e \n    event.title.toLowerCase().includes(\"birthday\")\n  )\n);\nconsole.log(\"Users with birthday events:\", birthdayEvents);\n// Output: Set(1) { \n//   { \n//     name: \"Alice Smith\",\n//     events: [{ title: \"Birthday Party\", type: \"personal\", ... }],\n//     ...\n//   } \n// }\n\nconst futureEvents = db.deepSearch(user =\u003e \n  user.address.includes(\"Main St\") \u0026\u0026\n  user.events.some(event =\u003e \n    new Date(event.date) \u003e new Date(\"2024-03-01\")\n  )\n);\nconsole.log(\"Users with future events on Main St:\", futureEvents);\n// Output: Set(1) { \n//   { \n//     name: \"Alice Smith\",\n//     address: \"123 Main St\",\n//     events: [\n//       { date: \"2024-03-20\", ... },\n//       { date: \"2024-04-15\", ... }\n//     ],\n//     ...\n//   } \n// }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimsusa%2Fketchup-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimsusa%2Fketchup-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimsusa%2Fketchup-db/lists"}