@quatrain/core 1.0.0-beta1 → 1.0.0-beta3

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.
Files changed (66) hide show
  1. package/lib/Auth.d.ts +25 -0
  2. package/lib/Auth.js +9 -0
  3. package/lib/Backend.d.ts +28 -0
  4. package/lib/Core.d.ts +6 -8
  5. package/lib/Core.js +6 -5
  6. package/lib/authentication/AbstractAuthAdapter.d.ts +21 -0
  7. package/lib/authentication/AbstractAuthAdapter.js +28 -0
  8. package/lib/authentication/AuthenticationError.d.ts +2 -0
  9. package/lib/authentication/AuthenticationError.js +6 -0
  10. package/lib/authentication/index.d.ts +4 -0
  11. package/lib/authentication/index.js +7 -0
  12. package/lib/authentication/middlewares/AbstractAuthMiddleware.d.ts +0 -0
  13. package/lib/authentication/middlewares/AbstractAuthMiddleware.js +1 -0
  14. package/lib/authentication/middlewares/Middleware.d.ts +5 -0
  15. package/lib/authentication/types/AuthInterface.d.ts +9 -0
  16. package/lib/authentication/types/AuthInterface.js +2 -0
  17. package/lib/backends/AbstractAdapter.d.ts +11 -41
  18. package/lib/backends/AbstractAdapter.js +18 -5
  19. package/lib/backends/MockAdapter.d.ts +6 -4
  20. package/lib/backends/MockAdapter.js +15 -6
  21. package/lib/backends/Query.d.ts +18 -7
  22. package/lib/backends/Query.js +8 -6
  23. package/lib/backends/index.d.ts +4 -3
  24. package/lib/backends/middlewares/InjectKeywordsMiddleware.d.ts +9 -0
  25. package/lib/backends/middlewares/InjectKeywordsMiddleware.js +47 -0
  26. package/lib/backends/middlewares/InjectMetaMiddleware.d.ts +3 -3
  27. package/lib/backends/types/BackendInterface.d.ts +13 -0
  28. package/lib/backends/types/BackendInterface.js +2 -0
  29. package/lib/common/statuses.d.ts +2 -0
  30. package/lib/common/statuses.js +3 -1
  31. package/lib/components/AbstractObject.d.ts +1 -1
  32. package/lib/components/BaseObject.d.ts +3 -3
  33. package/lib/components/BaseObjectCore.js +1 -2
  34. package/lib/components/BaseRepository.d.ts +9 -8
  35. package/lib/components/BaseRepository.js +7 -10
  36. package/lib/components/DataObject.d.ts +1 -0
  37. package/lib/components/DataObject.js +12 -6
  38. package/lib/components/Entity.d.ts +5 -19
  39. package/lib/components/Entity.js +9 -7
  40. package/lib/components/EntityRepository.d.ts +3 -3
  41. package/lib/components/EntityRepository.js +1 -1
  42. package/lib/components/ObjectUri.js +1 -2
  43. package/lib/components/User.d.ts +4 -5
  44. package/lib/components/User.js +19 -10
  45. package/lib/components/UserRepository.d.ts +4 -4
  46. package/lib/components/UserRepository.js +2 -2
  47. package/lib/components/index.d.ts +2 -2
  48. package/lib/components/index.js +4 -2
  49. package/lib/components/types/DataObjectClass.d.ts +2 -0
  50. package/lib/components/types/RepositoryClass.d.ts +1 -1
  51. package/lib/index.d.ts +7 -3
  52. package/lib/index.js +14 -1
  53. package/lib/properties/CollectionProperty.d.ts +2 -2
  54. package/lib/properties/HashProperty.d.ts +6 -0
  55. package/lib/properties/HashProperty.js +8 -0
  56. package/lib/properties/types/PropertyClassType.d.ts +1 -0
  57. package/package.json +2 -2
  58. package/README.md +0 -124
  59. package/lib/components/BaseObjectProperties.d.ts +0 -13
  60. package/lib/components/BaseObjectProperties.js +0 -87
  61. package/lib/components/EntityProperties.d.ts +0 -1
  62. package/lib/components/EntityProperties.js +0 -54
  63. package/lib/components/UserProperties.d.ts +0 -1
  64. package/lib/components/UserProperties.js +0 -103
  65. package/lib/components/types/UserClass.d.ts +0 -3
  66. /package/lib/{components/types/UserClass.js → authentication/middlewares/Middleware.js} +0 -0
package/lib/Auth.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import Middleware from './authentication/middlewares/Middleware';
2
+ export declare enum AuthAction {
3
+ SIGNIN = "signin",
4
+ SIGNUP = "signup",
5
+ SIGNOUT = "signout"
6
+ }
7
+ /**
8
+ * Authentication Parameters acceptable keys
9
+ */
10
+ export type AuthParametersKeys = 'host' | 'alias' | 'mapping' | 'middlewares' | 'config' | 'fixtures' | 'debug';
11
+ /**
12
+ * Backend parameters interface
13
+ */
14
+ export interface AuthParameters {
15
+ host?: string;
16
+ alias?: string;
17
+ mapping?: {
18
+ [x: string]: any;
19
+ };
20
+ middlewares?: Middleware[];
21
+ config?: any;
22
+ fixtures?: any;
23
+ softDelete?: boolean;
24
+ debug?: boolean;
25
+ }
package/lib/Auth.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthAction = void 0;
4
+ var AuthAction;
5
+ (function (AuthAction) {
6
+ AuthAction["SIGNIN"] = "signin";
7
+ AuthAction["SIGNUP"] = "signup";
8
+ AuthAction["SIGNOUT"] = "signout";
9
+ })(AuthAction || (exports.AuthAction = AuthAction = {}));
package/lib/Backend.d.ts CHANGED
@@ -1,6 +1,34 @@
1
+ import Middleware from "./backends/middlewares/Middleware";
1
2
  export declare enum BackendAction {
2
3
  CREATE = "create",
3
4
  READ = "read",
4
5
  UPDATE = "update",
5
6
  DELETE = "delete"
6
7
  }
8
+ /**
9
+ * Default interface for a backend record
10
+ */
11
+ export interface BackendRecordType {
12
+ uid: string | undefined;
13
+ path: string | undefined;
14
+ [key: string]: any;
15
+ }
16
+ /**
17
+ * Backend Parameters acceptable keys
18
+ */
19
+ export type BackendParametersKeys = 'host' | 'alias' | 'mapping' | 'middlewares' | 'config' | 'fixtures' | 'softDelete' | 'debug';
20
+ /**
21
+ * Backend parameters interface
22
+ */
23
+ export interface BackendParameters {
24
+ host?: string;
25
+ alias?: string;
26
+ mapping?: {
27
+ [x: string]: any;
28
+ };
29
+ middlewares?: Middleware[];
30
+ config?: any;
31
+ fixtures?: any;
32
+ softDelete?: boolean;
33
+ debug?: boolean;
34
+ }
package/lib/Core.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { AbstractAuthAdapter } from './authentication';
1
2
  import { AbstractAdapter } from './backends/AbstractAdapter';
2
3
  import { User } from './components/User';
3
4
  import { DataObjectClass } from './components/types/DataObjectClass';
@@ -6,14 +7,12 @@ export type BackendRegistry<T extends AbstractAdapter> = {
6
7
  };
7
8
  export declare class Core {
8
9
  static defaultBackend: string;
9
- static currentUser: User;
10
+ static userClass: typeof User;
10
11
  static classRegistry: {
11
12
  [key: string]: any;
12
13
  };
13
- static logger: {
14
- (...data: any[]): void;
15
- (message?: any, ...optionalParams: any[]): void;
16
- };
14
+ static logger: Console;
15
+ static auth: AbstractAuthAdapter;
17
16
  static timestamp: () => string;
18
17
  protected static _backends: BackendRegistry<any>;
19
18
  static definition(key: string): {
@@ -35,9 +34,8 @@ export declare class Core {
35
34
  /**
36
35
  * Log message using defined logger
37
36
  * This is currently just a stub that will be implemented from config in the future
38
- * @param message string
37
+ * @param message string | object
39
38
  * @param level string
40
39
  */
41
- static log(message: string, src?: string, level?: string): void;
42
- static getObject(): void;
40
+ static log(message: any, src?: string, level?: string): void;
43
41
  }
package/lib/Core.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Core = void 0;
4
4
  const DataObject_1 = require("./components/DataObject");
5
+ const User_1 = require("./components/User");
5
6
  class Core {
6
7
  static definition(key) {
7
8
  return {
@@ -42,18 +43,18 @@ class Core {
42
43
  /**
43
44
  * Log message using defined logger
44
45
  * This is currently just a stub that will be implemented from config in the future
45
- * @param message string
46
+ * @param message string | object
46
47
  * @param level string
47
48
  */
48
49
  static log(message, src = 'Core', level = 'NOTICE') {
49
- Core.logger(`${Date.now()} - [${src}] ${message}`);
50
+ Core.logger.log(`${Date.now()} - [${src}] ${typeof message === 'string' ? message : JSON.stringify(message)}`);
50
51
  }
51
- static getObject() { }
52
52
  }
53
53
  exports.Core = Core;
54
54
  Core.defaultBackend = '@default';
55
+ Core.userClass = User_1.User;
55
56
  Core.classRegistry = {};
56
- Core.logger = console.log;
57
+ Core.logger = console;
57
58
  // How timestamp are formatted
58
- Core.timestamp = () => (new Date().toISOString());
59
+ Core.timestamp = () => new Date().toISOString();
59
60
  Core._backends = {};
@@ -0,0 +1,21 @@
1
+ import { AuthParameters, AuthParametersKeys } from '../Auth';
2
+ import { User } from '../components/User';
3
+ import Middleware from './middlewares/Middleware';
4
+ import { AuthInterface } from './types/AuthInterface';
5
+ export declare abstract class AbstractAuthAdapter implements AuthInterface {
6
+ protected _alias: string;
7
+ protected _params: AuthParameters;
8
+ protected _middlewares: any[];
9
+ constructor(params?: AuthParameters);
10
+ setParam(key: AuthParametersKeys, value: any): void;
11
+ getParam(key: AuthParametersKeys): any;
12
+ addMiddleware(middleware: Middleware): void;
13
+ set alias(alias: string);
14
+ get alias(): string;
15
+ abstract register(user: User): Promise<any>;
16
+ abstract signup(login: string, password: string): Promise<any>;
17
+ abstract signout(user: User): Promise<any>;
18
+ abstract update(user: User, updatable: any): Promise<any>;
19
+ abstract delete(user: User): Promise<any>;
20
+ abstract getAuthToken(token: string): any;
21
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractAuthAdapter = void 0;
4
+ class AbstractAuthAdapter {
5
+ constructor(params = {}) {
6
+ this._alias = '';
7
+ this._params = {};
8
+ this._middlewares = [];
9
+ this._alias = params.alias || '';
10
+ this._middlewares = params.middlewares || [];
11
+ }
12
+ setParam(key, value) {
13
+ this._params[key] = value;
14
+ }
15
+ getParam(key) {
16
+ return this._params[key];
17
+ }
18
+ addMiddleware(middleware) {
19
+ this._middlewares.push(middleware);
20
+ }
21
+ set alias(alias) {
22
+ this._alias = alias;
23
+ }
24
+ get alias() {
25
+ return this._alias;
26
+ }
27
+ }
28
+ exports.AbstractAuthAdapter = AbstractAuthAdapter;
@@ -0,0 +1,2 @@
1
+ export declare class AuthenticationError extends Error {
2
+ }
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthenticationError = void 0;
4
+ class AuthenticationError extends Error {
5
+ }
6
+ exports.AuthenticationError = AuthenticationError;
@@ -0,0 +1,4 @@
1
+ import { AbstractAuthAdapter } from './AbstractAuthAdapter';
2
+ import { AuthInterface } from './types/AuthInterface';
3
+ import { AuthenticationError } from './AuthenticationError';
4
+ export { AuthInterface, AbstractAuthAdapter, AuthenticationError };
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AuthenticationError = exports.AbstractAuthAdapter = void 0;
4
+ const AbstractAuthAdapter_1 = require("./AbstractAuthAdapter");
5
+ Object.defineProperty(exports, "AbstractAuthAdapter", { enumerable: true, get: function () { return AbstractAuthAdapter_1.AbstractAuthAdapter; } });
6
+ const AuthenticationError_1 = require("./AuthenticationError");
7
+ Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return AuthenticationError_1.AuthenticationError; } });
@@ -0,0 +1,5 @@
1
+ import { AuthAction } from '../../Auth';
2
+ import { User } from '../../components/User';
3
+ export default interface Middleware {
4
+ execute: (user: User, action: AuthAction) => void;
5
+ }
@@ -0,0 +1,9 @@
1
+ import { User } from '../../components/User';
2
+ export interface AuthInterface {
3
+ register(user: User): Promise<any>;
4
+ signup(login: string, password: string): Promise<any>;
5
+ signout(user: User): Promise<any>;
6
+ update(user: User, updatable: any): Promise<any>;
7
+ delete(user: User): Promise<any>;
8
+ getAuthToken(token: string): any;
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,46 +1,11 @@
1
- import { BackendAction } from '../Backend';
1
+ import { BackendAction, BackendParameters, BackendParametersKeys } from '../Backend';
2
2
  import { DataObjectClass } from '../components/types/DataObjectClass';
3
+ import { BackendInterface } from './types/BackendInterface';
3
4
  import { Filter } from './Filter';
4
5
  import { Filters } from './Filters';
5
- import { Query } from './Query';
6
+ import { Query, QueryResultType } from './Query';
6
7
  import { SortAndLimit } from './SortAndLimit';
7
8
  import Middleware from './middlewares/Middleware';
8
- /**
9
- * Default interface for a backend record
10
- */
11
- export interface BackendRecordType {
12
- uid: string | undefined;
13
- path: string | undefined;
14
- [key: string]: any;
15
- }
16
- /**
17
- * Backend Parameters acceptable keys
18
- */
19
- export type BackendParametersKeys = 'host' | 'alias' | 'mapping' | 'middlewares' | 'config' | 'fixtures' | 'softDelete' | 'debug';
20
- /**
21
- * Backend parameters interface
22
- */
23
- export interface BackendParameters {
24
- host?: string;
25
- alias?: string;
26
- mapping?: {
27
- [x: string]: any;
28
- };
29
- middlewares?: Middleware[];
30
- config?: any;
31
- fixtures?: any;
32
- softDelete?: boolean;
33
- debug?: boolean;
34
- }
35
- export interface BackendInterface {
36
- create(dataObject: DataObjectClass<any>, desiredUid: string | undefined): Promise<DataObjectClass<any>>;
37
- read(param: string | DataObjectClass<any>): Promise<DataObjectClass<any>>;
38
- update(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
39
- delete(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
40
- query(query: Query<any>): Promise<DataObjectClass<any>[]>;
41
- find(dataObject: DataObjectClass<any>, filters: Filters | Filter[] | undefined, pagination: SortAndLimit | undefined): Promise<DataObjectClass<any>[]>;
42
- count(query: Query<any>): Promise<number>;
43
- }
44
9
  export declare abstract class AbstractAdapter implements BackendInterface {
45
10
  protected _alias: string;
46
11
  protected _params: BackendParameters;
@@ -49,6 +14,12 @@ export declare abstract class AbstractAdapter implements BackendInterface {
49
14
  setParam(key: BackendParametersKeys, value: any): void;
50
15
  getParam(key: BackendParametersKeys): any;
51
16
  addMiddleware(middleware: Middleware): void;
17
+ /**
18
+ * Returns true if a given middleware is attached
19
+ * @param className
20
+ * @returns boolean
21
+ */
22
+ hasMiddleware(className: string): boolean;
52
23
  set alias(alias: string);
53
24
  get alias(): string;
54
25
  getCollection(dao: DataObjectClass<any>): string | undefined;
@@ -62,9 +33,8 @@ export declare abstract class AbstractAdapter implements BackendInterface {
62
33
  * @param query
63
34
  * @returns Array
64
35
  */
65
- query(query: Query<any>): Promise<DataObjectClass<any>[]>;
66
- count(query: Query<any>): Promise<number>;
67
- abstract find(dataObject: DataObjectClass<any>, filters: Filters | Filter[] | undefined, pagination: SortAndLimit | undefined): Promise<DataObjectClass<any>[]>;
36
+ query(query: Query<any>): Promise<QueryResultType<DataObjectClass<any>>>;
37
+ abstract find(dataObject: DataObjectClass<any>, filters: Filters | Filter[] | undefined, pagination: SortAndLimit | undefined): Promise<QueryResultType<any>>;
68
38
  log(message: string): void;
69
39
  executeMiddlewares(dataObject: DataObjectClass<any>, action: BackendAction): Promise<DataObjectClass<any>>;
70
40
  }
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AbstractAdapter = void 0;
13
+ const BackendError_1 = require("./BackendError");
13
14
  class AbstractAdapter {
14
15
  constructor(params = {}) {
15
16
  this._alias = '';
@@ -25,8 +26,25 @@ class AbstractAdapter {
25
26
  return this._params[key];
26
27
  }
27
28
  addMiddleware(middleware) {
29
+ if (this.hasMiddleware(middleware.constructor.name)) {
30
+ throw new BackendError_1.BackendError(`Middleware '${middleware.constructor.name}' is already attached`);
31
+ }
28
32
  this._middlewares.push(middleware);
29
33
  }
34
+ /**
35
+ * Returns true if a given middleware is attached
36
+ * @param className
37
+ * @returns boolean
38
+ */
39
+ hasMiddleware(className) {
40
+ let has = false;
41
+ this._middlewares.forEach((middleware) => {
42
+ if (middleware.constructor.name === className) {
43
+ has = true;
44
+ }
45
+ });
46
+ return has;
47
+ }
30
48
  set alias(alias) {
31
49
  this._alias = alias;
32
50
  }
@@ -46,11 +64,6 @@ class AbstractAdapter {
46
64
  return yield this.find(yield query.obj.daoFactory(), query.filters, query.sortAndLimit);
47
65
  });
48
66
  }
49
- count(query) {
50
- return __awaiter(this, void 0, void 0, function* () {
51
- return (yield this.find(yield query.obj.daoFactory(), query.filters, undefined)).length;
52
- });
53
- }
54
67
  log(message) {
55
68
  if (this._params['debug'] === true) {
56
69
  console.log(message);
@@ -1,9 +1,11 @@
1
- import { AbstractAdapter, BackendInterface } from './AbstractAdapter';
2
- import { BackendRecordType } from './AbstractAdapter';
1
+ import { DataObjectClass } from '../components/types/DataObjectClass';
2
+ import { QueryResultType } from './Query';
3
3
  import { Filter } from './Filter';
4
4
  import { Filters } from './Filters';
5
5
  import { SortAndLimit } from './SortAndLimit';
6
- import { DataObjectClass } from '../components/types/DataObjectClass';
6
+ import { AbstractAdapter } from './AbstractAdapter';
7
+ import { BackendInterface } from './types/BackendInterface';
8
+ import { BackendRecordType } from '../Backend';
7
9
  export declare class MockAdapter extends AbstractAdapter implements BackendInterface {
8
10
  protected static _fixtures: any;
9
11
  /**
@@ -19,5 +21,5 @@ export declare class MockAdapter extends AbstractAdapter implements BackendInter
19
21
  update(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
20
22
  delete(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
21
23
  deleteCollection(collection: string, batchSize?: number | undefined): Promise<void>;
22
- find(dataObject: DataObjectClass<any>, filters?: Filters | Filter[] | undefined, pagination?: SortAndLimit | undefined): Promise<DataObjectClass<any>[]>;
24
+ find(dataObject: DataObjectClass<any>, filters?: Filters | Filter[] | undefined, pagination?: SortAndLimit | undefined): Promise<QueryResultType<DataObjectClass<any>>>;
23
25
  }
@@ -10,11 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MockAdapter = void 0;
13
- const AbstractAdapter_1 = require("./AbstractAdapter");
13
+ const Core_1 = require("../Core");
14
14
  const ObjectUri_1 = require("../components/ObjectUri");
15
15
  const faker_1 = require("@faker-js/faker");
16
16
  const BackendError_1 = require("./BackendError");
17
17
  const ResourcesErrors_1 = require("../common/ResourcesErrors");
18
+ const AbstractAdapter_1 = require("./AbstractAdapter");
18
19
  class MockAdapter extends AbstractAdapter_1.AbstractAdapter {
19
20
  /**
20
21
  * Inject fixtures data to backend adapter
@@ -116,12 +117,12 @@ class MockAdapter extends AbstractAdapter_1.AbstractAdapter {
116
117
  find(dataObject, filters = undefined, pagination = undefined) {
117
118
  return __awaiter(this, void 0, void 0, function* () {
118
119
  const limit = (pagination === null || pagination === void 0 ? void 0 : pagination.limits.batch) || 1e10;
119
- let result = [];
120
+ let items = [];
120
121
  const collection = this.getCollection(dataObject);
121
122
  // TODO filter only records matchinf collection
122
123
  for (let key in MockAdapter.getFixtures()) {
123
124
  let keep = true;
124
- if (key.startsWith(`${collection}/`) && result.length <= limit) {
125
+ if (key.startsWith(`${collection}/`) && items.length <= limit) {
125
126
  const dao = yield dataObject.clone(MockAdapter.getFixture(key));
126
127
  if (filters) {
127
128
  if (Array.isArray(filters)) {
@@ -164,15 +165,23 @@ class MockAdapter extends AbstractAdapter_1.AbstractAdapter {
164
165
  }
165
166
  if (keep) {
166
167
  dao.uri = new ObjectUri_1.ObjectUri(key);
167
- result.push(dao);
168
+ items.push(dao);
168
169
  }
169
170
  }
170
171
  }
171
172
  if (pagination && pagination.sortings.length > 0) {
172
- result = result.sort((a, b) => Number(a.val(pagination.sortings[0].prop) >
173
+ items = items.sort((a, b) => Number(a.val(pagination.sortings[0].prop) >
173
174
  b.val(pagination.sortings[0].prop)));
174
175
  }
175
- return result;
176
+ return {
177
+ items,
178
+ meta: {
179
+ count: 100,
180
+ offset: (pagination === null || pagination === void 0 ? void 0 : pagination.limits.offset) || 0,
181
+ batch: (pagination === null || pagination === void 0 ? void 0 : pagination.limits.batch) || 10,
182
+ executionTime: Core_1.Core.timestamp(),
183
+ },
184
+ };
176
185
  });
177
186
  }
178
187
  }
@@ -2,14 +2,24 @@ import { Filter } from './Filter';
2
2
  import { Limits } from './Limits';
3
3
  import { Sorting } from './Sorting';
4
4
  import { SortAndLimit } from './SortAndLimit';
5
- import { BackendInterface } from './AbstractAdapter';
5
+ import { BackendInterface } from './types/BackendInterface';
6
6
  import { DataObjectClass, ObjectUri } from '../components';
7
- import { Persisted } from '../components/types/Persisted';
8
- import { BaseObject } from '../components/BaseObject';
9
7
  import { BaseObjectCore } from '../components/BaseObjectCore';
10
8
  export declare const AS_OBJECTURIS = "objectUris";
11
9
  export declare const AS_DATAOBJECTS = "dataObjects";
12
10
  export declare const AS_INSTANCES = "classInstances";
11
+ export type QueryMetaType = {
12
+ count: number;
13
+ offset: number;
14
+ batch: number;
15
+ executionTime: string | number;
16
+ sortField?: string;
17
+ sortOrder?: 'asc' | 'desc';
18
+ };
19
+ export type QueryResultType<T> = {
20
+ items: Array<T>;
21
+ meta: QueryMetaType;
22
+ };
13
23
  export declare enum returnAs {
14
24
  AS_OBJECTURIS = "objectUris",
15
25
  AS_DATAOBJECTS = "dataObjects",
@@ -22,6 +32,7 @@ export declare class Query<T extends typeof BaseObjectCore> {
22
32
  filters: Filter[];
23
33
  sortings: Sorting[];
24
34
  limits: Limits;
35
+ meta: any;
25
36
  constructor(obj: T, params?: {
26
37
  [x: string]: any;
27
38
  });
@@ -32,8 +43,8 @@ export declare class Query<T extends typeof BaseObjectCore> {
32
43
  offset(offset?: number): this;
33
44
  batch(batch?: number): this;
34
45
  get sortAndLimit(): SortAndLimit;
35
- fetch(backend?: BackendInterface): Promise<DataObjectClass<any>[]>;
36
- fetchAsUri(backend?: BackendInterface): Promise<ObjectUri[]>;
37
- fetchAsInstances(backend?: BackendInterface): Promise<Persisted<BaseObject>[]>;
38
- execute(as?: returnAs, backend?: BackendInterface): Promise<any[]>;
46
+ fetch(backend?: BackendInterface): Promise<QueryResultType<DataObjectClass<any>>>;
47
+ fetchAsUri(backend?: BackendInterface): Promise<QueryResultType<ObjectUri>>;
48
+ fetchAsInstances(backend?: BackendInterface): Promise<QueryResultType<T>>;
49
+ execute(as?: returnAs, backend?: BackendInterface): Promise<QueryResultType<any>>;
39
50
  }
@@ -32,18 +32,20 @@ class Query {
32
32
  this.filters = [];
33
33
  this.sortings = [];
34
34
  this.limits = new Limits_1.Limits();
35
+ this.meta = {};
35
36
  }
36
37
  get obj() {
37
38
  return this._obj;
38
39
  }
39
40
  where(param, value = null, operator = 'equals') {
41
+ console.log(`received value`, value);
40
42
  if (typeof param == 'object') {
41
43
  this.filters.push(param);
42
44
  }
43
45
  else {
44
46
  if (operator === 'equals' && Array.isArray(value)) {
45
47
  // auto-convert operator if value is an array
46
- operator = 'containsAny';
48
+ operator = 'contains'; // Any'
47
49
  }
48
50
  this.filters.push(new Filter_1.Filter(param, value, operator));
49
51
  }
@@ -80,18 +82,18 @@ class Query {
80
82
  }
81
83
  fetchAsUri(backend = Core_1.Core.getBackend()) {
82
84
  return __awaiter(this, void 0, void 0, function* () {
83
- const result = yield this.fetch(backend);
84
- return yield Promise.all(result.map((dao) => dao.uri));
85
+ const { items, meta } = yield this.fetch(backend);
86
+ return { items: yield Promise.all(items.map((dao) => dao.uri)), meta };
85
87
  });
86
88
  }
87
89
  fetchAsInstances(backend = Core_1.Core.getBackend()) {
88
90
  return __awaiter(this, void 0, void 0, function* () {
89
- const result = yield this.fetch(backend);
91
+ const { items, meta } = yield this.fetch(backend);
90
92
  const instances = [];
91
- for (const item of result) {
93
+ for (const item of items) {
92
94
  instances.push(this._obj.fromDataObject(item));
93
95
  }
94
- return instances;
96
+ return { items: instances, meta };
95
97
  });
96
98
  }
97
99
  execute(as = returnAs.AS_DATAOBJECTS, backend = Core_1.Core.getBackend()) {
@@ -1,10 +1,11 @@
1
- import { AbstractAdapter, BackendInterface, BackendParameters } from './AbstractAdapter';
1
+ import { AbstractAdapter } from './AbstractAdapter';
2
2
  import { MockAdapter } from './MockAdapter';
3
3
  import { BackendError } from './BackendError';
4
- import { Query } from './Query';
4
+ import { BackendInterface } from './types/BackendInterface';
5
+ import { Query, QueryResultType, QueryMetaType } from './Query';
5
6
  import { Filter } from './Filter';
6
7
  import { Filters } from './Filters';
7
8
  import { Sorting } from './Sorting';
8
9
  import { Limits } from './Limits';
9
10
  import { SortAndLimit } from './SortAndLimit';
10
- export { AbstractAdapter, BackendInterface, BackendParameters, BackendError, MockAdapter, Query, Filter, Filters, Sorting, Limits, SortAndLimit, };
11
+ export { BackendInterface, AbstractAdapter, BackendError, MockAdapter, Query, QueryResultType, QueryMetaType, Filter, Filters, Sorting, Limits, SortAndLimit, };
@@ -0,0 +1,9 @@
1
+ import { DataObjectClass } from '../../components';
2
+ import { BackendAction } from '../../Backend';
3
+ import Middleware from './Middleware';
4
+ export interface InjectKeywordsMiddlewareParams {
5
+ }
6
+ export declare class InjectKeywordsMiddleware implements Middleware {
7
+ execute(dataObject: DataObjectClass<any>, action: BackendAction): void;
8
+ protected _createKeywords(dataObject: DataObjectClass<any>): string[];
9
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InjectKeywordsMiddleware = void 0;
4
+ const Backend_1 = require("../../Backend");
5
+ const properties_1 = require("../../properties");
6
+ class InjectKeywordsMiddleware {
7
+ execute(dataObject, action) {
8
+ if (!dataObject.has('keywords')) {
9
+ dataObject.addProperty(new properties_1.ArrayProperty({ name: 'keywords' }));
10
+ }
11
+ switch (action) {
12
+ case Backend_1.BackendAction.CREATE:
13
+ case Backend_1.BackendAction.UPDATE:
14
+ dataObject.set('keywords', this._createKeywords(dataObject));
15
+ break;
16
+ default:
17
+ break;
18
+ }
19
+ return;
20
+ }
21
+ _createKeywords(dataObject) {
22
+ const keywords = [];
23
+ Object.keys(dataObject.properties)
24
+ .filter((key) => dataObject.get(key).fullSearch === true)
25
+ .forEach((key) => {
26
+ const val = dataObject.val(key);
27
+ if (val) {
28
+ val.toLowerCase()
29
+ .split(' ')
30
+ .forEach((word) => {
31
+ let seq = '';
32
+ word
33
+ .split('')
34
+ .splice(0, 15)
35
+ .forEach((letter) => {
36
+ seq += letter;
37
+ if (seq.length > 1) {
38
+ keywords.push(seq);
39
+ }
40
+ });
41
+ });
42
+ }
43
+ });
44
+ return [...new Set(keywords)];
45
+ }
46
+ }
47
+ exports.InjectKeywordsMiddleware = InjectKeywordsMiddleware;
@@ -1,12 +1,12 @@
1
1
  import { DataObjectClass } from '../../components';
2
2
  import { BackendAction } from '../../Backend';
3
- import { UserCore } from '../../components/User';
3
+ import { User } from '../../components/User';
4
4
  import Middleware from './Middleware';
5
5
  export interface InjectMetaMiddlewareParams {
6
- user: UserCore;
6
+ user: User;
7
7
  }
8
8
  export declare class InjectMetaMiddleware implements Middleware {
9
- protected _user: UserCore;
9
+ protected _user: User;
10
10
  constructor(params: InjectMetaMiddlewareParams);
11
11
  execute(dataObject: DataObjectClass<any>, action: BackendAction): void;
12
12
  }
@@ -0,0 +1,13 @@
1
+ import { DataObjectClass } from '../../components';
2
+ import { Filter } from '../Filter';
3
+ import { Filters } from '../Filters';
4
+ import { Query, QueryResultType } from '../Query';
5
+ import { SortAndLimit } from '../SortAndLimit';
6
+ export interface BackendInterface {
7
+ create(dataObject: DataObjectClass<any>, desiredUid: string | undefined): Promise<DataObjectClass<any>>;
8
+ read(param: string | DataObjectClass<any>): Promise<DataObjectClass<any>>;
9
+ update(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
10
+ delete(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
11
+ query(query: Query<any>): Promise<QueryResultType<DataObjectClass<any>>>;
12
+ find(dataObject: DataObjectClass<any>, filters: Filters | Filter[] | undefined, pagination: SortAndLimit | undefined): Promise<QueryResultType<DataObjectClass<any>>>;
13
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });