1.0.4 • Published 3 years ago

@thomazmz/mongoose-repository v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Mongoose Repository

A repository implementation to be used alongside Mongoose.

Getting Started

Install mongoose-repository alongside with mongoose

// if you are using npm
npm install mongoose-repository mongoose
// if you are using yarn
yarn install mongoose-repository mongoose 

Open a MongoDB connection through Mongoose

import { mongoose } from 'mongoose'

mongoose.connect('your-connection-string-here')

Define your Storable type

type Cat = {
  id: string,
  createdAt: Date,
  updatedAt: Date,
  name: string,
  fur: string,
  age: number,
}

Define your StorableSchema

import { Schema } from 'mongoose-repository'

// This library uses the mongoose schema definition underneath
// in otder to define its own Schema type. 

// Storable properties sucha as id, createdAt and updatedAt are 
// implicitly defined (see "The Repository Interface" section
// for more details on the Storable type).

const catSchema: Schema<Cat> = {
  name: {
    type: String,
    required: true,
  },
  fur: {
    type: String,
    required: true,
    enum: [ 'black', 'brown', 'grey', 'white', 'Taby' ],
  },
  age: {
    type: Number,
    required: true,
    minimum: 0,
  }
}

Define your Repository class

import { MongooseRepository } from 'mongoose-repository'

@InjectSchema('cats', catsSchema)
class CatsRepository extends MongooseRepository<Cat> {
}

Instanciate and use your repository instance

const catsRepository = new CatsRepository()

catsRepository.create({
  name: 'Analu',
  fur: 'Taby',
  age: 5,
})

The MongooseRepository

This library, as it's name's suggest, implements an abstract generic implementation of the Repository interface that exposes persistency funcionalitiese from MongoDB/Mongoose in accordance to the Repository interface described bellow.

The Repository Interface

In the context of this library, we define a Repository is a TypeScript interface that implies a set of methods that can be used to interact with a given persistency layer. Each Repository instance takes care of articulating persistency operations regards specific Storable type/class instances which is explicity defined in the Repository type declaration through generics.

An Storable, in turn, can be any type/class instance that holds the properties id, createdAt and updatedAt. The id property, a string, should be understood as a value that uniquelly identifies each entity of that specific type/class. The createdAt and updatedAt properties, both Dates, represents, respectivelly, the moment in which that specific entity had been persisted on the database and the last moment in which that same entity has it's state changed on the persistency layer.

1.0.2

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago