Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @marcj/marshal-mongo

Mongo Database for Marshal

npm version

Marshal's MongoDB database abstraction makes it super easy to retrieve and store data from and into your MongoDB. We make sure the data from your JSON or class instance is correctly converted to MongoDB specific types and inserted IDs are applied to your class instance.

npm install @marcj/marshal-mongo reflect-metadata mongodb typeorm

Install buffer as well if you want binary support.

import {plainToClass} from "@marcj/marshal";
import {Database} from "@marcj/marshal-mongo";
import {createConnection} from "typeorm";

(async () => {
const connection = await createConnection({
    type: "mongodb",
    host: "localhost",
    port: 27017,
    database: "testing",
    useNewUrlParser: true,
});
const database = new Database(connection, 'testing');

const instance: SimpleModel = plainToClass(SimpleModel, {
    id: 'f2ee05ad-ca77-49ea-a571-8f0119e03038',
    name: 'myName',
});

await database.save(SimpleModel, instance);

const list: SimpleModel[] = await database.find(SimpleModel);
const oneItem: SimpleModel = await database.get(
    SimpleModel,
    {id: 'f2ee05ad-ca77-49ea-a571-8f0119e03038'}
);
});

Index

Type aliases

Converter

Converter: function

Type declaration

    • (convertClassType: ClassType<any>, path: string, value: any): any
    • Parameters

      • convertClassType: ClassType<any>
      • path: string
      • value: any

      Returns any

QueryCustomFields

QueryCustomFields: object

Type declaration

QueryFieldNames

QueryFieldNames: object

Type declaration

  • [name: string]: boolean

Functions

classToMongo

  • classToMongo<T>(classType: ClassType<T>, target: T): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • target: T

    Returns any

convertClassQueryToMongo

  • convertClassQueryToMongo<T, K>(classType: ClassType<T>, target: object, fieldNamesMap?: QueryFieldNames, customMapping?: object): object
  • Takes a mongo filter query and converts its class values to classType's mongo types, so you can use it to send it to mongo.

    Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • target: object
      • [path: string]: any
    • Default value fieldNamesMap: QueryFieldNames = {}
    • Default value customMapping: object = {}
      • [name: string]: function
          • (name: string, value: any, fieldNamesMap: object): any
          • Parameters

            • name: string
            • value: any
            • fieldNamesMap: object
              • [name: string]: boolean

            Returns any

    Returns object

    • [path: string]: any

convertPlainQueryToMongo

  • Takes a mongo filter query and converts its plain values to classType's mongo types, so you can use it to send it to mongo.

    Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • target: object
      • [path: string]: any
    • Default value fieldNamesMap: QueryFieldNames = {}
    • Default value customMapping: QueryCustomFields = {}

    Returns object

    • [path: string]: any

convertQueryToMongo

  • Type parameters

    • T

    • K: keyof T

    Parameters

    Returns object

    • [path: string]: any

getTypeOrmEntity

  • getTypeOrmEntity<T>(classType: ClassType<T>): EntitySchema<T>
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>

    Returns EntitySchema<T>

mongoToClass

  • mongoToClass<T>(classType: ClassType<T>, target: any, parents?: any[]): T
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • target: any
    • Optional parents: any[]

    Returns T

mongoToPlain

  • mongoToPlain<T>(classType: ClassType<T>, target: any): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • target: any

    Returns any

partialClassToMongo

  • partialClassToMongo<T, K>(classType: ClassType<T>, target: object): object
  • Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • target: object
      • [path: string]: any

    Returns object

    • [path: string]: any

partialMongoToPlain

  • partialMongoToPlain<T, K>(classType: ClassType<T>, target: object): object
  • Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • target: object
      • [path: string]: any

    Returns object

    • [path: string]: any

partialPlainToMongo

  • partialPlainToMongo<T, K>(classType: ClassType<T>, target: object): object
  • Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • target: object
      • [path: string]: any

    Returns object

    • [path: string]: any

plainToMongo

  • plainToMongo<T>(classType: ClassType<T>, target: object): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • target: object
      • [k: string]: any

    Returns any

propertyClassToMongo

  • propertyClassToMongo<T>(classType: ClassType<T>, propertyName: string, propertyValue: any): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • propertyName: string
    • propertyValue: any

    Returns any

propertyMongoToClass

  • propertyMongoToClass<T>(classType: ClassType<T>, propertyName: string, propertyValue: any, parents: any[], incomingLevel: number, state: ToClassState): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • propertyName: string
    • propertyValue: any
    • parents: any[]
    • incomingLevel: number
    • state: ToClassState

    Returns any

propertyMongoToPlain

  • propertyMongoToPlain<T>(classType: ClassType<T>, propertyName: string, propertyValue: any): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • propertyName: string
    • propertyValue: any

    Returns any

propertyPlainToMongo

  • propertyPlainToMongo<T>(classType: ClassType<T>, propertyName: string, propertyValue: any): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • propertyName: string
    • propertyValue: any

    Returns any

propertyToColumnOptions

  • propertyToColumnOptions<T>(classType: ClassType<T>, propertyName: string): EntitySchemaColumnOptions
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • propertyName: string

    Returns EntitySchemaColumnOptions

uuid4Binary

  • uuid4Binary(u?: undefined | string): Binary
  • Parameters

    • Optional u: undefined | string

    Returns Binary

uuid4Stringify

  • uuid4Stringify(u: Binary): string
  • Parameters

    • u: Binary

    Returns string

Generated using TypeDoc