Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

280 add documentdb interoperability #359

Merged
merged 16 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions app/repository/_base.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,29 @@ class BaseRepository extends AbstractRepository {
aggregation.push(match);
}

const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}
aggregation.push(facet);

// Retrieve the documents
return await this.model.aggregate(aggregation).exec();
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];
} catch (err) {
throw new DatabaseError(err);
}
Expand Down
27 changes: 16 additions & 11 deletions app/repository/assets-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,29 @@ class AssetsRepository extends BaseRepository {
aggregation.push(match);
}

const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}
aggregation.push(facet);

// Retrieve the documents
return await this.model.aggregate(aggregation).exec();
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];
} catch (err) {
throw new DatabaseError(err);
}
Expand Down
27 changes: 16 additions & 11 deletions app/repository/attack-objects-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,29 @@ class AttackObjectsRepository extends BaseRepository {
aggregation.push(match);
}

const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}
aggregation.push(facet);

// Retrieve the documents
return await this.model.aggregate(aggregation).exec();
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];
}
}

Expand Down
27 changes: 16 additions & 11 deletions app/repository/notes-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,29 @@ class NotesRepository extends BaseRepository {
aggregation.push(match);
}

const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}
aggregation.push(facet);

// Retrieve the documents
return await this.model.aggregate(aggregation).exec();
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];
} catch (err) {
throw new DatabaseError(err);
}
Expand Down
27 changes: 16 additions & 11 deletions app/repository/references-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,29 @@ class ReferencesRepository {
aggregation.push({ $sort: { source_name: 1 } });
aggregation.push({ $match: query });

const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}
aggregation.push(facet);

// Retrieve the documents
return await this.model.aggregate(aggregation).exec();
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];
}

async save(data) {
Expand Down
63 changes: 39 additions & 24 deletions app/repository/teams-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,32 @@ class TeamsRepository {
this.model = model;
}

retrieveByUserId(userAccountId, options) {
const aggregation = [
{ $sort: { name: 1 } },
{ $match: { userIDs: { $in: [userAccountId] } } },
async retrieveByUserId(userAccountId, options) {
const aggregation = [{ $sort: { name: 1 } }, { $match: { userIDs: { $in: [userAccountId] } } }];

// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
aggregation.push({ $skip: options.offset });
} else {
aggregation.push({ $skip: 0 });
}

if (options.limit) {
aggregation.push({ $limit: options.limit });
}

// Retrieve the documents
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [
{ $skip: options.offset || 0 },
...(options.limit ? [{ $limit: options.limit }] : []),
],
},
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];

return this.model.aggregate(aggregation).exec();
}

async retrieveAll(options) {
Expand All @@ -50,24 +60,29 @@ class TeamsRepository {
aggregation.push(match);
}

const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}
aggregation.push(facet);

// Retrieve the documents
return await this.model.aggregate(aggregation);
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];
}

async retrieveById(teamId) {
Expand Down
26 changes: 14 additions & 12 deletions app/repository/user-accounts-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,29 @@ class UserAccountsRepository {
aggregation.push(match);
}

const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await this.model.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}

aggregation.push(facet);

// Retrieve the documents
return await this.model.aggregate(aggregation).exec();
const documents = await this.model.aggregate(aggregation).exec();

// Return data in the format previously given by $facet
return [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];
} catch (err) {
throw new DatabaseError(err);
}
Expand Down
35 changes: 19 additions & 16 deletions app/services/teams-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,30 @@ class TeamsService {
};
aggregation.push(match);
}
const facet = {
$facet: {
totalCount: [{ $count: 'totalCount' }],
documents: [],
},
};
// Get the total count of documents, pre-limit
const totalCount = await UserAccount.aggregate(aggregation).count('totalCount').exec();

if (options.offset) {
facet.$facet.documents.push({ $skip: options.offset });
aggregation.push({ $skip: options.offset });
} else {
facet.$facet.documents.push({ $skip: 0 });
aggregation.push({ $skip: 0 });
}

if (options.limit) {
facet.$facet.documents.push({ $limit: options.limit });
aggregation.push({ $limit: options.limit });
}
aggregation.push(facet);

// Retrieve the documents
const documents = await UserAccount.aggregate(aggregation).exec();

const results = [
{
totalCount: [{ totalCount: totalCount[0]?.totalCount || 0 }],
documents: documents,
},
];

try {
const results = await UserAccount.aggregate(aggregation);
const userAccounts = results[0].documents;
userAccounts.forEach((userAccount) => {
userAccountsService.constructor.addEffectiveRole(userAccount);
Expand All @@ -145,13 +152,9 @@ class TeamsService {
});

if (options.includePagination) {
let derivedTotalCount = 0;
if (results[0].totalCount.length > 0) {
derivedTotalCount = results[0].totalCount[0].totalCount;
}
const returnValue = {
pagination: {
total: derivedTotalCount,
total: results[0].totalCount[0].totalCount,
offset: options.offset,
limit: options.limit,
},
Expand Down
Loading