fasturl.db
Version:
A simple JSON database for javacsript node.js
182 lines (150 loc) • 5.01 kB
Markdown
A simple database that you can use for your projects, supports multiple storage, uses lodash for fast reading and searching, supports nested data storage.
- [Installing package](
- [Setup database](
- [Create table database](
- [Create variables](
- [setKey()](
- [set()](
- [get()](
- [delete()](
- [has()](
- [all()](
- [abbreviate()](
Install package using `npm i`
```js
npm i fasturl.db@latest
```
Import package from `node_modules`, then add directory path to your database folder
```js
// import { FasturlDB } from "fasturl.db";
const { FasturlDB } = require("fasturl.db");
const db = new FasturlDB({
directory: "./database",
showLogs: false
})
```
This setup will create default database file on path `./database/main/db.json`
Tables are used to create other storage, you can use the same table to store storage files with the same category or usage.
```js
const { FasturlDB } = require("fasturl.db");
const db = new FasturlDB({
directory: "./database",
showLogs: false,
tables: {
"account": "/user/accounts.json"
"user": "/user/users.json"
}
})
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `directory` | `./database` | `false` | Defining database paths. |
| `showLogs` | `false` | `false` | Showing something logs into your terminal. |
| `tables` | `{ "main": "/main/db.json" }` | `false` | Used for creating custom database tables. |
With this, all tables will be created automatically by package
Actually you don't need this, it's just my need as the author. so this serves to create a default data value when using the `get()` function instead of returning undefined because the data is not in the `object`.
```js
// save variables to default table
db.variables({
"username": "unknown",
"balance": 0
});
// save variables to custom table
db.variables({
"username": "unknown",
"balance": 0
}, "user");
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `data` | `{}` | `true` | Create custom variables data. |
| `table` | `main` | `true` | Save variables to default table or custom table. |
You can access this variables using `db.Variables` or `db.Variables.values()`
Used to update the key of a data in the database
```js
db.setKey("user.username", "name") // output: true (if user have data named username)
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `oldKey` | `null` | `true` | oldKey data path |
| `newKey` | `null` | `true` | newKey data name |
| `table` | `main` | `false` | database table name |
Used for save or update data value
```js
// set data to table named account
db.set("totalUser", 999, "account"); // output: 999
// update data user balance
db.set("user.balance", 12000, "user"); // output: 12000
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `key` | `null` | `true` | key data path |
| `value` | `null` | `true` | data value |
| `table` | `main` | `false` | database table name |
Used to get data from database
```js
// get data from table named account
db.get("totalUser", "account"); // output: 999
// get data user balance
db.get("user.balance", "user"); // output: 12000
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `key` | `null` | `true` | key data path |
| `table` | `main` | `false` | database table name |
Used to delete data from database
```js
// delete data from table named account
db.delete("totalUser", "account"); // output: true
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `key` | `null` | `true` | key data path |
| `table` | `main` | `false` | database table name |
Used to check data in database is available or not
```js
// set data to table named account
db.has("totalUser", "account"); // output: false because the data was deleted
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `key` | `null` | `true` | key data path |
| `table` | `main` | `false` | database table name |
Used to get all data from database
```js
// get all data of table named account
db.all("user"); // output: { ...data }
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `table` | `main` | `false` | database table name |
Used for abbreviating number from long to short
```js
db.abbreviate(12_000_000, "0.00a"); // output: 12.00M
```
| Parameter | Default | Required | Description |
| --- | --- | --- | --- |
| `number` | `null` | `true` | big number |
| `format` | `0.00a` | `false` | number format output |