{"id":20522084,"url":"https://github.com/pannh/swift-database","last_synced_at":"2025-03-06T00:28:34.187Z","repository":{"id":65408774,"uuid":"583808011","full_name":"PannH/swift-database","owner":"PannH","description":"A light-weight module to interact with your local JSON database.","archived":false,"fork":false,"pushed_at":"2023-01-03T17:38:38.000Z","size":12,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-16T19:48:41.712Z","etag":null,"topics":["database","json","nosql"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/swift-database","language":null,"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/PannH.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}},"created_at":"2022-12-31T01:50:10.000Z","updated_at":"2023-01-03T21:47:16.000Z","dependencies_parsed_at":"2023-02-01T08:16:41.387Z","dependency_job_id":null,"html_url":"https://github.com/PannH/swift-database","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PannH%2Fswift-database","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PannH%2Fswift-database/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PannH%2Fswift-database/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PannH%2Fswift-database/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PannH","download_url":"https://codeload.github.com/PannH/swift-database/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242127563,"owners_count":20076134,"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","json","nosql"],"created_at":"2024-11-15T22:33:54.312Z","updated_at":"2025-03-06T00:28:34.160Z","avatar_url":"https://github.com/PannH.png","language":null,"readme":"![npm version](https://img.shields.io/npm/v/swift-database?color=c80000\u0026label=npm%20version) ![Downloads](https://img.shields.io/npm/dt/swift-database?label=Downloads)\n\n# \u003ccenter\u003e🗃 swift-database\u003c/center\u003e\n\n\u003e `swift-database` is a module that allows you to interact easily with your local JSON file.\n\n## 🔰 Getting started\n\n1. Install the module\n\n```\n npm install swift-database\n```\n\n2. Initialize the database class\n\n```javascript\nconst { default: JSONDatabase } = require('swift-database');\n\nconst database = new Database({\n   filePath: 'path/to/file.json'\n});\n```\n\n3. Create your first table and load it\n\n```javascript\ndatabase.createTable('users');\n\nconst users = database.table('users');\n```\n\n4. Interact with that table\n\n```javascript\nusers.createOne({\n   fullName: 'John Doe',\n   hobbies: ['programming', 'sport']\n});\n\nconst userDocument = users.findOne(\n   (userDocument) =\u003e userDocument.value.fullName === 'John Doe'\n);\n\nusers.deleteOne(userDocument.id);\n```\n\n## 📖 Documentation\n\n\u003cdetails\u003e\n\u003csummary\u003eJSONDatabase\u003c/summary\u003e\n\n\u003e Represents the database.\n\n\u003cdetails\u003e\n\u003csummary\u003eConstructor\u003c/summary\u003e\n\n| Parameter |          Type          | Required | Default |      Description      |\n| :-------: | :--------------------: | :------: | :-----: | :-------------------: |\n| `options` | `{ filePath: string }` |    ✓     | _None_  | The database options. |\n\n**Example :**\n\n```javascript\nconst database = new JSONDatabase({\n   filePath: 'path/to/file.json'\n});\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eJSONDatabase.tables\u003c/summary\u003e\n\n\u003e Returns an array of each table's name.\n\n**Type:** `string[]`\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eJSONDatabase.table(name)\u003c/summary\u003e\n\n\u003e Loads a table from the database.\n\n| Parameter |   Type   | Required | Default |               Description               |\n| :-------: | :------: | :------: | :-----: | :-------------------------------------: |\n|  `name`   | `string` |    ✓     | _None_  | The name of the table you want to load. |\n\n**Returns:** `DatabaseTable`\n\n**Example :**\n\n```javascript\nconst users = database.table('users');\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eJSONDatabase.createTable(name)\u003c/summary\u003e\n\n\u003e Creates a new table into the database.\n\n| Parameter |   Type   | Required | Default |                Description                |\n| :-------: | :------: | :------: | :-----: | :---------------------------------------: |\n|  `name`   | `string` |    ✓     | _None_  | The name of the table you want to create. |\n\n**Returns:** `DatabaseTable`\n\n**Example :**\n\n```javascript\nconst users = database.createTable('users');\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eJSONDatabase.deleteTable(name)\u003c/summary\u003e\n\n\u003e Delete an existing table from the database.\n\n| Parameter |   Type   | Required | Default |                Description                |\n| :-------: | :------: | :------: | :-----: | :---------------------------------------: |\n|  `name`   | `string` |    ✓     | _None_  | The name of the table you want to delete. |\n\n**Returns:** `void`\n\n**Example :**\n\n```javascript\ndatabase.deleteTable('users');\n```\n\n\u003c/details\u003e\n\n\u003c/details\u003e\n\n---\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable\u003c/summary\u003e\n\n\u003e Represents a database table.\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.size\u003c/summary\u003e\n\n\u003e Returns the amount of documents inside the table.\n\n**Type:** `number`\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.all\u003c/summary\u003e\n\n\u003e Returns an array of every table documents.\n\n**Type:** `TableDocument[]`\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.getById(documentId)\u003c/summary\u003e\n\n\u003e Returns the table document that matches the specified id.\n\n|  Parameter   |   Type   | Required | Default |               Description               |\n| :----------: | :------: | :------: | :-----: | :-------------------------------------: |\n| `documentId` | `string` |    ✓     | _None_  | The id of the document you want to get. |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\nconst userDocument = users.getById(DOCUMENT_ID);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.findOne(predicate)\u003c/summary\u003e\n\n\u003e Returns the first table document that matches the predicate.\n\n|  Parameter  |        Type         | Required | Default |                         Description                         |\n| :---------: | :-----------------: | :------: | :-----: | :---------------------------------------------------------: |\n| `predicate` | `PredicateFunction` |    ✓     | _None_  | The predicate function you want to filter the documents by. |\n\n💡 `PredicateFunction` = `(document: TableDocument, index: number, table: object[]) =\u003e boolean`\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst userDocument = users.findOne(\n   (userDocument) =\u003e userDocument.value.fullName === 'John Doe'\n);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.findMany(predicate)\u003c/summary\u003e\n\n\u003e Returns every documents that match the predicate.\n\n|  Parameter  |        Type         | Required | Default |                         Description                         |\n| :---------: | :-----------------: | :------: | :-----: | :---------------------------------------------------------: |\n| `predicate` | `PredicateFunction` |    ✓     | _None_  | The predicate function you want to filter the documents by. |\n\n💡 `PredicateFunction` = `(document: TableDocument, index: number, table: object[]) =\u003e boolean`\n\n**Returns:** `TableDocument[]`\n\n**Example :**\n\n```javascript\nconst userDocuments = users.findMany((userDocument) =\u003e\n   userDocument.value.hobbies.includes('programming')\n);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.createOne(data)\u003c/summary\u003e\n\n\u003e Creates a new table document and returns it.\n\n| Parameter |   Type   | Required | Default |     Description      |\n| :-------: | :------: | :------: | :-----: | :------------------: |\n|  `data`   | `object` |    ✓     | _None_  | The document's data. |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst createdUserDocument = users.createOne({\n   fullName: 'John Doe',\n   hobbies: ['programming', 'sport']\n});\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.createMany(...data)\u003c/summary\u003e\n\n\u003e Creates many table documents and returns them.\n\n| Parameter |    Type    | Required | Default |            Description            |\n| :-------: | :--------: | :------: | :-----: | :-------------------------------: |\n|  `data`   | `object[]` |    ✓     | _None_  | An array of each document's data. |\n\n**Returns:** `TableDocument[]`\n\n**Example :**\n\n```javascript\nconst createdUserDocuments = users.createMany(\n   {\n      fullName: 'John Doe',\n      hobbies: ['programming', 'sport']\n   },\n   {\n      fullName: 'Alice Doe',\n      hobbies: ['studying', 'videogames']\n   }\n);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.deleteOne(documentId)\u003c/summary\u003e\n\n\u003e Deletes a table document.\n\n|  Parameter   |   Type   | Required | Default |                Description                 |\n| :----------: | :------: | :------: | :-----: | :----------------------------------------: |\n| `documentId` | `string` |    ✓     | _None_  | The id of the document you want to delete. |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\nconst deletedUserDocument = users.deleteOne(DOCUMENT_ID);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.deleteMany(...documentIds)\u003c/summary\u003e\n\n\u003e Deletes many table documents.\n\n|   Parameter   |    Type    | Required | Default |                    Description                     |\n| :-----------: | :--------: | :------: | :-----: | :------------------------------------------------: |\n| `documentIds` | `string[]` |    ✓     | _None_  | An array of each document's id you want to delete. |\n\n**Returns:** `TableDocument[]`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_IDS = [\n   '0557f4db-5688-4d99-8f85-a83605cf8c1e',\n   '2fe5a45e-1ffe-47ba-ab14-ac94ee26ec68'\n];\n\nconst deletedUserDocuments = users.deleteMany(DOCUMENT_IDS);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.update(documentId, data)\u003c/summary\u003e\n\n\u003e Updates a table document.\n\n|  Parameter   |   Type   | Required | Default |                Description                 |\n| :----------: | :------: | :------: | :-----: | :----------------------------------------: |\n| `documentId` | `string` |    ✓     | _None_  | The id of the document you want to update. |\n|    `data`    | `object` |    ✓     | _None_  |        The data you want to update.        |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\n// Before: TableDocument { id: ..., value: { fullName: 'Alice Doe', ... } }\nconst updatedUserDocument = users.update(DOCUMENT_ID, {\n   fullName: 'Alice Dart'\n});\n// After: TableDocument { id: ..., value: { fullName: 'Alice Dart', ... } }\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.increment(documentId, propertyKey, value)\u003c/summary\u003e\n\n\u003e Increments a document's property.\n\n|   Parameter   |   Type   | Required | Default |                     Description                     |\n| :-----------: | :------: | :------: | :-----: | :-------------------------------------------------: |\n| `documentId`  | `string` |    ✓     | _None_  | The id of the document you want to make changes on. |\n| `propertyKey` | `string` |    ✓     | _None_  |   The key of the property you want to increment.    |\n|    `value`    | `string` |    X     |   `1`   |  The value you want to increment the property by.   |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\n// Before: TableDocument { id: ..., value: { age: 21, ... } }\nconst updatedUserDocument = users.increment(DOCUMENT_ID, 'age');\n// After: TableDocument { id: ..., value: { age: 22, ... } }\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.decrement(documentId, propertyKey, value)\u003c/summary\u003e\n\n\u003e Decrements a document's property.\n\n|   Parameter   |   Type   | Required | Default |                     Description                     |\n| :-----------: | :------: | :------: | :-----: | :-------------------------------------------------: |\n| `documentId`  | `string` |    ✓     | _None_  | The id of the document you want to make changes on. |\n| `propertyKey` | `string` |    ✓     | _None_  |   The key of the property you want to decrement.    |\n|    `value`    | `string` |    X     |   `1`   |  The value you want to decrement the property by.   |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\n// Before: TableDocument: { id: ..., value: { lives: 3, ... } }\nconst updatedUserDocument = users.decrement(DOCUMENT_ID, 'lives');\n// After: TableDocument: { id: ..., value: { lives: 2, ... } }\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.multiply(documentId, propertyKey, value)\u003c/summary\u003e\n\n\u003e Multiplies a document's property.\n\n|   Parameter   |   Type   | Required | Default |                     Description                     |\n| :-----------: | :------: | :------: | :-----: | :-------------------------------------------------: |\n| `documentId`  | `string` |    ✓     | _None_  | The id of the document you want to make changes on. |\n| `propertyKey` | `string` |    ✓     | _None_  |    The key of the property you want to multiply.    |\n|    `value`    | `string` |    ✓     | _None_  |   The value you want to multiply the property by.   |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\n// Before: TableDocument: { id: ..., value: { chances: 10, ... } }\nconst updatedUserDocument = users.multiply(DOCUMENT_ID, 'chances', 1.5);\n// After: TableDocument: { id: ..., value: { chances: 15, ... } }\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.divide(documentId, propertyKey, value)\u003c/summary\u003e\n\n\u003e Divides a document's property.\n\n|   Parameter   |   Type   | Required | Default |                     Description                     |\n| :-----------: | :------: | :------: | :-----: | :-------------------------------------------------: |\n| `documentId`  | `string` |    ✓     | _None_  | The id of the document you want to make changes on. |\n| `propertyKey` | `string` |    ✓     | _None_  |     The key of the property you want to divide.     |\n|    `value`    | `string` |    ✓     | _None_  |    The value you want to divide the property by.    |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\n// Before: TableDocument: { id: ..., value: { chances: 10, ... } }\nconst updatedUserDocument = users.divide(DOCUMENT_ID, 'chances', 2);\n// Before: TableDocument: { id: ..., value: { chances: 5, ... } }\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.deleteProperty(documentId, key)\u003c/summary\u003e\n\n\u003e Deletes a document's property.\n\n|  Parameter   |   Type   | Required | Default |                     Description                     |\n| :----------: | :------: | :------: | :-----: | :-------------------------------------------------: |\n| `documentId` | `string` |    ✓     | _None_  | The id of the document you want to make changes on. |\n|    `key`     | `string` |    ✓     | _None_  |     The key of the property you want to delete.     |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\n// Before: TableDocument: { id: ..., value: { fullName: 'John Doe', age: 21 } }\nconst updatedUserDocument = users.deleteProperty(DOCUMENT_ID, 'fullName');\n// Before: TableDocument: { id: ..., value: { age: 21 } }\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDatabaseTable.push(documentId, propertyKey, ...items)\u003c/summary\u003e\n\n\u003e Pushes items into an array document's property;\n\n|   Parameter   |   Type   | Required | Default |                         Description                         |\n| :-----------: | :------: | :------: | :-----: | :---------------------------------------------------------: |\n| `documentId`  | `string` |    ✓     | _None_  |     The id of the document you want to make changes on.     |\n| `propertyKey` | `string` |    ✓     | _None_  | The key to the array property you want to push the item to. |\n|    `items`    | `any[]`  |    ✓     | _None_  |         The items you want to push into the array.          |\n\n**Returns:** `TableDocument`\n\n**Example :**\n\n```javascript\nconst DOCUMENT_ID = '0557f4db-5688-4d99-8f85-a83605cf8c1e';\n\n// Before: TableDocument: { id: ..., value: { hobbies: ['programming'], ... } }\nconst updatedUserDocument = users.push(DOCUMENT_ID, 'hobbies', 'tennis');\n// Before: TableDocument: { id: ..., value: { hobbies: ['programming', 'tennis'], ... } }\n```\n\n\u003c/details\u003e\n\u003c/details\u003e\n\n---\n\n\u003cdetails\u003e\n\u003csummary\u003eTableDocument\u003c/summary\u003e\n\n\u003e Represents a table document.\n\n\u003cdetails\u003e\n\u003csummary\u003eTableDocument.id\u003c/summary\u003e\n\n\u003e Returns the document's id.\n\n**Type:** `string`\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eTableDocument.value\u003c/summary\u003e\n\n\u003e Returns the document's data.\n\n**Type:** `object`\n\n\u003c/details\u003e\n\u003c/details\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpannh%2Fswift-database","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpannh%2Fswift-database","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpannh%2Fswift-database/lists"}