Coder Social home page Coder Social logo

tungus's Introduction

Tungus

Build Status

Note! This version of the driver atm only works with Mongoose 4.x and 5.0. It might not be backwards compatible with Tungus 0.0.x databases, as it uses native ObjectIDs by default.

This module implements mongoose.js driver API and allows to use mongoose with TingoDB.

TingoDB is embedded Node.js database that is compatible with MongoDB on API level.

So far this module supports the most features of mongoose, but not all are tested yet.

It provides all features, that are supported by the embeddable TingoDB. PRs are welcome!

To use this module you have to install both tungus and mongoose.

npm install tungus
npm install mongoose

All it needs is to include the Tungus module once before mongoose is included. This rewrites global.MONGOOSE_DRIVER_PATH variable to point it to tungus.

require('tungus');
require('mongoose');

You can now use the TingoDB as a drop-in replacement for the MongoDB:

mongoose.connect('mongodb://localhost/big_project-dev');
/* or to prevent backwards compatibility, replace the protocol */
/* this throws errors, if tungus is not present */
mongoose.connect('tingodb://some/local/folder');
/* you can even use a global folder (here on windows) */
mongoose.connect('tingodb://C:\\data\\in\\a\\global\\path');

Optionally you can set tingodb options using TUNGUS_DB_OPTIONS global variable. For example this way it is possible to switch to TingoDBs simple ObjectIds which is not tested at the moment.

/* Current defaults */
global.TUNGUS_DB_OPTIONS = { nativeObjectID: true, searchInArray: true };

Full example:

const tungus = require('tungus');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

console.info('Running mongoose version %s', mongoose.version);

/**
 * Console schema and model
 */

const consoleSchema = Schema({
  name: String,
  manufacturer: String,
  released: Date
});
const Console = mongoose.model('Console', consoleSchema);

/**
 * Game schema and model
 */

const gameSchema = Schema({
  name: String,
  developer: String,
  released: Date,
  consoles: [{ type: Schema.Types.ObjectId, ref: 'Console' }]
});
const Game = mongoose.model('Game', gameSchema);

/**
 * Connect to the local tingo db file (NOT using MongoClient)
 */

mongoose.Promise = global.Promise;
mongoose.connect(`mongodb://data/base`, { useMongoClient: false })
  // Create data (console)
  .then(() => Console.create({
    name: 'Nintendo 64',
    manufacturer: 'Nintendo',
    released: 'September 29, 1996'
  }))
  // Create data (game)
  .then((nintendo64) => Game.create({
    name: 'Legend of Zelda: Ocarina of Time',
    developer: 'Nintendo',
    released: new Date('November 21, 1998'),
    consoles: [nintendo64]
  }))
  // Run example
  .then(example)
  // Something bad happened!
  .catch((err) => console.error('Example failed!', err));

/**
 * Example: Population
 */

function example () {
  return Game
    .findOne({ name: /^Legend of Zelda/ })
    .populate('consoles')
    .then((ocarina) => {
      console.log(ocarina);
      console.log(
        '"%s" was released for the %s on %s',
        ocarina.name,
        ocarina.consoles[0].name,
        ocarina.released.toLocaleDateString()
      );
    })
    .then(cleanup);
}

/**
 * Remove elements and disconnect
 */

function cleanup () {
  return Promise.resolve()
    .then(() => Console.remove({}))
    .then(() => Game.remove({}))
    .then(() => mongoose.disconnect());
}

tungus's People

Contributors

sergeyksv avatar root-core avatar anhoev avatar g13013 avatar fedescarpa avatar startswithaj avatar capaj avatar

Watchers

James Cloos avatar Yury Bushev avatar

Forkers

x0rium

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.