Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. [![NodeJs](https://github.com/Belphemur/node-json-db/actions/workflows/nodejs.yml/badge.svg)](https://github.com/Belphemur/node-json-db/actions/workflows/nodejs.yml)[![codecov](https://codecov.io/gh/Belphemur/node-json-db/branch/master/graph/badge.svg?token=J3Ppt4UCbY)](https://codecov.io/gh/Belphemur/node-json-db)[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FBelphemur%2Fnode-json-db.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FBelphemur%2Fnode-json-db?ref=badge_shield)
  2. [![NPM](https://nodei.co/npm/node-json-db.png?downloads=true&stars=true)](https://nodei.co/npm/node-json-db/)
  3. > A simple "database" that use JSON file for Node.JS.
  4. ## Installation
  5. Add `node-json-db` to your existing Node.js project.
  6. ```bash
  7. yarn add node-json-db
  8. ```
  9. ## Documentation
  10. [Auto Generated](https://belphemur.github.io/node-json-db)
  11. ## Inner Working
  12. ### Data
  13. The module stores the data using JavaScript Object directly into a JSON file. You can easily traverse the data to reach
  14. directly the interesting property using the DataPath. The principle of DataPath is the same as XMLPath.
  15. ### Example
  16. ```javascript
  17. {
  18. test: {
  19. data1 : {
  20. array : ['test','array']
  21. },
  22. data2 : 5
  23. }
  24. }
  25. ```
  26. If you want to fetch the value of array, the DataPath is **/test/data1/array**
  27. To reach the value of data2 : **/test/data2**
  28. You can of course get also the full object **test** : **/test**
  29. Or even the root : **/**
  30. ## Usage
  31. See [test](https://github.com/Belphemur/node-json-db/tree/master/test) for more usage details.
  32. ```javascript
  33. import { JsonDB } from 'node-json-db';
  34. import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
  35. // The first argument is the database filename. If no extension, '.json' is assumed and automatically added.
  36. // The second argument is used to tell the DB to save after each push
  37. // If you put false, you'll have to call the save() method.
  38. // The third argument is to ask JsonDB to save the database in an human readable format. (default false)
  39. // The last argument is the separator. By default it's slash (/)
  40. var db = new JsonDB(new Config("myDataBase", true, false, '/'));
  41. // Pushing the data into the database
  42. // With the wanted DataPath
  43. // By default the push will override the old value
  44. db.push("/test1","super test");
  45. // It also create automatically the hierarchy when pushing new data for a DataPath that doesn't exists
  46. db.push("/test2/my/test",5);
  47. // You can also push directly objects
  48. db.push("/test3", {test:"test", json: {test:["test"]}});
  49. // If you don't want to override the data but to merge them
  50. // The merge is recursive and work with Object and Array.
  51. db.push("/test3", {
  52. new:"cool",
  53. json: {
  54. important : 5
  55. }
  56. }, false);
  57. /*
  58. This give you this results :
  59. {
  60. "test":"test",
  61. "json":{
  62. "test":[
  63. "test"
  64. ],
  65. "important":5
  66. },
  67. "new":"cool"
  68. }
  69. */
  70. // You can't merge primitive.
  71. // If you do this:
  72. db.push("/test2/my/test/",10,false);
  73. // The data will be overriden
  74. // Get the data from the root
  75. var data = db.getData("/");
  76. // From a particular DataPath
  77. var data = db.getData("/test1");
  78. // If you try to get some data from a DataPath that doesn't exists
  79. // You'll get an Error
  80. try {
  81. var data = db.getData("/test1/test/dont/work");
  82. } catch(error) {
  83. // The error will tell you where the DataPath stopped. In this case test1
  84. // Since /test1/test does't exist.
  85. console.error(error);
  86. };
  87. // Deleting data
  88. db.delete("/test1");
  89. // Save the data (useful if you disable the saveOnPush)
  90. db.save();
  91. // In case you have a exterior change to the databse file and want to reload it
  92. // use this method
  93. db.reload();
  94. ```
  95. ### TypeScript Support
  96. #### v0.8.0
  97. As of v0.8.0, [TypeScript](https://www.typescriptlang.org) types are
  98. included in this package, so using `@types/node-json-db` is no longer required.
  99. #### v1.0.0
  100. JsonDB isn't exported as default any more. You'll need to change how you load the library.
  101. This change is done to follow the right way to import module.
  102. ```javascript
  103. import { JsonDB } from 'node-json-db';
  104. import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
  105. const db = new JsonDB(new Config("myDataBase", true, false, '/'));
  106. ```
  107. #### Typing
  108. With TypeScript, you have access to a new method: getObject<T> that will take care of typing your return object.
  109. ```typescript
  110. import { JsonDB } from 'node-json-db';
  111. import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
  112. const db = new JsonDB(new Config("myDataBase", true, false, '/'));
  113. interface FooBar {
  114. Hello: string
  115. World: number
  116. }
  117. const object = {Hello: "World", World: 5} as FooBar;
  118. db.push("/test", object);
  119. //Will be typed as FooBar in your IDE
  120. const result = db.getObject<FooBar>("/test");
  121. ```
  122. ### Array Support
  123. You can also access the information stored into arrays and manipulate them.
  124. ```typescript
  125. import { JsonDB } from 'node-json-db';
  126. import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
  127. // The first argument is the database filename. If no extension, '.json' is assumed and automatically added.
  128. // The second argument is used to tell the DB to save after each push
  129. // If you put false, you'll have to call the save() method.
  130. // The third argument is to ask JsonDB to save the database in an human readable format. (default false)
  131. const db = new JsonDB(new Config("myDataBase", true, false, '/'));
  132. // This will create an array 'myarray' with the object '{obj:'test'}' at index 0
  133. db.push("/arraytest/myarray[0]", {
  134. obj:'test'
  135. }, true);
  136. // You can retrieve a property of an object included in an array
  137. // testString = 'test';
  138. var testString = db.getData("/arraytest/myarray[0]/obj");
  139. // Doing this will delete the object stored at the index 0 of the array.
  140. // Keep in mind this won't delete the array even if it's empty.
  141. db.delete("/arraytest/myarray[0]");
  142. ```
  143. #### Appending in Array
  144. ```javascript
  145. // You can also easily append new item to an existing array
  146. // This set the next index with {obj: 'test'}
  147. db.push("/arraytest/myarray[]", {
  148. obj:'test'
  149. }, true);
  150. // The append feature can be used in conjuction with properties
  151. // This will set the next index as an object {myTest: 'test'}
  152. db.push("/arraytest/myarray[]/myTest", 'test', true);
  153. ```
  154. #### Last Item in Array
  155. ```javascript
  156. // Add basic array
  157. db.push("/arraytest/lastItemArray", [1, 2, 3], true);
  158. // You can easily get the last item of the array with the index -1
  159. // This will return 3
  160. db.getData("/arraytest/lastItemArray[-1]");
  161. // You can delete the last item of an array with -1
  162. // This will remove the integer "3" from the array
  163. db.delete("/arraytest/lastItemArray[-1]");
  164. // This will return 2 since 3 just got removed
  165. db.getData("/arraytest/lastItemArray[-1]");
  166. ```
  167. #### Count for Array
  168. ```javascript
  169. //
  170. db.push("/arraytest/list", [{id: 65464646155, name: "test"}], true);
  171. // You can have the number of element, in this case = 1
  172. let numberOfElement = db.count("/arraytest/list");
  173. ```
  174. #### Get Index in Array
  175. ```javascript
  176. // You can have the current index of an object
  177. db.push("/arraytest/myarray", {id: 65464646155, name: "test"}, true);
  178. db.getIndex("/arraytest/myarray", 65464646155);
  179. // By default, the property is 'id'
  180. // You can add another property instead
  181. db.getIndex("/arraytest/myarray", "test", "name");
  182. // It's useful if you want to delete some object
  183. db.delete("/arraytest/myarray[" + db.getIndex("/arraytest/myarray", 65464646155) + "]");
  184. ```
  185. ### Exception/Error
  186. #### Type
  187. | Type | Explanation |
  188. | ------------- |:----------------------------------------------------------------:|
  189. | DataError | When the error is linked to the Data Given |
  190. | DatabaseError | Linked to a problem with the loading or saving of the Database. |
  191. #### Errors
  192. | Error | Type | Explanation |
  193. | ------------------------------------------------------|:-------------:|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
  194. |The Data Path can't be empty |DataError |The Database expect to minimum receive the root **separator** as DataPath. |
  195. |Can't find dataPath: /XXX. Stopped at YYY |DataError |When the full hierarchy of the DataPath given is not present in the Database. It tells you until where it's valid. This error can happen when using *getData* and *delete* |
  196. |Can't merge another type of data with an Array |DataError |If you chose to not override the data (merging) when pushing and the new data is an array but the current data isn't an array (an Object by example). |
  197. |Can't merge an Array with an Object |DataError |Same idea as the previous message. You have an array as current data and ask to merge it with an Object. |
  198. |DataPath: /XXX. YYY is not an array. |DataError |When trying to access an object as an array. |
  199. |DataPath: /XXX. Can't find index INDEX in array YYY |DataError |When trying to access a non-existent index in the array. |
  200. |Only numerical values accepted for array index |DataError |An array can only use number for its indexes. For this use the normal object. |
  201. |The entry at the path (/XXX) needs to be either an Object or an Array |DataError |When using the find method, the rootPath need to point to an object or an array to search into it for the wanted value. |
  202. |Can't Load Database: XXXX |DatabaseError |JsonDB can't load the database for "err" reason. You can find the nested error in **error.inner** |
  203. |Can't save the database: XXX |DatabaseError |JsonDB can't save the database for "err" reason. You can find the nested error in **error.inner** |
  204. |DataBase not loaded. Can't write |DatabaseError |Since the database hasn't been loaded correctly, the module won't let you save the data to avoid erasing your database. |
  205. # Limitations
  206. ## Object with `separator` in key
  207. Object pushed with key containing the `separator` character won't be reachable. See [#75](https://github.com/Belphemur/node-json-db/issues/75).
  208. Please consider the `separator` as a reserved character by node-json-db.
  209. # Thanks
  210. [James Davis](https://github.com/davisjam) for helping to fix a regular expression vulnerable to [catastrophic backtracking](https://docs.microsoft.com/en-us/dotnet/standard/base-types/backtracking-in-regular-expressions).
  211. ## License
  212. [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FBelphemur%2Fnode-json-db.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FBelphemur%2Fnode-json-db?ref=badge_large)