Skip to content

Commit fae4507

Browse files
committed
chore: set dist pushable
1 parent 34d31b4 commit fae4507

24 files changed

+515
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ typings/
8080

8181
# Nuxt.js build / generate output
8282
.nuxt
83-
dist
8483

8584
# Gatsby files
8685
.cache/

.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./dist

dist/cli/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
export {};

dist/cli/index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env node
2+
import process from "process";
3+
import { writeFileSync } from "fs";
4+
import chalk from "chalk";
5+
import { CreateFileError } from "../errors/errorHandler.js";
6+
const args = process.argv.splice(2, 4);
7+
async function createFile(fileName, fileContent) {
8+
try {
9+
writeFileSync(fileName, fileContent);
10+
console.log(`Successfully created file ${fileName}`);
11+
}
12+
catch (error) {
13+
throw new CreateFileError(error.message);
14+
}
15+
}
16+
let exampleData = `
17+
{
18+
"$schema" : "http://json-schema.org/draft-04/schema#",
19+
"db" : {
20+
"users" : [
21+
{
22+
"id" : "1" ,
23+
"username" : "leerob",
24+
"email" : "[email protected]",
25+
"password" : "robinin"
26+
}
27+
],
28+
"posts" : [
29+
{
30+
"id" : "1",
31+
"userId" : "1",
32+
"photo" : "https://j.co/6235jjgk",
33+
"caption" : "Look at home"
34+
}
35+
]
36+
}
37+
38+
}
39+
`;
40+
const jsondb = `JSONDB`;
41+
const usage = `
42+
\t Usage of JSONDB cli
43+
\t npx jsondb <command>
44+
\t Commands :
45+
\t \t --version : Prints the npm version of jsondb
46+
\t \t --init : Initialize database.json in your project
47+
\t \t --help : Show this help message
48+
`;
49+
switch (args[0]) {
50+
case '--init':
51+
console.log(chalk.bold.white.bgBlue(`\t ${jsondb} \t`));
52+
console.log("Initializing database.json in your project...");
53+
createFile('database.json', exampleData);
54+
console.log(chalk.greenBright("💥 Created database.json successfully"));
55+
console.log(chalk.yellowBright("Happy coding :)"));
56+
process.exit(1);
57+
break;
58+
case '--help':
59+
case undefined:
60+
case null:
61+
default:
62+
console.log(usage);
63+
console.log(chalk.yellowBright("\t\tHappy coding :)"));
64+
}

dist/errors/errorHandler.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export declare class MissingParamError extends Error {
2+
constructor(param: string);
3+
}
4+
export declare class CollectionNotFoundError extends Error {
5+
constructor(collection: string);
6+
}
7+
export declare class DatabaseError extends Error {
8+
constructor(err: string);
9+
}
10+
export declare class NotFoundError extends Error {
11+
constructor(err: string);
12+
}
13+
export declare class DuplicationError extends Error {
14+
constructor(err?: string);
15+
}
16+
export declare class CreateFileError extends Error {
17+
constructor(err?: string);
18+
}

dist/errors/errorHandler.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export class MissingParamError extends Error {
2+
constructor(param) {
3+
super(`Missing param : ${param}`);
4+
this.name = "MissingParamError";
5+
}
6+
}
7+
export class CollectionNotFoundError extends Error {
8+
constructor(collection) {
9+
super(`Collection ${collection} was not found in database`);
10+
this.name = "CollectionNotFoundError";
11+
}
12+
}
13+
export class DatabaseError extends Error {
14+
constructor(err) {
15+
super(err);
16+
this.name = "DatabaseError";
17+
}
18+
}
19+
export class NotFoundError extends Error {
20+
constructor(err) {
21+
super(err);
22+
this.name = "NotFoundError";
23+
}
24+
}
25+
export class DuplicationError extends Error {
26+
constructor(err) {
27+
super(err || "Duplication error occured");
28+
this.name = "DuplicationError";
29+
}
30+
}
31+
export class CreateFileError extends Error {
32+
constructor(err) {
33+
super(err || "Failed to create database.json");
34+
this.name = "CreateFileError";
35+
}
36+
}

dist/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* JSONDB : A simple json dbms with an orm,
3+
* for nodejs, compatible with js and ts.
4+
*
5+
* Author : Regis NDIZIHIWE
6+
*/
7+
export * from './scripts/get.js';
8+
export * from './scripts/set.js';
9+
export * from './scripts/del.js';
10+
export * from './scripts/add.js';

dist/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* JSONDB : A simple json dbms with an orm,
3+
* for nodejs, compatible with js and ts.
4+
*
5+
* Author : Regis NDIZIHIWE
6+
*/
7+
export * from './scripts/get.js';
8+
export * from './scripts/set.js';
9+
export * from './scripts/del.js';
10+
export * from './scripts/add.js';
11+
// export * from './scripts/delAll.js'

dist/scripts/add.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
interface AddDataProps {
2+
collection: string;
3+
data: {};
4+
}
5+
/**
6+
* #### add()
7+
* An asyncronous json-base api to add data to a collection to jsondb
8+
* @async
9+
* @param params.collection Collection where the data will be added to
10+
* @param params.data Data to add to the collection
11+
* @returns Promise
12+
* @example
13+
* ```
14+
* import { add } from 'json-base'
15+
* (async function(){
16+
* await add({
17+
collection : "posts",
18+
data : {
19+
id : 1 ,
20+
userId : 1 ,
21+
photo : "https://linkto.img",
22+
caption : "The quick brown fox"
23+
}
24+
})
25+
* }())
26+
* ```
27+
*/
28+
export declare function add(params: AddDataProps): Promise<any>;
29+
export {};

dist/scripts/add.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { CollectionNotFoundError, DuplicationError, MissingParamError } from "../errors/errorHandler.js";
2+
import { getJSONDb } from "../utils/getJSONDb.js";
3+
import { setJSONDb } from "../utils/setJSONDb.js";
4+
/**
5+
* #### add()
6+
* An asyncronous json-base api to add data to a collection to jsondb
7+
* @async
8+
* @param params.collection Collection where the data will be added to
9+
* @param params.data Data to add to the collection
10+
* @returns Promise
11+
* @example
12+
* ```
13+
* import { add } from 'json-base'
14+
* (async function(){
15+
* await add({
16+
collection : "posts",
17+
data : {
18+
id : 1 ,
19+
userId : 1 ,
20+
photo : "https://linkto.img",
21+
caption : "The quick brown fox"
22+
}
23+
})
24+
* }())
25+
* ```
26+
*/
27+
export async function add(params) {
28+
try {
29+
const jsonDB = JSON.parse(await getJSONDb());
30+
if (!params.collection)
31+
throw new MissingParamError(`collection`);
32+
if (!params.data)
33+
throw new MissingParamError(`data`);
34+
const coll = jsonDB['db'][`${params.collection}`];
35+
if (!coll)
36+
throw new CollectionNotFoundError(`${params.collection}`);
37+
if (!coll.find(doc => doc == params.data))
38+
coll.push(params.data);
39+
else
40+
throw new DuplicationError();
41+
setJSONDb(JSON.stringify(jsonDB));
42+
return params.data;
43+
}
44+
catch (err) {
45+
throw new Error(err.message);
46+
}
47+
}

0 commit comments

Comments
 (0)