senter-mongo-repository
Advanced tools
Comparing version 1.0.13 to 1.0.14
@@ -134,3 +134,3 @@ const MongoClient = require('mongodb').MongoClient; | ||
document.id = document._id; | ||
document._id = null; | ||
delete document._id; | ||
return document.id; | ||
@@ -143,2 +143,41 @@ } catch (err) { | ||
async createMany(userId, documents) { | ||
if (!documents || documents.length === 0) { | ||
throw Error("Mongo repository: create error: creating object cannot be null or empty"); | ||
} | ||
if (!userId) { | ||
throw Error("Mongo repository: update error: userId be null or empty"); | ||
} | ||
documents.forEach(document => { | ||
if (!document.id) { | ||
document._id = uuidv4(); | ||
} | ||
else { | ||
document._id = document.id; | ||
delete document.id; | ||
} | ||
document.userId = userId; | ||
document.createdAt = new Date(); | ||
document.updatedAt = document.createdAt; | ||
}) | ||
try { | ||
await this.init(); | ||
let response = await this._collection.insertMany(documents); | ||
documents.forEach(document => { | ||
document.id = document._id; | ||
delete document._id; | ||
}); | ||
return documents; | ||
} catch (err) { | ||
console.log(err.stack); | ||
throw err; | ||
} | ||
} | ||
async upsert(id, userId, document) { | ||
@@ -145,0 +184,0 @@ await this._update(id, userId, document, true); |
{ | ||
"name": "senter-mongo-repository", | ||
"version": "1.0.13", | ||
"version": "1.0.14", | ||
"description": "Contain methods to work with mongo db", | ||
@@ -5,0 +5,0 @@ "main": "mongoReposotory.js", |
@@ -14,102 +14,102 @@ const MongoRepository = require("../mongoReposotory"); | ||
describe("create", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// describe("create", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await repository.init(); | ||
await client.connect(); | ||
}); | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await repository.init(); | ||
// await client.connect(); | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
test("should create", async () => { | ||
const document = { | ||
name: "should create" | ||
}; | ||
// test("should create", async () => { | ||
// const document = { | ||
// name: "should create" | ||
// }; | ||
id = await repository.create(userId, document); | ||
// id = await repository.create(userId, document); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
}); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// }); | ||
test("should create with provided id", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
name: "should create with provided id", | ||
id: id | ||
}; | ||
// test("should create with provided id", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// name: "should create with provided id", | ||
// id: id | ||
// }; | ||
await repository.create(userId, document); | ||
// await repository.create(userId, document); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].id).toBeUndefined(); | ||
}); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].id).toBeUndefined(); | ||
// }); | ||
test("should create and return id", async () => { | ||
const document = { | ||
name: "should create and return id" | ||
}; | ||
// test("should create and return id", async () => { | ||
// const document = { | ||
// name: "should create and return id" | ||
// }; | ||
id = await repository.create(userId, document); | ||
// id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
}); | ||
// expect(id).toBeDefined(); | ||
// }); | ||
test("should set createdAt and updatedAt", async () => { | ||
const document = { | ||
name: "should set createdAt and updatedAt" | ||
}; | ||
// test("should set createdAt and updatedAt", async () => { | ||
// const document = { | ||
// name: "should set createdAt and updatedAt" | ||
// }; | ||
id = await repository.create(userId, document); | ||
// id = await repository.create(userId, document); | ||
expect(document.createdAt).toBeDefined(); | ||
expect(document.updatedAt).toBeDefined(); | ||
expect(document.createdAt).toBe(document.updatedAt); | ||
var differenceFromNow = document.createdAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
// expect(document.createdAt).toBeDefined(); | ||
// expect(document.updatedAt).toBeDefined(); | ||
// expect(document.createdAt).toBe(document.updatedAt); | ||
// var differenceFromNow = document.createdAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
test("should set userId", async () => { | ||
const document = { | ||
name: "should set userId" | ||
}; | ||
// test("should set userId", async () => { | ||
// const document = { | ||
// name: "should set userId" | ||
// }; | ||
id = await repository.create(userId, document); | ||
// id = await repository.create(userId, document); | ||
expect(document.userId).toBe(userId); | ||
}); | ||
// expect(document.userId).toBe(userId); | ||
// }); | ||
test("should set id but not _id", async () => { | ||
const document = { | ||
name: "should set id but not _id" | ||
}; | ||
// test("should set id but not _id", async () => { | ||
// const document = { | ||
// name: "should set id but not _id" | ||
// }; | ||
id = await repository.create(userId, document); | ||
// id = await repository.create(userId, document); | ||
expect(document.id).toBe(id); | ||
expect(document._id).toBeNull(); | ||
}); | ||
}); | ||
// expect(document.id).toBe(id); | ||
// expect(document._id).toBeUndefined(); | ||
// }); | ||
// }); | ||
describe("update", () => { | ||
describe("createMany", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
var result = []; | ||
@@ -127,3 +127,5 @@ beforeEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
for (let i = 0; i < result.length; ++i) { | ||
await col.deleteOne({ _id: result[i].id }); | ||
} | ||
await client.close(); | ||
@@ -133,504 +135,549 @@ await repository.close(); | ||
test("should update", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should update" | ||
}; | ||
test("should create", async () => { | ||
const documents = [ | ||
{ | ||
name: "should create 1" | ||
}, | ||
{ | ||
name: "should create 2" | ||
}, | ||
{ | ||
name: "should create 3" | ||
} | ||
]; | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
result = await repository.createMany(userId, documents); | ||
document.name = expectedName; | ||
await repository.update(id, userId, document); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].name).toBe(expectedName); | ||
for (let i = 0; i < result.length; ++i) { | ||
const docs = await col.find({ _id: result[i].id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
} | ||
}); | ||
}); | ||
test("should set updatedAt", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should set updatedAt" | ||
}; | ||
// describe("update", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await repository.init(); | ||
// await client.connect(); | ||
// }); | ||
document.name = expectedName; | ||
document.createdAt = new Date(2019, 10, 10); | ||
document.updatedAt = new Date(2019, 10, 10); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
await repository.update(id, userId, document); | ||
// test("should update", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should update" | ||
// }; | ||
var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
test("should not update when object is not found", async () => { | ||
const document = { | ||
name: "should not update" | ||
}; | ||
// document.name = expectedName; | ||
id = uuidv4(); | ||
// await repository.update(id, userId, document); | ||
await repository.update(id, userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].name).toBe(expectedName); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(0); | ||
}); | ||
}); | ||
// test("should set updatedAt", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should set updatedAt" | ||
// }; | ||
describe("delete", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// document.name = expectedName; | ||
// document.createdAt = new Date(2019, 10, 10); | ||
// document.updatedAt = new Date(2019, 10, 10); | ||
afterEach(async () => { | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// await repository.update(id, userId, document); | ||
test("should delete", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should delete" | ||
}; | ||
// var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// test("should not update when object is not found", async () => { | ||
// const document = { | ||
// name: "should not update" | ||
// }; | ||
await repository.delete(id, userId); | ||
// id = uuidv4(); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(0); | ||
}); | ||
}); | ||
// await repository.update(id, userId, document); | ||
describe("markDeleted", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(0); | ||
// }); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// describe("delete", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
test("should mark deleted", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should mark deleted" | ||
}; | ||
// afterEach(async () => { | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// test("should delete", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should delete" | ||
// }; | ||
await repository.markDeleted(id, userId); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].deletedAt).toBeDefined(); | ||
var differenceFromNow = docs[0].deletedAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
}); | ||
// await repository.delete(id, userId); | ||
describe("getById", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(0); | ||
// }); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// describe("markDeleted", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
test("should retrieve exisiting object by id and userId", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}; | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// test("should mark deleted", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should mark deleted" | ||
// }; | ||
const result = await repository.getById(id, userId); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
expect(result).toBeDefined(); | ||
expect(result._id).toBeUndefined(); | ||
expect(result.id).toBe(id); | ||
expect(result.userId).toBe(userId); | ||
expect(result.name).toBe(document.name); | ||
}); | ||
// await repository.markDeleted(id, userId); | ||
test("should return null if only userId does not match", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}; | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].deletedAt).toBeDefined(); | ||
// var differenceFromNow = docs[0].deletedAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// describe("getById", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
const result = await repository.getById(id, 'anotherid'); | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
expect(result).toBeNull(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
test("should retrieve exisiting object by id", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}; | ||
// test("should retrieve exisiting object by id and userId", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should retrieve exisiting object by id and userId" | ||
// }; | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const result = await repository.getById(id); | ||
// const result = await repository.getById(id, userId); | ||
expect(result).toBeDefined(); | ||
expect(result._id).toBeUndefined(); | ||
expect(result.id).toBe(id); | ||
expect(result.userId).toBe(userId); | ||
expect(result.name).toBe(document.name); | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result._id).toBeUndefined(); | ||
// expect(result.id).toBe(id); | ||
// expect(result.userId).toBe(userId); | ||
// expect(result.name).toBe(document.name); | ||
// }); | ||
test("should return null when object doesn't exist. By id and userId", async () => { | ||
id = uuidv4(); | ||
// test("should return null if only userId does not match", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should retrieve exisiting object by id and userId" | ||
// }; | ||
const result = await repository.getById(id, userId); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
expect(result).toBeNull(); | ||
id = null; | ||
}); | ||
// const result = await repository.getById(id, 'anotherid'); | ||
test("should retrieve exisiting object by id", async () => { | ||
id = uuidv4(); | ||
// expect(result).toBeNull(); | ||
// }); | ||
const result = await repository.getById(id); | ||
// test("should retrieve exisiting object by id", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should retrieve exisiting object by id and userId" | ||
// }; | ||
expect(result).toBeNull(); | ||
id = null; | ||
}); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
test("should return null when exisiting object was deleted", async () => { | ||
const document = { | ||
userId: userId, | ||
name: "should return null when exisiting object was deleted" | ||
}; | ||
// const result = await repository.getById(id); | ||
id = await repository.create(userId, document); | ||
await repository.delete(id, userId); | ||
// expect(result).toBeDefined(); | ||
// expect(result._id).toBeUndefined(); | ||
// expect(result.id).toBe(id); | ||
// expect(result.userId).toBe(userId); | ||
// expect(result.name).toBe(document.name); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// test("should return null when object doesn't exist. By id and userId", async () => { | ||
// id = uuidv4(); | ||
const result = await repository.getById(id); | ||
// const result = await repository.getById(id, userId); | ||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
// expect(result).toBeNull(); | ||
// id = null; | ||
// }); | ||
describe("search", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var ids = []; | ||
// test("should retrieve exisiting object by id", async () => { | ||
// id = uuidv4(); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// const result = await repository.getById(id); | ||
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(); | ||
}); | ||
// expect(result).toBeNull(); | ||
// id = null; | ||
// }); | ||
test("should return all records by filter condition", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// test("should return null when exisiting object was deleted", async () => { | ||
// const document = { | ||
// userId: userId, | ||
// name: "should return null when exisiting object was deleted" | ||
// }; | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne({ | ||
_id: ids[0], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[3], | ||
userId: 'another', | ||
name: "should return all records by filter condition" | ||
}); | ||
// id = await repository.create(userId, document); | ||
// await repository.delete(id, userId); | ||
const result = await repository.search({ | ||
userId: userId | ||
}); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(3); | ||
}); | ||
// const result = await repository.getById(id); | ||
test("should return empty list if no records found", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// expect(result).toBeNull(); | ||
// }); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne({ | ||
_id: ids[0], | ||
userId: userId, | ||
name: "should return empty list if no records found" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return empty list if no records found" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return empty list if no records found" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[3], | ||
userId: 'another', | ||
name: "should return empty list if no records found" | ||
}); | ||
// describe("search", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var ids = []; | ||
const result = await repository.search({ | ||
userId: 'no records found' | ||
}); | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(0); | ||
}); | ||
// 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 return requested fields only if projection is set", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// test("should return all records by filter condition", async () => { | ||
// ids.push(uuidv4()); | ||
// 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 return requested fields only if projection is set", | ||
array: [1, 2, 3, 4, 5] | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return requested fields only if projection is set", | ||
array: [1, 2, 3, 4, 5] | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return requested fields only if projection is set", | ||
array: [1, 2, 3, 4, 5] | ||
}); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[3], | ||
// userId: 'another', | ||
// name: "should return all records by filter condition" | ||
// }); | ||
const result = await repository.search( | ||
{ | ||
userId: userId | ||
}, | ||
100, | ||
{ | ||
array: 0 | ||
}); | ||
// const result = await repository.search({ | ||
// userId: userId | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(3); | ||
expect(result[0].userId).toBe(userId); | ||
expect(result[0].name).toBe("should return requested fields only if projection is set"); | ||
expect(result[0].id).toBeDefined(); | ||
expect(result[0].array).toBeUndefined(); | ||
expect(result[1].array).toBeUndefined(); | ||
expect(result[2].array).toBeUndefined(); | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(3); | ||
// }); | ||
test("should not return deleted records ", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// test("should return empty list if no records found", async () => { | ||
// ids.push(uuidv4()); | ||
// 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 return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[3], | ||
userId: userId, | ||
name: "should return all records by filter condition", | ||
deletedAt: new Date() | ||
}); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return empty list if no records found" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return empty list if no records found" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return empty list if no records found" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[3], | ||
// userId: 'another', | ||
// name: "should return empty list if no records found" | ||
// }); | ||
const result = await repository.search({ | ||
userId: userId | ||
}); | ||
// const result = await repository.search({ | ||
// userId: 'no records found' | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(3); | ||
}); | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(0); | ||
// }); | ||
describe("upsert", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// test("should return requested fields only if projection is set", async () => { | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await repository.init(); | ||
await client.connect(); | ||
}); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return requested fields only if projection is set", | ||
// array: [1, 2, 3, 4, 5] | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return requested fields only if projection is set", | ||
// array: [1, 2, 3, 4, 5] | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return requested fields only if projection is set", | ||
// array: [1, 2, 3, 4, 5] | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// const result = await repository.search( | ||
// { | ||
// userId: userId | ||
// }, | ||
// 100, | ||
// { | ||
// array: 0 | ||
// }); | ||
test("should update", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should update" | ||
}; | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(3); | ||
// expect(result[0].userId).toBe(userId); | ||
// expect(result[0].name).toBe("should return requested fields only if projection is set"); | ||
// expect(result[0].id).toBeDefined(); | ||
// expect(result[0].array).toBeUndefined(); | ||
// expect(result[1].array).toBeUndefined(); | ||
// expect(result[2].array).toBeUndefined(); | ||
// }); | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
// test("should not return deleted records ", async () => { | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
document.name = expectedName; | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[3], | ||
// userId: userId, | ||
// name: "should return all records by filter condition", | ||
// deletedAt: new Date() | ||
// }); | ||
await repository.upsert(id, userId, document); | ||
// const result = await repository.search({ | ||
// userId: userId | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].name).toBe(expectedName); | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(3); | ||
// }); | ||
// }); | ||
test("should set updatedAt", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should set updatedAt" | ||
}; | ||
// describe("upsert", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await repository.init(); | ||
// await client.connect(); | ||
// }); | ||
document.name = expectedName; | ||
document.createdAt = new Date(2019, 10, 10); | ||
document.updatedAt = new Date(2019, 10, 10); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
await repository.upsert(id, userId, document); | ||
// test("should update", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should update" | ||
// }; | ||
var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
test("should create new object if not found", async () => { | ||
const document = { | ||
name: "should not update" | ||
}; | ||
// document.name = expectedName; | ||
id = uuidv4(); | ||
// await repository.upsert(id, userId, document); | ||
await repository.upsert(id, userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].name).toBe(expectedName); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].name).toBe(document.name); | ||
}); | ||
}); | ||
// test("should set updatedAt", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should set updatedAt" | ||
// }; | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
// document.name = expectedName; | ||
// document.createdAt = new Date(2019, 10, 10); | ||
// document.updatedAt = new Date(2019, 10, 10); | ||
// await repository.upsert(id, userId, document); | ||
// var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
// test("should create new object if not found", async () => { | ||
// const document = { | ||
// name: "should not update" | ||
// }; | ||
// id = uuidv4(); | ||
// await repository.upsert(id, userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].name).toBe(document.name); | ||
// }); | ||
// }); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
31257
801
1