# samjs-mongo

> Adds a model and interface to samjs to connect with mongoDB

Latest version **0.2.2** (published 2017-03-29) · 0 weekly downloads

## Install

```sh
npm install samjs-mongo
pnpm add samjs-mongo
yarn add samjs-mongo
bun add samjs-mongo
```

## 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.2 |
| Published | 2017-03-29 |
| First published | 2015-08-26 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+7 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Paul Pflugradt |
| Maintainers | paulpflug |

## Links

- npm: https://www.npmjs.com/package/samjs-mongo
- Repository: https://github.com/SAMjs/samjs-mongo
- Homepage: https://github.com/SAMjs/samjs-mongo#readme
- Issues: https://github.com/SAMjs/samjs-mongo/issues
- npm.io page: https://npm.io/package/samjs-mongo

## Dependencies (1)

- [mongoose](https://npm.io/package/mongoose.md) ^4.6.3

## Recent versions

- 0.2.2 (latest) — 2017-03-29
- 0.2.1 — 2016-09-07
- 0.2.0 — 2016-08-05
- 0.1.1 — 2015-09-02
- 0.1.0 — 2015-08-26

## README

# samjs-mongo

adds a model and interface to connect with mongoDB.

Client: [samjs-mongo-client](https://github.com/SAMjs/samjs-mongo-client)

## Getting Started
```sh
npm install --save samjs-mongo
npm install --save-dev samjs-mongo-client
```

## Usage

```js
// server-side
samjs
.plugins(require("samjs-mogno"))
.options()
.configs()
.models({name: "someModel", db: "mongo", schema:{someProp: String}})
.startup(server)


// will be in config mode after startup to setup the mongo connection
// after successsful configuration:
samjs.state.onceStarted.then(function(){
  // not in install mode anymore
})

//client in browser

samjs.plugins(require "samjs-mongo-client")

// when mongoURI isn't setted in config, samjs will go into
// install mode, there you can set it
samjs.install.set("mongoURI", "mongodb://localhost/tableName")
.then(function(){
  // success
})
.catch(function(){
  // failed
})

var someModel = new samjs.Mongo("someModel")
// has insert / count / find / update / remove
someModel.insert(someProp:"someValue")

// in another client events: inserted / updated / removed
someModel.on("inserted", function(id) {
  // has id of inserted item
})

```

### model props

name | type | default | description
---: | --- | --- | ---
schema | object or factory function | - | (required) mongoose schema

### model hooks

each hook has to return its arguments.

name | arguments| description
---: | --- | ---
beforeFind | `{query, client}` | will be called before each `find` and `count`
afterFind | `results` | will be called after each `find` and `count`
beforeInsert | `{query, client}` | will be called before each `insert`
afterInsert | `results` | will be called after each `insert`
beforeUpdate | `{query, client}` | will be called before each server-side `update`
afterUpdate | `results` | will be called after each server-side `update`
beforeRemove | `{query, client}` | will be called before each server-side `remove`
afterRemove | `results` | will be called after each server-side `remove`
beforeCreate | `model` | will be called before model creation
afterCreate | `model` | will be called after model creation

example:
```js
samjs
.plugins(require("samjs-mongo"))
.options()
.configs()
.models({
  name:"someModel",
  db:"mongo",
  schema:{someProp: String},
  beforeGet: [
    function(obj) {
      if (notPermitted){
        throw new Error("no Permission")
      }
      return obj
    }
  ]
})
```

### plugins
plugins are activated on model level
```js
samjs
.plugins(require("samjs-mongo"),require("samjs-mongo-auth"))
.options()
.configs()
.models({
  name:"someModel",
  db:"mongo",
  schema:{
    someProp: String
    },
  plugins: {
    "auth": null // or a options object to interact with the plugin
    }
  }
})
```

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