• Stars
    star
    111
  • Rank 313,293 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small database for node.js.

tiny

tiny is an in-process document/object store for node.js.

It is largely inspired by nStore, however, its goal was to implement real querying which goes easy on the memory.

Tiny is very simple, there are no schemas, just store your objects. It supports mongo-style querying, or alternatively a "mapreduce-like" interface similar to CouchDB's views.

Install

$ npm install tiny

How Tiny works...

Tiny takes advantage of the fact that, normally, when you query for records in a database, you're only comparing small properties (<128b) in the query itself. For example, when you query for articles on a weblog, you'll usually only be comparing the timestamp of the article, the title, the author, the category, the tags, etc. - pretty much everything except the content of the article itself.

Tiny stores each document/object's property individually in the DB file and caches all the small properties into memory when the DB loads, leaving anything above 128b behind. When a query is performed, Tiny only lets you compare the properties stored in memory, which is what you were going to do anyway. Once the query is complete, Tiny will perform lookups on the FD to grab the large properties and put them in their respective objects before results are returned to you.

This my attempt at combining what I think the best aspects of nStore and node-dirty are. node-dirty is incredibly fast and simple (everything is in-memory), and nStore is very memory effecient, (but this only lasts until you perform a query). node-tiny allows for queries that perform lookups on the db file, and it selectively caches properties as well, so it's fast and easier on memory.

Example Querying

var Tiny = require('./tiny');
Tiny('articles.tiny', function(err, db) {
  var time = Date.now()
    , low = time - (60*60*1000)
    , high = time - (30*60*1000);

  // mongo-style query
  db.find({$or: [
    { timestamp: { $lte: low } },
    { timestamp: { $gte: high } }
  ]})
  .desc('timestamp')
  .limit(3)(function(err, results) {
    console.log('Results:', results);
  });

  // is equivalent to...
  db.fetch({
    desc: 'timestamp',
    limit: 3
  }, function(doc, key) {
    if (doc.timestamp <= low
        || doc.timestamp >= high) {
      console.log('Found:', key);
      return true;
    }
  }, function(err, results) {
    console.log('Results:', results);
  });
});

The mongo-style querying should be fairly self-explanatory. The second query is supposed to be similar to a mapreduce interface, but it's the rough equivalent of a .filter function.

Note: there is a shallow parameter for .fetch, .find, and .get, wherein it will only lookup properties that are under 128b in size. This is to go easy on the memory. .each and .all are shallow by default, but they do have a deep parameter, (which I don't recommend using).

Other Usage

// save a document
db.set('myDocument', {
  title: 'a document',
  content: 'hello world'
}, function(err) {
  console.log('set!');
});

// .each will iterate through
// every object in the database
// it is shallow by default
db.each(function(doc) {
  console.log(doc.title);
});

// returns every object in the DB
// in an array, this is shallow
// by default
db.all(function(err, docs) {
  console.log(docs.length);
});

// remove a doc
db.remove('myDocument', function(err) {
  console.log('deleted');
});

// retrieve an object from the database
db.get('someOtherThing', function(err, data) {
  // data._key is a property which
  // holds the key of every object
  console.log('found:', data._key);
});

// updates the object
// without overwriting its other properties
db.update('article_1', {
  title: 'new title'
}, function(err) {
  console.log('done');
});

// close the file descriptor
db.close(function(err) {
  console.log('db closed');
});

// clean up the mess
db.compact(function(err) {
  console.log('done');
});

// dump the entire database to a JSON file
// in the same directory as the DB file
// (with an optional pretty-print parameter)
db.dump(true, function(err) {
  console.log('dump complete');
});

Making data more memory efficient

Because of the way Tiny works, there are ways to alter your data to make it more memory efficient. For example, if you have several properties on your objects that aren't necessary to for queries, its best to nest them in an object.

user: {
  name: 'joe',
  prop1: 'data',
  prop2: 'data',
  prop3: 'data'
}

user: {
  name: 'joe',
  data: {
    prop1: 'data',
    prop2: 'data',
    prop3: 'data'
  }
}

That way, the data will not be cached if it exceeds 128b collectively. Eventually there may be an ignore method or an index method, which will be explicitly inclusive or exclusive to which properties are cached and which properties are able to be referenced within a query.

Documentation

Database

Querying

Database

Tiny(name, callback)

Creates and returns a database with the given name.

Arguments

  • name - filename to store and load the Tiny database
  • callback(err, db) - Called after the database file is opened and loaded

Example

var db;
Tiny('./articles.tiny', function(err, db_) {
  if (err) throw err;
  db = db_;
  ...
});

dump(pretty, func) or dump(func)

Dumps the a database to a JSON file with the name as name.json. Pretty specifies whether to indent each line with two spaces or not. Alternatively, dump(func) can be called.

Arguments

  • pretty - if true, the JSON file will be indented with two spaces
  • func(err) - called after the dump is complete.

Example

db.dump(true, function(err) {
  console.log('dump complete');
});

close(func)

Closes the Tiny database file handle. A new Tiny object must be made to reopen the file.

Arguments

  • func() - callback function after the database has been closed

Example

db.close(function(err) {
  console.log('db closed');
});

kill(func)

Closes the Tiny database file, deletes the file and all the data in the database, and then creates a new database with the same name and file.

Arguments

  • func() - callback function after the database has been reloaded

Example

db.kill(function(err) {
  console.log('db has been destroyed and a new db has been loaded');
});

Querying

set(docKey, doc, func)

Saves a object doc to database under the key docKey. Ideally, docKey should be 128b or smaller.

Arguments

  • docKey - a key to search the database for
  • doc - an object to save to the database under the given key
  • func - callback function after the doc object has been saved to the database

Example

db.set('myDocument', {
  title: 'a document',
  content: 'hello world'
}, function(err) {
  console.log('set!');
});

each(func, deep) or each(func)

Iterates through every object in the database.

Arguments

  • func(doc) - Callback function that is called with every iterated object doc from the database
  • done() - Callback to be executed after the iterations complete.
  • deep - true if every object should be returned, false or unset if only cacheable objects should be returned (ones smaller than 128b)

Example

db.each(function(doc) {
  console.log(doc.title);
}, function() {
  console.log('done');
});

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work. </legalese>

License

Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License)

