@only-chat/elasticsearch-user-store 0.2.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ClientOptions } from '@elastic/elasticsearch';
|
|
2
|
+
import type { AuthenticationInfo, UserStore } from '@only-chat/types/userStore.js';
|
|
3
|
+
export interface StoreAuthenticationInfo extends AuthenticationInfo {
|
|
4
|
+
name: string;
|
|
5
|
+
password: string;
|
|
6
|
+
}
|
|
7
|
+
export type { ClientOptions } from '@elastic/elasticsearch';
|
|
8
|
+
export interface Config {
|
|
9
|
+
options: ClientOptions;
|
|
10
|
+
usersIndex: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function initialize(config: Config): Promise<UserStore>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Client } from '@elastic/elasticsearch';
|
|
2
|
+
export async function initialize(config) {
|
|
3
|
+
const usersIndex = config.usersIndex;
|
|
4
|
+
const client = new Client(config.options);
|
|
5
|
+
const exists = await client.indices.exists({ index: usersIndex });
|
|
6
|
+
if (!exists) {
|
|
7
|
+
await client.indices.create({
|
|
8
|
+
index: usersIndex,
|
|
9
|
+
mappings: {
|
|
10
|
+
dynamic: 'strict',
|
|
11
|
+
properties: {
|
|
12
|
+
password: { type: "keyword" },
|
|
13
|
+
timestamp: { type: "date" },
|
|
14
|
+
deletedAt: { type: "date" },
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async function getUser(name, password) {
|
|
20
|
+
const result = await client.search({
|
|
21
|
+
index: usersIndex,
|
|
22
|
+
size: 1,
|
|
23
|
+
query: {
|
|
24
|
+
bool: {
|
|
25
|
+
must: [
|
|
26
|
+
{ ids: { values: [name] } },
|
|
27
|
+
{ term: { password: { value: password } } },
|
|
28
|
+
],
|
|
29
|
+
must_not: {
|
|
30
|
+
exists: {
|
|
31
|
+
field: 'deletedAt'
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return result?.hits.hits[0]?._id;
|
|
38
|
+
}
|
|
39
|
+
function saveUser(name, password) {
|
|
40
|
+
return client.index({
|
|
41
|
+
id: name,
|
|
42
|
+
index: usersIndex,
|
|
43
|
+
body: { password, timestamp: new Date() },
|
|
44
|
+
op_type: 'create',
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
async authenticate(info) {
|
|
49
|
+
if (info.name) {
|
|
50
|
+
const name = info.name.trim().toLowerCase();
|
|
51
|
+
let userId = await getUser(name, info.password);
|
|
52
|
+
if (!userId) {
|
|
53
|
+
const result = await saveUser(name, info.password);
|
|
54
|
+
userId = result._id;
|
|
55
|
+
}
|
|
56
|
+
return userId;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAiB,MAAM,wBAAwB,CAAC;AAgB/D,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,MAAc;IAC3C,MAAM,UAAU,GAAG,MAAM,CAAC,UAAU,CAAC;IAErC,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAE1C,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC,CAAC;IAClE,IAAI,CAAC,MAAM,EAAE,CAAC;QACV,MAAM,MAAM,C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Z,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;gBAE5C,IAAI,MAAM,GAAG,MAAM,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;gBAEhD,IAAI,CAAC,MAAM,EAAE,CAAC;oBACV,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;oBACnD,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC;gBACxB,CAAC;gBAED,OAAO,MAAM,CAAC;YAClB,CAAC;QACL,CAAC;KACJ,CAAA;AACL,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@only-chat/elasticsearch-user-store",
|
|
3
|
+
"version": "0.2.0-beta.10",
|
|
4
|
+
"description": "Elasticsearchn storage for only-chat",
|
|
5
|
+
"homepage": "https://github.com/only-chat/node-backend/packages/stores/elasticsearch",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/only-chat/node-backend.git",
|
|
12
|
+
"directory": "packages/stores/elasticsearch"
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc"
|
|
16
|
+
},
|
|
17
|
+
"author": "only-chat",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@elastic/elasticsearch": "8.15.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@only-chat/types": "0.2.0-beta.10",
|
|
24
|
+
"@types/node": "^22.7.4",
|
|
25
|
+
"typescript": "^5.4.5"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist/"
|
|
29
|
+
],
|
|
30
|
+
"keywords": [
|
|
31
|
+
"elasticsearch",
|
|
32
|
+
"only-chat",
|
|
33
|
+
"store",
|
|
34
|
+
"user"
|
|
35
|
+
]
|
|
36
|
+
}
|