🎲
Simpl.DB
GitHubDonate
v2.13.0
v2.13.0
  • Introduction
  • Changelog
  • Getting Started
  • Troubleshooting
  • Documentation
    • Database
      • .add()
      • .clear()
      • .createCollection()
      • .delete()
      • .deleteCollection()
      • .extend()
      • .fetch()
      • .get()
      • .getCollection()
      • .has()
      • .pull()
      • .push()
      • .rename()
      • .save()
      • .set()
      • .subtract()
      • .toJSON()
      • .update()
    • Collection
      • .create()
      • .createBulk()
      • .extend()
      • .fetch()
      • .fetchAll()
      • .fetchMany()
      • .fetchOrCreate()
      • .get()
      • .getAll()
      • .getMany()
      • .getOrCreate()
      • .has()
      • .random()
      • .remove()
      • .reset()
      • .save()
      • .update()
  • Types/Interfaces
    • Data
    • JSONData
    • Modifiable<T>
Powered by GitBook
On this page
  • Setting up the Database
  • Requiring the package
  • Creating the Database
  • Queries
  • Setting up Collections
  • Creating Collections
  • Queries
  • Encrypting and Decrypting data

Was this helpful?

Getting Started

Please read the Introduction section if you haven't already.

PreviousChangelogNextTroubleshooting

Last updated 1 year ago

Was this helpful?

Setting up the Database

Requiring the package

const SimplDB = require('simpl.db');
import { Database } from 'simpl.db';

Creating the Database

The paths for the dataFile and collectionsFolder options, if provided, must be absolute.

If you are working with a large amount of data, it's highly recommended that the autoSave option is set to false and that you manually save the changes with .

const db = new SimplDB();
const db = new Database();

Queries

The database provide methods (and many more) for handling data. For more details for each method, please head over to the .

Setting up Collections

These structures allow you to store data in a more organized way. They are typically used to store information where every entry share a similar or even the same structure (e.g. users, posts, etc.). Collections provide methods to easily handle the data, easier than storing everything in the Database with simple key-value pairs.

Creating Collections

const Users = db.createCollection('users');
type User = {
  name: string
  age: number
  balance: number
}

const Users = db.createCollection<User>('users');

In order to reduce the queries, you can provide default values for a collection, which will be automatically assigned to the entries when created. Here is an example:

const Users = db.createCollection('users', {
  balance: 0
});
const Users = db.createCollection<User>('users', {
  balance: 0
});

Now, queries like this:

Users.create({
  name: 'John',
  balance: 0
});

Can be replaced with this:

Users.create({ name: 'John' });

Queries

Encrypting and Decrypting data

Encrypting and decrypting data will increase your code's execution time a little.

Please make sure that, when generating your Encryption Key through the website provided below, you only check the Include Numbers, Include Lowercase Characters, Include Uppercase Characters, Exclude Similar Characters and Generate On Your Device options and that you have the Password Length option set to 32.

It's highly recommended that the Encryption Key only includes numbers and uppercase and lowercase letters, since some special characters could break it.

Once you have your hands on a brand new Encryption Key, make sure you store it in a safe place, such as an environment variable in a .env file or something similar.

For more details, please head over to the page.

Collections provide methods (and many more) for handling data. For more details for each method, please head over to the .

Simpl.DB allows you to encrypt and decrypt data. This is useful to store important data such as passwords or secret tokens. In order to keep the package as speedy as possible, only the and methods support encryption and decryption, respectively.

To enable these functionalities, you must have an Encryption Key (you can read more about encryption keys ).

Since Simpl.DB uses AES256-CTR to encrypt and decrypt data, your Encryption Key must be a combination of 32 random characters. To generate yours, visit or simply write it yourself (not recommended).

Database#save()
CRUD
Database page and subpages
Database#createCollection()
CRUD
Collection page and subpages
Database#set()
Database#get()
here
this website