See LICENSE for more info.

More Repositories

1

blessed

A high-level terminal interface library for node.js.
JavaScript
11,260
star
2

tty.js

A terminal for your browser, using node/express/socket.io
JavaScript
4,184
star
3

ttystudio

A terminal-to-gif recorder minus the headaches.
JavaScript
3,237
star
4

compton

A compositor for X11.
C
2,245
star
5

term.js

A terminal written in javascript.
JavaScript
1,547
star
6

pty.js

Bindings to forkpty(3) for node.js.
C++
855
star
7

mako

Bitcoin node written in C
C
572
star
8

termcoin

A bitcoin wallet and blockchain explorer for your terminal.
JavaScript
480
star
9

liburkel

Authenticated key-value store (i.e. an urkel tree)
C
303
star
10

zest

An absurdly fast CSS selector engine.
JavaScript
238
star
11

slock

Fork of suckless screen locker for the extremely paranoid.
C
152
star
12

lcdb

LevelDB implemented in C (unofficial -- not affiliated with Google in any way)
C
94
star
13

bns

Recursive DNS server and resolver for node.js
JavaScript
66
star
14

parted

Streaming body parser for node.js.
JavaScript
63
star
15

bthreads

worker threads for javascript
JavaScript
48
star
16

bpkg

Bundler and release tool for node.js
JavaScript
44
star
17

tng

A full-featured PNG renderer for the terminal.
JavaScript
41
star
18

coined

A high-level wrapper around BCoin
JavaScript
25
star
19

node-uo

A UO server for node.js
JavaScript
25
star
20

n64

Int64 object for javascript
JavaScript
24
star
21

liquor

A templating engine minus the code.
JavaScript
19
star
22

daemonic

A dead-simple module to daemonize a node. No compilation required.
JavaScript
19
star
23

node-telnet2

Telnet implementation for node.js, based on node-telnet
JavaScript
18
star
24

gitj

gitk in your terminal.
JavaScript
15
star
25

node-pingback

pingbacks for node.js
JavaScript
15
star
26

dilated

A blog for node.js.
JavaScript
14
star
27

csslike

A CSS preprocessor for node.js, designed to conform to the most recent www-style proposals.
CSS
12
star
28

cmake-node

node.js toolchain for cmake
C
11
star
29

rondo

DOM library and app framework.
JavaScript
11
star
30

st

A fork of st implementing scrollback, keyboard selection, and tabs.
C
11
star
31

highlighter.js

a quick and dirty JS highlighter
JavaScript
10
star
32

charged

High-level Chargify API binding for node.js
JavaScript
10
star
33

supersha

Fast SHA256 for node.js
C
10
star
34

dwm

My dwm fork and configuration.
C
10
star
35

tmux

A fork of tmux implementing xterm behavior.
C
8
star
36

vanilla

A framework for node.js.
JavaScript
8
star
37

Live-Stylesheets

small google chrome extension for editing a page's raw css
JavaScript
8
star
38

shim.htc

An HTML5 Shim in an HTML Component
JavaScript
8
star
39

epsilon-not

Weblog
PHP
5
star
40

unbound

Bindings to libunbound for node.js
C
5
star
41

evilpart

A Node multipart parser that is positively evil
JavaScript
5
star
42

N

pretty control for js
JavaScript
5
star
43

nmterm

A wicd-curses-like interface for NetworkManager
JavaScript
5
star
44

pulsemixer.js

An alsamixer-like interface for PulseAudio
JavaScript
4
star
45

rocksdown

RocksDB backend for LevelUP
C++
4
star
46

bsert

Minimal assertions for javascript
JavaScript
4
star
47

bitcoind.js

bitcoind.js has moved to https://github.com/bitpay/bitcoind.js
C++
4
star
48

wazm

WASM abstraction and EMCC preamble
JavaScript
3
star
49

babylonia

zero-dependency babel
JavaScript
3
star
50

bslint

eslint with less (or more) bullshit
JavaScript
3
star
51

bdoc

zero-dependency jsdoc
JavaScript
3
star
52

pkg-verify

Dependency verifier for node.js
JavaScript
3
star
53

buffer-map

Buffer-keyed map for javascript
JavaScript
2
star
54

loady

dynamic loader for node.js
JavaScript
2
star
55

qrsuite

jsqrcode and qr.js rolled into one package
JavaScript
1
star