sjsondblib
v1.0.4
Published
A simple package that lets you use json as a database.
Downloads
2
Maintainers
Readme
jsondb
A super simple package that allows you to use JSON files (or objects) as a database.
I am fully aware that the code is bad and that there is a lot of better database packages out there, but I still want to share this one.
NOTE: The types are fixed now lol
How to install
Simply run npm install sjsondblib
in the project directory.
How to use
Require the package
You need to require the npm package
const jsondb = require('sjsondblib')
Create the database object
const db = new jsondb.database(<object>, '<type: optional>')
If you want to load from a file, just use
const db = new jsondb.database(<file name (string)>)
If you want to load json from variable, use
const db = new jsondb.database(<variable>, 'object')
Create some keys
These are the field of your database. You can create new ones at any time.
db.newKey('key name', <key defauld value>)
The
key name
always has to be string. The default value can be string or number or boolean. Here is an example of how can it lookdb.newKey('name', '') db.newKey('age', 0) db.newKey('likes_pancakes', true)
Create a new database line (entry)
This will add new entry to the database
db.newEntry('id - optional')
If you dont specify id, it will be created automatically.
db.newEntry() // Id: 1 db.newEntry('custom') // Id: custom db.newEntry() // Id: 2
Edit values
db.setValue('<id>', '<key>', <value>)
id
andkey
always have to be strings. Value can be anything.Lets say you have the user input the values
name
age
andlikes_pancakes
. Now lets save it to our database.db.setValue('1', 'name', name) db.setValue('1', 'age', age) db.setValue('1', 'likes_pancakes', likes_pancakes)
Get values
db.getValue('<id>', '<key>')
Both
id
andkey
have to be strings. This function will return th value in its original type.Lets say you want to write the values.
console.log(db.getValues('1', 'name')) console.log(db.getValues('1', 'age')) console.log(db.getValues('1', 'likes_pancakes'))
You can also use
db.getType('<id>', '<key>')
to get the type of the value.Find values You can find all entries with a specified value.
db.locate('<key>', <value>)
This will return an array of all the ids that have the specified value under the specified key.
Eg. If you want to find the list of people that like pancakes, you can do
const ids_of_ppl_that_like_pancakes = db.locate('likes_pancakes', true)
Save file
Saving the json into file is simple.
db.writeFile('<file - optional>')
- If you have the database loaded from a file, then the
file
parameter is optional. If you have an object loaded, you always need to specify file.
- If you have the database loaded from a file, then the
You can name the database
db.setName('<name>')