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

civet-orm

v1.0.7

Published

An extremely simple in-memory data store that exposes a Mongoose-like API.

Downloads

5

Readme

Civet

What is Civet?

An Asian palm
civet

A civet is a smaller relative of the mongoose. Similarly, Civet is a smaller relative of Mongoose (which we use to interact with Mongo), as in it's a small, lightweight ORM that exposes some of the same methods as Mongoose, especially as relate to collection-level CRUD.

Why is Civet?

Civet exists because it can be useful to play around with Express without first learning all of the ins and outs of Mongo and Mongoose. Sometimes you just want to be able to get started quickly, store your records in memory, and worry about persistence later.

Specifically, this was put together as a sort of "training lightsaber" for the younglings in General Assembly's Web Development Intensive. Hi, WDI DTLA 6!

How is Civet?

I'm good– how are you?

First, install Civet on your project:

npm install --save civet-orm

In your app.js (or wherever else you're going to want to use Civet):

var civetInstance = require('civet-orm');

Note that above, the variable name civetInstance can be whatever you want it to be– I'm using civetInstance so that we're perfectly clear what it is I'm talking about.

Civet#create

The first method you'll want to use is create. create takes two arguments: an object of the record you want to create, and a callback. The callback, meanwhile, takes one argument: an error (if there is one). Here's a simple example:

civetInstance.create({foo: "bar"}, function(error){
  if(error) {
    console.log(error);
  }
});

After running this, your Civet's data store will have one element in it: the object {foo: "bar", id: "1"}. The id property was added by Civet– we'll see why in a second.

Civet#find

Civet's find method returns all the records in its data store. This is something of a misnomer, but it's by design. In Mongoose, if you call find without specifying what you're looking for (as we're going to), it behaves similarly to ActiveRecord's all method– that is, it returns an array of all records. find takes only one argument: a callback function. The callback function takes two arguments: an error, and an array of records. Once again, let's see an example:

civetInstance.find(function(error, records){
	if(error) {
		console.log(error);
	} else {
		console.log(records);
	}
});

If we run this after we run the code we have for create, we'd hopefully get [{foo: "bar"}].

Civet#findById

findById does pretty much exactly what it says on the tin: you pass it an integer and a callback, it finds the record with that ID (set in create) and passes it to the callback. The method itself takes two arguments: an ID (as an integer), and a callback function (which, again, is called with an error and the record being returned). Again, let's look at an example:

civetInstance.findById(1, function(error, record){
	if(error) {
		console.log(error);
	} else {
		console.log(record);
	}
});

Calling this after the code we wrote above would log {foo: "bar"} out to console. find does double duty: we can also update our record within the callback:

civetInstance.findById(1, function(error, record){
	if(record) {
		record.baz = "wab";
	}
});

If we look at the record now, it would be {foo: "bar", baz: "wab"}. Because this is all in memory, we don't need to save anything– it'll update immediately. This changes in Mongoose, but we'll cross that bridge when we come to it.

Civet#remove

We've created, we've read (two ways), we've updated– now, it's time to delete. The remove method takes two arguments: the ID of the record to be deleted, and a callback to run after it deletes. The callback takes the customary error, but you can also give it the record you just deleted, in case you want to say goodbye. Again, let's see an example:

civetInstance.remove(1, function(error, record){
	if(error) {
		console.log(error);
	} else {
		console.log("Good riddance, " + record + "!");
	}
});