Skip to content

Commit 00526bf

Browse files
authored
chore(data-service): promisify all find* methods COMPASS-6620 (#4261)
chore(data-service): promisify all find* methods
1 parent c452f0b commit 00526bf

File tree

6 files changed

+118
-191
lines changed

6 files changed

+118
-191
lines changed

packages/compass-crud/src/stores/crud-store.spec.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ describe('store', function () {
653653
beforeEach(function () {
654654
sinon
655655
.stub(dataService, 'findOneAndUpdate')
656-
.yields({ message: 'error happened' });
656+
.throws({ message: 'error happened' });
657657
});
658658

659659
it('sets the error for the document', function (done) {
@@ -676,7 +676,7 @@ describe('store', function () {
676676
hadronDoc.elements.at(1).rename('new name');
677677
sinon
678678
.stub(dataService, 'findOneAndUpdate')
679-
.yields({ message: 'error happened' });
679+
.throws({ message: 'error happened' });
680680
});
681681

682682
it('sets the error for the document', function (done) {
@@ -695,7 +695,7 @@ describe('store', function () {
695695

696696
beforeEach(function () {
697697
hadronDoc.elements.at(1).rename('new name');
698-
sinon.stub(dataService, 'findOneAndUpdate').yields(null, null);
698+
sinon.stub(dataService, 'findOneAndUpdate').resolves(null);
699699
});
700700

701701
it('sets the update blocked for the document', function (done) {
@@ -714,7 +714,7 @@ describe('store', function () {
714714

715715
beforeEach(function () {
716716
hadronDoc.get('name').edit('Desert Sand');
717-
stub = sinon.stub(dataService, 'findOneAndUpdate').yields(null, {});
717+
stub = sinon.stub(dataService, 'findOneAndUpdate').resolves({});
718718
});
719719

720720
it('has the original value for the edited value in the query', async function () {
@@ -742,7 +742,7 @@ describe('store', function () {
742742
beforeEach(function () {
743743
store.state.shardKeys = { yes: 1 };
744744
hadronDoc.get('name').edit('Desert Sand');
745-
stub = sinon.stub(dataService, 'findOneAndUpdate').yields(null, {});
745+
stub = sinon.stub(dataService, 'findOneAndUpdate').resolves({});
746746
});
747747

748748
afterEach(function () {
@@ -797,10 +797,10 @@ describe('store', function () {
797797
hadronDoc.get('name').edit('Desert Sand');
798798
findOneAndReplaceStub = sinon
799799
.stub(dataService, 'findOneAndReplace')
800-
.yields(null, {});
800+
.resolves({});
801801
findOneAndUpdateStub = sinon
802802
.stub(dataService, 'findOneAndUpdate')
803-
.yields(null, {});
803+
.resolves({});
804804
isUpdateAllowedStub = sinon.stub().resolves(false);
805805
sinon.stub(dataService, 'getCSFLEMode').returns('enabled');
806806
sinon
@@ -869,7 +869,7 @@ describe('store', function () {
869869
beforeEach(function () {
870870
sinon
871871
.stub(dataService, 'findOneAndReplace')
872-
.yields({ message: 'error happened' });
872+
.rejects({ message: 'error happened' });
873873
});
874874

875875
it('sets the error for the document', function (done) {
@@ -889,7 +889,7 @@ describe('store', function () {
889889

890890
beforeEach(function () {
891891
hadronDoc.get('name').edit('Desert Sand');
892-
stub = sinon.stub(dataService, 'findOneAndReplace').yields(null, {});
892+
stub = sinon.stub(dataService, 'findOneAndReplace').resolves({});
893893
});
894894

895895
it('has the original value for the edited value in the query', async function () {
@@ -912,7 +912,7 @@ describe('store', function () {
912912
beforeEach(function () {
913913
store.state.shardKeys = { yes: 1 };
914914
hadronDoc.get('name').edit('Desert Sand');
915-
stub = sinon.stub(dataService, 'findOneAndReplace').yields(null, {});
915+
stub = sinon.stub(dataService, 'findOneAndReplace').resolves({});
916916
});
917917

918918
afterEach(function () {
@@ -948,10 +948,10 @@ describe('store', function () {
948948
hadronDoc.get('name').edit('Desert Sand');
949949
findOneAndReplaceStub = sinon
950950
.stub(dataService, 'findOneAndReplace')
951-
.yields(null, {});
951+
.resolves({});
952952
findOneAndUpdateStub = sinon
953953
.stub(dataService, 'findOneAndUpdate')
954-
.yields(null, {});
954+
.resolves({});
955955
isUpdateAllowedStub = sinon.stub().resolves(false);
956956
sinon.stub(dataService, 'getCSFLEMode').returns('enabled');
957957
sinon
@@ -2066,8 +2066,8 @@ describe('store', function () {
20662066

20672067
it('does the original findAndModify operation and nothing more if it succeeds', async function () {
20682068
const document = { _id: 1234 };
2069-
const stub = sinon.stub().callsFake((ds, ns, opts, cb) => {
2070-
cb(undefined, document);
2069+
const stub = sinon.stub().callsFake(() => {
2070+
return [undefined, document];
20712071
});
20722072
const [error, d] = await findAndModifyWithFLEFallback(
20732073
dataServiceStub,
@@ -2087,8 +2087,8 @@ describe('store', function () {
20872087

20882088
it('does the original findAndModify operation and nothing more if it fails with a non-FLE error', async function () {
20892089
const err = new Error('failed');
2090-
const stub = sinon.stub().callsFake((ds, ns, opts, cb) => {
2091-
cb(err);
2090+
const stub = sinon.stub().callsFake(() => {
2091+
return [err];
20922092
});
20932093
const [error, d] = await findAndModifyWithFLEFallback(
20942094
dataServiceStub,
@@ -2110,11 +2110,11 @@ describe('store', function () {
21102110
const document = { _id: 1234 };
21112111
const err = Object.assign(new Error('failed'), { code: 6371402 });
21122112
const stub = sinon.stub();
2113-
stub.onFirstCall().callsFake((ds, ns, opts, cb) => {
2114-
cb(err);
2113+
stub.onFirstCall().callsFake(() => {
2114+
return [err];
21152115
});
2116-
stub.onSecondCall().callsFake((ds, ns, opts, cb) => {
2117-
cb(undefined, document);
2116+
stub.onSecondCall().callsFake(() => {
2117+
return [undefined, document];
21182118
});
21192119
const [error, d] = await findAndModifyWithFLEFallback(
21202120
dataServiceStub,
@@ -2150,11 +2150,11 @@ describe('store', function () {
21502150
const document = { _id: 1234 };
21512151
const err = Object.assign(new Error('failed'), { code: 6371402 });
21522152
const stub = sinon.stub();
2153-
stub.onFirstCall().callsFake((ds, ns, opts, cb) => {
2154-
cb(err);
2153+
stub.onFirstCall().callsFake(() => {
2154+
return [err];
21552155
});
2156-
stub.onSecondCall().callsFake((ds, ns, opts, cb) => {
2157-
cb(undefined, document);
2156+
stub.onSecondCall().callsFake(() => {
2157+
return [undefined, document];
21582158
});
21592159
const [error, d] = await findAndModifyWithFLEFallback(
21602160
dataServiceStub,

packages/compass-crud/src/stores/crud-store.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,15 @@ class CrudStoreImpl
634634
const [error, d] = await findAndModifyWithFLEFallback(
635635
this.dataService,
636636
this.state.ns,
637-
(ds, ns, opts, cb) => {
638-
ds.findOneAndUpdate(ns, query, updateDoc, opts, cb);
637+
async (ds, ns, opts) => {
638+
try {
639+
return [
640+
undefined,
641+
await ds.findOneAndUpdate(ns, query, updateDoc, opts),
642+
] as ErrorOrResult;
643+
} catch (error) {
644+
return [error, undefined] as ErrorOrResult;
645+
}
639646
}
640647
);
641648

@@ -732,12 +739,18 @@ class CrudStoreImpl
732739
);
733740
debug('Performing findOneAndReplace', { query, object });
734741

735-
// eslint-disable-next-line no-shadow
736742
const [error, d] = await findAndModifyWithFLEFallback(
737743
this.dataService,
738744
this.state.ns,
739-
(ds, ns, opts, cb) => {
740-
ds.findOneAndReplace(ns, query, object, opts, cb);
745+
async (ds, ns, opts) => {
746+
try {
747+
return [
748+
undefined,
749+
await ds.findOneAndReplace(ns, query, object, opts),
750+
] as ErrorOrResult;
751+
} catch (error) {
752+
return [error, undefined] as ErrorOrResult;
753+
}
741754
}
742755
);
743756
if (error) {
@@ -1575,14 +1588,12 @@ export async function findAndModifyWithFLEFallback(
15751588
doFindAndModify: (
15761589
ds: DataService,
15771590
ns: string,
1578-
opts: { returnDocument: 'before' | 'after'; promoteValues: false },
1579-
cb: (error: { message: string } | undefined | null, doc: BSONObject) => void
1580-
) => void
1591+
opts: { returnDocument: 'before' | 'after'; promoteValues: false }
1592+
) => Promise<ErrorOrResult>
15811593
): Promise<ErrorOrResult> {
15821594
const opts = { returnDocument: 'after', promoteValues: false } as const;
1583-
let [error, d] = await new Promise<ErrorOrResult>((resolve) => {
1584-
doFindAndModify(ds, ns, opts, (...cbArgs) => resolve(cbArgs as any));
1585-
});
1595+
1596+
let [error, d] = await doFindAndModify(ds, ns, opts);
15861597
const originalError = error;
15871598

15881599
// 6371402 is "'findAndModify with encryption only supports new: false'"
@@ -1592,9 +1603,7 @@ export async function findAndModifyWithFLEFallback(
15921603
returnDocument: 'before',
15931604
promoteValues: false,
15941605
} as const;
1595-
[error, d] = await new Promise((resolve) => {
1596-
doFindAndModify(ds, ns, fallbackOpts, (...cbArgs) => resolve(cbArgs));
1597-
});
1606+
[error, d] = await doFindAndModify(ds, ns, fallbackOpts);
15981607

15991608
if (!error) {
16001609
let docs;

packages/compass-crud/test/aggrid-helper.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -92,39 +92,6 @@ export const getContext = function (path) {
9292
};
9393
};
9494

95-
export const getDataService = function (done) {
96-
const foarSpy = sinon.spy();
97-
const foauSpy = sinon.spy();
98-
const iSpy = sinon.spy();
99-
const dSpy = sinon.spy();
100-
return {
101-
foarSpy: foarSpy,
102-
findOneAndReplace: (ns, filter, obj, prefs, handleResult) => {
103-
foarSpy(filter, obj);
104-
handleResult(null, obj);
105-
done();
106-
},
107-
foauSpy: foauSpy,
108-
findOneAndUpdate: (ns, filter, obj, prefs, handleResult) => {
109-
foauSpy(filter, obj);
110-
handleResult(null, obj);
111-
done();
112-
},
113-
iSpy: iSpy,
114-
insertOne: (ns, obj, prefs, handleResult) => {
115-
iSpy(obj);
116-
handleResult(null);
117-
done();
118-
},
119-
dSpy: dSpy,
120-
deleteOne: (ns, filter, prefs, handleResult) => {
121-
dSpy(filter);
122-
handleResult(null, 1);
123-
done();
124-
},
125-
};
126-
};
127-
12895
export const checkPageRange = function (
12996
error: any,
13097
documents: any[],
@@ -184,7 +151,6 @@ export default {
184151
getActions: getActions,
185152
getColumnApi: getColumnApi,
186153
getContext: getContext,
187-
getDataService: getDataService,
188154
notCalledExcept: notCalledExcept,
189155
NUM_DOCS: NUM_DOCS,
190156
expectedDocs: expectedDocs,

packages/data-service/src/csfle-collection-tracker.spec.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -446,15 +446,16 @@ describe('CSFLECollectionTracker', function () {
446446

447447
it('ensures that writes fail when server validation has been removed in the background', async function () {
448448
//await metadataClient.db(dbName).collection('test2').insertOne({ a: 1 });
449-
const err = await new Promise<{ message: string }>((resolve) => {
450-
dataService.findOneAndUpdate(
449+
let err;
450+
try {
451+
await dataService.findOneAndUpdate(
451452
`${dbName}.test2`,
452453
{},
453-
{ $set: { a: '2' } },
454-
{},
455-
resolve
454+
{ $set: { a: '2' } }
456455
);
457-
});
456+
} catch (error) {
457+
err = error;
458+
}
458459
expect(err).to.be.instanceOf(Error);
459460
expect(err.message).to.match(
460461
/\[Compass\] Missing encrypted field information of collection/
@@ -470,15 +471,16 @@ describe('CSFLECollectionTracker', function () {
470471
fields: [{ path: 'b', keyId: SOME_UUID1, bsonType: 'string' }],
471472
},
472473
});
473-
const err = await new Promise<{ message: string }>((resolve) => {
474-
dataService.findOneAndUpdate(
474+
let err;
475+
try {
476+
await dataService.findOneAndUpdate(
475477
`${dbName}.test2`,
476478
{},
477-
{ $set: { a: '2' } },
478-
{},
479-
resolve
479+
{ $set: { a: '2' } }
480480
);
481-
});
481+
} catch (error) {
482+
err = error;
483+
}
482484
expect(err).to.be.instanceOf(Error);
483485
expect(err.message).to.match(
484486
/\[Compass\] Missing encrypted field 'a' of collection/
@@ -490,15 +492,11 @@ describe('CSFLECollectionTracker', function () {
490492
'CRUD'
491493
);
492494
await crudClient.db(dbName).collection('test4').insertOne({ a: 1 });
493-
await new Promise((resolve) => {
494-
dataService.findOneAndUpdate(
495-
`${dbName}.test4`,
496-
{},
497-
{ $set: { a: 2 } },
498-
{},
499-
resolve
500-
);
501-
});
495+
await dataService.findOneAndUpdate(
496+
`${dbName}.test4`,
497+
{},
498+
{ $set: { a: 2 } }
499+
);
502500
const result = await crudClient
503501
.db(dbName)
504502
.collection('test4')

0 commit comments

Comments
 (0)