/simpl.db

A lightweight, 0 dependency, easy-to-use local database using JSON to store data

Primary LanguageJavaScriptMIT LicenseMIT

Simpl.DB

Version Downloads Dependencies Quality



A lightweight, 0 dependency, easy-to-use local database using JSON to store data.

Installation

npm install simpl.db
yarn add simpl.db
pnpm add simpl.db

Example Usage

Database

const SimplDB = require('simpl.db');
const db = new SimplDB();


db.set('money', 100);
db.set('person.name', 'Peter');


db.has('money'); // true
db.has('person.name'); // true
db.has('person.age'); // false


db.get('person.name'); // 'Peter'
db.get('person.job'); // undefined


db.toJSON(); // { money: 100, person: { name: 'Peter' } }

Collections

const SimplDB = require('simpl.db');
const db = new SimplDB();

const Users = db.createCollection('users');


Users.create({ name: 'Peter', age: 19 });
Users.create({ name: 'John', age: 19 });


Users.update(
  user => user.age = 20,
  target => target.name === 'Peter'
);
// or (simpl.db@2.11.0+)
const user = Users.get(target => target.name === 'Peter');
user.age = 20;
user.save();


Users.get(user => user.name === 'Peter'); // { name: 'Peter', age: 20 }
Users.getMany(user => user.age > 18); // [{ name: 'Peter', age: 20 }, { name: 'John', age: 19 }]

With TypeScript:

import { Database, Modifiable } from 'simpl.db';
const db = new Database();

type User = {
  name: string
  age: number
}

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


Users.create({ name: 'Peter', age: 19 });
Users.create({ name: 'John', age: 19 });


Users.update(
  user => user.age = 20,
  target => target.name === 'Peter'
);
// or (simpl.db@2.11.0+)
const user = <Modifiable<User>> Users.get(target => target.name === 'Peter');
user.age = 20;
user.save();


Users.get(user => user.name === 'Peter'); // { name: 'Peter', age: 20 }
Users.getMany(user => user.age > 18); // [{ name: 'Peter', age: 20 }, { name: 'John', age: 19 }]

Contributing

Before creating an issue, please ensure that it hasn't already been reported or suggested.

When submitting a new pull request, please make sure the code style/format used is the same as the one used in the original code.

License

Refer to the LICENSE file.