1.0.7 • Published 2 years ago

fs-databases v1.0.7

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

Database Package

Documentation

Setup

const db = require('db.json');
const Database = new db.Database({
  name: "My Database", // the name of your database
  type: "object", // the type of your database: object or array
  folder: "./databases" // the folder where your database files are stored
});

Use environment file to setup

const db = require('db.json');
let Database;
db.convertToEnvironmentObject({
  path: "./environment.txt" // the environment file where your options are stored
}).then((result) => {
  Database = new db.Database(result.object);
});

Files

Get file by index
let file;
Database.files().then((files) =>{
  file = files[0 /*index of your file*/]
});
Get file by name
let file;
Database.files().then((files) => {
  file = files.find((file) => file.name === "database.json" /*Name of your file*/);
});
Get whole database
file.read().then((content) => {
  console.log(content.data /*newest data in file*/ || file.content /*value when the files were defined*/);
});
Object databases
Set key to value
file.set("foo" /*key*/, "bar" /*value*/).then((result) => {
  console.log(result);
});
Remove key
file.remove("foo" /*key*/).then((result) => {
  console.log(result);
});
Get key
file.get("foo" /*key*/).then((result) => {
  console.log(result.value);
});

Array databases

Add value
file.add("Hello World" /*value*/).then((result) => {
  console.log(result);
});
Set index to value
file.set(2 /*index*/, "db.json" /*value*/).then((result) => {
  console.log(result);
});
Remove index
file.remove(2, /*index*/).then((result) => {
  console.log(result);
});
Get index
file.get(1 /*index*/).then((result) => {
  console.log(result.value);
})

Synchronous Documentation

Setup

const db = require('db.json');
const Database = new db.Database({
  name: "My Database", // the name of your database
  type: "object", // the type of your database: object or array
  folder: "./databases" // the folder where your database files are stored
  synchronous: true // make your database synchronous
});

Use environment file to setup

const db = require('db.json');
const Database = new db.Database(db.convertToEnvironmentObjectSync({
  path: "./environment.txt" // the environment file where your options are stored
}));

Files

Get file by index
const file = Database.files()[0 /*index of your file*/]
Get file by name
const file = Database.files().find((file) => file.name === "database.json" /*Name of your file*/);
Get whole database
console.log(file.read().data || file.content);
Object databases
Set key to value
console.log(file.set("foo" /*key*/, "bar" /*value*/));
Remove key
console.log(file.remove("foo" /*key*/));
Get key
console.log(file.get("foo" /*key*/));

Array databases

Add value
console.log(file.add("Hello World" /*value*/));
Set index to value
console.log(file.set(2 /*index*/, "db.json" /*value*/));
Remove index
console.log(file.remove(2, /*index*/));
Get index
console.log(file.get(1 /*index*/).value);

Todo

  • There are currently no todos

Disclaimer

This package is mainly promise based but also contains a synchronous function for each function. If you're using a class to register a database it will call the asynchronous version and return the result synchronously.

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago