jayson-db
v0.2.3
Published
An easy way to create JSON database files.
Downloads
1
Readme
jayson-db
An easy way to create a JSON database files.
Features
- It's a NoSQL database... in a way, I think
- Easily create a JavaScript object database with JSON files
- Export the database instance back
- Integrate a schema for your JSON database (with JSON Schema)
What's it for?
- To easily create public APIs (at least for me, I would just publish them as a package or something)
- To make sure your data inside of the JSON files consistent
- Very small-scale and/or personally managed databases
What's it not for?
- Asynchronous operations such as a web server
- Complex database structures and compositions
- Holding out very large data
If you're looking for a better alternative, you can use SQLite or node-json-db.
Getting started
First, install it through npm:
npm i jayson-db
Then just include it as a module to one of your JavaScript files:
const jaysonDB = require("jayson-db");
// create an instance of the database
const dbName = "dogs";
const db = new jaysonDB(dbName);
db.create("breeds", ["chihuahua", "pug", "bullpit"]);
db.read("breeds");
db.update("breeds", function(value) {
// update the 'breeds'
})
// or you can import from an existing JSON
const jsonLocation = "./cats.json";
const importedDB = jaysonDB.getDB(jsonLocation);
Aaaaaand voila! You're good to go! You can also use the CLI program to quickly start using the module as well.
jayson-db Class Interface
new jaysonDB(dbName, options);
dbName
— It's simply the name of the databaseoptions
— It's an object that configures your database a bit. Below are the properties that you can fill up:
Property | Type | Description
--- | --- | --- |
path
| String
| The valid path of the JSON file to be exported. It defaults to the current directory when no value (null
) was given.
schema
| Object
| An object that describes the schema compliant to the JSON Schema spec (Draft 7). You can visit the link to get a grasp on how to declare those or you could continue to the JSON Schema section to get a very basic grasp before you continue to the link. Once you've set a schema for the jayson-db
instance, data operations such as creating and updating will have additional operations for schema validation against the newly created/updated data. It will also have an additional validation process when exporting as well.
data
| Object
/ Array
| The data to be put inside of the database. It could be anything as long as it is an array or an object (in other words, a valid JSON object).
isArray
| Boolean
| Indicates the JSON will be an object or an array. Though, if the data
property is present, this option is useless. This is only useful if you are starting with no data at all.
Methods and properties
You can view the available methods and properties of the Jayson DB instance in this documentation.
Schema
You could take a little exercise or a tutorial about JSON Schema right over here. Though, you can also go straight into the official JSON Schema spec website and learn about it. They provide a "Quickstart" section for those who are not truly familiar what's it about.
CLI program
You could also use the module as a CLI program. To use it, simply refer to it by the name of the package (jayson-db).
If you install the package locally, you can call it by npx <PACKAGE_NAME>
in the shell or from the package.json
.
create
There are a couple of commands to execute with. First, you could quickly create a database instance with create
and supplying it with a name parameter (jayson-db create <DATABASE_NAME>
). Then you'll be entered into a Node REPL (as if you entered node
in the shell) with the database instance as db
.
A couple of flags and option include:
-p
/--path
— The export location of the JSON file. Defaults to current directory.-s
/--schema
— The import location of the schema file. Defaults to having no schema.
jayson-db (<DATABASE_NAME>): db
DB { name: '<DATABASE_NAME>', path: '/somewhere/in/the/fs', objects: {} }
# ...
jayson-db (<DATABASE_NAME>):
get
You can also quickly get your already exported JSON files with the get
command. All it needs is a location of the JSON file and jayson-db
will create a jayson-db
instance out of the JSON file. It'll also get the schema file (<JSON_NAME>.schema.json
) automatically, if it detects one.
You can also explicitly tell the location of the schema file with the -s
or --schema
option.
Like the create
command, if it success to create an instance. You enter into a Node REPL with the added context of the resulted instance as db
.
$ jayson-db get ./<JSON_NAME>.json --schema ./path/to/schema.json
jayson-db (<JSON_NAME>): db
# the database instance object
repl
You could also have a quick test of the library with the repl
command which will make you enter into a REPL interface included with the DB class object as DB
.
jayson-db REPL: DB
[Function: DB]
# ...
jayson-db REPL: const dbInstance = new DB(<DATABASE_NAME>, [PATH], [OBJECTS])
undefined
jayson-db REPL: dbInstance
DB { name: '<DATABASE_NAME>', path: '/whatever/man', objects: {} }