senter-mongo-repository
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -63,2 +63,24 @@ const MongoClient = require('mongodb').MongoClient; | ||
async search(searchTerm, limit = 1000) { | ||
if (!searchTerm) { | ||
throw Error("Mongo repository: search error: searchTerm cannot be null or empty"); | ||
} | ||
try { | ||
await this.init(); | ||
let docs = await this._collection.find(searchTerm).limit(limit).toArray(); | ||
docs.forEach(x => { | ||
x.id = x._id; | ||
delete x._id; | ||
}); | ||
return docs; | ||
} catch (err) { | ||
console.log(err.stack); | ||
throw err; | ||
} | ||
} | ||
async create(userId, document) { | ||
@@ -65,0 +87,0 @@ if (!document) { |
{ | ||
"name": "senter-mongo-repository", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Contain methods to work with mongo db", | ||
@@ -5,0 +5,0 @@ "main": "mongoReposotory.js", |
@@ -5,3 +5,3 @@ const MongoRepository = require("../mongoReposotory"); | ||
const connectionString = ''; | ||
const connectionString = 'mongodb+srv://sandbox:MLiGyVdt97ECQSbO876hPtGExk36sp15@sandbox-5zqkh.mongodb.net/test?retryWrites=true&w=majority'; | ||
const dbName = 'senter'; | ||
@@ -332,2 +332,58 @@ const collectionName = MongoRepository.ContactListsCollectionName; | ||
}); | ||
}); | ||
describe("search", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var ids = []; | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
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 }); | ||
} | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
test("should retrieve exisiting object by id and userId", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne({ | ||
_id: ids[0], | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}); | ||
const result = await repository.search({ | ||
userId: userId | ||
}); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(3); | ||
}); | ||
}); |
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
18615
500