sjsondblib

A simple package that lets you use json as a database.

Usage no npm install needed!

<script type="module">
  import sjsondblib from 'https://cdn.skypack.dev/sjsondblib';
</script>

README

jsondb

A super simple package that allows you to use JSON files (or objects) as a database.

Version GitHub license Generic badge Generic badge Open Source Love svg2

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

  1. Require the package

    You need to require the npm package

    const jsondb = require('sjsondblib')
    
  2. 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')

  3. 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 look

    db.newKey('name', '')
    db.newKey('age', 0)
    db.newKey('likes_pancakes', true)
    
  4. 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
    
  5. Edit values

    db.setValue('<id>', '<key>', <value>)
    

    id and key always have to be strings. Value can be anything.

    Lets say you have the user input the values name age and likes_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)
    
  6. Get values

    db.getValue('<id>', '<key>')
    

    Both id and key 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.

  7. 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)
    
  8. 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.
  9. You can name the database

    db.setName('<name>')
    

Better readme comming soon