1.0.6 • Published 2 years ago

@aytacmalkoc/jsondb v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

📖 jsondb

Easy, fast local database system with JSON.

npm downloads npm version GitHub license

"Buy Me A Coffee"

🧧 Table of contents

🙇‍♂️ Motivation

We may need a lightweight database when developing simple applications. We use local database packages to avoid having to connect to any database. That's why I wanted to develop a JSON database application that works as a key-value. In this way, we can store data with keys as if creating a table in a single database file.

🔗 Installation

yarn add @aytacmallkoc/jsondb

or

npm install @aytacmalkoc/jsondb

👉 Usage

Please refer to the table below for the parameters of the methods.

JsonDB uses root directory as default database path.

const JsonDB = require('@aytacmalkoc/jsondb');
const db = new JsonDB('database/db.json');

➡️ Using with options

const db = new JsonDB('database/db.json', {
    uuid: true,
    primaryKey: 'id',
    timestamp: true,
});

⚙️ Options

OptionTypeDefaultDescription
uuidbooleantrueGenerate unique id for each record.
primaryKeystring'id'Define identity key
timestampbooleantrueAdd created_at and updated_at fields.

Methods

Methods return different values depending on usage. The add and update, findById, findAll methods return the object or array it affects. The delete, deleteAll, and clear methods return true or false.

add

The add method adds the objects given as parameters.

const user = db.add('users', {
    name: 'Aytac',
    surname: 'Malkoc',
    age: 22,
    email: 'aytacmalkoc@protonmail.com'
});

console.log(user); // Prints the user object

update

The update method is used to update data by id. Only given keys are updated. For example, we can only update the age of the user created in the add method.

const user = db.update('users', 1, {
    age: 23,
});

where

The where method finds items based on their key-value using comparison operators.

const users = db.where('users', 'age', '>', 18); // return array of users with age greater than 18

Operators

OperatorDescriptionAccepted types
=Equalstring, number, boolean
!=Not equalstring, number, boolean
>Greater thanstring, number, boolean
<Less thanstring, number, boolean
>=Greater than or equalstring, number, boolean
<=Less than or equalstring, number, boolean
likeLikestring, number, boolean
not likeNot likestring, number, boolean
betweenBetween (only numbers)number[]
not betweenNot between (only numbers)number[]

findById

const user = db.findById('users', 1);

findAll

const users = db.findAll('users');

delete

const deleteUser = db.delete('users', 1);

deleteAll

const deleteAll = db.deleteAll('users');

clear

Changes the database file to its default.

const clear = db.clear();

timeAgo

The timeAgo method can be used to convert the timestamp values of the data into readable format.

const user = db.findById('users', 1);
const ago = db.timeAgo(user.created_at);

console.log(ago); // 30 minutes ago

getDatabaseSize

The getDatabaseSize method returns an object containing the values of bytes, kilobytes, megabytes, and gigabytes.

const size = db.getDatabaseSize(); // return size object

console.log(`${size.kb} KB`); // 1.5 KB

⚙️ Method Parameters

MethodParameters
addkey, value
wheremodelName, key, operator, value
findByIdkey, id
findAllkey
updatekey, id, value
deletekey, id
deleteAllkey
clear-
timeAgodate
getDatabaseSizetoFixed (default: 2)

🔗 Examples

You can check the postman workspace collections for detailed examples.

  • NodeJS
  • Express
  • React
  • Vue

✏️ Formatting

📝 Lint

yarn lint

👀 Watch changes

yarn prettier-watch

✒️ Format Document

yarn prettier-format

🚀 Publish

yarn publish

or

npm run publish

💁 License

MIT license, Copyright (c) Aytac Malkoc. For more information see LICENSE.

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago