refactor(storage): Upstream-Aufrufstellen auf getStorage().brews/notifications umgestellt (ADR 0002 §4)
Mechanischer Ersatz der Datenzugriffe, Wartungsjobs unter Postgres mit 501. Specs minimal angepasst (_id -> id, Mocks). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a55c403527
commit
23d9b83567
8 files changed
+119
-243
No files matched your search
+10
-60
@@ -1,36 +1,18 @@
|
||||
import express from 'express';
|
||||
import asyncHandler from 'express-async-handler';
|
||||
import { model as HomebrewModel } from './homebrew.model.js';
|
||||
import { getStorage } from './storage/index.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
const titleConditions = (title)=>{
|
||||
if(!title) return {};
|
||||
return {
|
||||
$text : {
|
||||
$search : title,
|
||||
$caseSensitive : false,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const authorConditions = (author)=>{
|
||||
if(!author) return {};
|
||||
return { authors: author };
|
||||
};
|
||||
|
||||
const rendererConditions = (legacy, v3)=>{
|
||||
// [fork] ADR-0002: Die Abfrage selbst liegt im BrewRepository (Mongo: $text, Postgres: tsvector).
|
||||
const rendererFilter = (legacy, v3)=>{
|
||||
if(legacy === 'true' && v3 !== 'true')
|
||||
return { renderer: 'legacy' };
|
||||
return 'legacy';
|
||||
|
||||
if(v3 === 'true' && legacy !== 'true')
|
||||
return { renderer: 'V3' };
|
||||
return 'V3';
|
||||
|
||||
return {}; // If all renderers selected, renderer field not needed in query for speed
|
||||
};
|
||||
|
||||
const sortConditions = (sort, dir)=>{
|
||||
return { [sort]: dir === 'asc' ? 1 : -1 };
|
||||
return undefined; // If all renderers selected, renderer field not needed in query for speed
|
||||
};
|
||||
|
||||
const findBrews = async (req, res)=>{
|
||||
@@ -42,30 +24,8 @@ const findBrews = async (req, res)=>{
|
||||
const sort = req.query.sort || 'title';
|
||||
const dir = req.query.dir || 'asc';
|
||||
|
||||
const combinedQuery = {
|
||||
$and : [
|
||||
{ published: true },
|
||||
rendererConditions(req.query.legacy, req.query.v3),
|
||||
titleConditions(title),
|
||||
authorConditions(author)
|
||||
],
|
||||
};
|
||||
|
||||
const projection = {
|
||||
editId : 0,
|
||||
googleId : 0,
|
||||
text : 0,
|
||||
textBin : 0,
|
||||
version : 0
|
||||
};
|
||||
|
||||
await HomebrewModel.find(combinedQuery, projection)
|
||||
.sort(sortConditions(sort, dir))
|
||||
.skip(skip)
|
||||
.limit(count)
|
||||
.maxTimeMS(5000)
|
||||
.exec()
|
||||
.then((brews)=>{
|
||||
await getStorage().brews.searchPublished({ title, author, renderer: rendererFilter(req.query.legacy, req.query.v3), sort, dir, skip, limit: count })
|
||||
.then(({ brews })=>{
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
const processedBrews = brews.map((brew)=>{
|
||||
@@ -84,18 +44,8 @@ const findTotal = async (req, res)=>{
|
||||
const title = req.query.title || '';
|
||||
const author = req.query.author || '';
|
||||
|
||||
const combinedQuery = {
|
||||
$and : [
|
||||
{ published: true },
|
||||
rendererConditions(req.query.legacy, req.query.v3),
|
||||
titleConditions(title),
|
||||
authorConditions(author)
|
||||
],
|
||||
};
|
||||
|
||||
await HomebrewModel.countDocuments(combinedQuery)
|
||||
.then((totalBrews)=>{
|
||||
console.log(`when returning, the total of brews is ${totalBrews} for the query ${JSON.stringify(combinedQuery)}`);
|
||||
await getStorage().brews.searchPublished({ title, author, renderer: rendererFilter(req.query.legacy, req.query.v3), limit: 0, withTotal: true })
|
||||
.then(({ total: totalBrews })=>{
|
||||
res.json({ totalBrews });
|
||||
})
|
||||
.catch((error)=>{
|
||||
|
||||
Reference in new issue
Block a user