Skip to content

Commit df135f3

Browse files
committed
fix(user-data): make create and update return type to be boolean, like queries
1 parent 51ab01e commit df135f3

File tree

4 files changed

+69
-55
lines changed

4 files changed

+69
-55
lines changed

packages/compass-user-data/src/user-data.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -404,26 +404,9 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
404404
data: Partial<z.input<T>>
405405
): Promise<boolean> {
406406
try {
407-
// TODO: change this depending on whether or not updateAttributes can provide all current data
408-
const getResponse = await this.authenticatedFetch(
409-
await this.getResourceUrl(
410-
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
411-
),
412-
{
413-
method: 'GET',
414-
}
415-
);
416-
if (!getResponse.ok) {
417-
throw new Error(
418-
`Failed to fetch data: ${getResponse.status} ${getResponse.statusText}`
419-
);
420-
}
421-
const prevData = await getResponse.json();
422-
const validPrevData = this.validator.parse(
423-
this.deserialize(prevData.data as string)
424-
);
407+
const prevData = await this.readOne(id);
425408
const newData: z.input<T> = {
426-
...validPrevData,
409+
...prevData,
427410
...data,
428411
};
429412

@@ -460,4 +443,39 @@ export class AtlasUserData<T extends z.Schema> extends IUserData<T> {
460443
return false;
461444
}
462445
}
446+
447+
// TODO: change this depending on whether or not updateAttributes can provide all current data
448+
async readOne(id: string): Promise<z.output<T>> {
449+
try {
450+
const getResponse = await this.authenticatedFetch(
451+
await this.getResourceUrl(
452+
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
453+
),
454+
{
455+
method: 'GET',
456+
}
457+
);
458+
if (!getResponse.ok) {
459+
throw new Error(
460+
`Failed to fetch data: ${getResponse.status} ${getResponse.statusText}`
461+
);
462+
}
463+
const json = await getResponse.json();
464+
const data = this.validator.parse(this.deserialize(json.data as string));
465+
return data;
466+
} catch {
467+
log.error(
468+
mongoLogId(1_001_000_365),
469+
'Atlas Backend',
470+
'Error reading data',
471+
{
472+
url: await this.getResourceUrl(
473+
`${this.dataType}/${this.orgId}/${this.projectId}/${id}`
474+
),
475+
error: (error as Error).message,
476+
}
477+
);
478+
return null;
479+
}
480+
}
463481
}

packages/my-queries-storage/src/compass-pipeline-storage.spec.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,12 @@ describe('CompassPipelineStorage', function () {
8787
expect((e as any).code).to.equal('ENOENT');
8888
}
8989

90-
const pipeline = await pipelineStorage.createOrUpdate(data.id, data);
90+
const result = await pipelineStorage.createOrUpdate(data.id, data);
9191

9292
// Verify the file exists
9393
await fs.access(await getEnsuredFilePath(tmpDir, data.id));
9494

95-
expect(pipeline.id).to.equal(data.id);
96-
expect(pipeline.name).to.equal(data.name);
97-
expect(pipeline.pipelineText).to.equal(data.pipelineText);
95+
expect(result).to.be.true;
9896
});
9997

10098
it('createOrUpdate - updates a pipeline if it exists', async function () {
@@ -108,14 +106,12 @@ describe('CompassPipelineStorage', function () {
108106
await createPipeline(tmpDir, data);
109107
await fs.access(await getEnsuredFilePath(tmpDir, data.id));
110108

111-
const pipeline = await pipelineStorage.createOrUpdate(data.id, {
109+
const result = await pipelineStorage.createOrUpdate(data.id, {
112110
...data,
113111
name: 'modified listings',
114112
});
115113

116-
expect(pipeline.id).to.equal(data.id);
117-
expect(pipeline.name).to.equal('modified listings');
118-
expect(pipeline.pipelineText).to.equal(data.pipelineText);
114+
expect(result).to.be.true;
119115
});
120116

121117
it('updateAttributes - updates a pipeline if it exists', async function () {
@@ -136,17 +132,13 @@ describe('CompassPipelineStorage', function () {
136132
expect(restOfAggregation).to.deep.equal(data);
137133
}
138134
// eslint-disable-next-line @typescript-eslint/no-unused-vars
139-
const { lastModified, pipelineText, ...updatedAggregation } =
140-
await pipelineStorage.updateAttributes(data.id, {
141-
name: 'updated',
142-
namespace: 'airbnb.users',
143-
});
144-
145-
expect(updatedAggregation, 'returns updated pipeline').to.deep.equal({
146-
...data,
135+
const result = await pipelineStorage.updateAttributes(data.id, {
147136
name: 'updated',
137+
namespace: 'airbnb.users',
148138
});
149139

140+
expect(result).to.be.true;
141+
150142
{
151143
const aggregations = await pipelineStorage.loadAll();
152144
expect(aggregations).to.have.length(1);

packages/my-queries-storage/src/compass-pipeline-storage.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ export class CompassPipelineStorage implements PipelineStorage {
2727
return this.loadAll().then((pipelines) => pipelines.filter(predicate));
2828
}
2929

30-
private async loadOne(id: string): Promise<SavedPipeline> {
31-
return await this.userData.readOne(id);
32-
}
33-
3430
async createOrUpdate(
3531
id: string,
3632
attributes: Omit<SavedPipeline, 'lastModified'>
@@ -41,24 +37,32 @@ export class CompassPipelineStorage implements PipelineStorage {
4137
: this.create(attributes));
4238
}
4339

44-
async create(data: Omit<SavedPipeline, 'lastModified'>) {
45-
await this.userData.write(data.id, {
46-
...data,
47-
lastModified: Date.now(),
48-
});
49-
return await this.loadOne(data.id);
40+
async create(data: Omit<SavedPipeline, 'lastModified'>): Promise<boolean> {
41+
try {
42+
await this.userData.write(data.id, {
43+
...data,
44+
lastModified: Date.now(),
45+
});
46+
return true;
47+
} catch {
48+
return false;
49+
}
5050
}
5151

5252
async updateAttributes(
5353
id: string,
5454
attributes: Partial<SavedPipeline>
55-
): Promise<SavedPipeline> {
56-
await this.userData.write(id, {
57-
...(await this.loadOne(id)),
58-
...attributes,
59-
lastModified: Date.now(),
60-
});
61-
return await this.loadOne(id);
55+
): Promise<boolean> {
56+
try {
57+
await this.userData.write(id, {
58+
...(await this.userData.readOne(id)),
59+
...attributes,
60+
lastModified: Date.now(),
61+
});
62+
return true;
63+
} catch {
64+
return false;
65+
}
6266
}
6367

6468
async delete(id: string) {

packages/my-queries-storage/src/pipeline-storage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ export interface PipelineStorage {
55
loadMany(
66
predicate: (arg0: SavedPipeline) => boolean
77
): Promise<SavedPipeline[]>;
8-
createOrUpdate(id: string, attributes: SavedPipeline): Promise<SavedPipeline>;
9-
create(attributes: SavedPipeline): Promise<SavedPipeline>;
8+
createOrUpdate(id: string, attributes: SavedPipeline): Promise<boolean>;
9+
create(attributes: SavedPipeline): Promise<boolean>;
1010
updateAttributes(
1111
id: string,
1212
attributes: Partial<SavedPipeline>
13-
): Promise<SavedPipeline>;
13+
): Promise<boolean>;
1414
delete(id: string): Promise<void>;
1515
}

0 commit comments

Comments
 (0)