@quatrain/core 0.3.0 → 0.4.0
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/lib/Backend.d.ts +5 -0
- package/lib/Backend.js +7 -0
- package/lib/Core.d.ts +20 -2
- package/lib/Core.js +24 -5
- package/lib/backends/AbstractAdapter.d.ts +27 -13
- package/lib/backends/AbstractAdapter.js +31 -3
- package/lib/backends/Filter.d.ts +5 -3
- package/lib/backends/Filter.js +1 -1
- package/lib/backends/MockAdapter.d.ts +8 -16
- package/lib/backends/MockAdapter.js +67 -41
- package/lib/backends/NotFoundError.d.ts +2 -0
- package/lib/backends/NotFoundError.js +6 -0
- package/lib/backends/Query.d.ts +3 -3
- package/lib/backends/middlewares/AbstractMiddleware.d.ts +0 -0
- package/lib/backends/middlewares/AbstractMiddleware.js +1 -0
- package/lib/backends/middlewares/InjectMetaMiddleware.d.ts +11 -0
- package/lib/backends/middlewares/InjectMetaMiddleware.js +50 -0
- package/lib/components/AbstractObject.d.ts +8 -5
- package/lib/components/AbstractObject.js +9 -0
- package/lib/components/BaseObject.d.ts +2 -2
- package/lib/components/BaseObject.js +2 -2
- package/lib/components/BaseObjectProperties.d.ts +1 -2
- package/lib/components/BaseObjectProperties.js +2 -0
- package/lib/components/DataObject.d.ts +9 -7
- package/lib/components/DataObject.js +31 -18
- package/lib/components/EntityProperties.js +37 -2
- package/lib/components/ObjectUri.d.ts +1 -1
- package/lib/components/ObjectUri.js +16 -4
- package/lib/components/User.d.ts +2 -0
- package/lib/components/User.js +103 -3
- package/lib/components/UserProperties.js +45 -1
- package/lib/components/index.d.ts +2 -2
- package/lib/components/types/AbstractObjectClass.d.ts +2 -2
- package/lib/components/types/BaseObjectClass.d.ts +2 -2
- package/lib/components/types/DataObjectClass.d.ts +7 -5
- package/lib/index.d.ts +5 -1
- package/lib/index.js +7 -1
- package/lib/properties/BaseProperty.d.ts +8 -3
- package/lib/properties/BaseProperty.js +10 -1
- package/lib/properties/CollectionProperty.d.ts +11 -8
- package/lib/properties/CollectionProperty.js +10 -9
- package/lib/properties/EnumProperty.d.ts +1 -0
- package/lib/properties/EnumProperty.js +4 -1
- package/lib/properties/Property.d.ts +1 -1
- package/lib/properties/StringProperty.d.ts +4 -0
- package/lib/properties/StringProperty.js +32 -17
- package/lib/properties/types/PropertyClassType.d.ts +1 -1
- package/lib/properties/types/PropertyHTMLType.d.ts +10 -0
- package/lib/properties/types/PropertyHTMLType.js +14 -0
- package/lib/utils/DataGenerator.d.ts +9 -0
- package/lib/utils/DataGenerator.js +98 -0
- package/lib/utils/index.d.ts +2 -0
- package/lib/utils/index.js +5 -0
- package/package.json +1 -1
package/lib/Backend.d.ts
ADDED
package/lib/Backend.js
ADDED
package/lib/Core.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AbstractAdapter } from './backends/AbstractAdapter';
|
|
2
|
+
import { DataObjectClass } from './components/types/DataObjectClass';
|
|
2
3
|
import { UserClass } from './components/types/UserClass';
|
|
3
4
|
export declare type BackendRegistry<T extends AbstractAdapter> = {
|
|
4
5
|
[x: string]: T;
|
|
@@ -6,6 +7,10 @@ export declare type BackendRegistry<T extends AbstractAdapter> = {
|
|
|
6
7
|
export declare class Core {
|
|
7
8
|
static defaultBackend: string;
|
|
8
9
|
static currentUser: UserClass;
|
|
10
|
+
static logger: {
|
|
11
|
+
(...data: any[]): void;
|
|
12
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
13
|
+
};
|
|
9
14
|
protected static _backends: BackendRegistry<any>;
|
|
10
15
|
static definition(key: string): {
|
|
11
16
|
manifest: {
|
|
@@ -13,6 +18,19 @@ export declare class Core {
|
|
|
13
18
|
mandatory: boolean;
|
|
14
19
|
};
|
|
15
20
|
};
|
|
16
|
-
static addBackend(backend: AbstractAdapter, alias: string): void;
|
|
17
|
-
static getBackend(alias?: string):
|
|
21
|
+
static addBackend(backend: AbstractAdapter, alias: string, setDefault?: boolean): void;
|
|
22
|
+
static getBackend<T extends AbstractAdapter>(alias?: string): T;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the class to use for a data object
|
|
25
|
+
* This is currently just a stub that will be implemented from config in the future
|
|
26
|
+
* @returns DataObjectClass
|
|
27
|
+
*/
|
|
28
|
+
static getDataObjectClass(): DataObjectClass<any>;
|
|
29
|
+
/**
|
|
30
|
+
* Log message using defined logger
|
|
31
|
+
* This is currently just a stub that will be implemented from config in the future
|
|
32
|
+
* @param message string
|
|
33
|
+
* @param level string
|
|
34
|
+
*/
|
|
35
|
+
static log(message: string, src?: string, level?: string): void;
|
|
18
36
|
}
|
package/lib/Core.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Core = void 0;
|
|
4
|
-
const
|
|
4
|
+
const DataObject_1 = require("./components/DataObject");
|
|
5
5
|
class Core {
|
|
6
6
|
static definition(key) {
|
|
7
7
|
return {
|
|
@@ -11,8 +11,11 @@ class Core {
|
|
|
11
11
|
},
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
-
static addBackend(backend, alias) {
|
|
14
|
+
static addBackend(backend, alias, setDefault = false) {
|
|
15
15
|
Core._backends[alias] = backend;
|
|
16
|
+
if (setDefault) {
|
|
17
|
+
Core.defaultBackend = alias;
|
|
18
|
+
}
|
|
16
19
|
}
|
|
17
20
|
static getBackend(alias = this.defaultBackend) {
|
|
18
21
|
if (this._backends[alias]) {
|
|
@@ -22,9 +25,25 @@ class Core {
|
|
|
22
25
|
throw new Error(`Unknown backend alias: '${alias}'`);
|
|
23
26
|
}
|
|
24
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns the class to use for a data object
|
|
30
|
+
* This is currently just a stub that will be implemented from config in the future
|
|
31
|
+
* @returns DataObjectClass
|
|
32
|
+
*/
|
|
33
|
+
static getDataObjectClass() {
|
|
34
|
+
return DataObject_1.DataObject.prototype;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Log message using defined logger
|
|
38
|
+
* This is currently just a stub that will be implemented from config in the future
|
|
39
|
+
* @param message string
|
|
40
|
+
* @param level string
|
|
41
|
+
*/
|
|
42
|
+
static log(message, src = 'Core', level = 'NOTICE') {
|
|
43
|
+
Core.logger(`[${src}] ${message}`, level);
|
|
44
|
+
}
|
|
25
45
|
}
|
|
26
46
|
exports.Core = Core;
|
|
27
47
|
Core.defaultBackend = '@default';
|
|
28
|
-
Core.
|
|
29
|
-
|
|
30
|
-
};
|
|
48
|
+
Core.logger = console.log;
|
|
49
|
+
Core._backends = {};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { DataObjectClass } from '../components';
|
|
2
|
-
import { BaseObjectClass } from '../components/types/BaseObjectClass';
|
|
1
|
+
import { DataObjectClass } from '../components/types/DataObjectClass';
|
|
3
2
|
import { Filter } from './Filter';
|
|
4
3
|
import { Filters } from './Filters';
|
|
5
4
|
import { Query } from './Query';
|
|
@@ -15,37 +14,52 @@ export interface BackendRecordType {
|
|
|
15
14
|
/**
|
|
16
15
|
* Backend Parameters acceptable keys
|
|
17
16
|
*/
|
|
18
|
-
export declare type BackendParametersKeys = 'alias' | 'mapping' | '
|
|
17
|
+
export declare type BackendParametersKeys = 'host' | 'alias' | 'mapping' | 'middlewares' | 'config' | 'fixtures' | 'softDelete' | 'debug';
|
|
19
18
|
/**
|
|
20
19
|
* Backend parameters interface
|
|
21
20
|
*/
|
|
22
21
|
export interface BackendParameters {
|
|
22
|
+
host?: string;
|
|
23
23
|
alias?: string;
|
|
24
24
|
mapping?: {
|
|
25
25
|
[x: string]: any;
|
|
26
26
|
};
|
|
27
|
-
|
|
27
|
+
middlewares?: any[];
|
|
28
28
|
config?: any;
|
|
29
29
|
fixtures?: any;
|
|
30
30
|
softDelete?: boolean;
|
|
31
31
|
debug?: boolean;
|
|
32
32
|
}
|
|
33
|
-
export interface BackendInterface
|
|
34
|
-
create(dataObject: DataObjectClass
|
|
35
|
-
read(param: string | DataObjectClass): Promise<DataObjectClass
|
|
36
|
-
update(dataObject: DataObjectClass): Promise<DataObjectClass
|
|
37
|
-
delete(dataObject: DataObjectClass): Promise<DataObjectClass
|
|
38
|
-
query(query: Query<any>): Promise<DataObjectClass[]>;
|
|
39
|
-
find(dataObject: DataObjectClass
|
|
33
|
+
export interface BackendInterface {
|
|
34
|
+
create(dataObject: DataObjectClass<any>, desiredUid: string | undefined): Promise<DataObjectClass<any>>;
|
|
35
|
+
read(param: string | DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
36
|
+
update(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
37
|
+
delete(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
38
|
+
query(query: Query<any>): Promise<DataObjectClass<any>[]>;
|
|
39
|
+
find(dataObject: DataObjectClass<any>, filters: Filters | Filter[] | undefined, pagination: SortAndLimit | undefined): Promise<DataObjectClass<any>[]>;
|
|
40
40
|
}
|
|
41
41
|
export declare abstract class AbstractAdapter {
|
|
42
42
|
protected _alias: string;
|
|
43
43
|
protected _params: BackendParameters;
|
|
44
|
-
protected
|
|
45
|
-
constructor(params
|
|
44
|
+
protected _middlewares: any[];
|
|
45
|
+
constructor(params?: BackendParameters);
|
|
46
46
|
setParam(key: BackendParametersKeys, value: any): void;
|
|
47
47
|
getParam(key: BackendParametersKeys): any;
|
|
48
48
|
set alias(alias: string);
|
|
49
49
|
get alias(): string;
|
|
50
|
+
getCollection(dao: DataObjectClass<any>): any;
|
|
51
|
+
abstract create(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
52
|
+
abstract read(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
53
|
+
abstract update(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
54
|
+
abstract delete(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
55
|
+
abstract deleteCollection(collection: string, batchSize?: number): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Process Query instance and return result
|
|
58
|
+
* @param query
|
|
59
|
+
* @returns Array
|
|
60
|
+
*/
|
|
61
|
+
query(query: Query<any>): Promise<DataObjectClass<any>[]>;
|
|
62
|
+
abstract find(dataObject: DataObjectClass<any>, filters: Filters | Filter[] | undefined, pagination: SortAndLimit | undefined): Promise<DataObjectClass<any>[]>;
|
|
50
63
|
log(message: string): void;
|
|
64
|
+
executeMiddlewares(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
51
65
|
}
|
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.AbstractAdapter = void 0;
|
|
4
13
|
class AbstractAdapter {
|
|
5
|
-
constructor(params) {
|
|
14
|
+
constructor(params = {}) {
|
|
6
15
|
this._alias = '';
|
|
7
16
|
this._params = {};
|
|
8
|
-
this.
|
|
17
|
+
this._middlewares = [];
|
|
9
18
|
this._alias = params.alias || '';
|
|
10
|
-
this.
|
|
19
|
+
this._middlewares = params.middlewares || [];
|
|
11
20
|
}
|
|
12
21
|
setParam(key, value) {
|
|
13
22
|
this._params[key] = value;
|
|
@@ -21,10 +30,29 @@ class AbstractAdapter {
|
|
|
21
30
|
get alias() {
|
|
22
31
|
return this._alias;
|
|
23
32
|
}
|
|
33
|
+
getCollection(dao) {
|
|
34
|
+
return dao.uri.class.name.toLowerCase();
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Process Query instance and return result
|
|
38
|
+
* @param query
|
|
39
|
+
* @returns Array
|
|
40
|
+
*/
|
|
41
|
+
query(query) {
|
|
42
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
43
|
+
return this.find(yield query.obj.daoFactory(), query.filters, query.sortAndLimit);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
24
46
|
log(message) {
|
|
25
47
|
if (this._params['debug'] === true) {
|
|
26
48
|
console.log(message);
|
|
27
49
|
}
|
|
28
50
|
}
|
|
51
|
+
executeMiddlewares(dataObject) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
this._middlewares.forEach((middleware) => __awaiter(this, void 0, void 0, function* () { return yield middleware.execute(dataObject); }));
|
|
54
|
+
return dataObject;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
29
57
|
}
|
|
30
58
|
exports.AbstractAdapter = AbstractAdapter;
|
package/lib/backends/Filter.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { BaseObject } from '../components';
|
|
1
2
|
import { FilterOperatorType } from './FilterOperators';
|
|
3
|
+
export declare type FilterValueType<T extends BaseObject> = T | number | string | string[] | null;
|
|
2
4
|
export interface FilterType {
|
|
3
5
|
prop: string;
|
|
4
6
|
operator: FilterOperatorType | undefined;
|
|
5
|
-
value:
|
|
7
|
+
value: FilterValueType<any>;
|
|
6
8
|
}
|
|
7
9
|
/**
|
|
8
10
|
* Filter object
|
|
@@ -10,6 +12,6 @@ export interface FilterType {
|
|
|
10
12
|
export declare class Filter implements FilterType {
|
|
11
13
|
prop: string;
|
|
12
14
|
operator: FilterOperatorType;
|
|
13
|
-
value:
|
|
14
|
-
constructor(prop: string, value
|
|
15
|
+
value: FilterValueType<any>;
|
|
16
|
+
constructor(prop: string, value: FilterValueType<any>, operator?: FilterOperatorType);
|
|
15
17
|
}
|
package/lib/backends/Filter.js
CHANGED
|
@@ -1,31 +1,23 @@
|
|
|
1
1
|
import { AbstractAdapter, BackendInterface } from './AbstractAdapter';
|
|
2
2
|
import { BackendRecordType } from './AbstractAdapter';
|
|
3
|
-
import { Query } from './Query';
|
|
4
3
|
import { Filter } from './Filter';
|
|
5
4
|
import { Filters } from './Filters';
|
|
6
5
|
import { SortAndLimit } from './SortAndLimit';
|
|
7
6
|
import { DataObjectClass } from '../components/types/DataObjectClass';
|
|
8
|
-
|
|
9
|
-
export declare class MockAdapter<T extends BaseObjectClass> extends AbstractAdapter implements BackendInterface<T> {
|
|
7
|
+
export declare class MockAdapter extends AbstractAdapter implements BackendInterface {
|
|
10
8
|
protected static _fixtures: any;
|
|
11
9
|
/**
|
|
12
10
|
* Inject fixtures data to backend adapter
|
|
13
|
-
* Force this backend to be default one
|
|
14
11
|
* @param data BackendRecordType
|
|
15
12
|
*/
|
|
16
13
|
static inject(data: BackendRecordType): void;
|
|
17
14
|
static dao2backend(data: BackendRecordType): Promise<any>;
|
|
18
|
-
static getFixtures(): any;
|
|
15
|
+
static getFixtures(): any[];
|
|
19
16
|
static getFixture(key: string): any;
|
|
20
|
-
create(dataObject: DataObjectClass): Promise<DataObjectClass
|
|
21
|
-
read(dataObject: DataObjectClass): Promise<DataObjectClass
|
|
22
|
-
update(dataObject: DataObjectClass): Promise<DataObjectClass
|
|
23
|
-
delete(dataObject: DataObjectClass): Promise<DataObjectClass
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
* @param query
|
|
27
|
-
* @returns Array
|
|
28
|
-
*/
|
|
29
|
-
query(query: Query<any>): Promise<DataObjectClass[]>;
|
|
30
|
-
find(dataObject: DataObjectClass, filters?: Filters | Filter[] | undefined, pagination?: SortAndLimit | undefined): Promise<DataObjectClass[]>;
|
|
17
|
+
create(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
18
|
+
read(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
19
|
+
update(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
20
|
+
delete(dataObject: DataObjectClass<any>): Promise<DataObjectClass<any>>;
|
|
21
|
+
deleteCollection(collection: string, batchSize?: number | undefined): Promise<void>;
|
|
22
|
+
find(dataObject: DataObjectClass<any>, filters?: Filters | Filter[] | undefined, pagination?: SortAndLimit | undefined): Promise<DataObjectClass<any>[]>;
|
|
31
23
|
}
|
|
@@ -11,20 +11,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.MockAdapter = void 0;
|
|
13
13
|
const AbstractAdapter_1 = require("./AbstractAdapter");
|
|
14
|
-
const statuses_1 = require("../statuses");
|
|
15
14
|
const ObjectUri_1 = require("../components/ObjectUri");
|
|
16
|
-
const Core_1 = require("../Core");
|
|
17
15
|
const faker_1 = require("@faker-js/faker");
|
|
16
|
+
const BackendError_1 = require("./BackendError");
|
|
17
|
+
const NotFoundError_1 = require("./NotFoundError");
|
|
18
18
|
class MockAdapter extends AbstractAdapter_1.AbstractAdapter {
|
|
19
19
|
/**
|
|
20
20
|
* Inject fixtures data to backend adapter
|
|
21
|
-
* Force this backend to be default one
|
|
22
21
|
* @param data BackendRecordType
|
|
23
22
|
*/
|
|
24
23
|
static inject(data) {
|
|
25
24
|
if (data.path) {
|
|
26
25
|
MockAdapter._fixtures[data.path] = this.dao2backend(data);
|
|
27
|
-
Core_1.Core.defaultBackend = '@mock';
|
|
28
26
|
}
|
|
29
27
|
else {
|
|
30
28
|
throw new Error(`Can't inject data without a path`);
|
|
@@ -51,72 +49,100 @@ class MockAdapter extends AbstractAdapter_1.AbstractAdapter {
|
|
|
51
49
|
return MockAdapter._fixtures[key];
|
|
52
50
|
}
|
|
53
51
|
create(dataObject) {
|
|
54
|
-
const uri = `${
|
|
55
|
-
MockAdapter.inject(Object.assign(Object.assign({}, dataObject.toJSON()), { uid: uri, path: uri }));
|
|
56
|
-
if (this._params.injectMeta) {
|
|
57
|
-
dataObject.set('createdBy', Core_1.Core.currentUser);
|
|
58
|
-
dataObject.set('createdAt', Date.now());
|
|
59
|
-
}
|
|
52
|
+
const uri = `${this.getCollection(dataObject)}/${faker_1.faker.random.alphaNumeric(12)}`;
|
|
60
53
|
dataObject.uri = new ObjectUri_1.ObjectUri(uri);
|
|
61
|
-
return new Promise(() =>
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
try {
|
|
56
|
+
MockAdapter.inject(Object.assign(Object.assign({}, dataObject.toJSON()), { uid: uri, path: uri }));
|
|
57
|
+
resolve(dataObject);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
reject(new BackendError_1.BackendError(err.message));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
62
63
|
}
|
|
63
64
|
read(dataObject) {
|
|
64
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
const path = dataObject.path;
|
|
68
|
+
const data = MockAdapter._fixtures[path];
|
|
69
|
+
if (data === undefined) {
|
|
70
|
+
reject(new NotFoundError_1.NotFoundError(`[Mock] No data for ${path}`));
|
|
71
|
+
}
|
|
72
|
+
this.log(`[DAO] Populating ${dataObject.path}`);
|
|
73
|
+
resolve(yield dataObject.populate(data));
|
|
74
|
+
}));
|
|
72
75
|
});
|
|
73
76
|
}
|
|
74
77
|
update(dataObject) {
|
|
75
78
|
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
-
return new Promise(() =>
|
|
79
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
const path = dataObject.path;
|
|
81
|
+
const data = MockAdapter._fixtures[path];
|
|
82
|
+
if (data === undefined) {
|
|
83
|
+
reject(new NotFoundError_1.NotFoundError(`[Mock] No data for ${path}`));
|
|
84
|
+
}
|
|
85
|
+
this.log(`[DAO] Updating ${dataObject.path}`);
|
|
86
|
+
MockAdapter._fixtures[path] = Object.assign(Object.assign({}, data), dataObject.toJSON());
|
|
87
|
+
resolve(dataObject);
|
|
88
|
+
}));
|
|
77
89
|
});
|
|
78
90
|
}
|
|
79
91
|
delete(dataObject) {
|
|
80
92
|
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
|
|
82
|
-
dataObject.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
93
|
+
return new Promise((resolve, reject) => {
|
|
94
|
+
const path = dataObject.path;
|
|
95
|
+
const data = MockAdapter._fixtures[path];
|
|
96
|
+
if (data === undefined) {
|
|
97
|
+
reject(new NotFoundError_1.NotFoundError(`[Mock] No data for ${path}`));
|
|
98
|
+
}
|
|
99
|
+
delete MockAdapter._fixtures[dataObject.path];
|
|
87
100
|
dataObject.uri = new ObjectUri_1.ObjectUri();
|
|
88
|
-
|
|
89
|
-
|
|
101
|
+
resolve(dataObject);
|
|
102
|
+
});
|
|
90
103
|
});
|
|
91
104
|
}
|
|
92
|
-
|
|
93
|
-
* Process Query instance and return result
|
|
94
|
-
* @param query
|
|
95
|
-
* @returns Array
|
|
96
|
-
*/
|
|
97
|
-
query(query) {
|
|
105
|
+
deleteCollection(collection, batchSize) {
|
|
98
106
|
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
-
|
|
107
|
+
for (let key in MockAdapter._fixtures) {
|
|
108
|
+
if (key.startsWith(`${collection}/`)) {
|
|
109
|
+
delete MockAdapter._fixtures[key];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return new Promise(() => null);
|
|
100
113
|
});
|
|
101
114
|
}
|
|
102
115
|
find(dataObject, filters = undefined, pagination = undefined) {
|
|
103
116
|
return __awaiter(this, void 0, void 0, function* () {
|
|
104
117
|
const limit = (pagination === null || pagination === void 0 ? void 0 : pagination.limits.batch) || 1e10;
|
|
105
118
|
let result = [];
|
|
119
|
+
const collection = this.getCollection(dataObject);
|
|
120
|
+
// TODO filter only records matchinf collection
|
|
106
121
|
for (let key in MockAdapter.getFixtures()) {
|
|
107
122
|
let keep = true;
|
|
108
|
-
if (result.length <= limit) {
|
|
123
|
+
if (key.startsWith(`${collection}/`) && result.length <= limit) {
|
|
109
124
|
const dao = yield dataObject.clone(MockAdapter.getFixture(key));
|
|
110
125
|
if (filters) {
|
|
111
126
|
if (Array.isArray(filters)) {
|
|
112
127
|
filters.forEach((filter) => {
|
|
113
128
|
const prop = dao.get(filter.prop);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
129
|
+
const val = prop.val();
|
|
130
|
+
//console.log(filter.prop, prop)
|
|
131
|
+
if (typeof prop === 'object') {
|
|
132
|
+
if (prop.constructor.name === 'ObjectProperty') {
|
|
133
|
+
if (filter.value instanceof ObjectUri_1.ObjectUri &&
|
|
134
|
+
val &&
|
|
135
|
+
val.ref !== filter.value.path) {
|
|
136
|
+
keep = false;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else if (prop.constructor.name === 'CollectionProperty') {
|
|
141
|
+
if (filter.value instanceof ObjectUri_1.ObjectUri &&
|
|
142
|
+
prop.val() !== filter.value.path) {
|
|
143
|
+
keep = false;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
120
146
|
}
|
|
121
147
|
}
|
|
122
148
|
else {
|
package/lib/backends/Query.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Limits } from './Limits';
|
|
|
3
3
|
import { Sorting } from './Sorting';
|
|
4
4
|
import { SortAndLimit } from './SortAndLimit';
|
|
5
5
|
import { BackendInterface } from './AbstractAdapter';
|
|
6
|
-
import {
|
|
6
|
+
import { AbstractObject } from '../components';
|
|
7
7
|
export declare const AS_OBJECTURIS = "objectUris";
|
|
8
8
|
export declare const AS_DATAOBJECTS = "dataObjects";
|
|
9
9
|
export declare const AS_INSTANCES = "classInstances";
|
|
@@ -12,7 +12,7 @@ export declare enum returnAs {
|
|
|
12
12
|
AS_DATAOBJECTS = "dataObjects",
|
|
13
13
|
AS_INSTANCES = "classInstances"
|
|
14
14
|
}
|
|
15
|
-
export declare class Query<T extends typeof
|
|
15
|
+
export declare class Query<T extends typeof AbstractObject> {
|
|
16
16
|
protected _obj: T;
|
|
17
17
|
protected _params: any;
|
|
18
18
|
filters: Filter[];
|
|
@@ -28,5 +28,5 @@ export declare class Query<T extends typeof BaseObject> {
|
|
|
28
28
|
offset(offset?: number): this;
|
|
29
29
|
batch(batch?: number): this;
|
|
30
30
|
get sortAndLimit(): SortAndLimit;
|
|
31
|
-
execute(as?: returnAs, backend?: BackendInterface
|
|
31
|
+
execute(as?: returnAs, backend?: BackendInterface): Promise<any>;
|
|
32
32
|
}
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { DataObjectClass } from '../../components';
|
|
2
|
+
import { UserClass } from '../../components/types/UserClass';
|
|
3
|
+
import * as actions from '../../Backend';
|
|
4
|
+
export interface InjectMetaMiddlewareParams {
|
|
5
|
+
user: UserClass;
|
|
6
|
+
}
|
|
7
|
+
export declare class InjectMetaMiddleware {
|
|
8
|
+
protected _user: UserClass;
|
|
9
|
+
constructor(params: InjectMetaMiddlewareParams);
|
|
10
|
+
execute(dataObject: DataObjectClass<any>, action: actions.BackendActions): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.InjectMetaMiddleware = void 0;
|
|
27
|
+
const actions = __importStar(require("../../Backend"));
|
|
28
|
+
class InjectMetaMiddleware {
|
|
29
|
+
constructor(params) {
|
|
30
|
+
this._user = params.user;
|
|
31
|
+
}
|
|
32
|
+
execute(dataObject, action) {
|
|
33
|
+
switch (action) {
|
|
34
|
+
// add properties existence validation
|
|
35
|
+
case actions.CREATE:
|
|
36
|
+
dataObject.set('createdBy', this._user);
|
|
37
|
+
dataObject.set('createdAt', Date.now());
|
|
38
|
+
case actions.UPDATE:
|
|
39
|
+
dataObject.set('updatedBy', this._user);
|
|
40
|
+
dataObject.set('updatedAt', Date.now());
|
|
41
|
+
case actions.DELETE:
|
|
42
|
+
dataObject.set('deletedBy', this._user);
|
|
43
|
+
dataObject.set('deleteddAt', Date.now());
|
|
44
|
+
default:
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.InjectMetaMiddleware = InjectMetaMiddleware;
|
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
import { DataObjectClass } from './types/DataObjectClass';
|
|
2
2
|
import { DataObjectProperties } from '../properties/';
|
|
3
|
+
import { Query } from '../backends/Query';
|
|
3
4
|
export declare abstract class AbstractObject {
|
|
4
5
|
static PROPS_DEFINITION: DataObjectProperties;
|
|
5
6
|
static COLLECTION: string;
|
|
6
7
|
static LABEL_KEY: string;
|
|
7
|
-
protected _dataObject: DataObjectClass
|
|
8
|
-
constructor(dao: DataObjectClass);
|
|
8
|
+
protected _dataObject: DataObjectClass<any>;
|
|
9
|
+
constructor(dao: DataObjectClass<any>);
|
|
9
10
|
get(key: string): any;
|
|
10
11
|
set(key: string, val: any): any;
|
|
11
12
|
val(key: string): any;
|
|
12
13
|
get backend(): any;
|
|
13
14
|
get path(): string;
|
|
14
15
|
get uid(): any;
|
|
15
|
-
get dataObject(): DataObjectClass
|
|
16
|
+
get dataObject(): DataObjectClass<any>;
|
|
16
17
|
get uri(): import("./ObjectUri").ObjectUri;
|
|
17
18
|
toJSON(): {
|
|
18
19
|
backend: string;
|
|
19
20
|
ref: string;
|
|
20
|
-
label: string
|
|
21
|
+
label: string;
|
|
21
22
|
};
|
|
22
|
-
save(): Promise<DataObjectClass
|
|
23
|
+
save(): Promise<DataObjectClass<any>>;
|
|
24
|
+
delete(): Promise<DataObjectClass<any>>;
|
|
25
|
+
static query(): Query<typeof AbstractObject>;
|
|
23
26
|
}
|
|
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
var _a;
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
13
|
exports.AbstractObject = void 0;
|
|
14
|
+
const Query_1 = require("../backends/Query");
|
|
14
15
|
class AbstractObject {
|
|
15
16
|
constructor(dao) {
|
|
16
17
|
this._dataObject = dao;
|
|
@@ -54,6 +55,14 @@ class AbstractObject {
|
|
|
54
55
|
return yield this._dataObject.save();
|
|
55
56
|
});
|
|
56
57
|
}
|
|
58
|
+
delete() {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
return yield this._dataObject.delete();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
static query() {
|
|
64
|
+
return new Query_1.Query(this);
|
|
65
|
+
}
|
|
57
66
|
}
|
|
58
67
|
exports.AbstractObject = AbstractObject;
|
|
59
68
|
_a = AbstractObject;
|
|
@@ -8,8 +8,8 @@ export declare class BaseObject extends AbstractObject implements BaseObjectClas
|
|
|
8
8
|
static getProperty(key: string): any;
|
|
9
9
|
get status(): string;
|
|
10
10
|
set status(status: string);
|
|
11
|
-
static daoFactory(src?: string | ObjectUri | DataObjectClass | undefined, child?: any): Promise<DataObjectClass
|
|
12
|
-
static factory<T extends BaseObject>(src?: string | ObjectUri | DataObjectClass | undefined, child?: any): Promise<T | BaseObject>;
|
|
11
|
+
static daoFactory(src?: string | ObjectUri | DataObjectClass<any> | undefined, child?: any): Promise<DataObjectClass<any>>;
|
|
12
|
+
static factory<T extends BaseObject>(src?: string | ObjectUri | DataObjectClass<any> | undefined, child?: any): Promise<T | BaseObject>;
|
|
13
13
|
asReference(): any;
|
|
14
14
|
query(): Query<any>;
|
|
15
15
|
}
|