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 @marcj/marshal-mongo reflect-metadata mongodb

import {Database, Connection} from "@marcj/marshal-mongo";

const database = new Database(new Connection('localhost', 'mydb', 'username', 'password'));

const instance = new SimpleModel('My model');
await database.add(instance);

const list = await database.query(SimpleModel).find();
const oneItem = await database.query(SimpleModel).filter({id: 'f2ee05ad-ca77-49ea-a571-8f0119e03038'}).findOne();

Index

References

Classes

Type aliases

Variables

Functions

References

BaseQuery

Re-exports BaseQuery

Connection

Re-exports Connection

Converter

Re-exports Converter

Database

Re-exports Database

DatabaseQuery

Re-exports DatabaseQuery

DatabaseQueryModel

Re-exports DatabaseQueryModel

DatabaseSession

Re-exports DatabaseSession

EntityRegistry

Re-exports EntityRegistry

FilterQuery

Re-exports FilterQuery

Formatter

Re-exports Formatter

JoinDatabaseQuery

Re-exports JoinDatabaseQuery

NoIDDefinedError

Re-exports NoIDDefinedError

NotFoundError

Re-exports NotFoundError

Query

Re-exports Query

QueryCustomFields

Re-exports QueryCustomFields

QueryFieldNames

Re-exports QueryFieldNames

QueryMode

Re-exports QueryMode

SORT

Re-exports SORT

SORT_TYPE

Re-exports SORT_TYPE

classToMongo

Re-exports classToMongo

convertClassQueryToMongo

Re-exports convertClassQueryToMongo

convertPlainQueryToMongo

Re-exports convertPlainQueryToMongo

convertQueryToMongo

Re-exports convertQueryToMongo

getHydratedDatabaseSession

Re-exports getHydratedDatabaseSession

getLastKnownPKInDatabase

Re-exports getLastKnownPKInDatabase

getMongoFilter

Re-exports getMongoFilter

hydrateEntity

Re-exports hydrateEntity

isHydrated

Re-exports isHydrated

isItemKnownInDatabase

Re-exports isItemKnownInDatabase

markAsHydrated

Re-exports markAsHydrated

markItemAsKnownInDatabase

Re-exports markItemAsKnownInDatabase

mongoToClass

Re-exports mongoToClass

mongoToPlain

Re-exports mongoToPlain

partialClassToMongo

Re-exports partialClassToMongo

partialMongoToClass

Re-exports partialMongoToClass

partialMongoToPlain

Re-exports partialMongoToPlain

partialPlainToMongo

Re-exports partialPlainToMongo

plainToMongo

Re-exports plainToMongo

propertyClassToMongo

Re-exports propertyClassToMongo

propertyMongoToClass

Re-exports propertyMongoToClass

resolveCollectionName

Re-exports resolveCollectionName

setHydratedDatabaseSession

Re-exports setHydratedDatabaseSession

unmarkItemAsKnownInDatabase

Re-exports unmarkItemAsKnownInDatabase

Type aliases

Converter

Converter: (convertClassType: ClassType<any>, path: string, value: any) => any

Type declaration

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

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

      Returns any

FilterQuery

FilterQuery<T>: {} | Query<T>

Type parameters

  • T

FlattenIfArray

FlattenIfArray<T>:

Type parameters

  • T

PK

PK: any

Query

Query<T>: { $all?: Array<Partial<T>>; $and?: Array<FilterQuery<T>>; $eq?: T; $exists?: undefined | false | true; $gt?: T; $gte?: T; $in?: Array<T>; $lt?: T; $lte?: T; $mod?: number[]; $ne?: T; $nin?: Array<T>; $nor?: Array<FilterQuery<T>>; $not?: FilterQuery<T>; $options?: "i" | "g" | "m" | "u"; $or?: Array<FilterQuery<T>>; $parameter?: undefined | string; $regex?: RegExp | string; $size?: undefined | number; $type?: any }

Type parameters

  • T

Type declaration

  • Optional $all?: Array<Partial<T>>
  • Optional $and?: Array<FilterQuery<T>>
  • Optional $eq?: T
  • Optional $exists?: undefined | false | true
  • Optional $gt?: T
  • Optional $gte?: T
  • Optional $in?: Array<T>
  • Optional $lt?: T
  • Optional $lte?: T
  • Optional $mod?: number[]
  • Optional $ne?: T
  • Optional $nin?: Array<T>
  • Optional $nor?: Array<FilterQuery<T>>
  • Optional $not?: FilterQuery<T>
  • Optional $options?: "i" | "g" | "m" | "u"
  • Optional $or?: Array<FilterQuery<T>>
  • Optional $parameter?: undefined | string
  • Optional $regex?: RegExp | string
  • Optional $size?: undefined | number
  • Optional $type?: any

QueryCustomFields

QueryCustomFields: {}

Type declaration

QueryFieldNames

QueryFieldNames: {}

Type declaration

  • [name: string]: boolean

QueryMode

QueryMode: "find" | "findField" | "findOne" | "findOneOrUndefined" | "findOneField" | "findOneFieldOrUndefined" | "has" | "count" | "ids" | "updateOne" | "deleteOne" | "deleteMany" | "patchOne" | "patchMany"

SORT

SORT<T>: {} | {}

Type parameters

  • T

SORT_TYPE

SORT_TYPE: "asc" | "desc" | { $meta: "textScore" }

Store

Store: { ref: any; stale: boolean }

Type declaration

  • ref: any
  • stale: boolean

Variables

Let SESSION_IDS

SESSION_IDS: number = 0

Const globalKnownInDB

globalKnownInDB: WeakMap<any, { lastKnownPK: any }> = new WeakMap<any, {lastKnownPK: any,}>()

has

has: has

Functions

