npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fruit

v1.0.8

Published

A Node.JS ORM for both SQL and NoSQL databases

Downloads

75

Readme

Fruit

CI Buimd Dependencied MIT license Gitter

Table of Contents

Introduction

Fruit is a NodeJS ORM for database manipulations. This project is currently in Alpha version, so using it in production is at your own risk.

Feel free to contribute to this awesome project.

Install

The Fruit package needs to be installed along with an adapter, you can choose one of the available adapters bellow or write your own.

Available adapters

To use Fruit with mongodb for example, you can install both fruit and fruit-mongodb.

$ npm install fruit fruit-mongodb

If you want to install fruit with all the adapters, you can take a look at fruits package

How does it work

First you need to require both the fruit module and the adapter, for example let's use fruit-mongodb

  var Fruit   = require('fruit')
    , adapter = require('fruit-mongodb');

Then you need to instantiate the fruit object

  var fruit = new Fruit(mongodbAdapter);

Connection

To test connection to the database, you need to pass options as arguments. Those options are the information needed to get connected to the database. You need to check documentation for the adapter of your choice.

  fruit.connect(options)
    .success(successCallBack)
    .error(errorCalBack);

You can also specify your options without testing the connection to the database

  var fruit = new Fruit(adapter).config(options);

  // or you can do this

  var fruit = new Fruit(adapter.config(options));

Inserting data

To insert data, you can use the .insert() method:

  function successCallBack (results) {
    console.log(results);
  }

  function errorCallBack (error) {
    console.log(error);
  }

  fruit.insert({ name: 'Khalid', age: 26 })
    .into(collectionName)
    .success(successCallBack)
    .error(errorCallBack);

If data was successfully inserted, the results passed as argument to the successCallBack would be like this:

  {
      result : {
          success       : true
        , affectedCount : 1
        , count         : 1
      }
    , insertedId : [1] // id of the inserted row ( _id for mongodb )
  }

You can also insert multiple rows at the same time

  var collectionName = 'users'
    , data = [
        { name: 'Khalid', age: 26 }
      , { name: 'Ahmed', age: 29 }
    ];

  fruit.insert(data)
    .into(collectionName)
    .success(successCallBack)
    .error(errorCallBack);

Selecting data

To filter data, you may need to call one of the methods .find(), .findOne(), .findAll()

.find():

This method allows you to look for data that fulfills the specified conditions

  var collectionName  = 'users'
    , condition       = { name: 'Khalid' };

  fruit.find(condition)
    .from(collectionName)
    .success(successCallBack)
    .error(errorCallBack);

The data found and passed as argument to the success callBack will be an array of models created using fishbone. Each model has columns as attributes and a number of useful methods.

  • .print() : It prints the model as JSON on the console using the package jsome.
  • .save() : It updates changes made on the model directly to the database. It returns a promise.
  • .delete() : It deletes the concerned row from the database. It returns a primise.
  • .toJSON() : It converts results to JSON.

examples :

If you need to update or delete records, you can use .update() and .delete()

  // using .save() method
  fruit.find({ name : 'Khalid' })
    .from('users')
    .success(function (results) {
      results[0].name; // 'Khalid'
      results[0].age = 30;
      results[0].save()
        .success(successCB)
        .error(errorCB)
    });

  // using .delete() method
  fruit.find({ name : 'Khalid' })
    .from('users')
    .success(function (results) {
      results[0].name; // 'Khalid'
      if(results[0].age == 30) {
        results[0].delete()
          .success(successCB)
          .error(errorCB)
      }
    });

You also can specify an offset and a limit :

  var collectionName  = 'users'
    , condition       = { name: 'Khalid' };

  fruit.find(condition)
    .from(collectionName)
    .offset(5)
    .limit(10)
    .success(successCallBack)
    .error(errorCallBack);
.findOne():

This method is exactly like .find() but it returns only one model, not an array. The only difference on its usage, is that it can't be combined with offset and limit.

.findAll():

This method doesn't take any filters, it returns all data of a table or a collection.

  fruit.findAll('users')
    .success(successCallBack)
    .error(errorCallBack);

Counting data

To count rows fulfilling a condition, you can use the .count() method

  fruit.count('users')
    .where({ name : 'Khalid' })
    .success(function (count) {
      console.log(count);
    })

To count all the rows of a table, you can call it without adding .where() method

  fruit.count('users')
    .success(function (count) {
      console.log(count);
    })

Updating data

There are two methods to update data, .update() and .updateAll(). The difference between them is that .update() method, updates only one row, and the .updateAll() updates many. On MySQL when you run an update query without condition, you are updating all the rows. Fruit reduces the damage of day dreaming developers. If you need to update many rows, you actually need to type updateAll.

.update():

Updating one row :

  fruit.update('users')
    .set({ age : 30 })
    .where({ name : 'Khalid' })
    .success(successCallBack)
    .error(errorCallBack)

You can also call update without .where() method.

  fruit.update('users')
    .set({ age : 30 })
    .success(successCallBack)
    .error(errorCallBack)

The arguments passed to the successCallBack would be like this :

  {
      result : {
          success       : true
        , affectedCount : 1
        , count         : 1
      }
  }
.updateAll():

Updating many rows :

  fruit.updateAll('users')
    .set({ age : 30 })
    .where({ name : 'Khalid' })
    .success(successCallBack)
    .error(errorCallBack)

You also can use it without .where() method.

The argument passed to the successCallBack is similar to the one described for .update()

Deleting data

There are two methods to delete data, .delete() and .deleteAll(). The difference between them is that .delete() method, deletes only one row, and the .deleteAll() deletes many.

.delete():

Deleting one row :

  fruit.delete('users')
    .where({ name : 'Khalid' })
    .success(successCallBack)
    .error(errorCallBack)

You can also call delete without .where() method.

  fruit.delete('users')
    .success(successCallBack)
    .error(errorCallBack)

The argument passed to the successCallBack is similar to the one described for .update()

.deleteAll():

Deleting many rows :

  fruit.deleteAll('users')
    .where({ name : 'Khalid' })
    .success(successCallBack)
    .error(errorCallBack)

You also can use it without .where() method.

The argument passed to the successCallBack is similar to the one described for .update()

Contributing

All contributions are welcome. Let's get this project to the next level. Significant and valuable contributions will allow you to be part of Fruit organisation. See the contribution guide for more details

Community

If you'd like to chat and discuss this project, you can find us here:

Back to TOC