Skip to content
This repository has been archived by the owner on Mar 13, 2020. It is now read-only.

Commit

Permalink
Merge pull request #17 from chenshoo/master
Browse files Browse the repository at this point in the history
fix: add soft delete configs
  • Loading branch information
yarinvak authored Jun 4, 2019
2 parents 204abd1 + b5dfac8 commit 0142544
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 61 deletions.
25 changes: 6 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/model-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface ModelConfiguration {
allowSoftDelete?: boolean;
softDeleteReturnEntities?: boolean;
}
12 changes: 10 additions & 2 deletions src/model-creator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PolarisBaseContext, PolarisRequestHeaders } from '@enigmatis/utills';
import * as Joi from 'joi';
import { Model, model, models, Schema } from 'mongoose';
import { ModelConfiguration } from './model-config';
import { getCollectionName } from './schema-helpers/middleware-functions';
import {
addDocumentMiddleware,
Expand Down Expand Up @@ -29,14 +30,20 @@ export declare type SchemaCreator = (refNameCreator: (name: string) => string) =
export const getModelCreator = <T>(
collectionPrefix: string,
schemaOrCreator: Schema | SchemaCreator,
modelConfiguration?: ModelConfiguration,
): ModelCreator<T> => {
return ({ headers }: PolarisBaseContext): Model<InnerModelType<T>> => {
const collectionName = getCollectionName(collectionPrefix, headers);
return (
models[collectionName] ||
model<InnerModelType<T>>(
collectionName,
createSchemaForModel(collectionPrefix, schemaOrCreator, headers),
createSchemaForModel(
collectionPrefix,
schemaOrCreator,
headers,
modelConfiguration,
),
)
);
};
Expand All @@ -49,14 +56,15 @@ const createSchemaForModel = <T>(
collectionPrefix: string,
schemaOrCreator: Schema | SchemaCreator,
headers: PolarisRequestHeaders,
modelConfiguration?: ModelConfiguration,
) => {
const schema =
schemaOrCreator instanceof Function
? schemaOrCreator(getRefNameCreator(headers))
: schemaOrCreator.clone();
headers = checkHeaders(headers);
addFields(schema);
addQueryMiddleware(schema, headers);
addQueryMiddleware(schema, headers, modelConfiguration);
addDocumentMiddleware(schema, headers);
addModelMiddleware(schema, headers);
return schema;
Expand Down
15 changes: 11 additions & 4 deletions src/schema-helpers/middleware-functions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PolarisRequestHeaders } from '@enigmatis/utills';
import { Aggregate, HookNextFunction, Model } from 'mongoose';
import { ModelConfiguration } from '../model-config';
import { RepositoryModel } from '../model-creator';
import { InnerModelType } from '../types';
import { deleted, notDeleted } from './constants';
Expand Down Expand Up @@ -33,9 +34,15 @@ export const getPreInsertMany = (headers: PolarisRequestHeaders) => {
};
};

export const getFindHandler = (headers: PolarisRequestHeaders) => {
export const getFindHandler = (
headers: PolarisRequestHeaders,
modelConfig?: ModelConfiguration,
) => {
return function findHandler(this: any) {
const conditions = this._conditions;
if (modelConfig && modelConfig.softDeleteReturnEntities) {
conditions.deleted = true;
}
const realityId =
headers.realityId !== undefined &&
conditions.realityId === undefined &&
Expand All @@ -51,7 +58,7 @@ export const getFindHandler = (headers: PolarisRequestHeaders) => {
};
};

export function softRemoveFunc<T>(
export function softRemove<T>(
this: Model<any>,
query: any,
optionsOrCallback: any,
Expand All @@ -72,13 +79,13 @@ export function softRemoveFunc<T>(
}
}

export function singleSoftRemove(
export function softRemoveOne(
this: Model<any>,
query: any,
callback?: (err: any, raw: any) => void,
) {
// using thisModule to be abale to mock softRemoveFunc in tests
return thisModule.softRemoveFunc.call(this, query, { single: true }, callback);
return thisModule.softRemove.call(this, query, { single: true }, callback);
}

export function findOneAndSoftDelete(
Expand Down
36 changes: 23 additions & 13 deletions src/schema-helpers/middleware-setters.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import { PolarisRequestHeaders } from '@enigmatis/utills';
import { Aggregate, HookNextFunction, Model, Schema } from 'mongoose';
import { Schema } from 'mongoose';
import { ModelConfiguration } from '../model-config';
import {
findOneAndSoftDelete,
getFindHandler,
getPreInsertMany,
getPreSave,
preAggregate,
singleSoftRemove,
softRemoveFunc,
softRemove,
softRemoveOne,
} from './middleware-functions';

export const addQueryMiddleware = (schema: Schema, headers: PolarisRequestHeaders) => {
const findHandlerFunc = getFindHandler(headers);
export const addQueryMiddleware = (
schema: Schema,
headers: PolarisRequestHeaders,
modelConfiguration?: ModelConfiguration,
) => {
const findHandlerFunc = getFindHandler(headers, modelConfiguration);
['find', 'findOne', 'findOneAndUpdate', 'update', 'count', 'updateOne', 'updateMany'].forEach(
middleware => {
schema.pre(middleware, findHandlerFunc as any);
},
);
schema.pre('aggregate', preAggregate);
schema.statics = {
...schema.statics,
remove: softRemoveFunc,
deleteOne: singleSoftRemove,
deleteMany: softRemoveFunc,
findOneAndDelete: findOneAndSoftDelete,
findOneAndRemove: findOneAndSoftDelete,
};
if (
!modelConfiguration ||
(modelConfiguration && modelConfiguration.allowSoftDelete !== false)
) {
schema.statics = {
...schema.statics,
remove: softRemove,
deleteOne: softRemoveOne,
deleteMany: softRemove,
findOneAndDelete: findOneAndSoftDelete,
findOneAndRemove: findOneAndSoftDelete,
};
}
};

export const addModelMiddleware = (schema: Schema, headers: PolarisRequestHeaders) => {
Expand Down
14 changes: 7 additions & 7 deletions src/schema-helpers/query-with-irrelevant-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {QueryIrrelevantResult} from '@enigmatis/utills';
import {Model} from 'mongoose';
import {InnerModelType} from '../types';
import { QueryIrrelevantResult } from '@enigmatis/utills';
import { Model } from 'mongoose';
import { InnerModelType } from '../types';

export const QueryWithIrrelevant = async (
model: Model<InnerModelType<any>>,
Expand All @@ -12,11 +12,11 @@ export const QueryWithIrrelevant = async (
}
const irrelevant = await model.find(
{
_id: {$nin: result.map(x => x._id)},
dataVersion: {$gt: dataVersion},
deleted: {$in: [true, false]}
_id: { $nin: result.map(x => x._id) },
dataVersion: { $gt: dataVersion },
deleted: { $in: [true, false] },
},
{_id: true},
{ _id: true },
);

return new QueryIrrelevantResult(result, irrelevant.map(x => x._id));
Expand Down
Loading

0 comments on commit 0142544

Please sign in to comment.