forked from havsar/node-ts-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
380 additions
and
14 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
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 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,31 @@ | ||
# node-ts-cache-storage-elasticsearch | ||
|
||
ElasticSearch storage module for [node-ts-cache](https://www.npmjs.com/package/node-ts-cache). | ||
|
||
This module expects you to bring your own instance of ElasticSearch. | ||
|
||
```bash | ||
yarn add @boredland/node-ts-cache @boredland/node-ts-cache-storage-elasticsearch | ||
``` | ||
|
||
```ts | ||
import { ElasticSearchStorage } from "@boredland/node-ts-cache-storage-elasticsearch" | ||
import { Cache, CacheContainer } from "@boredland/node-ts-cache" | ||
import { Client } from "@elastic/elasticsearch"; | ||
|
||
const client = new Client({ | ||
node: "http://localhost:9200", | ||
Connection: mock.getConnection(), | ||
}); | ||
|
||
const storage = new ElasticSearchStorage(indexName, client); | ||
|
||
const userCache = new CacheContainer(storage) | ||
|
||
class MyService { | ||
@Cache(userCache, { ttl: 60 }) | ||
public async getUsers(): Promise<string[]> { | ||
return ["Max", "User"] | ||
} | ||
} | ||
``` |
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,42 @@ | ||
{ | ||
"name": "@boredland/node-ts-cache-storage-elasticsearch", | ||
"description": "ElasticSearch storage module for node-ts-cache", | ||
"version": "5.0.0", | ||
"private": false, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc -p tsconfig-build.json" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/boredland/node-ts-cache.git" | ||
}, | ||
"keywords": [ | ||
"node", | ||
"nodejs", | ||
"cache", | ||
"typescript", | ||
"ts", | ||
"caching", | ||
"cache", | ||
"elastic", | ||
"elasticsearch", | ||
"node-cache", | ||
"ts-cache" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/boredland/node-ts-cache/issues" | ||
}, | ||
"homepage": "https://github.com/boredland/node-ts-cache#readme", | ||
"peerDependencies": { | ||
"@boredland/node-ts-cache": "^6.0.0", | ||
"@elastic/elasticsearch": "^8.4.0" | ||
}, | ||
"devDependencies": { | ||
"@boredland/node-ts-cache": "^6.0.0", | ||
"@elastic/elasticsearch": "^8.4.0", | ||
"@elastic/elasticsearch-mock": "^2.0.0" | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/storage-elasticsearch/src/elastic-storage.test.ts
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,80 @@ | ||
import { ElasticSearchStorage } from "."; | ||
import { storageTestFactory } from "../../core/src/storage/storageTestFactory"; | ||
import { decoratorTestFactory } from "../../core/src/decorator/decoratorTestFactory"; | ||
import { Client } from "@elastic/elasticsearch"; | ||
import Mock from "@elastic/elasticsearch-mock"; | ||
import type { CachedItem } from "@boredland/node-ts-cache"; | ||
|
||
const mock = new Mock(); | ||
const indexName = "123abcTest"; | ||
|
||
const lastItemFromPath = (path: string) => | ||
decodeURIComponent(path.substring(path.lastIndexOf("/") + 1)); | ||
|
||
let memCache: Record<string, CachedItem> = {}; | ||
|
||
mock.add( | ||
{ | ||
method: "GET", | ||
path: `/${indexName}/_doc/:id`, | ||
}, | ||
(params) => { | ||
const key = lastItemFromPath(params.path as string); | ||
const item = memCache[key]; | ||
return { | ||
_index: "my-index-000001", | ||
_id: "0", | ||
_version: 1, | ||
_seq_no: 0, | ||
_primary_term: 1, | ||
found: !!item, | ||
_source: item, | ||
}; | ||
} | ||
); | ||
|
||
mock.add( | ||
{ | ||
method: "PUT", | ||
path: `/${indexName}/_doc/:id`, | ||
}, | ||
(params) => { | ||
const key = lastItemFromPath(params.path as string); | ||
memCache[key] = params.body as CachedItem; | ||
return "ok"; | ||
} | ||
); | ||
|
||
mock.add( | ||
{ | ||
method: "DELETE", | ||
path: `/${indexName}/_doc/:id`, | ||
}, | ||
async (params) => { | ||
const key = lastItemFromPath(params.path as string); | ||
delete memCache[key]; | ||
return "ok"; | ||
} | ||
); | ||
|
||
mock.add( | ||
{ | ||
method: "DELETE", | ||
path: `/${indexName}`, | ||
}, | ||
async () => { | ||
memCache = {}; | ||
return "ok"; | ||
} | ||
); | ||
|
||
describe("elasticsearch-storage", () => { | ||
const client = new Client({ | ||
node: "http://localhost:9200", | ||
Connection: mock.getConnection(), | ||
}); | ||
const storage = new ElasticSearchStorage(indexName, client); | ||
|
||
storageTestFactory(storage); | ||
decoratorTestFactory(storage); | ||
}); |
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,54 @@ | ||
import type { Client } from "@elastic/elasticsearch"; | ||
import type { CachedItem, Storage } from "@boredland/node-ts-cache"; | ||
import type { GetResponse } from "@elastic/elasticsearch/lib/api/types"; | ||
|
||
export class ElasticSearchStorage implements Storage { | ||
constructor( | ||
private indexName: string, | ||
private elasticsearchInstance: Client | ||
) {} | ||
|
||
async clear(): Promise<void> { | ||
try { | ||
await this.elasticsearchInstance.indices.delete({ | ||
index: this.indexName, | ||
ignore_unavailable: true, | ||
}); | ||
} catch (e) { | ||
return; | ||
} | ||
} | ||
|
||
async removeItem(key: string): Promise<void> { | ||
await this.elasticsearchInstance.delete({ | ||
index: this.indexName, | ||
id: encodeURIComponent(key), | ||
refresh: "wait_for", | ||
}); | ||
} | ||
|
||
async getItem(key: string): Promise<CachedItem | undefined> { | ||
let item: GetResponse<CachedItem | undefined>; | ||
|
||
try { | ||
item = await this.elasticsearchInstance.get<CachedItem>({ | ||
index: this.indexName, | ||
id: encodeURIComponent(key), | ||
}); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
|
||
if (!item.found) return undefined; | ||
return item._source; | ||
} | ||
|
||
async setItem(key: string, content: CachedItem): Promise<void> { | ||
await this.elasticsearchInstance.index({ | ||
index: this.indexName, | ||
id: encodeURIComponent(key), | ||
refresh: "wait_for", | ||
body: content, | ||
}); | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["./src"], | ||
"exclude": ["**/*.test.ts"], | ||
"compilerOptions": { | ||
"sourceMap": false | ||
} | ||
} |
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,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["./src"], | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
} | ||
} |
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 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 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 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
Oops, something went wrong.