Giter VIP home page Giter VIP logo

nodecsvdb's Introduction

CSV based Database

Build Status

I was looking for a lightweight replacement for a database in one of my node.js workshops. This is the result: the data is stored in files. The rows are separated by new line characters and the fields are separated by semicolons. The first version of the database implementation was completely synchronous whereas the current version heavily relies on promises.

Installation

npm install csv-db

Usage

Given a file named input.csv of the following format (columns ID, username and password):

1;admin;secret;

Initialization:

const CsvDb = require('csv-db');
const csvDb = new CsvDb('input.csv', ['id', 'username', 'password']);

If you omit the second argument of the constructor, the property names will be read from the first line

Usage:

csvDb.get().then((data) => {
  console.log(data);
}, (err) => {
  console.log(err);
});

API

CsvDb(filename, columns)

Create a file handle to access the database

get(id)

Read either all data of a certain file or just one data set. The output is an array containing one or more objects, depending on what was fetched.

csvDb.get().then((data) => {
  console.log(data);
}, (err) => {
  console.log(err);
});

insert(newData)

Insert a new row to the database. Data is an object with column names as keys and the values to be inserted as - the values.

const user = {
  name: 'basti',
  secret: 'topSecret'
};
csvDb.insert(user).then((data) => {
  console.log(data);
}, (err) => {
  console.log(err);
});

update(data, id)

Update an existing row. Data is an object containing ALL the values excluding the id. id is the identifier of the row to be updated.

const user = {
  id: 2,
  name: 'basti',
  secret: 'topSecret'
};
csvDb.update(user, 2).then((data) => {
  console.log(data);
}, (err) => {
  console.log(err);
});

delete(id)

Delete an existing row. id is the identifier of the row to be deleted.

csvDb.delete(2).then((data) => {
  console.log(data);
}, (err) => {
  console.log(err);
});

nodecsvdb's People

Contributors

sdinari avatar springer-mayflower avatar sspringer82 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nodecsvdb's Issues

Config

Configure CSVDB (e.g. failIfNotExists, lineSeparator, delimiter)

  • Default config
  • Config file
  • Conf object in constructor

File not existing

Add option to either create a db file if it doesn't exist or throw an error
make it configurable via config file and options object

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.