0.0.1 • Published 4 years ago

jsdat v0.0.1

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

JSDat - The simplest JSON database

Indostruction

JSON is a pretty good but sometimes you need more powerful data save method. SQL database is a good method but it also required to setting up. Now JSDat comes in! You just installing this package and now you can start working. Pretty simple :>

Docs

JSDat based on functions that creates classes so you don't need to use keyword new for create an class object. So there's 2 of them: Database (main class) and DBList. Also a blank object template

Database class

For create a Database object evaluate function imported by this module by default and pass your project's database's data folder as an argument

const jsdat = require('jsdat')(`${__dirname}/data`);

get(db)

Get DBList object using subfolder name (Use it only if you know that's the folder exists)

exists(db)

Check if database exists

create(db, blank)

Create a subfolder using blank object (Use it on your own risk)

db(db, blank)

Safely create&get a DBList object using subfolder name (Use estead of get&create combinations)

dispose(db)

Delete DBList from cache if application got broken

DBList

DBList represents a database list. You can use Database.create(db) function for create an instance of it

readonly blank

blank object of database

readonly data

(Not recommended, use .array() estead) data of database

get(line)

Get data of line

set(line, data)

Set data of line to data

forEach(fn)

Loop each entry of data with fn

array()

Return entries of database's data

Blank object

Represents a blank object that using in database by default. E. g. if you try to get an noneisting line it will send you blank data. You can't override data fields with .set() if its type does not match blank's field type or doesn't specified on it.

Examples

Simple profiles

You can simply use jsdat for store users' accounts profiles data

const db = require('jsdat')(`${__dirname}/data`).db("profiles", {
	username: "John Doe",
	password: "12345",
	ballance: 0,
	
	exists: false, //If you like, but better if it will be here
});

module.exports.addUser = (email, username, password) => {
	db.set(email, {
		username, password,
		
		ballance: 0,
		exists: true,
	});
}

module.exports.login = (email, password) => {
	const user = db.get(email);
	
	return user.password == password && user.exists;
}

//You can add a bit more functions but we here not for write you a bank system :>

"Enums" database (you need to make this thing first)

One of the main tricks with jsdat. Easy-to-edit-in-runtime enums. They have the simplest technology:

const enums = require('jsdat')("i dont want to write this again sry").db("enums", {
	values: ["string"] //Now will not work like that, but better add it because of future updates
});

enums.get("my_life").values[0]; //"is pointless"

ToDo list:

Make advanced 'typeof'

NodeJS's basic typeof can't give us a fully-functional and failsafe JSON database, so prepare to it, adding a type of arrays in their definition in blank

const {typeOf} = require("i dont want reval you this module name cuz you will steal it :<");

typeOf([]); //array[void]
typeof []   //object

typeOf({"hello": "world", "me": true}); //object{"hello": "string", "me": "boolean"}
typeof {"hello": "world", "me": true}   //object

Create an authefication and remote access

Sockets allows us to do warious things like our own protocols. So one of them will be a jsdat:// protocol. Adding this will also require an authefication system, so this will be a hard task (i don't a pro in sockets, sry)

Add database rules and data validation

In blank you can now set only type of data, but not any validator. This will cause a big changes like:

{
	"values": {
		"$type": "array",
		"$contents": [
			{
				"$type": "string",
				"$length": "1...30"
			}
		],
		"$access": {
			"user1": [
				"WRITE"
			],
			"%group1": [
				"WRITE",
				"MODIFY"
			],
			"$other": [
				"READ"
			]
		}
	}
}

You'll still be able to not use advanced rules

0.0.1

4 years ago