Skip to content

Commit cbae2aa

Browse files
committed
socket routes: let saveCollectionCache use the dbToCache functions
The Transition client does not have authority on the collection's data, the source of truth is the database, so it should not save a collection sent by a client. Also, currently, there is no support from the client side to different cache paths, so we drop these 2 parameters from the calls. In the backend, the `saveCollectionCache` socket route now use a function, different for each transit object, coming from the `dbToCache.ts` file, that will both load the data from DB and save it to the cache.
1 parent e2f13c8 commit cbae2aa

12 files changed

Lines changed: 64 additions & 131 deletions

File tree

packages/chaire-lib-common/src/services/dataSource/DataSourceCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class DataSourceCollection extends GenericObjectCollection<DataSource> implement
3737
return new DataSource(attribs, isNew, collectionManager);
3838
}
3939

40-
saveCache(socket, customCollection) {
41-
return CollectionCacheable.saveCache(this, socket, customCollection);
40+
saveCache(socket) {
41+
return CollectionCacheable.saveCache(this, socket);
4242
}
4343

4444
loadCache(socket) {

packages/chaire-lib-common/src/services/objects/CollectionCacheable.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,25 @@ export default {
1919
*
2020
* @param collection The collection to save
2121
* @param socket The socket
22-
* @param customCollection A custom collection of objects. If not set, the
23-
* entire collection will be saved to cache
2422
* @returns
2523
*/
26-
saveCache: async function (collection: GenericCollection<any>, socket, customCollection?) {
24+
saveCache: async function (collection: GenericCollection<any>, socket) {
2725
const socketPrefix = collection.socketPrefix;
2826
return new Promise((resolve, reject) => {
29-
socket.emit(
30-
`${socketPrefix}.saveCollectionCache`,
31-
customCollection,
32-
_get(collection.attributes, 'data.customCachePath'),
33-
(response) => {
34-
if (!response.error) {
35-
resolve(response);
36-
} else {
37-
console.error(response.error);
38-
reject(
39-
new TrError(
40-
`cannot save cache for ${socketPrefix}: ${response.error}`,
41-
'TSC0001',
42-
`${socketPrefix}CacheCouldNotBeSavedBecauseServerError`
43-
)
44-
);
45-
}
27+
socket.emit(`${socketPrefix}.saveCollectionCache`, (response) => {
28+
if (!response.error) {
29+
resolve(response);
30+
} else {
31+
console.error(response.error);
32+
reject(
33+
new TrError(
34+
`cannot save cache for ${socketPrefix}: ${response.error}`,
35+
'TSC0001',
36+
`${socketPrefix}CacheCouldNotBeSavedBecauseServerError`
37+
)
38+
);
4639
}
47-
);
40+
});
4841
});
4942
},
5043

packages/chaire-lib-common/src/services/objects/__tests__/CollectionCacheable.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,14 @@ const collectionCustomPath = new CollectionStub([obj1, obj2, obj3], {data: { cus
4242

4343
test('Save cache', async function() {
4444

45-
socketMock.emit.mockImplementation((socketPath, coll, path, ret) => {
45+
socketMock.emit.mockImplementation((socketPath, ret) => {
4646
ret({});
4747
})
4848

4949
// Save simple collection
5050
socketMock.emit.mockClear();
5151
await CollectionCacheable.saveCache(collection, socketMock);
52-
expect(socketMock.emit).toHaveBeenCalled();
53-
expect(socketMock.emit).toHaveBeenCalledWith('collectionStub.saveCollectionCache', undefined, undefined, expect.anything());
54-
55-
// Save specific collection with custom path
56-
socketMock.emit.mockClear();
57-
const subColl = new CollectionStub([obj1, obj2]);
58-
await CollectionCacheable.saveCache(collectionCustomPath, socketMock, subColl);
59-
expect(socketMock.emit).toHaveBeenCalledWith('collectionStub.saveCollectionCache', subColl, customCachePath, expect.anything());
52+
expect(socketMock.emit).toHaveBeenCalledWith('collectionStub.saveCollectionCache',expect.anything());
6053
});
6154

6255
test('should load a progressable collection and emit progress', async function() {
@@ -68,7 +61,6 @@ test('should load a progressable collection and emit progress', async function()
6861
// Load simple collection
6962
socketMock.emit.mockClear();
7063
let loaded = await CollectionCacheable.loadCache(collection, socketMock);
71-
expect(socketMock.emit).toHaveBeenCalled();
7264
expect(socketMock.emit).toHaveBeenCalledWith('collectionStub.loadCollectionCache', undefined, expect.anything());
7365
expect(loaded).toEqual(collection);
7466

packages/transition-backend/src/api/transitObjects.socketRoutes.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,10 @@ function setupObjectSocketRoutes(socket: EventEmitter) {
105105
}
106106

107107
if (dataHandler.saveCollectionCache) {
108-
socket.on(
109-
`transit${dataHandler.classNamePlural}.saveCollectionCache`,
110-
async (collection = null, customCachePath, callback) => {
111-
const response = await dataHandler.saveCollectionCache!(collection, customCachePath);
112-
callback(response);
113-
}
114-
);
108+
socket.on(`transit${dataHandler.classNamePlural}.saveCollectionCache`, async (callback) => {
109+
const response = await dataHandler.saveCollectionCache!();
110+
callback(response);
111+
});
115112
}
116113

117114
// TODO Do we still need to load an entire collection from cache. Cache should be one-way?

packages/transition-backend/src/services/transitObjects/TransitObjectsDataHandler.ts

Lines changed: 29 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import * as pathsCacheQueries from '../../models/capnpCache/transitPaths.cache.q
3030
import * as scenariosCacheQueries from '../../models/capnpCache/transitScenarios.cache.queries';
3131
import * as servicesCacheQueries from '../../models/capnpCache/transitServices.cache.queries';
3232

33+
import * as dbToCacheQueries from '../capnpCache/dbToCache';
34+
3335
import { GenericAttributes } from 'chaire-lib-common/lib/utils/objects/GenericObject';
3436
import * as Status from 'chaire-lib-common/lib/utils/Status';
3537
import TrError from 'chaire-lib-common/lib/utils/TrError';
@@ -53,7 +55,7 @@ export interface TransitObjectDataHandler {
5355
deleteCache?: (id: string, customCachePath: string | undefined) => Promise<Record<string, any>>;
5456
deleteMultipleCache?: (ids: string[], customCachePath: string) => Promise<Record<string, any>>;
5557
loadCache?: (id: string, customCachePath: string | undefined) => Promise<Record<string, any>>;
56-
saveCollectionCache?: (collection, customCachePath) => Promise<Record<string, any>>;
58+
saveCollectionCache?: () => Promise<Record<string, any>>;
5759
loadCollectionCache?: (customCachePath) => Promise<Record<string, any>>;
5860
updateBatch?: (socket: EventEmitter, attributes: GenericAttributes[]) => Promise<Record<string, any>>;
5961
}
@@ -65,15 +67,17 @@ const transitClassesConfig = {
6567
classNamePlural: 'Agencies',
6668
dbQueries: agenciesDbQueries,
6769
cacheQueries: agenciesCacheQueries,
68-
collection: new AgencyCollection([], {})
70+
collection: new AgencyCollection([], {}),
71+
saveCollectionToCacheFct: dbToCacheQueries.loadAndSaveAgenciesToCache
6972
},
7073
lines: {
7174
lowerCaseName: 'line',
7275
className: 'Line',
7376
classNamePlural: 'Lines',
7477
dbQueries: linesDbQueries,
7578
cacheQueries: linesCacheQueries,
76-
collection: new LineCollection([], {})
79+
collection: new LineCollection([], {}),
80+
saveCollectionToCacheFct: dbToCacheQueries.loadAndSaveLinesToCache
7781
},
7882
nodes: {
7983
lowerCaseName: 'node',
@@ -82,7 +86,8 @@ const transitClassesConfig = {
8286
hasIntegerId: true,
8387
dbQueries: nodesDbQueries,
8488
cacheQueries: nodesCacheQueries,
85-
collection: new NodeCollection([], {})
89+
collection: new NodeCollection([], {}),
90+
saveCollectionToCacheFct: dbToCacheQueries.loadAndSaveNodesToCache
8691
},
8792
paths: {
8893
lowerCaseName: 'path',
@@ -91,23 +96,26 @@ const transitClassesConfig = {
9196
hasIntegerId: true,
9297
dbQueries: pathsDbQueries,
9398
cacheQueries: pathsCacheQueries,
94-
collection: new PathCollection([], {})
99+
collection: new PathCollection([], {}),
100+
saveCollectionToCacheFct: dbToCacheQueries.loadAndSavePathsToCache
95101
},
96102
scenarios: {
97103
lowerCaseName: 'scenario',
98104
className: 'Scenario',
99105
classNamePlural: 'Scenarios',
100106
dbQueries: scenariosDbQueries,
101107
cacheQueries: scenariosCacheQueries,
102-
collection: new ScenarioCollection([], {})
108+
collection: new ScenarioCollection([], {}),
109+
saveCollectionToCacheFct: dbToCacheQueries.loadAndSaveScenariosToCache
103110
},
104111
services: {
105112
lowerCaseName: 'service',
106113
className: 'Service',
107114
classNamePlural: 'Services',
108115
dbQueries: servicesDbQueries,
109116
cacheQueries: servicesCacheQueries,
110-
collection: new ServiceCollection([], {})
117+
collection: new ServiceCollection([], {}),
118+
saveCollectionToCacheFct: dbToCacheQueries.loadAndSaveServicesToCache
111119
},
112120
schedules: {
113121
lowerCaseName: 'schedule',
@@ -359,73 +367,20 @@ function createDataHandlers(): Record<string, TransitObjectDataHandler> {
359367
};
360368
}
361369

362-
if (transitClassConfig.cacheQueries.collectionToCache) {
363-
dataHandler.saveCollectionCache = async (collection = null, customCachePath) => {
364-
if (collection) {
365-
try {
366-
await transitClassConfig.cacheQueries.collectionToCache(collection, customCachePath);
367-
return {
368-
error: null
369-
};
370-
} catch (error) {
371-
throw new TrError(
372-
`cannot save cache collection file ${transitClassConfig.classNamePlural} because of an error: ${error}`,
373-
'SKTTRSGC0001',
374-
'CacheCouldNotBeSavedBecauseError'
375-
);
376-
}
377-
} else if (transitClassConfig.dbQueries.geojsonCollection) {
378-
try {
379-
const collection = await transitClassConfig.dbQueries.geojsonCollection();
380-
transitClassConfig.collection.loadFromCollection(collection.features);
381-
try {
382-
await transitClassConfig.cacheQueries.collectionToCache(
383-
transitClassConfig.collection,
384-
customCachePath
385-
);
386-
return {
387-
error: null
388-
};
389-
} catch (error) {
390-
throw new TrError(
391-
`cannot save cache collection file ${transitClassConfig.classNamePlural} because of an error: ${error}`,
392-
'SKTTRSGC0002',
393-
'CacheCouldNotBeSavedBecauseError'
394-
);
395-
}
396-
} catch (error) {
397-
throw new TrError(
398-
`cannot save cache collection file ${transitClassConfig.classNamePlural} because of an error: ${error}`,
399-
'SKTTRSGC0003',
400-
'CacheCouldNotBeSavedBecauseError'
401-
);
402-
}
403-
} else {
404-
try {
405-
const collection = await transitClassConfig.dbQueries.collection();
406-
transitClassConfig.collection.loadFromCollection(collection);
407-
try {
408-
await transitClassConfig.cacheQueries.collectionToCache(
409-
transitClassConfig.collection,
410-
customCachePath
411-
);
412-
return {
413-
error: null
414-
};
415-
} catch (error) {
416-
throw new TrError(
417-
`cannot save cache collection file ${transitClassConfig.classNamePlural} because of an error: ${error}`,
418-
'SKTTRSC0001',
419-
'CacheCouldNotBeSavedBecauseError'
420-
);
421-
}
422-
} catch (error) {
423-
throw new TrError(
424-
`cannot save cache collection file ${transitClassConfig.classNamePlural} because of an error: ${error}`,
425-
'SKTTRSC0002',
426-
'CacheCouldNotBeSavedBecauseError'
427-
);
428-
}
370+
if (transitClassConfig.saveCollectionToCacheFct) {
371+
dataHandler.saveCollectionCache = async () => {
372+
try {
373+
await transitClassConfig.saveCollectionToCacheFct!();
374+
return {
375+
error: null
376+
};
377+
} catch (error) {
378+
console.error(error);
379+
return new TrError(
380+
`cannot save cache collection file ${transitClassConfig.classNamePlural} because of an error: ${error}`,
381+
'SKTTRSGC0001',
382+
'CacheCouldNotBeSavedBecauseError'
383+
).export();
429384
}
430385
};
431386
}

packages/transition-common/src/services/agency/AgencyCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class AgencyCollection extends GenericObjectCollection<Agency> implements Progre
5858
}
5959

6060
// TODO Have this collection and others implement some interface from which to get those methods
61-
saveCache(socket, customCollection) {
62-
return CollectionCacheable.saveCache(this, socket, customCollection);
61+
saveCache(socket) {
62+
return CollectionCacheable.saveCache(this, socket);
6363
}
6464

6565
loadCache(socket) {

packages/transition-common/src/services/line/LineCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ class LineCollection extends GenericObjectCollection<Line> implements Progressab
8383
return new Line(attribs, isNew, collectionManager);
8484
}
8585

86-
saveCache(socket, customCollection) {
87-
return CollectionCacheable.saveCache(this, socket, customCollection);
86+
saveCache(socket) {
87+
return CollectionCacheable.saveCache(this, socket);
8888
}
8989

9090
loadCache(socket) {

packages/transition-common/src/services/nodes/NodeCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ export class NodeCollection extends GenericPlaceCollection<NodeAttributes, Node>
180180
return this.pointsInWalkingTravelTimeRadiusSecondsAround(geometry, maxWalkingTravelTimeRadiusSeconds);
181181
}
182182

183-
saveCache(socket, customCollection) {
184-
return CollectionCacheable.saveCache(this, socket, customCollection);
183+
saveCache(socket) {
184+
return CollectionCacheable.saveCache(this, socket);
185185
}
186186

187187
loadCache(socket) {

packages/transition-common/src/services/path/PathCollection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export class PathCollection
4545
);
4646
}
4747

48-
saveCache(socket, customCollection?: any) {
49-
return CollectionCacheable.saveCache(this, socket, customCollection);
48+
saveCache(socket) {
49+
return CollectionCacheable.saveCache(this, socket);
5050
}
5151

5252
loadCache(socket) {

packages/transition-common/src/services/path/__tests__/PathCollection.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,7 @@ test('Save path collection to cache', async () => {
146146
const collection = new PathCollection([], {}, eventManager);
147147
await collection.saveCache(eventManager);
148148
expect(eventManager.emit).toHaveBeenCalled();
149-
expect(eventManager.emit).toHaveBeenCalledWith('transitPaths.saveCollectionCache', undefined, undefined, expect.anything());
150-
151-
await collection.saveCache(eventManager, [path1Geojson, path2Geojson]);
152-
expect(eventManager.emit).toHaveBeenCalled();
153-
expect(eventManager.emit).toHaveBeenCalledWith('transitPaths.saveCollectionCache', [path1Geojson, path2Geojson], undefined, expect.anything());
149+
expect(eventManager.emit).toHaveBeenCalledWith('transitPaths.saveCollectionCache', expect.anything());
154150
});
155151

156152
test('Load path collection from server', async () => {

0 commit comments

Comments
 (0)