2.0.9 • Published 3 years ago

knect-mongo v2.0.9

Weekly downloads
1
License
ISC
Repository
github
Last release
3 years ago

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.

$ npm install knect-mongo s-

OR

$ yarn add knect-mongo

After install import or require knect-mongo.

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 file.

FINDING DATA

Additional boilerplate from above left out for clarity.

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.

// 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/

Change

See CHANGE.md

License

See LICENSE.md

2.0.9

3 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.5

4 years ago

2.0.6

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.0

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.16

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago