Skip to content

Commit 1848123

Browse files
author
regisrex
committed
feat(core): mend arthmetics ops, thx @pacifiquem
1 parent 2d2e172 commit 1848123

File tree

12 files changed

+101
-70
lines changed

12 files changed

+101
-70
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ To get started, first make sure you have the latest stable version of nodejs on
2121

2222
**1. Install json-base dependecy with npm**
2323

24-
You have to install jsondb with npm, it's not currently available on other package managers like yarn, etc.
24+
You have to install json-base with npm, it's not currently available on other package managers like yarn, etc.
2525
```bash
2626
$ npm install @ndzhwr/json-base
2727
```
2828

29-
**2. Initialize jsondb in your project**
29+
**2. Initialize json-base in your project**
3030

31-
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.
31+
Initialize json-base 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.
3232

3333
```bash
3434
$ npx json-base --init
@@ -55,7 +55,7 @@ Let's have a simple tutorial on how we can use this `json-base`. We're going to
5555
4. Adding data with add() api
5656
5. Deleting All data with delAll() api
5757

58-
#### 0. Setting up jsondb in a project
58+
#### 0. Setting up json-base in a project
5959
Before starting to perform transactions to the database, let's
6060
learn a little bit on the usage of the CLI
6161
```

SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Supported Versions
44

5-
The latest version of `jsondb` is secure
5+
The latest version of `json-base` is secure
66
| Version | Supported |
77
| ------- | ------------------ |
88
| 1.0.x | :white_check_mark: |

cli/index.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
2+
import chalk from "chalk"
3+
import { writeFileSync } from "fs"
24
import process from "process"
3-
import { writeFileSync } from "fs"
4-
import chalk from "chalk"
55
import { CreateFileError } from "../errors/errorHandler.js"
66

77

@@ -12,7 +12,7 @@ async function createFile(fileName: string, fileContent: string) {
1212
try {
1313
writeFileSync(fileName, fileContent)
1414
console.log(`Successfully created file ${fileName}`);
15-
} catch (error : any) {
15+
} catch (error: any) {
1616
throw new CreateFileError(error.message);
1717
}
1818
}
@@ -42,29 +42,28 @@ let exampleData: string = `
4242
}
4343
`
4444

45-
const jsondb = `JSONDB`
45+
const title = `json-base`
4646

4747
const usage = `
48-
\t Usage of JSONDB cli
49-
\t npx jsondb <command>
48+
\t Usage of json-base cli
49+
\t npx json-base <command>
5050
\t Commands :
51-
\t \t --version : Prints the npm version of jsondb
51+
\t \t --version : Prints the npm version of json-base
5252
\t \t --init : Initialize database.json in your project
5353
\t \t --help : Show this help message
5454
`
5555

5656
switch (args[0]) {
5757
case '--init':
58-
console.log(chalk.bold.white.bgBlue(`\t ${ jsondb } \t`))
58+
console.log(chalk.bold.white.bgBlue(`\t ${title} \t`))
5959
console.log("Initializing database.json in your project...")
60-
createFile('database.json', exampleData)
61-
console.log( chalk.greenBright("💥 Created database.json successfully"))
62-
console.log(chalk.yellowBright("Happy coding :)"))
63-
process.exit(1)
64-
break;
60+
createFile('database.json', exampleData)
61+
console.log(chalk.greenBright("💥 Created database.json successfully"))
62+
console.log(chalk.yellowBright("Happy coding :)"))
63+
process.exit(1)
6564
case '--help':
6665
case undefined:
67-
case null :
66+
case null:
6867
default:
6968
console.log(usage)
7069
console.log(chalk.yellowBright("\t\tHappy coding :)"))

index.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
1+
2+
import add from './scripts/add.js'
3+
import del from './scripts/del.js'
4+
import delAll from './scripts/delAll.js'
5+
import get from './scripts/get.js'
6+
import set from './scripts/set.js'
7+
8+
19
/**
2-
* JSONDB : A simple json dbms with an orm,
3-
* for nodejs, compatible with js and ts.
10+
* # 📦JSON-BASE
11+
* **a lightweight odm for json file inspired by prisma**
412
*
5-
* Author : Regis NDIZIHIWE
13+
*
14+
* 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.
15+
* @repository https://github.com/regisrex/json-base
16+
* @license MIT
17+
* @version 0.1.6
18+
* @example
19+
* ```
20+
* import { add } from 'json-base'
21+
* (async function(){
22+
* await add({
23+
collection : "posts",
24+
data : {
25+
id : 1 ,
26+
userId : 1 ,
27+
photo : "https://linkto.img",
28+
caption : "The quick brown fox"
29+
}
30+
})
31+
* }())
32+
* ```
33+
*
34+
* @author Regis NDIZIHIWE <https://regisndizihiwe.me>
635
*/
7-
export * from './scripts/get.js'
8-
export * from './scripts/set.js'
9-
export * from './scripts/del.js'
10-
export * from './scripts/add.js'
36+
export default {
37+
add,
38+
del,
39+
get,
40+
set,
41+
delAll
42+
}
43+
1144
// export * from './scripts/delAll.js'

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"name": "@ndzhwr/json-base",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"main": "dist/index.js",
5-
"description": "A simple json-as-db tool with an orm, for nodejs, compatible with js and ts.",
5+
"description": "a lightweight odm for json file inspired by prisma",
66
"files": [
77
"/dist"
88
],
99
"author": {
1010
"name": "Regis NDIZIHIWE",
1111
"email": "[email protected]",
12-
"url": "https://github.com/ndzhwr"
12+
"url": "https://github.com/regisreex"
1313
},
1414
"repository": {
1515
"type": "git",
16-
"url": "https://github.com/ndzhwr/json-base.git"
16+
"url": "git+https://github.com/ndzhwr/json-base.git"
1717
},
1818
"engines": {
1919
"node": ">=8.10.0"

scripts/add.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface AddDataProps {
3030
* }())
3131
* ```
3232
*/
33-
export async function add(params: AddDataProps) : Promise<any> {
33+
export default async function add(params: AddDataProps) : Promise<any> {
3434
try {
3535
const jsonDB: {} | any = JSON.parse(await getJSONDb())
3636

scripts/del.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface DeleteParams {
2626
* }())
2727
* ```
2828
*/
29-
export async function del(params: DeleteParams): Promise<void> {
29+
export default async function del(params: DeleteParams): Promise<void> {
3030
try {
3131
const where_clause = Object.entries(params.where)
3232
const jsonDB = JSON.parse(await getJSONDb())

scripts/delAll.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getJSONDb } from "../utils/getJSONDb.js";
22
import { setJSONDb } from "../utils/setJSONDb.js";
33

4-
export async function delAll() {
4+
export default async function delAll() {
55
try {
66
const jsonDB = JSON.parse(await getJSONDb());
77
jsonDB["db"] = {};

scripts/get.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface GetDataProps extends GetDataOptions {
3131
* }())
3232
* ```
3333
*/
34-
export async function get(params: GetDataProps): Promise<any> {
34+
export default async function get(params: GetDataProps): Promise<any> {
3535
let data_to_return: any;
3636
if (!params.collection) {
3737
throw new MissingParamError("collection");
@@ -41,7 +41,7 @@ export async function get(params: GetDataProps): Promise<any> {
4141
if (params.where) {
4242
const where_data = Object.entries(params.where);
4343
for (let i: number = 0; i < where_data.length; i++) {
44-
const [field, filter]:[any, any] = where_data[i];
44+
const [field, filter]: [any, any] = where_data[i];
4545
if (typeof filter === "object") {
4646
const filterEntries = Object.entries(filter);
4747
let data: any = data_to_return;

scripts/set.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { CollectionNotFoundError, DuplicationError, MissingParamError, NotFoundE
22
import { getJSONDb } from "../utils/getJSONDb.js";
33
import { setJSONDb } from "../utils/setJSONDb.js";
44

5-
interface SetDataProps {
6-
collection : string
7-
where : any ,
8-
data : any
5+
interface SetDataProps {
6+
collection: string
7+
where: any,
8+
data: any
99
}
1010
/**
1111
* ### set()
@@ -28,38 +28,37 @@ interface SetDataProps {
2828
})
2929
* ```
3030
*/
31-
export async function set(params: SetDataProps) : Promise<void> {
31+
export default async function set(params: SetDataProps): Promise<void> {
3232
try {
33-
34-
if(!params.collection || !params.where || !params.data ){
33+
34+
if (!params.collection || !params.where || !params.data) {
3535
throw new MissingParamError("Some requiered parameters for the set function was not added")
3636
}
37-
37+
3838
// const collection = await getCollection(params.collection)
3939
const where_clause = Object.entries(params.where)
40-
const jsonDB : any = JSON.parse(await getJSONDb())
40+
const jsonDB: any = JSON.parse(await getJSONDb())
4141
const coll = jsonDB['db'][`${params.collection}`]
42-
if(!coll)
42+
if (!coll)
4343
throw new CollectionNotFoundError(`${params.collection}`)
44-
const datum = coll.filter(( collection : any) => collection[`${where_clause[0][0]}`] == `${where_clause[0][1]}`)
45-
if(!datum){
44+
const datum = coll.filter((collection: any) => collection[`${where_clause[0][0]}`] == `${where_clause[0][1]}`)
45+
if (!datum) {
4646
throw new NotFoundError(`Datum with '${where_clause[0][0]}' set to '${where_clause[0][1]}' was not found in '${params.collection}' Collection`)
4747
}
48-
if(datum.length > 1)
49-
{
48+
if (datum.length > 1) {
5049
throw new DuplicationError(`Found 2 elements with the same '${where_clause[0][0]}' set to '${where_clause[0][1]}'`)
5150
}
52-
51+
5352
const data_clause = Object.entries(params.data)
54-
55-
const document = datum[0] ;
56-
for(let i : number = 0 ; i < data_clause.length ; i++){
53+
54+
const document = datum[0];
55+
for (let i: number = 0; i < data_clause.length; i++) {
5756
document[`${data_clause[i][0]}`] = `${data_clause[i][1]}`
5857
}
5958
setJSONDb(JSON.stringify(jsonDB))
60-
} catch (err : any) {
59+
} catch (err: any) {
6160
throw new Error(err.message)
6261
}
6362

64-
63+
6564
}

0 commit comments

Comments
 (0)