@highoutput/repository
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -1,7 +0,8 @@ | ||
import { FilterQuery, Connection, Schema } from 'mongoose'; | ||
import { Model, Document, FilterQuery, Connection, Schema } from 'mongoose'; | ||
import { ObjectId } from '@highoutput/object-id'; | ||
import { BaseEntity } from './types'; | ||
export declare class Repository<TEntity extends BaseEntity = BaseEntity, TCreate extends BaseEntity = BaseEntity & Partial<Omit<TEntity, 'id'>>> { | ||
private model; | ||
private _model; | ||
constructor(mongoose: Connection, collectionName: string, schema: Schema); | ||
get model(): Model<Document & TEntity>; | ||
exists(filter: FilterQuery<TEntity>): Promise<boolean>; | ||
@@ -13,3 +14,3 @@ create(args: TCreate): Promise<TEntity>; | ||
new: boolean; | ||
}>): Promise<(BaseEntity & Record<string, unknown>) | null>; | ||
}>): Promise<TEntity>; | ||
find(filter: FilterQuery<TEntity>): Promise<TEntity[]>; | ||
@@ -16,0 +17,0 @@ findOne(filter: FilterQuery<TEntity>): Promise<TEntity>; |
@@ -60,30 +60,33 @@ "use strict"; | ||
constructor(mongoose, collectionName, schema) { | ||
this.model = mongoose.model(collectionName, schema); | ||
this._model = mongoose.model(collectionName, schema); | ||
} | ||
get model() { | ||
return this._model; | ||
} | ||
async exists(filter) { | ||
const entity = await this.model.exists(serializeFilter(filter)); | ||
const entity = await this._model.exists(serializeFilter(filter)); | ||
return !!entity; | ||
} | ||
async create(args) { | ||
return deserialize(await this.model.create(serialize(args))); | ||
return deserialize(await this._model.create(serialize(args))); | ||
} | ||
async findById(id) { | ||
const doc = await this.model.findById(id.toBuffer()); | ||
const doc = await this._model.findById(id.toBuffer()); | ||
return deserialize(doc); | ||
} | ||
async updateOne(filter, data, options) { | ||
const document = await this.model.findOneAndUpdate(serializeFilter(filter), ramda_1.default.filter((value) => value !== undefined)(data), Object.assign({ upsert: false, new: true }, options)); | ||
const document = await this._model.findOneAndUpdate(serializeFilter(filter), ramda_1.default.filter((value) => value !== undefined)(serialize(data)), Object.assign({ upsert: false, new: true }, options)); | ||
return deserialize(document); | ||
} | ||
async find(filter) { | ||
return ramda_1.default.map((entity) => deserialize(entity), await this.model.find(serializeFilter(filter))); | ||
return ramda_1.default.map((entity) => deserialize(entity), await this._model.find(serializeFilter(filter))); | ||
} | ||
async findOne(filter) { | ||
return deserialize(await this.model.findOne(serializeFilter(filter))); | ||
return deserialize(await this._model.findOne(serializeFilter(filter))); | ||
} | ||
async deleteById(id) { | ||
await this.model.deleteOne({ _id: id.toBuffer() }); | ||
await this._model.deleteOne({ _id: id.toBuffer() }); | ||
} | ||
async deleteMany(filter) { | ||
await this.model.deleteMany(filter); | ||
await this._model.deleteMany(filter); | ||
} | ||
@@ -90,0 +93,0 @@ } |
{ | ||
"name": "@highoutput/repository", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Repository Class Implementation", | ||
@@ -52,3 +52,3 @@ "author": "High Output Ventures", | ||
}, | ||
"gitHead": "2241ed4bc721c83fb07e49a7c7868143f2d30ee1" | ||
"gitHead": "2515569eb0f995f4cfddbcf093e7906d20569c7e" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12674
138