Skip to content

Commit 11b75a8

Browse files
committed
docs: done with documentation
1 parent f6d68bf commit 11b75a8

File tree

9 files changed

+173
-2787
lines changed

9 files changed

+173
-2787
lines changed

README.md

Lines changed: 71 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,65 @@
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.
33
___
44

5-
65
```bash
7-
$ npm install jsondb
6+
npm install -g json-base
87
```
98

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.
1311

1412
## 2. Get Started
1513

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.
1715

1816

19-
**1. Install jsonbd dependecy with npm**
17+
**1. Install json-base dependecy with npm**
2018

2119
You have to install jsondb with npm, it's not currently available on other package managers like yarn, etc.
2220
```bash
23-
$ npm install jsondb
21+
$ npm install -g json-base
2422
```
2523

2624
**2. Initialize jsondb in your project**
2725

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+
2928
```bash
30-
$ npx jsondb --init
29+
$ json-base --init
3130
```
3231

3332
**3. Create your base schema or just leave it.**
3433

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.
3735

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.
3837
<br>
3938

39+
4040
**4. Perform crud on the schemas as you need it**
4141

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
4343

4444
## 3. A simple tutorial
4545

4646
**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
5655
```
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
6161
6262
```
63-
$ npx jsondb init
64-
```
65-
On this step, file called `database.json` will be created in the root folder.
6663

6764
*database.json*
6865
```json
@@ -93,132 +90,85 @@ On this step, file called `database.json` will be created in the root folder.
9390
You will need to remove the models in the json database as long as the sample ones may not match with your purpose.
9491

9592
#### 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.
9794
```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+
}())
117105
```
118-
106+
The code above retreives `1 record` from the `users` collection, where `user.id == 2`
119107

120108
#### 3. Create data
121109
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.
122110
```ts
123-
import { createData } from 'jsondb'
124-
import UserType from './types/usertype'
125-
126-
const newUser : UserType = {
127-
id : 4 ,
128-
username : "delba",
129-
email : "[email protected]",
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-
email : "[email protected]",
148-
password : "3456yojp%7-055"
149-
},
150-
{
151-
id : 5 ,
152-
username : "palmer",
153-
email : "[email protected]",
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"
155120
}
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)
164121
})
122+
}())
165123
```
166-
124+
The code snipped above adds the `data ` object in the `posts` collection
167125

168126
#### 4. Update and delete data
169127

170128
Let's combine the update and the delete sections into one section as long as they're kinda simple and easy to understand.
171129

172130
Updating data will require a unique identifier of the record we're updating.
173131
```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",
183135
where : {
184-
id : userId
136+
username : "leerob"
185137
},
186-
// update the user with a new username
187138
data : {
188-
username
139+
189140
}
190141
})
191-
192-
})
193142
```
194143

195144
Enough for updating data, the next is to learn how to delete some records from the json database. Here, a unique identifier is required.
196145

197146
```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+
```
198158

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*
209161

210-
```
211162

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.
213163
## Contributing
214164
Willing to contribute to this Open Source project ?
215165
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.
216166

217167
## 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).
219169

220170
## Licence
221-
This project is [MIT]() licenced.
171+
This project is [MIT](LICENCE) licenced.
222172
___
223173

224174
<p align="right">⁕ <a href="https://github.com/ndzhwr/works">ndzhwr-works</a> 2023. </p>

0 commit comments

Comments
 (0)