Coder Social home page Coder Social logo

datacsv's Introduction

Datacsv

Database + csv is very simple file based database for node. It uses CSV file structure and locally creates manipulates files as real db would.

Features:

  • create table
  • clear (truncate) table
  • get row/s
  • add row/s
  • edit row/s
  • delete row/s

Motivation

I was in need of very simple database for creating a mock database and tought that CSV would actually do what I need. I can open csv files, edit them manually and with this package also do everything programmatically.

Installation

To install the stable version:

npm i --save datacsv

This assumes you are using npm as your package manager.

Documentation

Require package in the project.

const db = require('datacsv');

And now we can initialise table/s.

  • path: string (Location of csv file)
  • headers: { [string]: string | Function }

Schema is entirely optional (required only if for example data needs custom transformation).

// `age` will be saved and treated as number in database
const headers = {
  name: String,
  email: String,
  age: Number,
}

const users = await db('users.csv', headers)

add

Adding data to table.

  • data: { [string]: any }[]

It will automatically add id field. Returns added rows.

await users.add([
  {
    name: 'John',
    email: 'john.doe@localhost',
    age: 21,
  }
])
/* [ { id: 'h4HNOfnLp',
 *     name: 'John',
 *     email: 'john.doe@localhost',
 *     age: 21 } ]
 */

get

To get all existing fields from table.

  • filter?: { [string]: any }

If no filter provided, it will return all rows from table.

await users.get({
  age: 21
})
/* [ { id: 'h4HNOfnLp',
 *     name: 'John',
 *     email: 'john.doe@localhost',
 *     age: 21 } ]
 */

edit

To edit data in table.

  • filter: { [string]: any }
  • data: { [string]: any }

If no filter provided, it will update all rows in table. Returns edited rows.

await users.edit(
  {
    id: 'h4HNOfnLp'
  },
  {
    name: 'Jane',
    email: 'jane.doe@localhost',
  }
)
/* [ { id: 'h4HNOfnLp',
 *     name: 'Jane',
 *     email: 'jane.doe@localhost',
 *     age: 21 } ]
 */

delete

To delete data from table.

  • filter?: { [string]: any }

If no filter provided, it will update all rows in table. Returns deleted rows.

await users.delete({
  name: 'Jane'
})
/* [ { id: 'h4HNOfnLp',
 *     name: 'Jane',
 *     email: 'jane.doe@localhost',
 *     age: 21 } ]
 */

Stay In Touch

License

MIT

Copyright (c) 2019-present, Marcis (Marcisbee) Bergmanis

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.