# moodle

> Model module for node for use with both mongodb and mysql.

Latest version **1.2.1** (published 2013-05-29) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2013-05-29 |
| First published | 2013-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Marc Riegel |
| Maintainers | mrcrgl |
| Keywords | node.js, model, mongodb, mysql |

## Links

- npm: https://www.npmjs.com/package/moodle
- Repository: https://github.com/mrcrgl/moodle
- Homepage: http://mrcrgl.github.com/moodle
- Issues: https://github.com/mrcrgl/moodle/issues
- npm.io page: https://npm.io/package/moodle

## Dependencies (1)

- [querybuilder](https://npm.io/package/querybuilder.md) 1.x

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.2.1 (latest) — 2013-05-29
- 1.2.0 — 2013-05-29
- 1.1.8 — 2013-05-16
- 1.1.7 — 2013-04-23
- 1.1.6 — 2013-03-28
- 1.1.5 — 2013-03-28
- 1.1.4 — 2013-03-28
- 1.1.3 — 2013-03-27
- 1.1.2 — 2013-03-27
- 1.1.1 — 2013-03-26
- 1.0.0 — 2013-03-26

## README

moodle [![Build Status](https://secure.travis-ci.org/mrcrgl/moodle.png)](http://travis-ci.org/mrcrgl/moodle)
==========

## Install
```bash
$ npm install moodle
```

## Usage

### Create model (example file)
```javascript
    
    var db    = require('your-mongo-instance');
    var Model = require('moodle');
    
    var User = new Model('User');
    
    User.storage('mongodb')
        .connect(
            function() { 
                // Return your mongo instance with selected collection
                return db.users 
            }
        );

    /**
     * Define attributes
     */
    User.attr('_id', 'ObjectId');
    
    User.attr('username', 'String')
        .required();
    
    User.attr('permissions', 'Object');
    
    User.attr('password', 'String')
        .required();
    
    User.attr('last_login', 'Date')
        .default(null);
    
    User.attr('status', 'Bool')
        .default(true)
        .required();
    
    modules.export = User;
```

### Using the model
```javascript
    
    var User = require('./models/user');

    /**
     * Save new
     */
    var user = new User();
    
    user.owner("51223c75bb64ba8d60000000");
    user.username("root");
    user.password("secret");
    user.permissions({
        wheel: true
    });

    user.save(function(err) {
        if (err) throw err;
        
        user._id(); // return the generated _id
        // _id
        
        console.log("saved!");
    });

    /**
     * Update model
     */
    User.findOne({_id: "51519576d2381d38df000003"}, function(err, user) {
        if (err) throw err;
        
        user.last_login(new Date());
        user.save(function(err) {
            if (err) throw err;
        });
    });

    /**
     * Find multiple
     */
    User.find({status:true}, function(err, users) {
    
        for (var user in users) {
            if (user instanceof User)
                // true
        
        }
    
    });
```

## Supported storages

* MongoDB (Tested with [node-mongodb-native](https://github.com/mongodb/node-mongodb-native))
* MySQL (Tested with [node-mysql](https://github.com/felixge/node-mysql))

## Supported attribute types

* String
* Integer
* Bool
* Enum
* Id
* ObjectId
* Object
* Array
* Date

## API

### Attribute API

#### .alias()

Set an alias name for this attribute. It will be used for the error messages.

Returns the Attribute object for chaining.

#### .required(/* bool */)

Set this attribute as requirement. If a field is not required and not undefined, it will be validated.
Otherwise, if it's required, it will be validated in every case.

Returns the Attribute object for chaining.

#### .validator(/* function(self, value) {} */)

Add your own function to validate a field. This function is called with two parameters,
the current attribute instance and the value. Errors have to be pushed to self.errors.

Example:
```javascript
function(self, v) {
    if (!v instanceof Array) {
        self.errors.push("Not a valid array: "+v);
    }
}
```

Returns the Attribute object for chaining.

#### .default(/* mixed */)

Set the default value of this attribute. If a value is undefined, the default value is used.

Returns the Attribute object for chaining.

#### .option(/* string */) -> only for "Enum"

Adds a possible option to this attribute. Only added options were validated.

Returns the Attribute object for chaining.

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