Data
base + 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
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.
To install the stable version:
npm i --save datacsv
This assumes you are using npm as your package manager.
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)
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 } ]
*/
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 } ]
*/
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 } ]
*/
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 } ]
*/
Copyright (c) 2019-present, Marcis (Marcisbee) Bergmanis