Skip to content

Commit 16253bc

Browse files
AmbratolmAmbratolm
Ambratolm
authored and
Ambratolm
committed
Added mock data generation and Updated README.
1 parent fefbdc8 commit 16253bc

File tree

5 files changed

+456
-72
lines changed

5 files changed

+456
-72
lines changed

.replrc.js

+137-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,143 @@
1+
//==============================================================================
2+
// ■ .REPLRC (.replrc.js)
3+
//------------------------------------------------------------------------------
4+
// Local-REPL configuration.
5+
//==============================================================================
6+
require("colors");
17
const $0db = require("0db");
2-
const { prompt, banner, cls } = require("./core");
8+
const faker = require("faker");
39

10+
//------------------------------------------------------------------------------
11+
// ● REPL-Context
12+
//------------------------------------------------------------------------------
13+
const context = {
14+
$0db,
15+
db: $0db(),
16+
faker,
17+
gen: gen(),
18+
cls,
19+
help,
20+
};
21+
const contextHelp = {
22+
$0db: "0db object",
23+
db: "Default instance (./db.json)",
24+
faker: "Fake data generator (See: https://npmjs.com/package/faker)",
25+
gen: "Built-in example data objects generator (using faker)",
26+
cls: "Clears screen",
27+
help: "Shows this help message",
28+
};
29+
function help(name = "") {
30+
if (!name) {
31+
_logContext(context);
32+
}
33+
_logContextHelp(context, contextHelp, name);
34+
return "==================================================";
35+
}
36+
37+
//------------------------------------------------------------------------------
38+
// ● Data-Objects-Generator
39+
//------------------------------------------------------------------------------
40+
function gen() {
41+
return {
42+
user(count = 1) {
43+
if (count < 1) count = 1;
44+
const users = [];
45+
for (let i = 0; i < count; i++) {
46+
const { name, internet, datatype } = faker;
47+
const gender = name.gender("binary");
48+
const firstName = name.firstName(gender);
49+
const lastName = name.lastName(gender);
50+
users[i] = {
51+
name: internet.userName(firstName, lastName),
52+
firstName: name.firstName(gender),
53+
lastName: name.lastName(gender),
54+
gender,
55+
email: internet.email(firstName, lastName),
56+
password: internet.password(),
57+
points: datatype.number({ min: 0, max: 8000 }),
58+
};
59+
}
60+
return users.length === 1 ? users[0] : users;
61+
},
62+
note(author, count = 1) {
63+
if (count < 1) count = 1;
64+
if (!author) author = "/** Put here the $id of some existent user **/";
65+
const notes = [];
66+
for (let i = 0; i < count; i++) {
67+
notes[i] = {
68+
author,
69+
title: faker.lorem.sentence(),
70+
content: faker.lorem.paragraph(),
71+
};
72+
}
73+
return notes.length === 1 ? notes[0] : notes;
74+
},
75+
};
76+
}
77+
78+
//------------------------------------------------------------------------------
79+
// ● REPL-Prompt
80+
//------------------------------------------------------------------------------
81+
function prompt(context, package) {
82+
return "[0db] ► ";
83+
}
84+
85+
//------------------------------------------------------------------------------
86+
// ● REPL-Banner
87+
//------------------------------------------------------------------------------
88+
function banner(context, package) {
89+
cls();
90+
_logPackage(package);
91+
_logContext(context);
92+
console.log();
93+
}
94+
95+
//------------------------------------------------------------------------------
96+
// ● Clear-Console
97+
//------------------------------------------------------------------------------
98+
function cls() {
99+
const readline = require("readline");
100+
console.log("\n".repeat(process.stdout.rows));
101+
readline.cursorTo(process.stdout, 0, 0);
102+
readline.clearScreenDown(process.stdout);
103+
console.clear();
104+
return "Console cleared.";
105+
}
106+
107+
//------------------------------------------------------------------------------
108+
// ● Helpers
109+
//------------------------------------------------------------------------------
110+
function _logContext(context) {
111+
const vars = Object.keys(context).join(", ");
112+
console.log("● Context variables:", `${vars}`.green);
113+
}
114+
function _logContextHelp(context, contextHelp, name) {
115+
const filter = name ? (key) => key === name : (key) => true;
116+
const keys = Object.keys(context).filter(filter);
117+
if (keys.length) {
118+
for (const key of keys) {
119+
console.log(`\t• ${key.green}: ${contextHelp[key]}.`);
120+
}
121+
} else {
122+
console.log(`\t${name.red} object does not exist in the built-in context.`);
123+
}
124+
}
125+
function _logPackage(package) {
126+
const { name, version, description } = package;
127+
console.log(
128+
"♛ Welcome to",
129+
`${name}@${version}`.blue,
130+
`✎ ${description}`.grey
131+
);
132+
}
133+
134+
//------------------------------------------------------------------------------
135+
// ► Exports
136+
//------------------------------------------------------------------------------
4137
module.exports = {
5-
context: {
6-
$0db,
7-
db: $0db(),
8-
cls,
9-
},
10-
enableAwait: true,
138+
context,
11139
prompt,
12140
banner,
141+
cls,
142+
enableAwait: true,
13143
};

