@quatrain/core 0.5.6 → 1.0.0-beta10

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 (100) hide show
  1. package/lib/Auth.d.ts +25 -0
  2. package/lib/Auth.js +9 -0
  3. package/lib/Backend.d.ts +34 -1
  4. package/lib/Backend.js +1 -9
  5. package/lib/Core.d.ts +11 -7
  6. package/lib/Core.js +13 -3
  7. package/lib/authentication/AbstractAuthAdapter.d.ts +18 -0
  8. package/lib/authentication/AbstractAuthAdapter.js +23 -0
  9. package/lib/authentication/AuthenticationError.d.ts +2 -0
  10. package/lib/authentication/AuthenticationError.js +6 -0
  11. package/lib/authentication/index.d.ts +4 -0
  12. package/lib/authentication/index.js +7 -0
  13. package/lib/authentication/middlewares/AbstractAuthMiddleware.d.ts +0 -0
  14. package/lib/authentication/middlewares/AbstractAuthMiddleware.js +1 -0
  15. package/lib/authentication/middlewares/AuthMiddleware.d.ts +5 -0
  16. package/lib/authentication/middlewares/AuthMiddleware.js +2 -0
  17. package/lib/authentication/middlewares/Middleware.d.ts +5 -0
  18. package/lib/authentication/middlewares/Middleware.js +2 -0
  19. package/lib/authentication/types/AuthInterface.d.ts +9 -0
  20. package/lib/authentication/types/AuthInterface.js +2 -0
  21. package/lib/backends/AbstractAdapter.d.ts +20 -45
  22. package/lib/backends/AbstractAdapter.js +27 -9
  23. package/lib/backends/Filters.d.ts +2 -1
  24. package/lib/backends/MockAdapter.d.ts +6 -4
  25. package/lib/backends/MockAdapter.js +15 -6
  26. package/lib/backends/Query.d.ts +29 -13
  27. package/lib/backends/Query.js +22 -9
  28. package/lib/backends/index.d.ts +5 -3
  29. package/lib/backends/index.js +26 -1
  30. package/lib/backends/middlewares/InjectKeywordsMiddleware.d.ts +9 -0
  31. package/lib/backends/middlewares/InjectKeywordsMiddleware.js +47 -0
  32. package/lib/backends/middlewares/InjectMetaMiddleware.d.ts +6 -6
  33. package/lib/backends/middlewares/InjectMetaMiddleware.js +10 -7
  34. package/lib/backends/middlewares/Middleware.d.ts +2 -2
  35. package/lib/backends/middlewares/types/MiddlewareParams.d.ts +3 -0
  36. package/lib/backends/middlewares/types/MiddlewareParams.js +2 -0
  37. package/lib/backends/types/BackendInterface.d.ts +13 -0
  38. package/lib/backends/types/BackendInterface.js +2 -0
  39. package/lib/backends/types/CollectionHierarchy.d.ts +3 -0
  40. package/lib/backends/types/CollectionHierarchy.js +6 -0
  41. package/lib/common/statuses.d.ts +3 -0
  42. package/lib/common/statuses.js +4 -1
  43. package/lib/components/AbstractObject.d.ts +9 -1
  44. package/lib/components/AbstractObject.js +9 -0
  45. package/lib/components/BaseObject.d.ts +3 -3
  46. package/lib/components/BaseObject.js +3 -0
  47. package/lib/components/BaseObjectCore.d.ts +16 -3
  48. package/lib/components/BaseObjectCore.js +21 -5
  49. package/lib/components/BaseRepository.d.ts +14 -13
  50. package/lib/components/BaseRepository.js +37 -21
  51. package/lib/components/DataObject.d.ts +20 -3
  52. package/lib/components/DataObject.js +73 -13
  53. package/lib/components/Entity.d.ts +4 -21
  54. package/lib/components/Entity.js +34 -32
  55. package/lib/components/EntityRepository.d.ts +3 -3
  56. package/lib/components/EntityRepository.js +1 -1
  57. package/lib/components/ObjectUri.js +2 -3
  58. package/lib/components/User.d.ts +4 -5
  59. package/lib/components/User.js +19 -10
  60. package/lib/components/UserRepository.d.ts +4 -4
  61. package/lib/components/UserRepository.js +2 -2
  62. package/lib/components/index.d.ts +3 -2
  63. package/lib/components/index.js +9 -2
  64. package/lib/components/types/DataObjectClass.d.ts +7 -1
  65. package/lib/components/types/RepositoryClass.d.ts +2 -2
  66. package/lib/components/types/toJSONParams.d.ts +8 -0
  67. package/lib/components/types/toJSONParams.js +2 -0
  68. package/lib/index.d.ts +10 -5
  69. package/lib/index.js +17 -2
  70. package/lib/properties/ArrayProperty.d.ts +1 -1
  71. package/lib/properties/ArrayProperty.js +2 -2
  72. package/lib/properties/BaseProperty.d.ts +4 -1
  73. package/lib/properties/BaseProperty.js +15 -3
  74. package/lib/properties/BooleanProperty.d.ts +1 -1
  75. package/lib/properties/BooleanProperty.js +2 -2
  76. package/lib/properties/CollectionProperty.d.ts +3 -3
  77. package/lib/properties/CollectionProperty.js +4 -3
  78. package/lib/properties/DateTimeProperty.d.ts +1 -1
  79. package/lib/properties/DateTimeProperty.js +2 -2
  80. package/lib/properties/EnumProperty.d.ts +1 -1
  81. package/lib/properties/EnumProperty.js +2 -2
  82. package/lib/properties/HashProperty.d.ts +7 -1
  83. package/lib/properties/HashProperty.js +10 -2
  84. package/lib/properties/MapProperty.d.ts +1 -1
  85. package/lib/properties/MapProperty.js +2 -2
  86. package/lib/properties/NumberProperty.d.ts +1 -1
  87. package/lib/properties/NumberProperty.js +2 -2
  88. package/lib/properties/ObjectProperty.d.ts +6 -6
  89. package/lib/properties/ObjectProperty.js +57 -5
  90. package/lib/properties/Property.d.ts +9 -2
  91. package/lib/properties/StringProperty.d.ts +2 -2
  92. package/lib/properties/StringProperty.js +2 -2
  93. package/lib/properties/types/AbstractPropertyType.d.ts +1 -0
  94. package/lib/properties/types/PropertyClassType.d.ts +4 -1
  95. package/lib/utils/DataGenerator.d.ts +2 -3
  96. package/lib/utils/DataGenerator.js +2 -1
  97. package/lib/wrappers/cloud/AbstractCloudWrapper.d.ts +4 -0
  98. package/lib/wrappers/cloud/AbstractCloudWrapper.js +9 -0
  99. package/package.json +3 -2
  100. package/README.md +0 -124
