{"_id":"0db","_rev":"3-1e57097330d08baceb4b1a372dca93fa","name":"0db","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.0":{"name":"0db","version":"0.0.0","description":"Simple JSON file database.","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"repository":{"type":"git","url":"git+https://github.com/Ambratolm/0db.git"},"keywords":["database","db","json","local","file","fs","query","crud"],"author":{"name":"Ambratolm","email":"ambratolm@gmail.com"},"license":"MIT","bugs":{"url":"https://github.com/Ambratolm/0db/issues"},"homepage":"https://github.com/Ambratolm/0db#readme","_id":"0db@0.0.0","_nodeVersion":"13.14.0","_npmVersion":"6.14.4","dist":{"integrity":"sha512-v7G4oi2MBUWfjFg50x0Vrj6sKE7MmT8qtfBHks/N9ln778v6Ow/Cd9N7s6FHAAK/KNof72LlA3w6JzIoNUuBUA==","shasum":"122389ea03f9fd616968e685a97b242d2d72684b","tarball":"https://registry.npmjs.org/0db/-/0db-0.0.0.tgz","fileCount":4,"unpackedSize":2136,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDa4DErgkcMQKBmfYAawCPNrx4ymlc0abDWHS+Z9uHqxgIgaMUhru0vvn6sAKHprAtkFqF7DNSv8oRtlftItr+9fuM="}]},"_npmUser":{"name":"ambratolm","email":"ambratolm@gmail.com"},"directories":{},"maintainers":[{"name":"ambratolm","email":"ambratolm@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/0db_0.0.0_1632854008335_0.783181050511691"},"_hasShrinkwrap":false},"0.0.1":{"name":"0db","version":"0.0.1","description":"Simple JSON file database.","main":"index.js","scripts":{"readme":"markdown-toc -i README.md","test":"nodemon $test/ --ignore db.json --ignore big.db.json"},"dependencies":{"bcryptjs":"^2.4.3","i":"^0.3.7","jsonwebtoken":"^8.5.1","lodash":"^4.17.21","uuid":"^8.3.2"},"devDependencies":{},"author":{"name":"Ambratolm","email":"ambratolm@gmail.com"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/Ambratolm/0db.git"},"keywords":["database","db","json","local","file","fs","query","crud"],"bugs":{"url":"https://github.com/Ambratolm/0db/issues"},"homepage":"https://github.com/Ambratolm/0db#readme","gitHead":"519b967e5fc0e6f4809bf7ba4ca76c9902917ad2","_id":"0db@0.0.1","_nodeVersion":"13.14.0","_npmVersion":"6.14.4","dist":{"integrity":"sha512-rkKbJO2uaMHufQiky1khZ/8w61jnNlljGnSitxSOSl9+OihyEj1+SJPSmlP5VdyyzLmzfo/46/lgOqkHGiBRtA==","shasum":"b2e608e7ae1b9684d48056c96726aefd831bc9a8","tarball":"https://registry.npmjs.org/0db/-/0db-0.0.1.tgz","fileCount":25,"unpackedSize":1166476,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhudzrCRA9TVsSAnZWagAAmWgP/iZzYf/mM0iAWcTEpggM\nknh5gvVyOEWZHGngcarsIUpQ6mZD4WGPhGzN/1rXxpf2e86nuE9HCBoRWCIl\nXPx3nDeO0h+xnXMs5LaUPFEW8BxpdaC8Sc2GAUxd6/uz20Ph6tyJYdK4TK2v\npP1jQXldk6Okzlrn9u8XnnVX1nJG1oZb3zo/3YnKOFB44upHmKrviH/Jt1ub\n+BPv9p0gS+JNeaquM8mObJJx7QAGGtktx4s+lG++Gs0eZ5HDWv6PHtuV07nS\nsvd8muvrfWIXfpvR0qKmjrHy/fkvElcUPbR7KQHpMI8ek8Llh6iDUpdFVKDV\ngv0Ef+8NnWuIeg1Q1BI+k//yoHbn3GtEN6vLwvcT0e1emtnrYZrHvCmnlvcV\n+wC4PUGhz+K+JKrLYhw7A9hzykYWVJDzOIiPTFd7qXqXEG+M2AUIdubPNRiy\nf6p4NKXGLUgDo1DBOeB5jqJ2XyYjm9pj7AOUPfU5KroGEsfzcLnr7q+nqX0k\n9TfoF7YHZjYo2suhLJv+Qq4B1BqeDkwvMAgwy+rAf+sYEgJuquaf4Wfgh0eI\nUBFR9RczrpI+OptdYKBPpyhhIz4ZarZ5eEIdI2Y8MDKLQETjqVgp2CtKV1Wj\nfnCOs4mNr5/yMe7c+lVeX5baJCduVGPhcJPcdgajOs1pNzP7TkVWC2G3CrK8\nHmAz\r\n=sx9a\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFfSkEGhknsbQ6m597xkd7EGLQbVK7HOOdpFmujcwscfAiA/gaB7JAIcbo8Ffr7+hdWeJ40ap12GzDJm/krFDUIfqQ=="}]},"_npmUser":{"name":"ambratolm","email":"ambratolm@gmail.com"},"directories":{},"maintainers":[{"name":"ambratolm","email":"ambratolm@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/0db_0.0.1_1633321620290_0.48503127963450776"},"_hasShrinkwrap":false}},"time":{"created":"2021-09-28T18:33:28.334Z","0.0.0":"2021-09-28T18:33:28.503Z","modified":"2022-04-04T10:08:39.693Z","0.0.1":"2021-10-04T04:27:00.463Z"},"maintainers":[{"name":"ambratolm","email":"ambratolm@gmail.com"}],"description":"Simple JSON file database.","homepage":"https://github.com/Ambratolm/0db#readme","keywords":["database","db","json","local","file","fs","query","crud"],"repository":{"type":"git","url":"git+https://github.com/Ambratolm/0db.git"},"author":{"name":"Ambratolm","email":"ambratolm@gmail.com"},"bugs":{"url":"https://github.com/Ambratolm/0db/issues"},"license":"MIT","readme":"> ### 🚧 Work in progress...\n\n# ⭕ 0db\n\n[![NPM version](https://badge.fury.io/js/0db.svg)](https://npmjs.org/package/0db)\n\n<!-- [![Build Status](https://travis-ci.org/Ambratolm/0db.svg?branch=master)](https://travis-ci.org/Ambratolm/0db) -->\n\nSimple JSON file based database with easy querying interface and common utilities.\n\n> For whatever reason, If you don't want to use a real database, and instead simply use a file. but also want some query and utility functions of a database. this is **0db** 😁.\n\n👇 Glimpse example :\n\n```js\ndb(\"users\").create({ name: \"kenza\", email: \"kenza@old.com\" });\ndb(\"users\").read({ name: \"kenza\" });\ndb(\"users\").update({ name: \"kenza\" }, { email: \"kenza@new.com\" });\ndb(\"users\").delete({ name: \"kenza\" });\n```\n**0db** is constituted of **four** (`create`, `read`, `update`, and `delete`) methods that represents the **CRUD** operations and **three** (or less) parameters (`item`, `query`, and `options`) considerably (more or less) recurring in the four methods.\n\n<!-- toc -->\n\n- [📥 Installation](#%F0%9F%93%A5-installation)\n- [🏁 Getting Started](#%F0%9F%8F%81-getting-started)\n- [🚩 Initialize](#%F0%9F%9A%A9-initialize)\n- [🔎 Query](#%F0%9F%94%8E-query)\n- [☑️ Options](#%E2%98%91%EF%B8%8F-options)\n- [💠 CREATE](#%F0%9F%92%A0-create)\n  * [☑️ CREATE Options](#%E2%98%91%EF%B8%8F-create-options)\n  * [🎯 CREATE Examples](#%F0%9F%8E%AF-create-examples)\n- [💠 READ](#%F0%9F%92%A0-read)\n  * [☑️ READ Options](#%E2%98%91%EF%B8%8F-read-options)\n  * [🎯 READ Examples](#%F0%9F%8E%AF-read-examples)\n- [💠 UPDATE](#%F0%9F%92%A0-update)\n  * [☑️ UPDATE Options](#%E2%98%91%EF%B8%8F-update-options)\n  * [🎯 UPDATE Examples](#%F0%9F%8E%AF-update-examples)\n- [💠 DELETE](#%F0%9F%92%A0-delete)\n  * [☑️ DELETE Options](#%E2%98%91%EF%B8%8F-delete-options)\n  * [🎯 DELETE Examples](#%F0%9F%8E%AF-delete-examples)\n- [↘️ Other](#%E2%86%98%EF%B8%8F-other)\n- [📃 License](#%F0%9F%93%83-license)\n\n<!-- tocstop -->\n\n## 📥 Installation\n\n```bash\nnpm i 0db\n```\n\n## 🏁 Getting Started\n\n👇 Learn by a simple common example :\n\n```js\n// Require library\nconst $0db = require(\"0db\");\n\n// Initialize database\n// 💡 By default, A \"db.json\" file will be created in root directory\nconst db = await $0db();\n\n// Create a new item within a collection named \"users\"\n// 💡 If the collection doesn't exist it will be created automatically\n// 💡 With the utility option \"encrypt\", the \"password\" field\n//    will be saved as an encrypted hash string instead of the original\nconst createdUser = await db(\"users\").create(\n  {\n    name: \"kenza\",\n    email: \"kenza@email.com\",\n    password: \"secret123\",\n  },\n  {\n    encrypt: \"password\",\n  }\n);\n\n// Read all items from \"users\" collection where \"name\" is \"kenza\"\n// 💡 The \"omit\" option hides the \"password\" and \"email\"\n//    fields in the returned results\nconst users = await db(\"users\").read(\n  { name: \"kenza\" },\n  {\n    omit: [\"password\", \"email\"],\n  }\n);\n\n// Update all \"users\" items where \"name\" is \"kenza\"\n// with new values for \"email\" and \"password\"\nconst updatedUser = await db(\"users\").update(\n  { name: \"kenza\" },\n  {\n    email: \"kenza@NewEmail.com\",\n    password: \"NEW_SECRET_123456789\",\n  }\n);\n\n// Delete all \"users\" items where \"email\" is \"kenza@NewEmail.com\"\nconst deletedUser = await db(\"users\").update({ email: \"kenza@NewEmail.com\" });\n```\n\n💡 A JSON file named `db.json` (by default) is created in the root directory. This is an example of its content :\n\n```json\n{\n  \"users\": [\n    {\n      \"name\": \"kenza\",\n      \"email\": \"kenza@example.com\",\n      \"$id\": \"8c8f128e-4905-4e77-b664-e03f6de5e952\",\n      \"$createdAt\": \"2021-09-05T21:40:27Z\"\n    },\n    {\n      \"name\": \"ambratolm\",\n      \"email\": \"ambratolm@example.com\",\n      \"$id\": \"5211133c-a183-4c99-90ab-f857adf5442a\",\n      \"$createdAt\": \"2002-11-01T22:12:55Z\",\n      \"$updatedAt\": \"2021-10-02T00:00:00Z\"\n    }\n  ]\n}\n```\n\n💡 Note that the `$id` and `$createdAt` fields are created automatically when an item is created, and `$updatedAt` when it is updated.\n\n## 🚩 Initialize\n\n```js\n// Initialize with a \"db.json\" file in the root directory\nconst db = await fsdb();\n\n// Initialize with a custom named JSON file in the root directory\nconst db = await fsdb(\"my-database-file.json\");\n\n// Initialize with a custom named JSON file in the current directory\nconst db = await fsdb(__dirname + \"/my-database-file\");\n```\n\n## 🔎 Query\n\nQuery parameter in `Read`, `Update`, and `Delete` methods is an object or function that allows targeting specific items in collection.\n\n- Query object :\n\n```\n{\n  fieldName: fieldValue,\n  fieldName: fieldValue,\n  ...etc\n}\n```\n\nQuery object is an object of property values to match against collection items.<br />\nA comparison is performed between every item object property values in collection and the query object property values to determine if an item object contains equivalences.<br />\nThe items containing the equivalences are returned.\n\nExample:\n```js\nconst queryObj = {\n  firstName: \"kenza\",\n  age: 20,\n  rating: 5\n};\n\nconst users = await db(\"users\").read(queryObj);\n```\n\n- Query function :\n\n```\n(item, index?, collection?) => [Boolean]\n```\n\nQuery function is a predicate called for every item when iterating over items of collection.<br />\nAll items predicate returns truthy for are returned.<br />\nThe predicate is invoked with three arguments :\n- `value` : **Required**. The value of the current item.\n- `index` : _Optional_. The index of the current item in collection.\n- `collection` : _Optional_. The collection array-object the current item belongs to.\n\nExample:\n\n```js\nconst queryFn = (user) => {\n  return user.name.startsWith(\"k\") && user.age >= 20 && rating >= 5;\n};\n\nconst users = await db(\"users\").read(queryFn);\n```\n\n## ☑️ Options\n\n```\n{\n  optionName: optionValue,\n  optionName: optionValue,\n  ...etc\n}\n```\n\nOptions parameter in **all** methods is an object that allows to apply additional stuff to the method's subject item or to the method's returned items result.<br />\nEvery method can have specific options or common options depending on the context of the method.\n\nExample :\n\n```js\nconst options = {\n  unique: [\"name\", \"email\"],\n  encrypt: \"password\",\n  omit: [\"email\", \"password\"],\n  nocase: true\n};\n\nconst user = {\n  name: \"moulay-elhassan\",\n  email: \"hasson@example.com\",\n  password: \"secret#hasson?1980\"\n}\n\nconst createdUser = await db(\"users\").create(user, options);\n```\n\n## 💠 CREATE\n\n```js\nawait db(collectionName).create(item?, options?);\n```\n\nCreates a new item in a collection.<br />\n💡 If the specified collection doesn't exist it will be created automatically.<br />\n💡 If no item object is specified (omitted, `null`, or `undefined`), an empty item is created with no fields except the system fields (with names starting with $ sign).<br />\n💡 The created item is returned.\n\n| Parameter      | Type   | Default | Description                             |\n| -------------- | ------ | ------- | --------------------------------------- |\n| collectionName | String |         | Targeted collection name                |\n| item           | Object | {}      | Item to create                          |\n| options        | Object | {}      | CREATE options                          |\n| **@returns**   | Object |         | The created item                        |\n| **@throws**    | Error  |         | If a unique field value is already used |\n| **@throws**    | Error  |         | If a value to encrypt is not a string   |\n\n### ☑️ CREATE Options\n\n| Property | Type               | Default | Description                      |\n| -------- | ------------------ | ------- | -------------------------------- |\n| unique   | String or String[] | \"\"      | Fields to declare as unique      |\n| encrypt  | String or String[] | \"\"      | Fields to encrypt                |\n| pick     | String or String[] | \"\"      | Fields to pick in returned items |\n| omit     | String or String[] | \"\"      | Fields to omit in returned items |\n| nocase   | Boolean            | false   | If true ignores case in search   |\n\n💡 When fields are declared as `unique`, a checking for duplicates is done before adding the item.\n\n💡 If `nocase` is true, letter case comparison will be ignored in search operations, like for example checking `unique` values.\n\n### 🎯 CREATE Examples\n\n```js\n// Create an item within a collection named \"players\" (automatically created)\n// The created item is returned\nconst createdPlayer = await db(\"players\").create({\n  name: \"ambratolm\",\n  level: 99,\n  inventory: [\"sword\", \"shield\", \"potion\"],\n});\n\n// Create an item within a collection named \"players\" with some options\nconst createdPlayer = await db(\"players\").create(\n  {\n    name: \"ambratolm\",\n    level: 99,\n    inventory: [\"sword\", \"shield\", \"potion\"],\n    password: \"this_is_a_secret\",\n  },\n  {\n    // Options\n    unique: \"name\", // Make \"name\" field unique\n    encrypt: \"password\", // Encrypt \"password\" field\n    omit: [\"password\", \"level\"], // Omit fields in the returned item object\n    nocase: true, // Ignore case when comparing strings\n  }\n);\n```\n\n## 💠 READ\n\n```js\nawait db(collectionName).read(query?, options?);\n```\n\nReads an existing item in a collection.<br />\n💡 If the specified collection doesn't exist it will be created automatically.<br />\n💡 If no query is specified (omitted, `null`, or `undefined`), the query defaults to empty query `{}` which returns all items.<br />\n💡 The read items are returned.\n\n| Parameter      | Type   | Default | Description                       |\n| -------------- | ------ | ------- | --------------------------------- |\n| collectionName | String |         | Targeted collection name          |\n| query          | Object | {}      | Query object or function          |\n| options        | Object | {}      | READ options                      |\n| **@returns**   | Array  |         | The read item                     |\n| **@throws**    | Error  |         | If an encrypted field not matched |\n\n### ☑️ READ Options\n\n| Property | Type               | Default | Description                        |\n| -------- | ------------------ | ------- | ---------------------------------- |\n| one      | Boolean            | false   | Return only one result (Object)    |\n| pick     | String or String[] | []      | Fields to pick in returned items   |\n| omit     | String or String[] | []      | Fields to omit in returned items   |\n| nocase   | Boolean            | false   | Ignore case in search              |\n| sort     | String or String[] | \"\"      | Fields to sort by returned items   |\n| order    | String or String[] | \"asc\"   | Order of sorting of returned items |\n| encrypt  | String or String[] | []      | Fields to encrypt                  |\n| limit    | Number             | MAX     | Number of returned items           |\n| page     | Number             | 0       | Index of pagination (with limit)   |\n| expand   | String             | \"\"      | Name of collection to expand to    |\n| embed    | String             | \"\"      | Name of collection to embed        |\n\n### 🎯 READ Examples\n\n```js\n// Read all items in \"players\" collection\nconst players = await db(\"players\").read();\n\n// Read items matching a query object\nconst somePlayers = await db(\"players\").read({ name: \"ambratolm\" });\n\n// Read items matching a query function\nconst someOtherPlayers = await db(\"players\").read(\n  (player) => player.level >= 90\n);\n\n// Read items matching a query with some options\nconst player = await db(\"players\").read(\n  { name: \"AmBrAtOlM\" },\n  {\n    // Options\n    nocase: true, // Ignore case when comparing strings\n    one: true, // return only one result (an object instead of array)\n  }\n);\n```\n\n## 💠 UPDATE\n\n```js\nawait db(collectionName).update(query?, changes?, options?);\n```\n\nUpdates an existing item in a collection.<br />\n💡 If the specified collection doesn't exist it will be created automatically.<br />\n💡 If no query is specified (omitted, `null`, or `undefined`), no item is updated.<br />\n💡 If an empty query `{}` is specified, all items are updated.\n💡 If no changes are specified (omitted, `null`, or `undefined`), the changes default to empty changes `{}` which only updates the `$updatedAt` field in targeted items.\n💡 The updated items are returned.\n\n| Parameter      | Type   | Default | Description                             |\n| -------------- | ------ | ------- | --------------------------------------- |\n| collectionName | String |         | Targeted collection                     |\n| query          | Object | {}      | Query object or function                |\n| changes        | Object | {}      | Changes to apply                        |\n| options        | Object | {}      | Additional options                      |\n| **@returns**   | Array  |         | The updated item                        |\n| **@throws**    | Error  |         | If Items matching query not found       |\n| **@throws**    | Error  |         | If a unique field value is already used |\n| **@throws**    | Error  |         | If a value to encrypt is not a string   |\n\n### ☑️ UPDATE Options\n\n| Property | Type               | Default | Description                       |\n| -------- | ------------------ | ------- | --------------------------------- |\n| total    | Boolean            | false   | If true overrides all item fields |\n| one      | Boolean            | false   | Return only one result (Object)   |\n| unique   | String or String[] | \"\"      | Fields to declare as unique       |\n| encrypt  | String or String[] | []      | Fields to encrypt                 |\n| pick     | String or String[] | []      | Fields to pick in returned items  |\n| omit     | String or String[] | []      | Fields to omit in returned items  |\n| nocase   | Boolean            | false   | Ignore case in search             |\n\n### 🎯 UPDATE Examples\n\n```js\n// Update item(s)\n// The updated item is returned\nconst updatedPlayer = await db(\"players\").update(\n  { name: \"ambratolm\" }, // Query can also be a function\n  { name: \"new name\", level: 0 } // Changes to apply\n);\n\n// Update item(s) with some options\nconst updatedPlayer = await db(\"players\").update(\n  { name: \"ambratolm\" }, // Query can also be a function\n  { name: \"new name\", level: 0 }, // Changes to apply\n  {\n    // Options\n  }\n);\n```\n\n## 💠 DELETE\n\n```js\nawait db(collectionName).delete(query?, options?);\n```\n\nDeletes an existing item in a collection.<br />\n💡 If the specified collection doesn't exist it will be created automatically.<br />\n💡 If no query is specified (omitted, `null`, or `undefined`), no item is deleted.<br />\n💡 If an empty query `{}` is specified, all items are deleted.<br />\n💡 The deleted items are returned.\n\n| Parameter      | Type   | Default | Description                       |\n| -------------- | ------ | ------- | --------------------------------- |\n| collectionName | String |         | Targeted collection name          |\n| query          | Object | {}      | Query object or function          |\n| options        | Object | {}      | Additional options                |\n| **@returns**   | Object |         | The deleted item                  |\n| **@throws**    | Error  |         | If Items matching query not found |\n\n### ☑️ DELETE Options\n\n| Property | Type               | Default | Description                      |\n| -------- | ------------------ | ------- | -------------------------------- |\n| one      | Boolean            | false   | Return only one result (Object)  |\n| pick     | String or String[] | []      | Fields to pick in returned items |\n| omit     | String or String[] | []      | Fields to omit in returned items |\n| nocase   | Boolean            | false   | Ignore case in search            |\n\n### 🎯 DELETE Examples\n\n```js\n// Delete item(s)\n// The deleted item is returned\nconst deletedPlayer = await db(\"players\").delete(\n  { name: \"ambratolm\" } // Query can also be a function\n);\n\n// Delete item(s) with some options\nconst deletedPlayer = await db(\"players\").delete(\n  { name: \"ambratolm\" }, // Query can also be a function\n  {\n    // Options\n  }\n);\n```\n\n## ↘️ Other\n\n```js\n// Remove all collections\nawait db.drop();\n\n// Remove a collection named \"players\"\nawait db(\"players\").drop();\n\n// Remove all items in a collection named \"players\" and keep it\nawait db(\"players\").clear();\n```\n\n## 📃 License\n\n[MIT](./LICENSE) © [Ambratolm](https://github.com/Ambratolm)\n","readmeFilename":"README.md"}