Skip to content

Commit ea1bb37

Browse files
authoredMay 7, 2024··
Merge pull request #2529 from kuzzleio/feature/expose-store-repository
2 parents 708a17f + b52f6f2 commit ea1bb37

27 files changed

+275
-247
lines changed
 

‎.eslintc-ts.json

-8
This file was deleted.

‎.eslintrc.json

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
2-
"plugins": [
3-
"kuzzle"
4-
],
5-
"extends": [
6-
"plugin:kuzzle/default",
7-
"plugin:kuzzle/node"
8-
],
2+
"plugins": ["kuzzle"],
3+
"extends": ["plugin:kuzzle/default", "plugin:kuzzle/node"],
94
"rules": {
105
"sort-keys": "warn",
116
"kuzzle/array-foreach": "warn"
12-
}
13-
}
7+
},
8+
"overrides": [
9+
{
10+
"files": ["*.ts"],
11+
"extends": ["plugin:kuzzle/typescript"]
12+
}
13+
]
14+
}

‎index.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
export * from './lib/core/backend';
1+
export * from "./lib/core/backend";
22

3-
export * from './lib/types';
3+
export * from "./lib/types";
44

5-
export * from './lib/core/plugin/pluginContext';
5+
export * from "./lib/core/plugin/pluginContext";
66

7-
export * from './lib/core/shared/sdk/embeddedSdk';
7+
export * from "./lib/core/shared/sdk/embeddedSdk";
88

9-
export * from './lib/api/request';
9+
export * from "./lib/api/request";
1010

11-
export * from './lib/kerror/errors';
11+
export * from "./lib/kerror/errors";
1212

13-
export * from './lib/util/mutex';
13+
export * from "./lib/util/mutex";
1414

15-
export * from './lib/util/Inflector';
15+
export * from "./lib/util/Inflector";
1616

17-
export { NameGenerator } from './lib/util/name-generator';
17+
export { NameGenerator } from "./lib/util/name-generator";
1818

19-
export * from 'kuzzle-sdk';
19+
export * from "kuzzle-sdk";
2020

21-
export * from './lib/core/shared/KoncordeWrapper';
21+
export * from "./lib/core/shared/KoncordeWrapper";
22+
23+
export * from "./lib/core/shared/ObjectRepository";
24+
25+
export * from "./lib/core/shared/store";
26+
27+
export * from "./lib/core/cache/cacheDbEnum";
28+
29+
export * from "./lib/core/storage/storeScopeEnum";

‎lib/core/cache/cacheDbEnum.js ‎lib/core/cache/cacheDbEnum.ts

+5-15
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,12 @@
1919
* limitations under the License.
2020
*/
2121

22-
"use strict";
23-
24-
/**
25-
* @typedef {string} cacheDbEnum
26-
*/
27-
2822
/**
2923
* Enum for the cache engine target (used by classes using either the
3024
* internal or the public DB)
31-
* @readOnly
32-
* @enum {cacheDbEnum}
3325
*/
34-
const cacheDbEnum = Object.freeze({
35-
INTERNAL: "internal",
36-
NONE: "none",
37-
PUBLIC: "public",
38-
});
39-
40-
module.exports = cacheDbEnum;
26+
export enum cacheDbEnum {
27+
INTERNAL = "internal",
28+
NONE = "none",
29+
PUBLIC = "public",
30+
}

‎lib/core/plugin/pluginContext.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,25 @@
1919
* limitations under the License.
2020
*/
2121

22-
import Bluebird from "bluebird";
23-
import { Koncorde } from "../shared/KoncordeWrapper";
2422
import { Client } from "@elastic/elasticsearch";
23+
import Bluebird from "bluebird";
2524
import { JSONObject } from "kuzzle-sdk";
25+
import { Koncorde } from "../shared/KoncordeWrapper";
2626

