# genus

> Genus is a simple library for creating Mongo models in Node. It's original inspiration is Sails` modeling, where models are defined in a folder (/models) with a simple JSON syntax. Genus takes the concept further by emphasizing extendability a great deal

Latest version **0.2.0** (published 2015-07-02) · ISC license · 0 weekly downloads

## Install

```sh
npm install genus
pnpm add genus
yarn add genus
bun add genus
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2015-07-02 |
| First published | 2015-06-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+8 in 3 direct dependencies) |
| Install scripts | no |
| Maintainers | jprodahl |

## Links

- npm: https://www.npmjs.com/package/genus
- Repository: https://github.com/xjpro/genus
- Issues: https://github.com/xjpro/genus/issues
- npm.io page: https://npm.io/package/genus

## Dependencies (5)

- [fs](https://npm.io/package/fs.md) 0.0.2
- [path](https://npm.io/package/path.md) ^0.11.14
- [lodash](https://npm.io/package/lodash.md) ^3.9.3
- [mongodb](https://npm.io/package/mongodb.md) ^2.0.35
- [bluebird](https://npm.io/package/bluebird.md) ^2.9.30

## Recent versions

- 0.2.0 (latest) — 2015-07-02
- 0.1.3 — 2015-07-01
- 0.1.2 — 2015-06-26
- 0.1.1 — 2015-06-25
- 0.1.0 — 2015-06-25

## README

# genus

Genus is a simple library for creating Mongo models in Node. It's original inspiration is Sails` modeling, where models are defined in a folder (/models) with a simple JSON syntax. Genus takes the concept further by emphasizing extendability a great deal more than the original Sails modeling would allow.

### Install

Somewhere in your server start

```javascript
require('genus').init(previouslyEstablishedMongoConnection);
```

### Usage

Register models

```javascript
module.exports = genus.model('Person', {
	attributes: { // the attributes of the model
		name: {
			type: 'string', // tring, integer, or array
			required: true // is the field required?
		},
		email: {
		  type: 'string'
		},
		age: {
		  type: 'integer',
	  }
	},
	
	// by default every model has count, find, findOne, 
	// save, remove, validate, and clean methods
	
	// you can overwrite one of the default methods
	findOne: function (criteria) {
		return this.super.findOne.call(this, (criteria)).then(function() {
		  // Do something special
		});
	},
	
	// or add a new method
	findOneWithName: function (name) {
		return this.super.find.call(this, {name: name});
	}
});
```

Assuming I named that model file Person.js, I would now be able to access my model via

```javascript
var Person = require('models/Person.js');
Person.findOneWithName('john')
  .then(function(personNamedJohn) {
  	personNamedJohn.name = 'Kelly';
  	return Person.save(req.session.userId, personNamedJohn);
  })
  .then(function(personFormerlyKnownAsJohn) {
	// etc
  });
```

### Model default methods

1. count(criteria) - mongo's count
1. findOne(id or criteria) - mongo's findOne
1. find(id or criteria) - mongo's find
1. save(requestorId, itemToBeSaved) - mongo's update (with upsert true if the model is new)
1. remove(criteria) - mongo's remove
1. validate(itemToBeValidated) - validate that a given item fits the model defintion
1. clean(itemToBeCleaned) - return a new item that contains only defined attributes

---
_Source: https://npm.io/package/genus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