README.md

+189-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,204 @@
1-
# 💽 0db REPL
1+
# 🅾️ 0db REPL
22

3-
[![Web](https://img.shields.io/badge/web-blue?logo=w3c)](https://github.com/topics/web)
4-
[![JavaScript](https://img.shields.io/badge/javascript-blue?logo=javascript)](https://github.com/topics/javascript)
3+
[![NPM version](https://badge.fury.io/js/0db.svg)](https://npmjs.org/package/0db-repl)
54
[![NodeJS](https://img.shields.io/badge/node-blue?logo=node.js)](https://github.com/topics/node)
5+
[![JavaScript](https://img.shields.io/badge/javascript-blue?logo=javascript)](https://github.com/topics/javascript)
6+
[![Web](https://img.shields.io/badge/web-blue?logo=w3c)](https://github.com/topics/web)
67

78
REPL playground for testing [**0db**](https://github.com/Ambratolm/0db).
89

10+
> This is a nodeJS REPL that comes with some handy context variables to easily play around with 0db on the command line.
11+
912
![Screenshot](./screenshot.gif?raw=true)
1013

11-
## 🏁 Getting started
14+
<!-- toc -->
15+
16+
- [📥 Installation](#%F0%9F%93%A5-installation)
17+
- [ℹ️ Usage](#%E2%84%B9%EF%B8%8F-usage)
18+
- [Context Variables:](#context-variables)
19+
- [`$0db`](#0db)
20+
- [`db`](#db)
21+
- [`faker`](#faker)
22+
- [`gen`](#gen)
23+
- [`cls`](#cls)
24+
- [`help`](#help)
25+
- [🚀 Development](#%F0%9F%9A%80-development)
26+
- [🏭 Environment](#%F0%9F%8F%AD-environment)
27+
- [🌑 Application](#%F0%9F%8C%91-application)
28+
- [📃 License](#%F0%9F%93%83-license)
29+
30+
<!-- tocstop -->
31+
32+
## 📥 Installation
33+
34+
```bash
35+
npm i 0db-repl -g
36+
```
37+
38+
## ℹ️ Usage
39+
40+
Launch using:
41+
42+
```bash
43+
0db
44+
```
45+
46+
Type `help()` to see a description of all available context variables.
47+
48+
### Context Variables:
49+
50+
Context variables are objects and functions that are already defined and available for usage during the REPL session.
51+
52+
#### `$0db`
53+
54+
[0db](https://github.com/Ambratolm/0db) object.
55+
56+
🎯 Example:
57+
58+
```js
59+
const myDb = await $0db("my-db.json")
60+
```
61+
62+
#### `db`
63+
64+
A default instance using the default file `./db.json` ready for use.
65+
66+
🎯 Example:
67+
68+
```js
69+
await db("users").create({ name: "ambratolm" })
70+
```
71+
72+
#### `faker`
73+
74+
Fake data generator to generate some realistic mock data. (See: [Faker.JS](https://npmjs.com/package/faker))
75+
76+
🎯 Example:
77+
78+
```js
79+
faker.name.firstName()
80+
faker.name.lastName()
81+
faker.name.gender()
82+
faker.internet.userName()
83+
faker.internet.email()
84+
faker.internet.password()
85+
```
86+
87+
```js
88+
await db("users").create({
89+
name: faker.internet.userName(),
90+
email: faker.internet.email(),
91+
password: faker.internet.password()
92+
})
93+
```
94+
95+
#### `gen`
96+
97+
Built-in example data objects generator (using faker) that has some functions to generate example objects that can be added to database.
98+
99+
- `gen.user(count?)` : Generates `user` objects.
100+
101+
- Parameters:
102+
- `count` : Number, _Optional_. Number of objects to generate. **Default: 1**.
103+
- Returns an **object** if `count <= 1`, else an **array** of objects.
104+
105+
`user` object is based on this schema:
106+
107+
```js
108+
{
109+
name: String,
110+
firstName: String,
111+
lastName: String,
112+
gender: String,
113+
email: String,
114+
password: String,
115+
points: Number
116+
}
117+
```
118+
119+
🎯 Example:
120+
121+
```js
122+
gen.user() // Returns 1 user object
123+
gen.user(100) // Returns an array of 100 user objects
124+
```
125+
126+
```js
127+
await db("users").create(gen.user())
128+
```
129+
130+
- `gen.note(author?, count?)` : Generates `note` objects.
131+
132+
- Parameters:
133+
- `author` : String, _Optional_. Identifier for the author of the note (Expected to be the `$id` field of a created `user` object that exists in database).
134+
- `count` : Number, _Optional_. Number of objects to generate. **Default: 1**.
135+
- Returns an **object** if `count <= 1`, else an **array** of objects.
136+
137+
`note` object is based on this schema:
138+
139+
```js
140+
{
141+
author: String,
142+
title: String,
143+
content: String
144+
}
145+
```
146+
147+
🎯 Example:
148+
149+
```js
150+
const user = await db("users").create(gen.user()) // Create a user item
151+
gen.note(user.$id) // Returns 1 note object
152+
gen.note(user.$id, 100) // Returns an array of 100 note objects
153+
```
154+
155+
```js
156+
const user = await db("users").create(gen.user())
157+
await db("notes").create(gen.note(user.$id))
158+
```
159+
160+
#### `cls`
161+
162+
Clears screen.
163+
164+
🎯 Example:
165+
166+
```js
167+
cls()
168+
```
169+
170+
#### `help`
171+
172+
Shows a description of all context variables.
173+
174+
- Parameters:
175+
- `name` : String, _Optional_. Name of a specific context object or function to show description for.
176+
177+
🎯 Example:
178+
179+
```js
180+
help() // Shows all descriptions
181+
help("db") // Shows description only for "db" function
182+
help("gen") // Shows description only for "gen" object
183+
```
184+
185+
## 🚀 Development
12186

13187
1. Clone the repository.
14188

15-
```bash
16-
cd somewhere
17-
git clone https://github.com/practical-works/0db-repl.git
18-
cd 0db-repl
19-
```
189+
```bash
190+
cd somewhere
191+
git clone https://github.com/practical-works/0db-repl.git
192+
cd 0db-repl
193+
```
20194

21195
2. Install dependencies: `npm i`
22196

23-
3. Run with `npm start` or `npm run dev` to use [nodemon](https://github.com/remy/nodemon/).
197+
3. Run:
198+
- `npm run dev` for development.
199+
- `npm start` for production.
24200

25-
## 🚀 Development
201+
💡 In development, you can type `rs` to manually restart. (See: [Nodemon](https://www.npmjs.com/package/nodemon))
26202

27203
### 🏭 Environment
28204

@@ -36,8 +212,9 @@ REPL playground for testing [**0db**](https://github.com/Ambratolm/0db).
36212
- Main: [**NodeJS**](https://github.com/nodejs/node) / [**0db**](https://github.com/Ambratolm/0db)
37213
- REPL: [**Local-REPL**](https://github.com/sloria/local-repl)
38214
- 🔧 Utils:
215+
- Mock Data Generation: [**Faker.JS**](https://github.com/Marak/Faker.js)
39216
- Console Colors: [**Colors**](https://github.com/Marak/colors.js)
40217

41218
## 📃 License
42219

43-
Licensed under [MIT](./LICENSE).
220+
Licensed under [MIT](./LICENSE).

0 commit comments

Comments
 (0)