27-
import { EmbeddedSDK } from "../shared/sdk/embeddedSdk";
28-
import PluginRepository from "./pluginRepository";
29-
import Store from "../shared/store";
30-
import Elasticsearch from "../../service/storage/elasticsearch";
31-
import { isPlainObject } from "../../util/safeObject";
32-
import Promback from "../../util/promback";
33-
import { Mutex } from "../../util/mutex";
27+
import {
28+
KuzzleRequest,
29+
Request,
30+
RequestContext,
31+
RequestInput,
32+
} from "../../../index";
3433
import * as kerror from "../../kerror";
35-
import storeScopeEnum from "../storage/storeScopeEnum";
3634
import {
3735
BadRequestError,
3836
ExternalServiceError,
3937
ForbiddenError,
4038
GatewayTimeoutError,
41-
InternalError as KuzzleInternalError,
4239
KuzzleError,
40+
InternalError as KuzzleInternalError,
4341
NotFoundError,
4442
PartialError,
4543
PluginImplementationError,
@@ -49,13 +47,15 @@ import {
4947
TooManyRequestsError,
5048
UnauthorizedError,
5149
} from "../../kerror/errors";
52-
import {
53-
RequestContext,
54-
RequestInput,
55-
KuzzleRequest,
56-
Request,
57-
} from "../../../index";
50+
import Elasticsearch from "../../service/storage/elasticsearch";
51+
import { Mutex } from "../../util/mutex";
52+
import Promback from "../../util/promback";
53+
import { isPlainObject } from "../../util/safeObject";
5854
import { BackendCluster } from "../backend";
55+
import { EmbeddedSDK } from "../shared/sdk/embeddedSdk";
56+
import { Store } from "../shared/store";
57+
import { storeScopeEnum } from "../storage/storeScopeEnum";
58+
import PluginRepository from "./pluginRepository";
5959

6060
const contextError = kerror.wrap("plugin", "context");
6161

‎lib/core/plugin/pluginRepository.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
const { merge } = require("lodash");
2525

2626
const { NotFoundError } = require("../../kerror/errors");
27-
const { Repository } = require("../shared/repository");
28-
const cacheDbEnum = require("../cache/cacheDbEnum");
27+
const { ObjectRepository } = require("../shared/ObjectRepository");
28+
const { cacheDbEnum } = require("../cache/cacheDbEnum");
2929

30-
class PluginRepository extends Repository {
30+
class PluginRepository extends ObjectRepository {
3131
constructor(store, collection) {
3232
super({ cache: cacheDbEnum.NONE, store });
3333

‎lib/core/security/profileRepository.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
* limitations under the License.
2020
*/
2121

22-
import { omit } from "lodash";
2322
import Bluebird from "bluebird";
23+
import { omit } from "lodash";
2424

25-
import { Profile } from "../../model/security/profile";
26-
import { Repository } from "../shared/repository";
27-
import * as kerror from "../../kerror";
28-
import cacheDbEnum from "../cache/cacheDbEnum";
2925
import { JSONObject } from "kuzzle-sdk";
30-
import { Policy, OptimizedPolicy } from "../../../index";
26+
import { OptimizedPolicy, Policy } from "../../../index";
27+
import * as kerror from "../../kerror";
28+
import { Profile } from "../../model/security/profile";
29+
import { cacheDbEnum } from "../cache/cacheDbEnum";
30+
import { ObjectRepository } from "../shared/ObjectRepository";
3131

3232
/** @internal */
3333
type CreateOrReplaceOptions = {
@@ -55,9 +55,9 @@ type UpdateOptions = {
5555

5656
/**
5757
* @class ProfileRepository
58-
* @extends Repository
58+
* @extends ObjectRepository
5959
*/
60-
export class ProfileRepository extends Repository<Profile> {
60+
export class ProfileRepository extends ObjectRepository<Profile> {
6161
private module: any;
6262
private profiles: Map<string, Profile>;
6363

‎lib/core/security/roleRepository.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ const Bluebird = require("bluebird");
2525

2626
const kuzzleStateEnum = require("../../kuzzle/kuzzleStateEnum");
2727
const { Role } = require("../../model/security/role");
28-
const { Repository } = require("../shared/repository");
28+
const { ObjectRepository } = require("../shared/ObjectRepository");
2929
const kerror = require("../../kerror");
3030
const didYouMean = require("../../util/didYouMean");
31-
const cacheDbEnum = require("../cache/cacheDbEnum");
31+
const { cacheDbEnum } = require("../cache/cacheDbEnum");
3232

3333
const roleRightsError = kerror.wrap("security", "role");
3434

3535
/**
3636
* @class RoleRepository
37-
* @extends Repository
37+
* @extends ObjectRepository
3838
*/
39-
class RoleRepository extends Repository {
39+
class RoleRepository extends ObjectRepository {
4040
/**
4141
* @constructor
4242
* @param {SecurityModule} securityModule

‎lib/core/security/tokenRepository.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@
1919
* limitations under the License.
2020
*/
2121

22-
import _ from "lodash";
2322
import jwt from "jsonwebtoken";
24-
import ms from "ms";
2523
import { JSONObject } from "kuzzle-sdk";
24+
import _ from "lodash";
25+
import ms from "ms";
2626

27-
import ApiKey from "../../model/storage/apiKey";
27+
import * as kerror from "../../kerror";
2828
import { UnauthorizedError } from "../../kerror/errors";
2929
import { Token } from "../../model/security/token";
30-
import * as kerror from "../../kerror";
30+
import { User } from "../../model/security/user";
31+
import ApiKey from "../../model/storage/apiKey";
3132
import debugFactory from "../../util/debug";
3233
import { Mutex } from "../../util/mutex";
33-
import { Repository } from "../shared/repository";
34-
import { User } from "../../model/security/user";
34+
import { ObjectRepository } from "../shared/ObjectRepository";
3535

3636
const securityError = kerror.wrap("security", "token");
3737
const debug = debugFactory("kuzzle:bootstrap:tokens");
3838

3939
const BOOTSTRAP_DONE_KEY = "token/bootstrap";
4040

41-
export class TokenRepository extends Repository<Token> {
41+
export class TokenRepository extends ObjectRepository<Token> {
4242
private tokenGracePeriod: number;
4343
private anonymousToken: Token;
4444

‎lib/core/security/userRepository.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323

2424
const { Request } = require("../../api/request");
2525
const debug = require("../../util/debug")("kuzzle:core:security:users");
26-
const { Repository } = require("../shared/repository");
26+
const { ObjectRepository } = require("../shared/ObjectRepository");
2727
const kerror = require("../../kerror");
2828
const { User } = require("../../model/security/user");
2929
const ApiKey = require("../../model/storage/apiKey");
3030

3131
/**
3232
* @class UserRepository
33-
* @extends Repository
33+
* @extends ObjectRepository
3434
*/
35-
class UserRepository extends Repository {
35+
class UserRepository extends ObjectRepository {
3636
/**
3737
* @param {SecurityModule} securityModule
3838
* @constructor

‎lib/core/shared/repository.ts ‎lib/core/shared/ObjectRepository.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
* limitations under the License.
2020
*/
2121

22-
import cacheDbEnum from "../cache/cacheDbEnum";
23-
import * as kerror from "../../kerror";
2422
import { JSONObject } from "kuzzle-sdk";
23+
import * as kerror from "../../kerror";
24+
import { cacheDbEnum } from "../cache/cacheDbEnum";
2525

26-
export class Repository<TObject extends { _id: string }> {
26+
export class ObjectRepository<TObject extends { _id: string }> {
2727
protected ttl: number;
2828
protected index: string;
2929
protected collection: string;
3030
protected ObjectConstructor: any;
3131
protected store: any;
32-
protected cacheDb: any;
32+
protected cacheDb: cacheDbEnum;
3333

3434
constructor({ cache = cacheDbEnum.INTERNAL, store = null } = {}) {
3535
this.ttl = global.kuzzle.config.repositories.common.cacheTTL;
@@ -142,7 +142,7 @@ export class Repository<TObject extends { _id: string }> {
142142
return null;
143143
}
144144

145-
return await this.fromDTO(Object.assign({}, JSON.parse(response)));
145+
return await this.fromDTO({ ...JSON.parse(response) });
146146
} catch (err) {
147147
throw kerror.get("services", "cache", "read_failed", err.message);
148148
}
@@ -256,7 +256,7 @@ export class Repository<TObject extends { _id: string }> {
256256
): Promise<TObject> {
257257
const key = options.key || this.getCacheKey(object._id);
258258
const value = JSON.stringify(this.serializeToCache(object));
259-
const ttl = options.ttl !== undefined ? options.ttl : this.ttl;
259+
const ttl = options.ttl ?? this.ttl;
260260

261261
await global.kuzzle.ask(`core:cache:${this.cacheDb}:store`, key, value, {
262262
ttl,
@@ -359,7 +359,7 @@ export class Repository<TObject extends { _id: string }> {
359359
* @returns {object}
360360
*/
361361
toDTO(o: TObject): any {
362-
return Object.assign({}, o);
362+
return { ...o };
363363
}
364364

365365
/**

0 commit comments

Comments
 (0)
Please sign in to comment.