1.0.1 • Published 5 years ago

datacsv v1.0.1

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

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.

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.

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.

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.

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