usnpdb v1.0.11
UltraSimple Not for Production DataBase (usnpdb)
Okay, this is just a test module, and we use it for some simple projects since we are too lazy to write file handler functions. If you like it and want to use it, it's up to you :) If you find it useful and want to extend it, please join us; we will be happy to have someone who actually does some work.
What it is?
This is a simple interface to JSON text files that we use as a simple database for configurations and some simple data. Mainly, we use it in the development phase, but have found a place for it even in some production solutions.
Installation
This is simple package, just use standard npm install : npm install -s usnpdb
Usage
Inicialization
To use usnpdb you need to initialise it by giving a path where your JSON files will be stored. For example:
const iotDB = require('usnpdb');
iotDB.initRepository('./data');
Initially, the database is initialized with a 4-character identation, and if you want files to take up less space, add a second parameter to the initialization, like in the example.
const iotDB = require('usnpdb');
iotDB.initRepository('./data', false);
Basic usage
For example, we have JSON object like this:
let myUser = {
"name" : "John Doe",
"age" : 25,
"address" : {
"street" : "Some street",
"houseNo" : 15,
"town" : "Gotham City",
"postal" : 666666,
"country" : "Mordor"
},
"active" : false
}
Here is few basic examples of usage.
Drop collection/table
iotDB.drop('users');
Count records
iotDB.count('users');
Insert record
iotDB.insert('users', myUser);
Insert data as new record. Before insert, add _uid as unique ID for object {string}.
Read all records
let users = iotDB.getAll('users');
Return array of objects or empty array.
Update record
iotDB.update('users', myUser);
If the object doesn't exist, it behaves like an insert record.
Delete one record
iotDB.deleteOne('users', { age : 25 });
Delete first record that matches the specified conditions.
Delete records
iotDB.delete('users', { age : 25 });
Delete all records that matches the specified conditions.
Delete record by ID
iotDB.deleteByID('users', _uid);
Delete record with specified ID.
Find by ID
let user = iotDB.findByID('users', _uid);
Find one record
let user = iotDB.findOne('users', { age : 25 });
Find one by path
let user = iotDB.findOneByPath('users', { 'address.houseNo' : 25 });
Find all records by JSON path
Find like
let users = iotDB.findLike('users', { name : 'hn' });
Find that starts with
let users = iotDB.findStartsWith('users', { name : 'Joh' });
Find all
let users = iotDB.find('users', { age : 25 });
Utility functions
IoT date/time
Ok, this is funny story. So, we had some serious issues with date format management, and for the needs of IoT sensors and processing, we needed a simple and fast way to handle time. So, we tried this approach, and we liked it.
The principle is very simple, so we created a string from the date in the format 'ddMMyyyyhhmmss' and then converted it into a number. This way, we achieved fast searching and straightforward parsing when we need any part of the date. It also made it much easier for us to manipulate and check elapsed time and other aspects, primarily related to IoT sensors and monitoring. Furthermore, when we transmit this format to small IoT microcontrollers, we can easily manage it.
Similarly, when we convert this format into a string, finding data has proven to be much faster than using the Date object. An example is locating and compering a month within a date. It turns out that the substring function is much faster.
And, of course, since we're a bit peculiar, this seemed more logical and simpler to us than having to figure out how to use the JS Date() object every time and wrestle with its logic.
I understand, some might say that we could have achieved all of this with milliseconds, but... well, this way, it was just simpler for us.
For example, if we have January 12 2023 @ 10:05:33
it is converted to "20230112100533"
and from that to number 20230112100533
.
Full date/time to IoT dateTime
let iotDate = iotDB.toIoTDateTime(new Date());
Convert Date object to IoT date-time format
Date to IoT date
let iotDate = iotDB.toIoTDate(new Date());
Convert Date object to IoT date format
Date to IoT time
let iotDate = iotDB.toIoTTime(new Date());
Convert Date object to IoT time format
FAQ
Why almost no error handling has been implemented?
Alright, I know we should have implemented at least basic error handling, but honestly, we're too lazy to do that. On the other hand, since we wrote it, we know how to use it, so there you go... (yes, we're really that lazy). Also, like all good programmers, we're thrilled about writing documentation and error management, so...
Will you correct the errors if we report them?
Of course, we can't guarantee a specific timeframe for addressing errors if they occur. If it's about new features, we'll assess how much time it takes and how it fits into our plans before making a decision.
Can we join and help with the development?
Of course, we'd be happy if you want to join. We're just not sure how to include you, as this is the first package we've put on npm, so we don't know how it's done, but if you explain it to us... no problem.