classToMongo

  • Type parameters

    • T

    Parameters

    Returns any

convertClassQueryToMongo

  • convertClassQueryToMongo<T, K>(classType: ClassType<T>, target: {}, fieldNamesMap?: QueryFieldNames, customMapping?: {}): {}
  • 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: {}
      • [path: string]: any
    • Default value fieldNamesMap: QueryFieldNames = {}
    • Default value customMapping: {} = {}
      • [name: string]: (name: string, value: any, fieldNamesMap: {}) => any
          • (name: string, value: any, fieldNamesMap: {}): any
          • Parameters

            • name: string
            • value: any
            • fieldNamesMap: {}
              • [name: string]: boolean

            Returns any

    Returns {}

    • [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

    Returns {}

    • [path: string]: any

convertQueryToMongo

  • Type parameters

    • T

    • K: keyof T

    Parameters

    Returns {}

    • [path: string]: any

Const convertTypedArrayToClass

  • convertTypedArrayToClass(setter: string, accessor: string, property: PropertyCompilerSchema): { template: string; context: object }
  • Parameters

    Returns { template: string; context: object }

    • template: string
    • context: object
      • nodeBufferToTypedArray: nodeBufferToTypedArray
      • typedArrayNamesMap: Map<"string" | "number" | "boolean" | "class" | "objectId" | "uuid" | "union" | "moment" | "date" | "Int8Array" | "Uint8Array" | "Uint8ClampedArray" | "Int16Array" | "Uint16Array" | "Int32Array" | "Uint32Array" | "Float32Array" | "Float64Array" | "arrayBuffer" | "enum" | "any", any>

Const convertTypedArrayToMongo

  • convertTypedArrayToMongo(setter: string, accessor: string, property: PropertyCompilerSchema): { template: string; context: object }
  • Parameters

    Returns { template: string; context: object }

getHydratedDatabaseSession

  • getHydratedDatabaseSession(item: any): any
  • Parameters

    • item: any

    Returns any

getLastKnownPKInDatabase

  • getLastKnownPKInDatabase<T>(item: T): any
  • Cross session state whether the item is currently known in the database, for the current node process.

    Type parameters

    • T

    Parameters

    • item: T

    Returns any

getMongoFilter

  • Parameters

    Returns any

hydrateEntity

  • hydrateEntity<T>(item: T): Promise<any>
  • Hydrates not completely populated item and makes it completely accessible.

    Type parameters

    • T

    Parameters

    • item: T

    Returns Promise<any>

isHydrated

  • isHydrated(item: any): boolean
  • Returns true if item is hydrated. Returns false when its a unpopulated proxy.

    Parameters

    • item: any

    Returns boolean

isItemKnownInDatabase

  • isItemKnownInDatabase<T>(item: T): boolean
  • Cross session state whether the item is currently known in the database, for the current node process.

    Type parameters

    • T

    Parameters

    • item: T

    Returns boolean

markAsHydrated

  • markAsHydrated(item: any): void
  • Parameters

    • item: any

    Returns void

markItemAsKnownInDatabase

  • markItemAsKnownInDatabase<T>(classSchema: ClassSchema<T>, item: T): void
  • Marks that item as currently known in the database, for the current node process.

    Type parameters

    • T

    Parameters

    Returns void

mongoToClass

  • Type parameters

    • T

    Parameters

    Returns T

mongoToPlain

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

    • T

    Parameters

    • classType: ClassType<T>
    • record: any

    Returns any

partialClassToMongo

  • partialClassToMongo<T, K>(classType: ClassType<T>, partial: {}, options?: JitConverterOptions): {}
  • Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • partial: {}
      • [path: string]: any
    • Default value options: JitConverterOptions = {}

    Returns {}

    • [path: string]: any

partialMongoToClass

  • partialMongoToClass<T, K>(classType: ClassType<T>, partial: {}, options?: JitConverterOptions): {}
  • Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • partial: {}
      • [path: string]: any
    • Default value options: JitConverterOptions = {}

    Returns {}

    • [path: string]: any

partialMongoToPlain

  • partialMongoToPlain<T, K>(classType: ClassType<T>, target: {}, options?: JitConverterOptions): {}
  • Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • target: {}
      • [path: string]: any
    • Default value options: JitConverterOptions = {}

    Returns {}

    • [path: string]: any

partialPlainToMongo

  • partialPlainToMongo<T, K>(classType: ClassType<T>, target: {}, options?: JitConverterOptions): {}
  • Type parameters

    • T

    • K: keyof T

    Parameters

    • classType: ClassType<T>
    • target: {}
      • [path: string]: any
    • Default value options: JitConverterOptions = {}

    Returns {}

    • [path: string]: any

plainToMongo

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

    • T

    Parameters

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

    Returns any

propertyClassToMongo

  • propertyClassToMongo<T>(classType: ClassType<T>, name: (keyof T & string) | string, value: any): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • name: (keyof T & string) | string
    • value: any

    Returns any

propertyMongoToClass

  • propertyMongoToClass<T>(classType: ClassType<T>, name: (keyof T & string) | string, value: any): any
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>
    • name: (keyof T & string) | string
    • value: any

    Returns any

resolveCollectionName

  • resolveCollectionName<T>(classType: ClassType<T>): string
  • Type parameters

    • T

    Parameters

    • classType: ClassType<T>

    Returns string

setHydratedDatabaseSession

  • setHydratedDatabaseSession(item: any, databaseSession: DatabaseSession): void
  • Parameters

    Returns void

unmarkItemAsKnownInDatabase

  • unmarkItemAsKnownInDatabase<T>(item: T): boolean
  • Unmarks that item as currently known in the database, for the current node process.

    Type parameters

    • T

    Parameters

    • item: T

    Returns boolean

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