{"id":20489119,"url":"https://github.com/euvii/database","last_synced_at":"2025-03-05T17:16:32.358Z","repository":{"id":263018253,"uuid":"889093809","full_name":"euvii/database","owner":"euvii","description":"best way to save your database on JSON file using .json and make your own database better","archived":false,"fork":false,"pushed_at":"2024-11-15T15:50:33.000Z","size":0,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-15T16:36:41.208Z","etag":null,"topics":["data","data-json","database","database-json","fs","javascript","json","json-data","json-database","nodejs","npm","path","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/euvii.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-15T15:44:14.000Z","updated_at":"2024-11-15T16:00:24.000Z","dependencies_parsed_at":"2024-11-15T16:47:01.632Z","dependency_job_id":null,"html_url":"https://github.com/euvii/database","commit_stats":null,"previous_names":["euvii/database"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euvii%2Fdatabase","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euvii%2Fdatabase/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euvii%2Fdatabase/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euvii%2Fdatabase/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/euvii","download_url":"https://codeload.github.com/euvii/database/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234152721,"owners_count":18787675,"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":["data","data-json","database","database-json","fs","javascript","json","json-data","json-database","nodejs","npm","path","typescript"],"created_at":"2024-11-15T17:11:19.959Z","updated_at":"2025-01-16T05:13:04.366Z","avatar_url":"https://github.com/euvii.png","language":"TypeScript","readme":"# Database Class - README\n\nThis project provides a simple file-based database system using Node.js. It allows you to store, retrieve, delete, check for keys, and back up data in JSON format. The database can be accessed asynchronously with `Promises`.\n\n## Features\n\n- **Set data**: Store key-value pairs in the database.\n- **Get data**: Retrieve data stored under a specific key.\n- **Delete data**: Remove data from the database using a key.\n- **Check key existence**: Check if a key exists in the database.\n- **Backup data**: Create a backup of the database in a specified file.\n- **Reset database**: Clear all data stored in the database.\n- **Close database**: Close the database and release any resources.\n- **Auto-backup option**: Option to automatically back up data when saving.\n\n## Installation\n\nTo use the database class in your Node.js project, follow the steps below:\n\n1. Clone or download the repository.\n2. Install the necessary dependencies by running:\n   ```bash\n   npm install\n   ```\n3. Create an instance of the `Database` class in your project.\n\n## Usage\n\n### Importing the Database Class\n\nTo use the `Database` class, import it into your project.\n\n```typescript\nconst Database = require('./database');  // Import the Database class\n```\n\n### Creating an Instance\n\nCreate an instance of the `Database` class, passing the file path for storing data as an argument.\n\n```typescript\nconst db = new Database('your-data-base-file-path');\n```\n\nThe `Database` constructor accepts two parameters:\n- `filePath`: The file path where the database will be stored. Defaults to `'./database.json'`.\n- `autoBackup`: A boolean that enables automatic backup after saving the data. Defaults to `false`.\n\n### Example Operations\n\n#### Set Data\nYou can store data by calling the `set()` method. It accepts a key and a value.\n\n```typescript\ndb.set('name', 'John Doe')\n  .then(() =\u003e {\n    console.log('Data set successfully!');\n  })\n  .catch((error) =\u003e {\n    console.error('Error setting data:', error);\n  });\n```\n\n#### Get Data\nYou can retrieve data by calling the `get()` method with the key.\n\n```typescript\ndb.get('name')\n  .then((name) =\u003e {\n    console.log(`Fetched data: ${name}`);\n  })\n  .catch((error) =\u003e {\n    console.error('Error getting data:', error);\n  });\n```\n\n#### Check If Key Exists\nYou can check if a key exists in the database with the `has()` method.\n\n```typescript\ndb.has('name')\n  .then((exists) =\u003e {\n    console.log(`Has name key? ${exists}`);\n  })\n  .catch((error) =\u003e {\n    console.error('Error checking if key exists:', error);\n  });\n```\n\n#### Delete Data\nYou can delete a key-value pair from the database by calling the `delete()` method.\n\n```typescript\ndb.delete('name')\n  .then(() =\u003e {\n    console.log('Data deleted successfully!');\n  })\n  .catch((error) =\u003e {\n    console.error('Error deleting data:', error);\n  });\n```\n\n#### Backup Data\nYou can back up your database to a file using the `backup()` method.\n\n```typescript\ndb.backup('your-data-base-file-path.backup')\n  .then(() =\u003e {\n    console.log('Backup created successfully!');\n  })\n  .catch((error) =\u003e {\n    console.error('Error creating backup:', error);\n  });\n```\n\n#### Reset the Database\nTo clear all data in the database, you can call the `reset()` method.\n\n```typescript\ndb.reset()\n  .then(() =\u003e {\n    console.log('Database reset successfully!');\n  })\n  .catch((error) =\u003e {\n    console.error('Error resetting database:', error);\n  });\n```\n\n#### Close the Database\nWhen you're done with the database, you can close it using the `close()` method.\n\n```typescript\ndb.close()\n  .then(() =\u003e {\n    console.log('Database closed successfully!');\n  })\n  .catch((error) =\u003e {\n    console.error('Error closing database:', error);\n  });\n```\n\n### File Structure\n\nThe file-based database is stored as a `.json` file. Each operation is asynchronous, and the data is stored in the specified file in JSON format. The class provides methods for working with the database, including automatic backups and error handling.\n\n### Methods Overview\n\n#### `set(key: string, value: any): Promise\u003cvoid\u003e`\nStores a key-value pair in the database.\n\n#### `get(key: string): Promise\u003cany\u003e`\nRetrieves the value for a given key.\n\n#### `delete(key: string): Promise\u003cvoid\u003e`\nDeletes a key-value pair from the database.\n\n#### `has(key: string): Promise\u003cboolean\u003e`\nChecks if the database contains a given key.\n\n#### `backup(fileName: string): Promise\u003cvoid\u003e`\nCreates a backup of the database to the specified file.\n\n#### `reset(): Promise\u003cvoid\u003e`\nClears all data from the database.\n\n#### `close(): Promise\u003cvoid\u003e`\nCloses the database and cleans up any resources, including removing lock files.\n\n## Error Handling\n\nAll operations on the database return Promises, so you should handle errors using `.catch()` or `try/catch` blocks.\n\nExample:\n\n```typescript\ndb.set('name', 'John Doe')\n  .catch((error) =\u003e {\n    console.error('Error setting data:', error);\n  });\n```\n\n## Conclusion\n\nThis simple file-based database system provides essential operations such as setting, getting, deleting, and backing up data. It also supports auto-backup and safe closing of the database to ensure data integrity. \n\nYou can easily integrate this database into any Node.js project that requires persistent data storage in a file-based format.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuvii%2Fdatabase","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feuvii%2Fdatabase","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuvii%2Fdatabase/lists"}