1.0.11 • Published 2 years ago

ff-db-liba v1.0.11

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

ff-db-liba

This package enables you to work with your local database as a JSON file. Your local file would act as a database in order to prevent data loss during npm manipulations.

Installation

First and foremost run following command to install

npm install ff-db-liba

Sample Use Cases

import { FlatFileDb } from 'ff-db-liba';

// give ralative path for your file
const inputPath = path.join(__dirname, '/data.json');

const flatfileDb = new FlatFileDb(inputPath);

const records = flatfileDb
  .getAllRecords()
  .then((records) => console.log('Retrieved records: ', records));

Snippet above would log content of your local JSON database. Alternatively if it is needed to retrieve specific record run

const id = '1';
const records = flatfileDb
  .getRecordById(id)
  .then((record) => console.log(`Retrieved record with id ${id}`, record));

When initializing new database ypu should create a file with .json extension.

Make sure the content in your local data.json is initialized as follows

[]

To create a record use createRecord method described below.

import { Record } from 'ff-db-liba';

const data: Record<Object> = {
  id: '2',
  data: {
    id: '2',
    title: 'Some Title',
  },
  toJSON() {
    return this.data;
  },
};

const createRecord = flatfileDb
  .createRecord(data)
  .then((record) => console.log('New record created: ', record));

Afterwards your data.json would look like this:

[{"id":"2","title":"Some Title"}]

In order to update your specific record you should pass id as a string as well as all data parameters for an updated record.

const id = '4';
const updatedRecord = flatfileDb
  .updateRecord(id, data)
  .then((record) => console.log(`Updated record at id ${id}`, record));

To delete specific record u should pass id parameter only

const id = '3';
const deletedRecord = flatfileDb
  .deleteRecord(id)
  .then(() => console.log(`Deleted record with id ${id}`));
1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

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.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago