Home

Awesome

<img src="https://user-images.githubusercontent.com/822159/76696101-03818400-665e-11ea-9f31-5464c274d08c.png" alt="Minimal MEAN" width="250"/>

CircleCI DeepScan grade Coverage Status

npm npm

A lightweight TypeScript MongoDB ODM with standout convenience features like CollectionFactory and findWithPagination

Read the excerpt from Angular for Enterprise on Understanding DocumentTS on the Wiki.

Looking to containerize MongoDB? Checkout excellalabs/mongo for a fully featured Mongo container (with Auth & SSL) inherited from the official Mongo Docker image and instructions on how to deploy it on AWS.

Major Features

DocumentTS is an ODM (Object Document Mapper) for MongoDB.

Quick Start

Supports MongoDB v4+, Mongo Driver 3.3+ and TypeScript 3.7+

import { connect } from 'document-ts'

async function start() {
  // If isProd is set to true and a .pem file is provided, SSL will be used to connect: i.e. connect(config.mongoUri, isProd, 'server/compose-ca.pem')
  await connect(process.env.MONGO_URI)
}

start()
import { getDbInstance } from 'document-ts'

// assuming this is called within an async function
await dbInstance.collection('users').createIndexes([
  {
    key: {
      displayName: 1,
    },
  },
  {
    key: {
      email: 1,
    },
    unique: true,
  },
])
import { IDocument } from 'document-ts'

export interface IUser extends IDocument {
  email: string;
  firstName: string;
  lastName: string;
  role: string;
}
import { Document } from 'document-ts'

export class User extends Document<IUser> implements IUser {
  static collectionName = 'users'

  private password: string

  public email: string
  public firstName: string
  public lastName: string
  public role: string

  constructor(user?: IUser) {
    super(User.collectionName, user)
  }
  ...
}
  getCalculatedPropertiesToInclude(): string[]{
      return ['fullName']
  }
  getPropertiesToExclude(): string[]{
      return ['password']
  }
import { CollectionFactory } from 'document-ts'

class UserCollectionFactory extends CollectionFactory<User> {
  constructor(docType: typeof User) {
    super(User.collectionName, docType, ['firstName', 'lastName', 'email'])
  }
}

export let UserCollection = new UserCollectionFactory(User)
export interface IQueryParameters {
  filter?: string
  skip?: number
  limit?: number
  sortKeyOrList?: string | Object[] | Object
  projectionKeyOrList?: string | Object[] | Object
}
  toJSON() {
    let keys = Object.keys(this).concat(['fullAddress', 'localAddress'])
    return Serialize(SerializationStrategy.JSON, this, keys)
  }
  toBSON() {
    let keys = Object.keys(this).concat(['fullAddress', 'localAddress'])
    return Serialize(SerializationStrategy.BSON, this, keys)
  }
  toJSON() {
    // drop a breakpoint here or console.log(this)
    return super.toJSON()
  }

  toBSON() {
    return super.toBSON()
  }

See the Lemon Mart Server sample project for usage - https://github.com/duluca/lemon-mart-server

Goals

What It Isn't

Not a full-fledged ODM or ORM replacement and doesn't aspire to be one like Mongoose or Camo. Databases are HARD. MongoDB took many years to mature, and Microsoft has been trying for a really long time to build a reliable ORM with Entity Framework, Mongoose and many other ODMs are ridden with bugs (no offense) when you push them beyond the basics. It takes great resources to deliver a solid data access experience, so with DocumentTS you can develop directly against MongoDB while enjoying some conveniences as you choose.

Inspirations

Although DocumentTS doesn't aspire to replace Mongoose or Camo, it most definitely is inspired by them in the way they've solved certain problems such as hydration. Check out the source code for those projects here:

Building This Project