Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Make And USe Ur Own Database (NODE.JS)
GatewayDuckYT (55)

Far warnings having the folder name or file name with db, data, database etc will not always work
Far Warning As far as I know this does not work on python thou i am making api for html

Have more info or have questions ask below

Protip: I use the npm package simple-json-db
To set this up do this in the shell
npm i simple-json-db

In your code just require it like so

const JSONdb = require('simple-json-db');
//make a new database path folders must be there
const db = new JSONdb('/path/to/your/database.json');

Its as easy as that for this Tutorial i will be using this file tree line

  • root
    • index.js
    • info.json

So My setup code would be
index.js

const JSONdb = require('simple-json-db');
const db = new JSONdb('./info.json');

Simple use

all i need to do to add a value is

db.set('key', 'value')

Output

{
    "key": "value"
}

You can also set arrays

db.set('key', ["gamer", "time"])

output

{
    "key": ["gamer", "Time"]
}

And as far as i know u can set json

db.set('key', {gamer: "me", upvote: true})

output

{
    "key": {
      "gamer": "me",
      "upvote": "true"
    }
}

Getting Value

Getting values are easy
u can do

console.log(db.get('key'))
//=> value

also getting arrays and json from them is also easy

//array
console.log(db.get('key'))
//=> ["gamer", "Time"]
//json
console.log(db.get('key'))
//=>{ "gamer": "me", "upvote": "true"}
var x = db.get('key')
console.log(x.gamer)
//=> me

You can also use this to see if a value even is there

console.log(db.has('key'))
//=> true
console.log(db.has('thisisnotevenhere'))
//=> false

and last of all u can delete values

{
    "key": "i am here"
}
db.delete('key');

output

{
    
}
Comments
hotnewtop
EpicGamer007 (1693)

file databases will not work on replit. this means .db files and json file storage will not work

GatewayDuckYT (55)

@EpicGamer007 no sir do not use .db
And what the heck are u talking about i use them daily on my repl

EpicGamer007 (1693)

@GatewayDuckYT can you send me an example? I am 100% certain your file saving sometimes get rolled back.