# knect-mongo

> Mongodb connection and model helper.

Latest version **2.0.9** (published 2020-11-22) · ISC license · 0 weekly downloads

## Install

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

## 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 | 2.0.9 |
| Published | 2020-11-22 |
| First published | 2019-04-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 808 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Blujedis |
| Maintainers | blujedis |

## Links

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

## Dependencies (1)

- [mustad](https://npm.io/package/mustad.md) ^0.1.8

## Recent versions

- 2.0.9 (latest) — 2020-11-22
- 2.0.8 — 2020-10-12
- 2.0.7 — 2020-10-06
- 2.0.6 — 2020-10-03
- 2.0.5 — 2020-10-02
- 2.0.3 — 2020-07-27
- 2.0.2 — 2020-07-25
- 2.0.0 — 2020-07-12
- 1.3.9 — 2020-07-12
- 1.3.8 — 2020-06-22
- 1.3.7 — 2020-05-01
- 1.3.6 — 2020-05-01
- 1.3.5 — 2020-03-30
- 1.3.4 — 2020-01-09
- 1.3.2 — 2019-10-28
- … 23 more at https://npm.io/package/knect-mongo/versions

## README

# Knect Mongo

Mongodb connection and model helper.

## Usage

Below example utilizes async/await you can also use promises or callbacks if you wish, all supported.

```sh
$ npm install knect-mongo s-
```

OR

```sh
$ yarn add knect-mongo
```

After install import or require knect-mongo.

```ts
import { KnectMongo }  from 'knect-mongo';

/** 
 * Create singleton instance.
 */
const knect = new KnectMongo();

(async function init() {

  const dbOptions = {
    // your options here.
  };

  await knect.connect('mongodb://localhost:27017/mydb', dbOptions);

  export interface IBase {
    _id?: LikeObjectId;
    created?: number;
    modified?: number;
  }

  export interface IPost extends IBase {
    title: string;
    body: string;
    user: LikeObjectId;
  }

  export interface IUser extends IBase {
    firstName: string;
    lastName: string;
    posts: (string | number | IPost | Model<any>)[];
  }

  export const UserSchema: ISchema<IUser> = {
    joins: {
      posts: { collection: 'post' }
    }
  };

  export const PostSchema: ISchema<IPost> = {
    joins: {
      user: { collection: 'user' }
    }
  };


  const User = knect.model('user', UserSchema);

  const Post = knect.model('post', PostSchema);

  try {
    // Create a new user from model.
    const user = new User({ firstName: 'Milton', lastName: 'Waddams' });

    // Save our new model.
    await user.save(); 

  }
  catch(err) {
    if (err) throw err;
  }

})();
```

## WORKING WITH DATA

You can find all static methods for a model in the source [document.ts](https://github.com/blujedis/knect-mongo/blob/master/src/document.ts) file.

### FINDING DATA

Additional boilerplate from above left out for clarity.

```ts
async function getUser(_id) {

  // Create the Model
  const User = knect.model('user', { your_schema_here });

  // Using await get the user data.
  const userData = await User.findOne({ _id });

}
```

In the above example we create the model but what if a model already exists? It's important to note if using Typescript we need to pass back in the schema for the model. This is because we cannot store the design time type in our map. Simply pass in our defined schema as **show above** then import and reuse it here.

```ts
// file containing all our defined schemas NOT SHOWN ABOVE.
// this is NOT required if NOT using Typescript.
import { IUserSchema } from './schemas'; 

async function getUser(_id) {

  // Get the Model
  // NOTE: if NOT using Typescript "<IUserSchema>" is NOT required.
  const User = knect.model<IUserSchema>('user');

  // Using await get the user data.
  const userData = await User.findOne({ _id });

}
```

## Docs

See [https://blujedis.github.io/knect-mongo/](https://blujedis.github.io/knect-mongo/)

## Change

See [CHANGE.md](CHANGE.md)

## License

See [LICENSE.md](LICENSE)

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