Giter Site home page Giter Site logo

db4js's Introduction

db4js

A Simple light DB for Javascript

Usage

var Db = require('./db');

Load from (empty) file:

var myDb = Db.create();
myDb.loadFromFile("my.db", {
	keyField : "id",
	indexes : {
		id : Db.indexBuilders.fromKey
	}
}, "id");

Read by ID:

var id = "abcd"
myDb.get("id", id).then(function(data) {
	if(data.indexItems)
		doSomeThing(data.indexItems[0][0])
	else
		error("not found")
}, function(message) {
	error(message);
})

Save:

myDb.save("id", object, errorCB, true).then(function(data) {
		doSomething(data)
})

API

loadFromFile = function(path, metadata, keyField)

loadData = function(data, metadata)

save = function(key, data, error, reindex)

delete = function(key, error)

listKeys = function(forKey, options)

get = function(index, key)

search = function(index, input, keyField, limit)

Index Builders

indexBuilders.fromKey

indexBuilders.fromField

indexBuilders.fromAlias

indexBuilders.fromRecursive

indexBuilders.trigramFromIndex

Example 1

var itemsDb = Db.create();
var itemMetaData = {
	keyField : "text",
	indexes : {
		name : Db.indexBuilders.fromKey,
		unit : function(key, data) {
			return Db.indexBuilders.fromField(key, data, function(obj) {
				return obj.units;
			});
		},
		search : function(index, data) {
			return Db.indexBuilders.fromKey(index, data)
			.then(function(data) { 
				return Db.indexBuilders.trigramFromIndex(index, data)
			});
		},
		count : function(key, data) {
			return Db.indexBuilders.fromField(key, data, function(obj) {
				return obj.count;
			});
		}
	}
};
itemMetaData.indexes.count.sort = function(a, b){return a-b};
itemsDb.loadFromFile(dataFile, itemMetaData, 'text');

Example 2

var recipeDb = Db.create();
var recipeMetaData = {
	keyField : "name",
	indexes : {
		recipeName : Db.indexBuilders.fromKey,
		recipeSearch : function(index, data) {
			return Db.indexBuilders.fromKey(index, data)
			.then(function(data) { 
				return Db.indexBuilders.trigramFromIndex(index, data)
			});
		},
		recipeIngredients : function(key, data) {
			return Db.indexBuilders.fromField(key, data, function(obj) {
				var toReturn = [];
				for (var i = obj.ingredients.length - 1; i >= 0; i--) {
					for (var j = obj.ingredients[i].length - 1; j >= 0; j--) {
						toReturn.push(obj.ingredients[i].i[j])
					};
				};

				return toReturn;
			} );
		}
	}
}

recipeDb.loadFromFile(recipeFile, recipeMetaData, 'name');

db4js's People

Watchers

James Cloos avatar Jeremy Scott avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.