package/lib/Auth.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ import Middleware from './authentication/middlewares/AuthMiddleware';
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,39 @@
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
- DELETE = "delete"
6
+ DELETE = "delete",
7
+ WRITE = "write"
8
+ }
9
+ /**
10
+ * Default interface for a backend record
11
+ */
12
+ export interface BackendRecordType {
13
+ uid: string | undefined;
14
+ path: string | undefined;
15
+ [key: string]: any;
16
+ }
17
+ /**
18
+ * Backend Parameters acceptable keys
19
+ */
20
+ export type BackendParametersKeys = 'host' | 'alias' | 'mapping' | 'middlewares' | 'config' | 'fixtures' | 'softDelete' | 'debug';
21
+ /**
22
+ * Backend parameters interface
23
+ */
24
+ export interface BackendParameters {
25
+ host?: string;
26
+ alias?: string;
27
+ hierarchy?: {
28
+ [collection: string]: any;
29
+ };
30
+ mapping?: {
31
+ [x: string]: any;
32
+ };
33
+ middlewares?: Middleware[];
34
+ config?: any;
35
+ fixtures?: any;
36
+ softDelete?: boolean;
37
+ useNativeForeignKeys?: boolean;
38
+ debug?: boolean;
6
39
  }
package/lib/Backend.js CHANGED
@@ -1,19 +1,11 @@
1
1
  "use strict";
2
- // export const CREATE = 'create'
3
- // export const READ = 'read'
4
- // export const UPDATE = 'update'
5
- // export const DELETE = 'delete'
6
2
  Object.defineProperty(exports, "__esModule", { value: true });
7
3
  exports.BackendAction = void 0;
8
- // export type BackendActions =
9
- // | typeof CREATE
10
- // | typeof READ
11
- // | typeof UPDATE
12
- // | typeof DELETE
13
4
  var BackendAction;
14
5
  (function (BackendAction) {
15
6
  BackendAction["CREATE"] = "create";
16
7
  BackendAction["READ"] = "read";
17
8
  BackendAction["UPDATE"] = "update";
18
9
  BackendAction["DELETE"] = "delete";
10
+ BackendAction["WRITE"] = "write";
19
11
  })(BackendAction || (exports.BackendAction = BackendAction = {}));
package/lib/Core.d.ts CHANGED
@@ -1,19 +1,21 @@
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';
4
5
  export type BackendRegistry<T extends AbstractAdapter> = {
5
6
  [x: string]: T;
6
7
  };
8
+ export interface AuthAdapter extends AbstractAuthAdapter {
9
+ }
7
10
  export declare class Core {
8
11
  static defaultBackend: string;
9
- static currentUser: User;
12
+ static userClass: typeof User;
10
13
  static classRegistry: {
11
14
  [key: string]: any;
12
15
  };
13
- static logger: {
14
- (...data: any[]): void;
15
- (message?: any, ...optionalParams: any[]): void;
16
- };
16
+ static logger: Console;
17
+ static auth: AuthAdapter | AbstractAuthAdapter;
18
+ static timestamp: () => string;
17
19
  protected static _backends: BackendRegistry<any>;
18
20
  static definition(key: string): {
19
21
  manifest: {
@@ -23,6 +25,8 @@ export declare class Core {
23
25
  };
24
26
  static addBackend(backend: AbstractAdapter, alias: string, setDefault?: boolean): void;
25
27
  static getBackend<T extends AbstractAdapter>(alias?: string): T;
28
+ static addClass(name: string, obj: any): void;
29
+ static getClass(name: string): any;
26
30
  /**
27
31
  * Returns the class to use for a data object
28
32
  * This is currently just a stub that will be implemented from config in the future
@@ -32,8 +36,8 @@ export declare class Core {
32
36
  /**
33
37
  * Log message using defined logger
34
38
  * This is currently just a stub that will be implemented from config in the future
35
- * @param message string
39
+ * @param message string | object
36
40
  * @param level string
37
41
  */
38
- static log(message: string, src?: string, level?: string): void;
42
+ static log(message: any, src?: string, level?: string): void;
39
43
  }
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 {
@@ -25,6 +26,12 @@ class Core {
25
26
  throw new Error(`Unknown backend alias: '${alias}'`);
26
27
  }
27
28
  }
29
+ static addClass(name, obj) {
30
+ Core.classRegistry[name] = obj;
31
+ }
32
+ static getClass(name) {
33
+ return Core.classRegistry[name];
34
+ }
28
35
  /**
29
36
  * Returns the class to use for a data object
30
37
  * This is currently just a stub that will be implemented from config in the future
@@ -36,15 +43,18 @@ class Core {
36
43
  /**
37
44
  * Log message using defined logger
38
45
  * This is currently just a stub that will be implemented from config in the future
39
- * @param message string
46
+ * @param message string | object
40
47
  * @param level string
41
48
  */
42
49
  static log(message, src = 'Core', level = 'NOTICE') {
43
- Core.logger(`${Date.now()} - [${src}] ${message}`);
50
+ Core.logger.log(`${Date.now()} - [${src}] ${typeof message === 'string' ? message : JSON.stringify(message)}`);
44
51
  }
45
52
  }
46
53
  exports.Core = Core;
47
54
  Core.defaultBackend = '@default';
55
+ Core.userClass = User_1.User;
48
56
  Core.classRegistry = {};
49
- Core.logger = console.log;
57
+ Core.logger = console;
58
+ // How timestamp are formatted
59
+ Core.timestamp = () => new Date().toISOString();
50
60
  Core._backends = {};
@@ -0,0 +1,18 @@
1
+ import { AuthParameters, AuthParametersKeys } from '../Auth';
2
+ import { User } from '../components/User';
3
+ import { AuthInterface } from './types/AuthInterface';
4
+ export declare abstract class AbstractAuthAdapter implements AuthInterface {
5
+ protected _alias: string;
6
+ protected _params: AuthParameters;
7
+ constructor(params?: AuthParameters);
8
+ setParam(key: AuthParametersKeys, value: any): void;
9
+ getParam(key: AuthParametersKeys): any;
10
+ set alias(alias: string);
11
+ get alias(): string;
12
+ abstract register(user: User): Promise<any>;
13
+ abstract signup(login: string, password: string): Promise<any>;
14
+ abstract signout(user: User): Promise<any>;
15
+ abstract update(user: User, updatable: any): Promise<any>;
16
+ abstract delete(user: User): Promise<any>;
17
+ abstract getAuthToken(token: string): any;
18
+ }
@@ -0,0 +1,23 @@
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._alias = params.alias || '';
9
+ }
10
+ setParam(key, value) {
11
+ this._params[key] = value;
12
+ }
13
+ getParam(key) {
14
+ return this._params[key];
15
+ }
16
+ set alias(alias) {
17
+ this._alias = alias;
18
+ }
19
+ get alias() {
20
+ return this._alias;
21
+ }
22
+ }
23
+ 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 AuthMiddleware {
4
+ execute: (user: User, action: AuthAction) => void;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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,58 +1,33 @@
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
- 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>;
8
+ import BackendMiddleware from './middlewares/Middleware';
9
+ import { MiddlewareParams } from './middlewares/types/MiddlewareParams';
10
+ interface BM extends BackendMiddleware {
43
11
  }
44
12
  export declare abstract class AbstractAdapter implements BackendInterface {
13
+ static PKEY_IDENTIFIER: any;
45
14
  protected _alias: string;
46
15
  protected _params: BackendParameters;
47
- protected _middlewares: Middleware[];
16
+ protected _middlewares: BM[];
48
17
  constructor(params?: BackendParameters);
49
18
  setParam(key: BackendParametersKeys, value: any): void;
50
19
  getParam(key: BackendParametersKeys): any;
51
- addMiddleware(middleware: Middleware): void;
20
+ addMiddleware(middleware: BM): void;
21
+ /**
22
+ * Returns true if a given middleware is attached
23
+ * @param className
24
+ * @returns boolean
25
+ */
26
+ hasMiddleware(className: string): boolean;
52
27
  set alias(alias: string);
53
28
  get alias(): string;
54
29
  getCollection(dao: DataObjectClass<any>): string | undefined;
55
- abstract create(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
30
+ abstract create(dataObject: DataObjectClass<any>, desiredUid?: string | undefined): Promise<DataObjectClass<any>>;
56
31
  abstract read(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
57
32
  abstract update(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
58
33
  abstract delete(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
@@ -62,9 +37,9 @@ export declare abstract class AbstractAdapter implements BackendInterface {
62
37
  * @param query
63
38
  * @returns Array
64
39
  */
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>[]>;
40
+ query(query: Query<any>): Promise<QueryResultType<DataObjectClass<any>>>;
41
+ abstract find(dataObject: DataObjectClass<any>, filters: Filters | Filter[] | undefined, pagination: SortAndLimit | undefined, parent: any): Promise<QueryResultType<any>>;
68
42
  log(message: string): void;
69
- executeMiddlewares(dataObject: DataObjectClass<any>, action: BackendAction): Promise<DataObjectClass<any>>;
43
+ executeMiddlewares(dataObject: DataObjectClass<any>, action: BackendAction, params?: MiddlewareParams): Promise<DataObjectClass<any>>;
70
44
  }
45
+ export {};
@@ -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 = '';
@@ -17,6 +18,7 @@ class AbstractAdapter {
17
18
  this._middlewares = [];
18
19
  this._alias = params.alias || '';
19
20
  this._middlewares = params.middlewares || [];
21
+ this._params = params;
20
22
  }
21
23
  setParam(key, value) {
22
24
  this._params[key] = value;
@@ -25,8 +27,25 @@ class AbstractAdapter {
25
27
  return this._params[key];
26
28
  }
27
29
  addMiddleware(middleware) {
30
+ if (this.hasMiddleware(middleware.constructor.name)) {
31
+ throw new BackendError_1.BackendError(`Middleware '${middleware.constructor.name}' is already attached`);
32
+ }
28
33
  this._middlewares.push(middleware);
29
34
  }
35
+ /**
36
+ * Returns true if a given middleware is attached
37
+ * @param className
38
+ * @returns boolean
39
+ */
40
+ hasMiddleware(className) {
41
+ let has = false;
42
+ this._middlewares.forEach((middleware) => {
43
+ if (middleware.constructor.name === className) {
44
+ has = true;
45
+ }
46
+ });
47
+ return has;
48
+ }
30
49
  set alias(alias) {
31
50
  this._alias = alias;
32
51
  }
@@ -34,7 +53,7 @@ class AbstractAdapter {
34
53
  return this._alias;
35
54
  }
36
55
  getCollection(dao) {
37
- return dao.uri.collection; //|| dao.uri.class.name.toLowerCase() //class.COLLECTION
56
+ return dao.uri.collection;
38
57
  }
39
58
  /**
40
59
  * Process Query instance and return result
@@ -43,12 +62,10 @@ class AbstractAdapter {
43
62
  */
44
63
  query(query) {
45
64
  return __awaiter(this, void 0, void 0, function* () {
46
- return yield this.find(yield query.obj.daoFactory(), query.filters, query.sortAndLimit);
47
- });
48
- }
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;
65
+ const dao = yield query.obj.daoFactory();
66
+ if (query.parent) {
67
+ }
68
+ return yield this.find(dao, query.filters, query.sortAndLimit, query.parent);
52
69
  });
53
70
  }
54
71
  log(message) {
@@ -56,13 +73,14 @@ class AbstractAdapter {
56
73
  console.log(message);
57
74
  }
58
75
  }
59
- executeMiddlewares(dataObject, action) {
76
+ executeMiddlewares(dataObject, action, params) {
60
77
  return __awaiter(this, void 0, void 0, function* () {
61
78
  for (const middleware of this._middlewares) {
62
- yield middleware.execute(dataObject, action);
79
+ yield middleware.execute(dataObject, action, params);
63
80
  }
64
81
  return dataObject;
65
82
  });
66
83
  }
67
84
  }
68
85
  exports.AbstractAdapter = AbstractAdapter;
86
+ AbstractAdapter.PKEY_IDENTIFIER = 'id';
@@ -2,9 +2,10 @@ import { Filter } from './Filter';
2
2
  export interface FiltersType {
3
3
  or?: Filter[];
4
4
  and?: Filter[];
5
+ base?: string;
5
6
  }
6
7
  export declare class Filters implements FiltersType {
7
8
  or?: Filter[];
8
9
  and?: Filter[];
9
- constructor(or: Filter[] | undefined, and: Filter[] | undefined);
10
+ constructor(or?: Filter[], and?: Filter[]);
10
11
  }
@@ -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,37 +2,53 @@ 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",
16
- AS_INSTANCES = "classInstances"
26
+ AS_INSTANCES = "classInstances",
27
+ AS_IS = "asIs"
17
28
  }
18
29
  export declare class Query<T extends typeof BaseObjectCore> {
19
30
  protected _obj: T;
20
- protected _params: any;
31
+ protected _parent: T | undefined;
21
32
  filters: Filter[];
22
33
  sortings: Sorting[];
23
34
  limits: Limits;
24
- constructor(obj: T, params?: {
25
- [x: string]: any;
26
- });
35
+ meta: any;
36
+ constructor(obj: T, parent?: T);
27
37
  get obj(): T;
28
- where(param: Filter | string, value?: any, operator?: any): this;
38
+ get parent(): T | undefined;
39
+ /**
40
+ * Declare query parent record
41
+ * This may be need in some NoSQL backend to query subcollections
42
+ */
43
+ set parent(parent: T | undefined);
44
+ where(param: Filter | string | any, value?: any, operator?: any): this;
29
45
  sortBy(param: Sorting | string, order?: any): this;
30
46
  setLimits(limits: Limits): this;
31
47
  offset(offset?: number): this;
32
48
  batch(batch?: number): this;
33
49
  get sortAndLimit(): SortAndLimit;
34
- fetch(backend?: BackendInterface): Promise<DataObjectClass<any>[]>;
35
- fetchAsUri(backend?: BackendInterface): Promise<ObjectUri[]>;
36
- fetchAsInstances(backend?: BackendInterface): Promise<Persisted<BaseObject>[]>;
37
- execute(as?: returnAs, backend?: BackendInterface): Promise<DataObjectClass<any>[] | ObjectUri[] | Persisted<BaseObject>[]>;
50
+ fetch(backend?: BackendInterface): Promise<QueryResultType<DataObjectClass<any>>>;
51
+ fetchAsUri(backend?: BackendInterface): Promise<QueryResultType<ObjectUri>>;
52
+ fetchAsInstances(backend?: BackendInterface): Promise<QueryResultType<T>>;
53
+ execute(as?: returnAs, backend?: BackendInterface): Promise<QueryResultType<any>>;
38
54
  }