senter-mongo-repository
Advanced tools
Comparing version 3.3.0 to 3.3.1
@@ -1,1 +0,1 @@ | ||
{"mongoUri":"mongodb://127.0.0.1:62759/"} | ||
{"mongoUri":"mongodb://127.0.0.1:63116/"} |
@@ -125,2 +125,67 @@ const uuid = require('uuid').v4 | ||
// Searches using match. Returns result with the total count of items | ||
async searchWithPagination(match, pagination = null, sorting = null, projection = null, options = defaultCallOptions) { | ||
if (!match) { | ||
throw Error("Mongo repository.search error: match cannot be null or empty") | ||
} | ||
if (!pagination) { | ||
throw Error("Mongo repository.search error: pagination cannot be null or empty") | ||
} | ||
if (!Number.isInteger(pagination.page) || !Number.isInteger(pagination.pageSize)) { | ||
throw Error("Mongo repository: search error: pagination.page & pagination.pageSize must be set") | ||
} | ||
if (options.forceOrganisationIdCheck && !match.userId && !match.organisationId) { | ||
throw Error("Mongo repository: cannot perform 'searchWithPagination' operation without organisationId. If you really need to use this operation without organisationId please specify option.") | ||
} | ||
if (options.excludeDeleted) { | ||
match.deletedAt = { $exists: false } | ||
} | ||
try { | ||
const pipeline = [{ | ||
"$match": match | ||
}]; | ||
if (sorting) { | ||
pipeline.push({ | ||
"$sort": sorting | ||
}) | ||
} | ||
if (projection) { | ||
pipeline.push({ | ||
"$project": projection, | ||
}) | ||
} | ||
pipeline.push({ | ||
"$facet": { | ||
"data": [ | ||
{ "$skip": pagination.pageSize * pagination.page }, | ||
{ "$limit": pagination.pageSize } | ||
], | ||
"pagination": [ | ||
{ "$count": "total" } | ||
] | ||
} | ||
}) | ||
const cursor = this._collection.aggregate(pipeline) | ||
const result = await cursor.next(); | ||
result.data.forEach(doc => { | ||
doc.id = doc._id | ||
delete doc._id | ||
}) | ||
return { | ||
data: result.data, | ||
total: result.pagination.length > 0 ? result.pagination[0].total : 0, | ||
} | ||
} catch (err) { | ||
console.log(err.stack) | ||
throw err | ||
} | ||
} | ||
async create(organisationId, document) { | ||
@@ -127,0 +192,0 @@ if (!document) { |
{ | ||
"name": "senter-mongo-repository", | ||
"version": "3.3.0", | ||
"version": "3.3.1", | ||
"description": "Contain methods to work with mongo db", | ||
@@ -5,0 +5,0 @@ "main": "mongoReposotory.js", |
@@ -234,3 +234,3 @@ const MongoRepository = require("../mongoReposotory") | ||
test.only("should return requested page of requested size", async () => { | ||
test("should return requested page of requested size", async () => { | ||
ids.push(uuid()) | ||
@@ -525,2 +525,300 @@ ids.push(uuid()) | ||
describe("searchWithPagination", () => { | ||
let repository; | ||
let organisationId; | ||
let client; | ||
const ids = []; | ||
beforeAll(async () => { | ||
client = await MongoRepository.BuildClient(connectionString) | ||
repository = new MongoRepository(client, dbName, collectionName) | ||
}) | ||
afterAll(async () => { | ||
await client.close() | ||
}) | ||
beforeEach(async () => { | ||
organisationId = lastOrganisationId.toString() | ||
lastOrganisationId++ | ||
}) | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let index = 0; index < ids.length; index++) { | ||
const id = ids[index]; | ||
await col.deleteOne({ _id: id }); | ||
} | ||
}) | ||
test("should return paged search result and total amount of pages", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
const result = await repository.searchWithPagination({ | ||
organisationId: organisationId | ||
}, | ||
{ | ||
page: 2, | ||
pageSize: 10 | ||
}, | ||
{ | ||
name : 1 | ||
}) | ||
expect(result).toNotBeNullOrUndefined() | ||
expect(result.data.length).toBe(10) | ||
expect(result.total).toBe(50) | ||
}) | ||
test("should return paged search result in sorted order", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
const result = await repository.searchWithPagination({ | ||
organisationId: organisationId | ||
}, | ||
{ | ||
page: 2, | ||
pageSize: 10 | ||
}, | ||
{ | ||
name : -1 | ||
}) | ||
expect(result).toNotBeNullOrUndefined() | ||
expect(result.data.length).toBe(10) | ||
expect(result.data[0].name).toBe(30) | ||
expect(result.data[9].name).toBe(21) | ||
}) | ||
test("should return empty list when no matched elements with total equals to 0", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
const result = await repository.searchWithPagination({ | ||
organisationId: 'SomethingElse' | ||
}, | ||
{ | ||
page: 2, | ||
pageSize: 10 | ||
}, | ||
{ | ||
name : 1 | ||
}) | ||
expect(result).toNotBeNullOrUndefined() | ||
expect(result.data.length).toBe(0) | ||
expect(result.total).toBe(0) | ||
}) | ||
test("should return requested fields only if projection is set", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
const result = await repository.searchWithPagination({ | ||
organisationId: organisationId | ||
}, | ||
{ | ||
page: 2, | ||
pageSize: 10 | ||
}, | ||
null, | ||
{ | ||
name : 0 | ||
}) | ||
expect(result).toNotBeNullOrUndefined() | ||
expect(result.data.length).toBe(10) | ||
result.data.forEach(x => expect(x.name).toBeUndefined()) | ||
}) | ||
test("should not return deleted records", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
deletedAt: new Date() | ||
}) | ||
} | ||
for (let i = 51; i <= 100; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
const result = await repository.searchWithPagination({ | ||
organisationId: organisationId | ||
}, | ||
{ | ||
page: 2, | ||
pageSize: 10 | ||
}, | ||
{ | ||
name : 1 | ||
}) | ||
expect(result).toNotBeNullOrUndefined() | ||
expect(result.data.length).toBe(10) | ||
expect(result.total).toBe(50) | ||
expect(result.data[0].name).toBe(71) | ||
expect(result.data[9].name).toBe(80) | ||
}) | ||
test("should return deleted records if requested with option", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
deletedAt: new Date() | ||
}) | ||
} | ||
for (let i = 51; i <= 100; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
const result = await repository.searchWithPagination({ | ||
organisationId: organisationId | ||
}, | ||
{ | ||
page: 2, | ||
pageSize: 20 | ||
}, | ||
{ | ||
name : 1 | ||
}, | ||
null, | ||
MongoRepository.BuildCallOptions(false)) | ||
expect(result).toNotBeNullOrUndefined() | ||
expect(result.data.length).toBe(20) | ||
expect(result.total).toBe(100) | ||
expect(result.data[0].name).toBe(41) | ||
expect(result.data[0].deletedAt).toBeDefined() | ||
expect(result.data[19].name).toBe(60) | ||
expect(result.data[19].deletedAt).toBeUndefined() | ||
}) | ||
test("should return search results without organisationId filter when specify mandatory organisation id filter to be false", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
const result = await repository.searchWithPagination({ | ||
name: 1 | ||
}, | ||
{ | ||
page: 0, | ||
pageSize: 20 | ||
}, | ||
{ | ||
name : 1 | ||
}, | ||
null, | ||
MongoRepository.BuildCallOptions(true, false)) | ||
expect(result).toNotBeNullOrUndefined() | ||
expect(result.data.length).toBe(1) | ||
expect(result.total).toBe(1) | ||
expect(result.data[0].name).toBe(1) | ||
}) | ||
test("should throw error when trying to search with default call options and no organisation id", async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let i = 1; i <= 50; ++i) | ||
{ | ||
const id = uuid() | ||
ids.push(id) | ||
await col.insertOne({ | ||
_id: id, | ||
organisationId: organisationId, | ||
name: i, | ||
}) | ||
} | ||
expect.assertions(1) | ||
try { | ||
await repository.searchWithPagination({ | ||
name: 1 | ||
}, | ||
{ | ||
page: 0, | ||
pageSize: 20 | ||
}, | ||
{ | ||
name : 1 | ||
}) | ||
} catch (e) { | ||
expect(e.message).toEqual("Mongo repository: cannot perform 'searchWithPagination' operation without organisationId. If you really need to use this operation without organisationId please specify option.") | ||
} | ||
}) | ||
}) | ||
describe("create", () => { | ||
@@ -527,0 +825,0 @@ var repository |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55898
1505