|
1 | | -# **JSONDB** |
2 | | -A simple database-like tool for nodejs using JSON for data storage |
| 1 | +# **json-base** |
| 2 | +A simple json dbms with an orm, for nodejs, compatible with js and ts. |
3 | 3 | ___ |
4 | 4 |
|
5 | | - |
6 | 5 | ```bash |
7 | | -$ npm install jsondb |
| 6 | +npm install -g json-base |
8 | 7 | ``` |
9 | 8 |
|
10 | | - |
11 | | -## 1. Features |
12 | | -Generally, the point is that jsondb helps you perform databasa like transactions, currently CRUD. You can go there for sure here we go there this is the best choice. |
| 9 | +<br></br> |
| 10 | +Use json file as your database on the backend, json-base create the database json file for you and also provides you with a lightweight ORM to perform transactions on your database. |
13 | 11 |
|
14 | 12 | ## 2. Get Started |
15 | 13 |
|
16 | | -To get started, first make sure you have the latest stable version of nodejs on your computer. Another thing is that `jsondb` runs only in a node environemt. |
| 14 | +To get started, first make sure you have the latest stable version of nodejs on your computer. Another thing is that `json-base` runs only in a node environemt. |
17 | 15 |
|
18 | 16 |
|
19 | | -**1. Install jsonbd dependecy with npm** |
| 17 | +**1. Install json-base dependecy with npm** |
20 | 18 |
|
21 | 19 | You have to install jsondb with npm, it's not currently available on other package managers like yarn, etc. |
22 | 20 | ```bash |
23 | | -$ npm install jsondb |
| 21 | +$ npm install -g json-base |
24 | 22 | ``` |
25 | 23 |
|
26 | 24 | **2. Initialize jsondb in your project** |
27 | 25 |
|
28 | | -Initialize jsondb in your project. If you have a `src` folder in your project, this will create a `jsondb/database.json` file in the src folder. |
| 26 | +Initialize jsondb in your project. Open the root folder of your project in your terminal and initialize json-base. This will create a `database.json` file in the project. |
| 27 | + |
29 | 28 | ```bash |
30 | | -$ npx jsondb --init |
| 29 | +$ json-base --init |
31 | 30 | ``` |
32 | 31 |
|
33 | 32 | **3. Create your base schema or just leave it.** |
34 | 33 |
|
35 | | -Initialize jsondb in your project. If you have a `src` folder in your project, this will create a `jsondb/database.json` file in the src folder. |
36 | | -More about this can be found in the [tutorial](#2-get-started) section and in the [setting up jsondb in your project](#1-setting-up-jsondb-in-a-project) to be exact. |
| 34 | +Normally, the database.json created comes with 2 sample data collections, 'users' ans 'posts', you can add more collections as you please, it's allow. |
37 | 35 |
|
| 36 | +**NB** : Respect the sythax of json files while adding new collection and also keep in mind that all collections must be enwrapped in the `db` field. |
38 | 37 | <br> |
39 | 38 |
|
| 39 | + |
40 | 40 | **4. Perform crud on the schemas as you need it** |
41 | 41 |
|
42 | | -Let's use a simple tutorial on how we can use this `jsondb`. We're going to create a simple app to interact with the `users` model where we can get all the users, get a particular user with a unique key or get a group of users but not all. The developer can also get a limited number of users if he or she specifies the limit arguement in the get function. We're doing this in [tutorial](#3-a-simple-tutorial) section |
| 42 | +Let's have a simple tutorial on how we can use this `json-base`. We're going to create a simple app to interact with our models where we can get all the users, get a particular user with a unique key or get a group of users but not all. The developer can also get a limited number of users if he or she specifies the limit arguement in the get function. We're doing this in [tutorial](#3-a-simple-tutorial) section |
43 | 43 |
|
44 | 44 | ## 3. A simple tutorial |
45 | 45 |
|
46 | 46 | **Table of contents** |
47 | | -1. Setting up jsondb in a project |
48 | | -2. Creating a base models |
49 | | -3. Performing CRUD on the models |
50 | | -4. Upcoming features |
51 | | - |
52 | | -#### 1. Setting up jsondb in a project |
53 | | -In this section, we're going to start by creating a nodejs project and add and configure jsondb in it. |
54 | | - |
55 | | -Let's start by installing jsondb from npm package manager |
| 47 | +1. Getting data with get() api |
| 48 | +2. Deleting data with del() api |
| 49 | +3. Updating data with set() api |
| 50 | +4. Adding data with add() api |
| 51 | + |
| 52 | +#### 0. Setting up jsondb in a project |
| 53 | +Before starting to perform transactions to the database, let's |
| 54 | +learn a little bit on the usage of the CLI |
56 | 55 | ``` |
57 | | -$ npm install jsondb |
58 | | -``` |
59 | | - |
60 | | -Next, let's initialize jsondb in the nodejs project that we've created |
| 56 | + Usage : json-base [command] |
| 57 | + Commands : |
| 58 | + --init Initialized the database.json in the current directory |
| 59 | + --version Prints the current version of json-base |
| 60 | + --help Prints the help message |
61 | 61 |
|
62 | 62 | ``` |
63 | | -$ npx jsondb init |
64 | | -``` |
65 | | -On this step, file called `database.json` will be created in the root folder. |
66 | 63 |
|
67 | 64 | *database.json* |
68 | 65 | ```json |
@@ -93,132 +90,85 @@ On this step, file called `database.json` will be created in the root folder. |
93 | 90 | You will need to remove the models in the json database as long as the sample ones may not match with your purpose. |
94 | 91 |
|
95 | 92 | #### 2. Getting data |
96 | | -In this section, we're going to see how we can get data from our json database. |
| 93 | +In this section, we're going to see how we can get data from our json database. |
97 | 94 | ```ts |
98 | | - |
99 | | - import { getData } from 'jsondb' |
100 | | - import UserType from './types/usertype' |
101 | | - |
102 | | - |
103 | | -/* 1. Getting all the users */ |
104 | | - |
105 | | - const allUsers : UserType[] = await getData("users") |
106 | | - console.log(allUsers) // prints the list of all users |
107 | | - |
108 | | - /* 2. Getting a user with a unique key */ |
109 | | - |
110 | | - const userWithId2 = await getData("users", { id : 2 }) |
111 | | - console.log(userWithId2) // prints the user with a unique id of 2 |
112 | | - |
113 | | -/* 3. Limiting the number of users to retreive */ |
114 | | - const firstTenUsers = await getData("users", {} , 10) |
115 | | - console.log(firstTenUsers) // prints the first ten users in the json database |
116 | | - |
| 95 | +import { get } from 'json-base' |
| 96 | + (async function(){ |
| 97 | + const aUsersWithId2 = await get({ |
| 98 | + collection : "users", |
| 99 | + where : { |
| 100 | + id : 2 |
| 101 | + }, |
| 102 | + limit : 1 |
| 103 | + }) |
| 104 | + }()) |
117 | 105 | ``` |
118 | | - |
| 106 | +The code above retreives `1 record` from the `users` collection, where `user.id == 2` |
119 | 107 |
|
120 | 108 | #### 3. Create data |
121 | 109 | As said before, we'll be creating a simple crud operation, this means that we already have read checked on our todo checklist as read is the same as getting data. Next we're going to work on creating data. |
122 | 110 | ```ts |
123 | | - import { createData } from 'jsondb' |
124 | | - import UserType from './types/usertype' |
125 | | - |
126 | | - const newUser : UserType = { |
127 | | - id : 4 , |
128 | | - username : "delba", |
129 | | - |
130 | | - password : "8ui4gi82k4kj889n" |
131 | | - } |
132 | | - |
133 | | - createData("users" ,{ data : newUser }, (err , user) => { |
134 | | - if(err){ |
135 | | - console.log(err.message) |
136 | | - process.exit(0) |
137 | | - } |
138 | | - console.log(user) |
139 | | - }) |
140 | | - |
141 | | - /* It's also possible to add an array of users, jsondb api can handle that too */ |
142 | | - |
143 | | - const unsavedUsers : UserType[] = [ |
144 | | - { |
145 | | - id : 4 , |
146 | | - username : "tim", |
147 | | - |
148 | | - password : "3456yojp%7-055" |
149 | | - }, |
150 | | - { |
151 | | - id : 5 , |
152 | | - username : "palmer", |
153 | | - |
154 | | - password : "uhnui0no082kn9" |
| 111 | + import { add } from 'json-base' |
| 112 | + (async function(){ |
| 113 | + await add({ |
| 114 | + collection : "posts", |
| 115 | + data : { |
| 116 | + id : 1 , |
| 117 | + userId : 1 , |
| 118 | + photo : "https://linkto.img", |
| 119 | + caption : "The quick brown fox" |
155 | 120 | } |
156 | | - ] |
157 | | - |
158 | | - createData("users" , { data : unsavedUsers } , (err , users) => { |
159 | | - if(err){ |
160 | | - console.log(err.message) |
161 | | - process.exit(0) |
162 | | - } |
163 | | - console.log(users) |
164 | 121 | }) |
| 122 | + }()) |
165 | 123 | ``` |
166 | | - |
| 124 | +The code snipped above adds the `data ` object in the `posts` collection |
167 | 125 |
|
168 | 126 | #### 4. Update and delete data |
169 | 127 |
|
170 | 128 | Let's combine the update and the delete sections into one section as long as they're kinda simple and easy to understand. |
171 | 129 |
|
172 | 130 | Updating data will require a unique identifier of the record we're updating. |
173 | 131 | ```ts |
174 | | -import { updateData } from "jsondb" |
175 | | -import { Request , Response } from "@types/node" |
176 | | -import UserType from "./types/usertype" |
177 | | - |
178 | | -app.put("/updateUser/:id" , async (req : Request ,res) => { |
179 | | - const userId = req.params.id |
180 | | - const { username } = req.body |
181 | | - const updatedUser = await updateData("users" , { |
182 | | - // update user wher the id equals to userId |
| 132 | + import { set } from 'json-base' |
| 133 | + await set({ |
| 134 | + collection : "users", |
183 | 135 | where : { |
184 | | - id : userId |
| 136 | + username : "leerob" |
185 | 137 | }, |
186 | | - // update the user with a new username |
187 | 138 | data : { |
188 | | - username |
| 139 | + |
189 | 140 | } |
190 | 141 | }) |
191 | | - |
192 | | -}) |
193 | 142 | ``` |
194 | 143 |
|
195 | 144 | Enough for updating data, the next is to learn how to delete some records from the json database. Here, a unique identifier is required. |
196 | 145 |
|
197 | 146 | ```ts |
| 147 | + import { del } from 'json-base' |
| 148 | + |
| 149 | + (async function(){ |
| 150 | + await del({ |
| 151 | + collection : "posts", |
| 152 | + where : { |
| 153 | + id : 1 |
| 154 | + } |
| 155 | + }) |
| 156 | + }()) |
| 157 | +``` |
198 | 158 |
|
199 | | -import { deleteData } from "jsondb" |
200 | | -import UserType from "./types/usertype" |
201 | | - |
202 | | -deleteData("users" , { id : 2 } , (err,data) => { |
203 | | - if(err){ |
204 | | - console.log("Uncaught error " , err) |
205 | | - process.exit(0) |
206 | | - } |
207 | | - console.log(data) |
208 | | -} ) |
| 159 | +🎉 Congrats! Now we've finished creating our CRUD operations on the users model or collection and I hope now you're able to consume our API and make your life easier. |
| 160 | +*For more, jsdoc was used , hover on your imported function to see the documentation* |
209 | 161 |
|
210 | | -``` |
211 | 162 |
|
212 | | -🎉 Congrats! Now we've finished creating our CRUD operations on the users model or collection and I hope now you're able to consume our API and make your life easier. |
213 | 163 | ## Contributing |
214 | 164 | Willing to contribute to this Open Source project ? |
215 | 165 | You can contribute to this project by **making bug reports** , **requesing feautures** , **Adding features** and more other ways. Read more [here](./CONTRIBUTING.md) before contributing. |
216 | 166 |
|
217 | 167 | ## Maintainers |
218 | | -This project is built and maintained by [@ndzhwr](https://twitter.com/ndzhwr). |
| 168 | +This repo is maintained by [@ndzhwr](https://github.com/ndzhwr). |
219 | 169 |
|
220 | 170 | ## Licence |
221 | | -This project is [MIT]() licenced. |
| 171 | +This project is [MIT](LICENCE) licenced. |
222 | 172 | ___ |
223 | 173 |
|
224 | 174 | <p align="right">⁕ <a href="https://github.com/ndzhwr/works">ndzhwr-works</a> 2023. </p> |
0 commit comments