Skip to content

Commit

Permalink
feat: add logger support
Browse files Browse the repository at this point in the history
  • Loading branch information
playerx committed Oct 25, 2019
1 parent cadaa13 commit 97f7554
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/operations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export default function createFn<TDocument extends DocumentBase>(
throw new Error('CREATE_OPERATION_FAILED')
}

if (repositoryOptions && repositoryOptions.logger) {
const duration = Date.now() - now.getTime()

repositoryOptions.logger(collectionName, 'create', duration)
}

return (repositoryOptions &&
(repositoryOptions.skipIdTransformations && !repositoryOptions.enableIdMapping))
? doc
Expand Down
6 changes: 6 additions & 0 deletions src/operations/createMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export default function createManyFn<TDocument extends DocumentBase>(
throw new Error('CREATE_OPERATION_FAILED')
}

if (repositoryOptions && repositoryOptions.logger) {
const duration = Date.now() - now.getTime()

repositoryOptions.logger(collectionName, 'createMany', duration)
}

return insertedCount
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export default function deleteFn<TDocument extends DocumentBase>(
throw new Error('SOFT_DELETE_DOCUMENTS_FAILED')
}

if (repositoryOptions && repositoryOptions.logger) {
const duration = Date.now() - now.getTime()

repositoryOptions.logger(collectionName, 'delete', duration)
}

return modifiedCount
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/operations/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default function getFn<TDocument extends DocumentBase>(
let idParam: string | undefined
let doc: any

const now = new Date()

const session = (repositoryOptions && repositoryOptions.session) || undefined

if (repositoryOptions && repositoryOptions.skipIdTransformations) {
Expand All @@ -35,6 +37,12 @@ export default function getFn<TDocument extends DocumentBase>(
}, { session })
}

if (repositoryOptions && repositoryOptions.logger) {
const duration = Date.now() - now.getTime()

repositoryOptions.logger(collectionName, 'get', duration)
}

return (repositoryOptions &&
(repositoryOptions.skipIdTransformations && !repositoryOptions.enableIdMapping))
? doc
Expand Down
12 changes: 12 additions & 0 deletions src/operations/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export default function queryFn<TDocument extends DocumentBase>(
options: FindOneOptions & ExtendOptionProps = {},
): Promise<TDocument[]> {

const now = new Date()

if (repositoryOptions && repositoryOptions.query) {
if (repositoryOptions.query.defaultLimit) {
// tslint:disable-next-line
Expand Down Expand Up @@ -61,6 +63,16 @@ export default function queryFn<TDocument extends DocumentBase>(
.filter(x => !!x)
.map(x => <TDocument>x),
)
.then(result => {

if (repositoryOptions && repositoryOptions.logger) {
const duration = Date.now() - now.getTime()

repositoryOptions.logger(collectionName, 'query', duration)
}

return result
})
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/operations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export default function updateFn<TDocument extends DocumentBase>(
throw new Error('UPDATE_DOCUMENTS_FAILED')
}

if (repositoryOptions && repositoryOptions.logger) {
const duration = Date.now() - now.getTime()

repositoryOptions.logger(collectionName, 'update', duration)
}

return <TDocument>mapObject(value)
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/operations/updateMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ export default function updateManyFn<TDocument extends DocumentBase>(
throw new Error('UPDATE_DOCUMENTS_FAILED')
}

if (repositoryOptions && repositoryOptions.logger) {
const duration = Date.now() - now.getTime()

repositoryOptions.logger(collectionName, 'updateMany', duration)
}

return modifiedCount
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export interface RepositoryOptions {
query?: QueryOptions
update?: UpdateOptions
delete?: DeleteOptions
logger?: MangoLogger
}

export type MangoLogger = (collectionName: string, action: string, duration: number) => void

export interface QueryOptions {
defaultLimit: number
}
Expand Down

0 comments on commit 97f7554

Please sign in to comment.