-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from Klimatbyran/feat/redisInsteadOfElasticsearch
feat: use redis instead of elastic for noew
- Loading branch information
Showing
9 changed files
with
224 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import config from './config/redis' | ||
import { createClient } from 'redis' | ||
import * as crypto from 'crypto' | ||
|
||
class Db { | ||
client: any | ||
|
||
constructor(config: any) { | ||
try { | ||
this.client = createClient(config) | ||
} catch (error) { | ||
console.error('Redis constructor error:', error) | ||
} | ||
} | ||
|
||
public hashPdf(pdfBuffer: Buffer): string { | ||
return crypto.createHash('sha256').update(pdfBuffer).digest('hex') | ||
} | ||
|
||
async indexReport(pdfHash: string, reportData: string, url: string) { | ||
try { | ||
const response = await this.client.set( | ||
pdfHash, | ||
JSON.stringify({ | ||
url: url, | ||
pdfHash: pdfHash, | ||
report: reportData, | ||
state: 'pending', | ||
timestamp: new Date(), | ||
}) | ||
) | ||
console.log(`Report data added. Document ID: ${pdfHash}`) | ||
return pdfHash | ||
} catch (error) { | ||
console.error(`Error adding report data:`, error) | ||
} | ||
} | ||
|
||
async updateDocumentState(documentId: string, newState: string) { | ||
try { | ||
const existing = await this.client.get(documentId) | ||
if (!existing) { | ||
console.error(`Document ID ${documentId} not found.`) | ||
return | ||
} | ||
existing.state = newState | ||
await this.client.set(documentId, JSON.stringify(existing)) | ||
console.log(`Document ${documentId} state updated to ${newState}.`) | ||
} catch (error) { | ||
console.error( | ||
`Error updating document state for Document ID ${documentId}:`, | ||
error | ||
) | ||
} | ||
} | ||
} | ||
|
||
export default new Db(config) |
Oops, something went wrong.