borgoose
v1.0.7
Published
A Simple local JSON database
Downloads
10
Maintainers
Readme
Borgoose
Borgoose is great database management library for small projects. With Borgoose, you can easily store your data in a local JSON file, which makes it easy to access and use.
Borgoose takes functions from Mongoose. This will make it easy for you to migrate your project to MongoDB in the future.
Install
npm i borgoose
Functions
- insertOne
- insertMany
- find
- findById
- findOne
- findMany
- updateOne
- updateMany
- deleteOne
- deleteMany
- write
- shuffle
- sync
Usage
new Borgoose(path, options)
const Borgoose = require('borgoose')
// default value of syncOnWrite is true
// I'm just writing you to see that such a setting exist :)
const bdb = new Borgoose('db.json', { syncOnWrite: true, createWithId: true })
Options
| Option | Type | Description | Default | | ------------ | ------- | ------------------------------------------------------------- | ------- | | syncOnWrite | Boolean | Automatically saves data to JSON file after each modification | true | | createWithId | Boolean | Automatically generate ID when you create new Object | false |
Examples
Insert (Create)
// Create single data
bdb.insertOne({ name: 'Bora', age: 19 })
// Create multiple data
bdb.insertMany([
{ name: 'Burak', age: 19 },
{ name: 'Baris', age: 26 },
])
Find (Read)
// Find single data
bdb.findOne({ age: 19 })
// Find by ID
bdb.findById('b0df200b-00f3-4a5b-8389-233e928e84e6')
// You can also use functions
bdb.findOne((o) => o.age > 25)
// Find multiple data
bdb.findMany({ age: 19 })
// Get all data
bdb.findMany()
Update
// Update single data
// It will set the age value of the data whose 'name' key is 'Bora' to 20.
bdb.updateOne({ name: 'Bora' }, { age: 20 })
// Update multiple data
// This will set the age values of all data less than 18 years old as 20.
bdb.updateMany((o) => o.age < 18, { age: 20 })
Delete
// Delete single data
bdb.deleteOne({ name: 'Bora' })
// You can also use functions
bdb.deleteOne((o) => o.age < 20)
// Delete multiple data
bdb.deleteMany((o) => o.age < 20)
Write
// Replace all data with specified in parameter
bdb.write([{ name: 'Bora' }])
Shuffle
// Shuffle data
bdb.shuffle()
Sync
// If syncOnWrite setting is false you have to use it to print data to json file.
bdb.sync()
Developer Notes
- Use MongoDB.