• Complain

Teixeira - Node Patterns - Databases

Here you can read online Teixeira - Node Patterns - Databases full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2015, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Teixeira Node Patterns - Databases
  • Book:
    Node Patterns - Databases
  • Author:
  • Genre:
  • Year:
    2015
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

Node Patterns - Databases: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Node Patterns - Databases" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Teixeira: author's other books


Who wrote Node Patterns - Databases? Find out the surname, the name of the author of the book and a list of all author's works by series.

Node Patterns - Databases — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Node Patterns - Databases" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Database Patterns
Pedro Teixeira

This book is for sale at http://leanpub.com/databasepatterns

This version was published on 2015-02-11

This is a Leanpub book Leanpub empowers authors and publishers with - photo 1

* * * * *

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.

* * * * *

2015 Pedro Teixeira
Table of Contents
The source code

You can find the source code for the entire series of books at the following URL:

https://github.com/pgte/node-patterns-code

You can browse the code for this particular book here:

https://github.com/pgte/node-patterns-code/tree/master/06-database-patterns

You are free to download it, try it and tinker with it. If you have any issue with the code, please submit an issue on Github. Also, pull requests with fixes or recommendations are welcome!

Introduction

Node.js has been designed to do quick and efficient network I/O. Its event-driven streams make it ideal to be used as a kind of smart proxy, often working as the glue between back-end systems and clients. Node was originally designed with that intention in mind, but meanwhile it has been successfully used to build traditional Web applications: an HTTP server that serves HTML pages or replies with JSON messages and uses a database to store the data. Even though Web frameworks in other platforms and languages have preferred to stick with traditional open-source relational databases like MySQL or PostgreSQL, most of the existing Node Web frameworks (like Express, Hapi and others) dont impose any particular database, or even any type of database at all. This bring-your-own-database approach has been fed in part by the explosion in the variety of database servers now available, but also by the ease with which the Node module system and NPM allow you to instal and use third-party libraries.

In this short book we will analyse some of the existing solutions for interacting with some types of databases.

An embedded database using LevelDB

LevelDB is a fast key-value store written by Google engineers that has the following characteristics:

  • Keys and values are arbitrary byte arrays.
  • It stores data sorted by key.
  • It provides basic put(key, value), get(key) and delete(key) primitives.
  • It allows multiple changes to be performed in one atomic batch.
  • It allows you to read a transient consistent snapshot of the data.

LevelDB can be used from within Node.js by installing the level package. When doing so, it compiles and instals LevelDB as a library, which you can then access through JavaScript.

LevelDB is thread-safe, but is not suited to being accessed from multiple processes. This means that you should only have a LevelDB database open from a single Node process. If you have multiple Node processes, a LevelDB database cannot be shared between them.

Also, LevelDB just exposes a simple key-value store API, on top of which a multitude of extension plugins exist, implementing functions as diverse as job queues, map-reduce views, partitioning, replication, locking, network exposure, and others. In this chapter we will explore some of these.

3.1 Installing LevelDB

To instal LevelDB you should add the level package to your package.json application manifest using NPM from the command line:

$ npm install level --save
Here we assume you already have a packagejson file at the root directory of - photo 2

Here we assume you already have a package.json file at the root directory of your application.

This last commands adds the level dependency to your manifest and installs it. Now you can require it and use it inside your application:

db.js:

varlevel=require('level');varpath=require('path');vardbPath=process.env.DB_PATH||path.join(__dirname,'mydb');vardb=level(dbPath);module.exports=db;

Here we created a singleton module that exports a LevelDB database. We start by requiring the level module and use it to instatiate the database, providing it with a path. This path is the path of the directory where LevelDB will store its files. This directory should be fully dedicated to LevelDB, and may not exist at the beginning. In our case the path for the database is contained in the DB_PATH envionment variable or, if not defined, defaults to the mydb directory inside the directory of the current source file.

3.2 Using LevelDB

Now, from another module we can get a reference to the database by requiring our singleton db.js module:

app.js:

vardb=require('./db');

After this we can use it immediately to put some values:

db.put('key 1','value 1');db.put('key 2','value 2');
You can start issuing commands and queries into a LevelDB database object that - photo 3

You can start issuing commands and queries into a LevelDB database object that has recently been created, without having to wait for it to actually open the database. While the database isnt ready for action, the queries and commands are queued.

You can also use the db object to get some values:

db.get('key 1',function(err,value){if(err){returnhandleError(err);}console.log('value:',value);});

or even delete some entries:

db.del('key 1');

Any of these operations take an optional callback as the last argument. When an error happens while performing the operation, this callback gets the error object as the first argument:

db.put(
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Node Patterns - Databases»

Look at similar books to Node Patterns - Databases. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Node Patterns - Databases»

Discussion, reviews of the book Node Patterns - Databases and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.