Sir
👨Sir.Js -- A very simple database on the file system, using JSON files organised into subdirectories for each table.
Uses discohash for hashing key values.
Around 500 source lines of code (see stats folder)
features
- Store any JSON-able object
- Index on any property (for nested objects, only index top-level properties)
- Auto ID or custom ID
- Diffable by git
- All records and indexes and table information is just JSON files
- 1 file per record, 1 file per unique index value, 1 file per table info
- 1 sub-directory per table, 1 sub-directory (nested inside table) per indexed property
Roadmap
- Transactions
- ACID guarantee (as long as accessed by single node thread)
- Can expand ACID to multi-threaded access with a request queue.
All in all this makes the database easy to understand and inspect. As well as making the code easy to read and maintain.
get
npm i --save sirjs
api
There's only a couple of handful of calls in this api: config
, dropTable
, getIndexedTable
, getTable
, put
, get
, getAllMatchingKeysFromIndex
and getAllMatchingRecordsFromIndex
config({root: path})
Configure the root directory of your database.
dropTable(name: string)
Deletes a table.
getTable(name: string): Table
Creates a table (if it doesn't exist) and returns it.
getIndexedTable(name: string, indexed_properties: string[]): IndexedTable
Creates a table (if it doesn't exist) that has indexes for the given properties, and returns it.
<Table>.put(key: string, value: any, greenlights?: function | function[] | Evaluator)
Adds (or updates) an item to the table by key.
<Table>.get(string: key, greenlights?: function | function[] | Evaluator)
Gets an item from the table by key.
<IndexedTable>.getAllMatchingKeysFromIndex(prop: string, value: any): string[]
Gets all item keys from the table that have a property prop
that matches value
, if that property is indexed.
<IndexedTable>.getAllMatchingRecordsFromIndex(prop: string, value: any): any[]
Gets all items from the table that have a property prop
that matches value
, if that property is indexed.
<Table>.getAll(): any[]
Gets all items from the table.
<PageableTable>.getPage(cursor: string, count?: int): any[]
Get count
(default 10) items from the table, starting at the first item after cursor
. Note: not yet implemented.
Example
See below for how they're used:
;;; ; ;;;;;;; { const root = path; ; ; ; ; ; assert; assert; fs;} { const root = path; ; ; ; ; ; try assert; assert; assert; assert; assert; catche console; fs;} { const root = path; ; ; ; ; ; assert; assert; fs;} { const root = path; const errors = ; let gotItems; let key = 1; ; ; const Items = ; const items = key: `key` name: 'Apple' type: 'fruit' grams: 325 key: `key` name: 'Pear' type: 'fruit' grams: 410 key: `key` name: 'Soledado' type: 'career' grams: null qualities_of_winners: "devisiveness" "rationality" "aggression" "calmness" ; try items; catche errors; assert; try gotItems = items; catche errors; assert; assert; fs;} { const root = path; const errors = ; let gotItems1 = gotItems2 = gotItems3 = ; let key = 1; ; ; const Items = ; const items = key: `key` name: 'Apple' type: 'fruit' grams: 325 key: `key` name: 'Pear' type: 'fruit' grams: 410 key: `key` name: 'Soledado' type: 'career' grams: null qualities_of_winners: "devisiveness" "rationality" "aggression" "calmness" ; try try items; catche errors; assert; try gotItems1 = items; catche errors; assert; assert; try gotItems2 = Items; catche errors; assert; assert; assert; try gotItems3 = Items; catche errors; assert; assert; assert; catche console; console; fs;} { const root = path; const errors = ; const gotItems = ; let key = 1; ; ; const Items = ; const items = key: `key` name: 'Apple' type: 'fruit' grams: 325 key: `key` name: 'Pear' type: 'fruit' grams: 410 key: `key` name: 'Soledado' type: 'career' grams: null qualities_of_winners: "devisiveness" "rationality" "aggression" "calmness" ; for const item of items try gotItems; catche errors; assert; assert; fs;} { const root = path; const errors = ; let gotItems; let key = 1; ; ; const Items = ; const items = key: `key` name: 'Apple' type: 'fruit' grams: 325 key: `key` name: 'Pear' type: 'fruit' grams: 410 key: `key` name: 'Soledado' type: 'career' grams: null qualities_of_winners: "devisiveness" "rationality" "aggression" "calmness" ; try items; catche errors; assert; try gotItems = Items; catche errors; assert; items; gotItems; assert; fs;}