Gathering detailed insights and metrics for @stolksdorf/pico-db
Gathering detailed insights and metrics for @stolksdorf/pico-db
npm install @stolksdorf/pico-db
Typescript
Module System
Node Version
NPM Version
68.3
Supply Chain
98.7
Quality
75
Maintenance
100
Vulnerability
100
License
Total Downloads
400
Last Day
1
Last Week
3
Last Month
7
Last Year
56
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
@stolksdorf/pico-db@1.0.0
Unpacked Size
6.59 kB
Size
3.01 kB
File Count
8
NPM Version
6.14.4
Node Version
13.7.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
3
Compared to previous week
Last month
75%
7
Compared to previous month
Last year
-36.4%
56
Compared to previous year
1
pico-db
is an incredibly simple filesystem-based database where each table is a folder, and each record is it's own JSON file. Useful for small/local projects, unit testing, and quickly bootstrapping new projects.
npm install @stolksdorf/pico-db
Why use json files for records?
Let's see it in action
1const DB = require('pico-db')('./path/to/db_folder'); 2 3const Users = DB.table('users'); 4const Auth = DB.table('users.auth'); 5 6await Users.put({ _id : 'foofoo', name : 'Bob', points : 50 }); 7 8const alice = await Users.put({ name : 'Alice', points : 25 }); 9await Auth.put({ method : 'password', value : 'dont_look_plz', user_id : alice._id});
Resulting filestructure
/path/to/db_folder
/users
foofoo.json
thx425nyt5z7nyuz.json
/auth
xz53vad9n4yi77tw.json
picodb(root_folder_path, opts)
-> db instance
Creates a pico-db
instance for the given root_folder_path
. All tables and records will be stored within that folder, and that folder will be created if it doesn't already exist.
1default_opts = { 2 id_key : '_id', //Key used to store the id field on records 3 id_func : (record)=>base32(16), //Function used to generate the record ids. Can use a hash of the record data if you want 4 json_space : '\t', //character used as spaces when stringifying the record JSON. Use '' for compressed JSON 5}
Example
1const DB = require('pico-db')('./db', { 2 id_key : '__ID__', 3 id_func : (user)=>user.name[0], // Uses the user's name first letter as it's id (probably not a good idea) 4 json_space : '', // Produces a more compressed json file 5}); 6 7const Users = DB.table('users'); 8Users.put({ name : 'bobert'}); 9 10/* 11{__ID__:'b',name:'bobert'} 12*/ 13 14
db.table(name)
-> table instance
Creates a table instance at root_folder_path
+ name
. name
can be '.'
separated to created nested tables, eg. .table('users.secrets.auth')
. Will create the folders if they don't already exist.
db.reset()
Completely removes everything at the root_folder_path
from the filesystem.
async table.get(id) / table.get([ids])
-> record(s)
Gets a single record from the filesystem or an array of ids. Returns undefined
if the record does not exist.
async table.put(record)
-> record
Inserts a new record into the filesystem. If the record did not have an id
one will be generated. The record data will be returned.
async table.query(queryObj)
Look up records within a table using a query object. A query object is a object made of key-function pairs. For each of the key's within a record, it calls this function with that key's value. If the function returns a false value, the record is removed from this query. Nesting also works!
1const Users = require('pico-db')('./db').table('users'); 2 3await Users.put({ name : 'bobert', score: { points : 51 }}); 4await Users.put({ name : 'alice', score: { points : 12 }}); 5await Users.put({ name : 'aaron', score: { points : 26 }}); 6 7const result = await Users.query({ 8 name : (val)=>val[0]=='a', //Only names that start with 'a' 9 score : { 10 points : (val)=>val > 25, //Only points over 25 11 } 12}); 13 14/* result 15[ { id:'...', name : 'aaron', score: { points : 26 }} ] 16*/
async table.del(id)
Removes a record from the filesystem based on it's id.
table.drop()
Removes all records from the filesystem in this table folder.
No vulnerabilities found.
No security vulnerabilities found.