@quatrain/core 0.4.4 → 0.5.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 +6 -5
- package/lib/Backend.js +17 -5
- package/lib/Core.d.ts +3 -3
- package/lib/backends/AbstractAdapter.d.ts +11 -6
- package/lib/backends/AbstractAdapter.js +13 -3
- package/lib/backends/Filter.d.ts +2 -2
- package/lib/backends/FilterOperators.d.ts +1 -1
- package/lib/backends/FilterOperators.js +1 -1
- package/lib/backends/MockAdapter.js +13 -5
- package/lib/backends/Query.d.ts +9 -3
- package/lib/backends/Query.js +26 -6
- package/lib/backends/middlewares/InjectMetaMiddleware.d.ts +7 -6
- package/lib/backends/middlewares/InjectMetaMiddleware.js +8 -28
- package/lib/backends/middlewares/Middleware.d.ts +5 -0
- package/lib/common/ResourcesErrors.d.ts +13 -0
- package/lib/common/ResourcesErrors.js +25 -0
- package/lib/components/AbstractObject.d.ts +0 -2
- package/lib/components/AbstractObject.js +0 -4
- package/lib/components/BaseObject.d.ts +11 -13
- package/lib/components/BaseObject.js +84 -78
- package/lib/components/BaseObjectCore.d.ts +60 -0
- package/lib/components/BaseObjectCore.js +153 -0
- package/lib/components/BaseRepository.d.ts +24 -0
- package/lib/components/BaseRepository.js +116 -0
- package/lib/components/DataObject.d.ts +19 -4
- package/lib/components/DataObject.js +51 -18
- package/lib/components/Entity.d.ts +22 -3
- package/lib/components/Entity.js +23 -22
- package/lib/components/EntityRepository.d.ts +6 -0
- package/lib/components/EntityRepository.js +14 -0
- package/lib/components/ObjectUri.js +3 -3
- package/lib/components/User.d.ts +14 -3
- package/lib/components/User.js +10 -8
- package/lib/components/UserRepository.d.ts +7 -0
- package/lib/components/UserRepository.js +36 -0
- package/lib/components/index.d.ts +4 -5
- package/lib/components/index.js +6 -9
- package/lib/components/types/AbstractObjectClass.d.ts +1 -1
- package/lib/components/types/BaseObjectClass.d.ts +3 -2
- package/lib/components/types/DataObjectClass.d.ts +3 -0
- package/lib/components/types/Payload.d.ts +10 -0
- package/lib/components/types/Payload.js +2 -0
- package/lib/components/types/Persisted.d.ts +6 -0
- package/lib/components/types/Persisted.js +2 -0
- package/lib/components/types/Properties.d.ts +3 -0
- package/lib/components/types/Properties.js +1 -0
- package/lib/components/types/ProxyConstructor.d.ts +10 -0
- package/lib/components/types/ProxyConstructor.js +4 -0
- package/lib/components/types/RepositoryClass.d.ts +40 -0
- package/lib/components/types/RepositoryClass.js +2 -0
- package/lib/index.d.ts +11 -3
- package/lib/index.js +21 -8
- package/lib/properties/BaseProperty.d.ts +2 -1
- package/lib/properties/BaseProperty.js +5 -1
- package/lib/properties/CollectionProperty.d.ts +4 -3
- package/lib/properties/HashProperty.d.ts +1 -1
- package/lib/properties/NumberProperty.d.ts +2 -2
- package/lib/properties/ObjectProperty.d.ts +5 -3
- package/lib/properties/ObjectProperty.js +5 -2
- package/lib/properties/Property.js +6 -0
- package/lib/properties/index.d.ts +2 -2
- package/lib/properties/types/PropertyClassType.d.ts +2 -1
- package/lib/properties/types/PropertyHTMLType.d.ts +1 -1
- package/lib/properties/types/PropertyTypes.d.ts +2 -2
- package/lib/utils/DataGenerator.d.ts +3 -2
- package/lib/utils/DataGenerator.js +1 -1
- package/package.json +31 -29
- package/lib/components/BaseObjectProperties.d.ts +0 -13
- package/lib/components/BaseObjectProperties.js +0 -87
- package/lib/components/UserProperties.d.ts +0 -1
- package/lib/components/UserProperties.js +0 -103
- package/lib/components/types/UserClass.d.ts +0 -3
- /package/lib/{components/types/UserClass.js → backends/middlewares/Middleware.js} +0 -0
|
@@ -1,81 +1,87 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
10
24
|
};
|
|
11
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return this._dataObject.toReference();
|
|
75
|
-
}
|
|
76
|
-
query() {
|
|
77
|
-
return new Query_1.Query(this.constructor.prototype);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
exports.BaseObject = BaseObject;
|
|
81
|
-
BaseObject.PROPS_DEFINITION = BaseObjectProperties_1.BaseObjectProperties;
|
|
26
|
+
exports.BaseObjectProperties = void 0;
|
|
27
|
+
const statuses = __importStar(require("../statuses"));
|
|
28
|
+
const StringProperty_1 = require("../properties/StringProperty");
|
|
29
|
+
const ObjectProperty_1 = require("../properties/ObjectProperty");
|
|
30
|
+
const EnumProperty_1 = require("../properties/EnumProperty");
|
|
31
|
+
const DateTimeProperty_1 = require("../properties/DateTimeProperty");
|
|
32
|
+
const htmlType = __importStar(require("../properties/types/PropertyHTMLType"));
|
|
33
|
+
exports.BaseObjectProperties = [
|
|
34
|
+
{
|
|
35
|
+
name: 'name',
|
|
36
|
+
mandatory: true,
|
|
37
|
+
type: StringProperty_1.StringProperty.TYPE,
|
|
38
|
+
minLength: 1,
|
|
39
|
+
maxLength: 100,
|
|
40
|
+
htmlType: htmlType.NAME,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'status',
|
|
44
|
+
mandatory: true,
|
|
45
|
+
type: EnumProperty_1.EnumProperty.TYPE,
|
|
46
|
+
values: [
|
|
47
|
+
statuses.CREATED,
|
|
48
|
+
statuses.PENDING,
|
|
49
|
+
statuses.ACTIVE,
|
|
50
|
+
statuses.DELETED,
|
|
51
|
+
],
|
|
52
|
+
defaultValue: statuses.CREATED,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'createdBy',
|
|
56
|
+
type: ObjectProperty_1.ObjectProperty.TYPE,
|
|
57
|
+
instanceOf: 'User',
|
|
58
|
+
mandatory: true,
|
|
59
|
+
protected: true,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: 'createdAt',
|
|
63
|
+
type: DateTimeProperty_1.DateTimeProperty.TYPE,
|
|
64
|
+
mandatory: true,
|
|
65
|
+
protected: true,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'updatedBy',
|
|
69
|
+
type: ObjectProperty_1.ObjectProperty.TYPE,
|
|
70
|
+
instanceOf: 'User',
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: 'updatedAt',
|
|
74
|
+
type: DateTimeProperty_1.DateTimeProperty.TYPE,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'deletedBy',
|
|
78
|
+
type: ObjectProperty_1.ObjectProperty.TYPE,
|
|
79
|
+
instanceOf: 'User',
|
|
80
|
+
protected: true,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: 'deletedAt',
|
|
84
|
+
type: DateTimeProperty_1.DateTimeProperty.TYPE,
|
|
85
|
+
protected: true,
|
|
86
|
+
},
|
|
87
|
+
];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ObjectUri } from './ObjectUri';
|
|
2
|
+
import { DataObjectClass } from './types/DataObjectClass';
|
|
3
|
+
import { BaseObjectClass } from './types/BaseObjectClass';
|
|
4
|
+
import { AbstractObject } from './AbstractObject';
|
|
5
|
+
import { BaseObject } from './BaseObject';
|
|
6
|
+
import { Query } from '../backends/Query';
|
|
7
|
+
import { DataObject } from './DataObject';
|
|
8
|
+
import { Persisted } from './types/Persisted';
|
|
9
|
+
import { Proxy } from './types/ProxyConstructor';
|
|
10
|
+
import { DataObjectProperties } from '../properties';
|
|
11
|
+
export declare class BaseObjectCore extends AbstractObject implements BaseObjectClass {
|
|
12
|
+
static PROPS_DEFINITION: DataObjectProperties;
|
|
13
|
+
static getProperty(key: string): (import("../properties").NumberPropertyType & {
|
|
14
|
+
type: string;
|
|
15
|
+
}) | (import("../properties").BooleanPropertyType & {
|
|
16
|
+
type: string;
|
|
17
|
+
}) | (import("../properties").EnumPropertyType & {
|
|
18
|
+
type: string;
|
|
19
|
+
}) | (import("../properties").ObjectPropertyType & {
|
|
20
|
+
type: string;
|
|
21
|
+
}) | (import("../properties").StringPropertyType & {
|
|
22
|
+
type: string;
|
|
23
|
+
}) | (import("../properties").HashPropertyType & {
|
|
24
|
+
type: string;
|
|
25
|
+
}) | (import("../properties").DateTimeProperty & {
|
|
26
|
+
type: string;
|
|
27
|
+
}) | (import("../properties").ArrayPropertyType & {
|
|
28
|
+
type: string;
|
|
29
|
+
}) | (import("../properties").MapPropertyType & {
|
|
30
|
+
type: string;
|
|
31
|
+
}) | (import("../properties").CollectionPropertyType & {
|
|
32
|
+
type: string;
|
|
33
|
+
}) | undefined;
|
|
34
|
+
static fillProperties(child?: any): DataObject;
|
|
35
|
+
static daoFactory(src?: string | ObjectUri | DataObjectClass<any> | undefined, child?: any): Promise<DataObjectClass<any>>;
|
|
36
|
+
/**
|
|
37
|
+
* Instantiates from an object
|
|
38
|
+
* @param src
|
|
39
|
+
* @param child
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
static fromObject<T extends BaseObject>(src: T, child?: any): Proxy<T>;
|
|
43
|
+
static factory(src?: string | ObjectUri | BaseObject | undefined, child?: any): Promise<any>;
|
|
44
|
+
/**
|
|
45
|
+
* Fetches an object from its backend path
|
|
46
|
+
* @param path
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
static fromBackend<T>(path: string): Promise<Persisted<T>>;
|
|
50
|
+
/**
|
|
51
|
+
* Instantiates from a DataObject
|
|
52
|
+
* @param dao
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
static fromDataObject<T extends BaseObject>(dao: DataObjectClass<any>): T;
|
|
56
|
+
protected toProxy<T extends BaseObject>(): Proxy<Proxy<T>>;
|
|
57
|
+
asReference(): any;
|
|
58
|
+
query(): Query<any>;
|
|
59
|
+
static query(): Query<typeof BaseObjectCore>;
|
|
60
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseObjectCore = void 0;
|
|
13
|
+
const ObjectUri_1 = require("./ObjectUri");
|
|
14
|
+
const AbstractObject_1 = require("./AbstractObject");
|
|
15
|
+
const BaseObject_1 = require("./BaseObject");
|
|
16
|
+
const Query_1 = require("../backends/Query");
|
|
17
|
+
const DataObject_1 = require("./DataObject");
|
|
18
|
+
const ProxyConstructor_1 = require("./types/ProxyConstructor");
|
|
19
|
+
class BaseObjectCore extends AbstractObject_1.AbstractObject {
|
|
20
|
+
static getProperty(key) {
|
|
21
|
+
return BaseObjectCore.PROPS_DEFINITION.find((prop) => prop.name === key);
|
|
22
|
+
}
|
|
23
|
+
static fillProperties(child = this) {
|
|
24
|
+
// merge base properties with additional or redefined ones
|
|
25
|
+
const base = [...BaseObjectCore.PROPS_DEFINITION];
|
|
26
|
+
child.PROPS_DEFINITION.forEach((property) => {
|
|
27
|
+
// manage parent properties potential redeclaration
|
|
28
|
+
const found = base.findIndex((el) => el.name === property.name);
|
|
29
|
+
if (found !== -1) {
|
|
30
|
+
base[found] = property;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
base.push(property);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const dao = DataObject_1.DataObject.factory({ properties: base });
|
|
37
|
+
dao.uri.class = child;
|
|
38
|
+
return dao;
|
|
39
|
+
}
|
|
40
|
+
static daoFactory(src = undefined, child = this) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
const dao = this.fillProperties(child);
|
|
43
|
+
if (src instanceof ObjectUri_1.ObjectUri) {
|
|
44
|
+
dao.uri = src;
|
|
45
|
+
yield dao.read();
|
|
46
|
+
}
|
|
47
|
+
else if (typeof src == 'string') {
|
|
48
|
+
dao.uri.path = src;
|
|
49
|
+
yield dao.read();
|
|
50
|
+
}
|
|
51
|
+
return dao;
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Instantiates from an object
|
|
56
|
+
* @param src
|
|
57
|
+
* @param child
|
|
58
|
+
* @returns
|
|
59
|
+
*/
|
|
60
|
+
static fromObject(src, child = this) {
|
|
61
|
+
const dao = this.fillProperties(child);
|
|
62
|
+
dao.uri = new ObjectUri_1.ObjectUri(`${this.COLLECTION}${ObjectUri_1.ObjectUri.DEFAULT}`, Reflect.get(src, 'name'));
|
|
63
|
+
dao.uri.class = child;
|
|
64
|
+
dao.populateFromData(src);
|
|
65
|
+
const obj = new this(dao);
|
|
66
|
+
return obj.toProxy();
|
|
67
|
+
}
|
|
68
|
+
static factory(src = undefined, child = this) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
try {
|
|
71
|
+
if (typeof src == 'object' && !(src instanceof ObjectUri_1.ObjectUri)) {
|
|
72
|
+
return this.fromObject(src);
|
|
73
|
+
}
|
|
74
|
+
const dao = yield this.daoFactory(src, child);
|
|
75
|
+
const constructedObject = Reflect.construct(this, [dao]);
|
|
76
|
+
return constructedObject.toProxy();
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
throw new Error(`Unable to build instance for '${this.name}': ${err.message}`);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Fetches an object from its backend path
|
|
85
|
+
* @param path
|
|
86
|
+
* @returns
|
|
87
|
+
*/
|
|
88
|
+
static fromBackend(path) {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
if (!path.includes('/')) {
|
|
91
|
+
return this.factory(`${this.COLLECTION}/${path}`);
|
|
92
|
+
}
|
|
93
|
+
return this.factory(path);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Instantiates from a DataObject
|
|
98
|
+
* @param dao
|
|
99
|
+
* @returns
|
|
100
|
+
*/
|
|
101
|
+
static fromDataObject(dao) {
|
|
102
|
+
const obj = new this(dao);
|
|
103
|
+
return obj.toProxy();
|
|
104
|
+
}
|
|
105
|
+
toProxy() {
|
|
106
|
+
return new ProxyConstructor_1.ProxyConstructor(this, {
|
|
107
|
+
get: (target, prop) => {
|
|
108
|
+
if (prop === 'uid') {
|
|
109
|
+
return target.uid;
|
|
110
|
+
}
|
|
111
|
+
if (prop === 'uri') {
|
|
112
|
+
return target.uri;
|
|
113
|
+
}
|
|
114
|
+
if (prop == 'toJSON') {
|
|
115
|
+
return target.toJSON;
|
|
116
|
+
}
|
|
117
|
+
if (prop == 'save') {
|
|
118
|
+
return target.save;
|
|
119
|
+
}
|
|
120
|
+
if (prop == 'constructor') {
|
|
121
|
+
return target.constructor;
|
|
122
|
+
}
|
|
123
|
+
if (prop === 'core') {
|
|
124
|
+
return target;
|
|
125
|
+
}
|
|
126
|
+
// i don't know why and i shouldn't have to wonder why
|
|
127
|
+
// but everything crashes unless we do this terribleness
|
|
128
|
+
if (prop == 'then') {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
return target.val(prop);
|
|
132
|
+
},
|
|
133
|
+
set(target, prop, newValue) {
|
|
134
|
+
if (prop === 'uid' || prop === 'core') {
|
|
135
|
+
throw new Error(`Property '${prop}' is readonly`);
|
|
136
|
+
}
|
|
137
|
+
target.set(prop, newValue);
|
|
138
|
+
return true;
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
asReference() {
|
|
143
|
+
return this._dataObject.toReference();
|
|
144
|
+
}
|
|
145
|
+
query() {
|
|
146
|
+
return new Query_1.Query(this.constructor.prototype);
|
|
147
|
+
}
|
|
148
|
+
static query() {
|
|
149
|
+
return new Query_1.Query(this);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.BaseObjectCore = BaseObjectCore;
|
|
153
|
+
BaseObjectCore.PROPS_DEFINITION = BaseObject_1.BaseObjectProperties;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BackendInterface } from '../backends/AbstractAdapter';
|
|
2
|
+
import { Query } from '../backends/Query';
|
|
3
|
+
import { BaseObjectCore } from './BaseObjectCore';
|
|
4
|
+
import { DataObjectClass } from './types/DataObjectClass';
|
|
5
|
+
import Payload from './types/Payload';
|
|
6
|
+
import RepositoryClass from './types/RepositoryClass';
|
|
7
|
+
import { BaseObject } from './BaseObject';
|
|
8
|
+
import { Persisted } from './types/Persisted';
|
|
9
|
+
import { Proxy } from './types/ProxyConstructor';
|
|
10
|
+
/**
|
|
11
|
+
* CRUD methods for models/entities inheriting from BaseObject
|
|
12
|
+
* Extend this by passing the typeof of the desired class to the constructor
|
|
13
|
+
*/
|
|
14
|
+
export default class BaseRepository<T extends BaseObject> implements RepositoryClass<T> {
|
|
15
|
+
protected model: typeof BaseObjectCore;
|
|
16
|
+
backendAdapter: BackendInterface;
|
|
17
|
+
constructor(model: typeof BaseObjectCore, backendAdapter?: BackendInterface);
|
|
18
|
+
protected getDataObjectFromUid(uid: string): Promise<DataObjectClass<any>>;
|
|
19
|
+
create(obj: Proxy<T>, uid?: string): Promise<Persisted<T>>;
|
|
20
|
+
read(uid: string): Promise<Persisted<T>>;
|
|
21
|
+
update(obj: Proxy<T>): Promise<Persisted<T>>;
|
|
22
|
+
delete(uid: string): Promise<void>;
|
|
23
|
+
query(query: Query<typeof BaseObjectCore>): Promise<Payload<Persisted<T>>>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
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
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
const Core_1 = require("../Core");
|
|
36
|
+
const statuses = __importStar(require("../statuses"));
|
|
37
|
+
const ResourcesErrors_1 = require("../common/ResourcesErrors");
|
|
38
|
+
const DataObject_1 = require("./DataObject");
|
|
39
|
+
const RESOURCE_GONE_ERROR = `The resource you are trying to access has been deleted.`;
|
|
40
|
+
/**
|
|
41
|
+
* CRUD methods for models/entities inheriting from BaseObject
|
|
42
|
+
* Extend this by passing the typeof of the desired class to the constructor
|
|
43
|
+
*/
|
|
44
|
+
class BaseRepository {
|
|
45
|
+
constructor(model, backendAdapter = Core_1.Core.getBackend()) {
|
|
46
|
+
this.model = model;
|
|
47
|
+
this.backendAdapter = backendAdapter;
|
|
48
|
+
}
|
|
49
|
+
getDataObjectFromUid(uid) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const collection = this.model.COLLECTION || this.model.name.toLowerCase();
|
|
52
|
+
const path = `${collection}/${uid}`;
|
|
53
|
+
const dataObject = yield DataObject_1.DataObject.factory({
|
|
54
|
+
properties: this.model.PROPS_DEFINITION,
|
|
55
|
+
uri: path,
|
|
56
|
+
});
|
|
57
|
+
dataObject.uid = uid;
|
|
58
|
+
return dataObject;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
create(obj, uid) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const dataObject = obj.core.dataObject;
|
|
64
|
+
const savedObj = yield this.backendAdapter.create(dataObject, uid);
|
|
65
|
+
return this.model.fromDataObject(savedObj);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
read(uid) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
try {
|
|
71
|
+
const dataObject = yield this.getDataObjectFromUid(uid);
|
|
72
|
+
const response = yield this.backendAdapter.read(dataObject);
|
|
73
|
+
const obj = this.model.fromDataObject(response);
|
|
74
|
+
if (obj.status === statuses.DELETED) {
|
|
75
|
+
throw new ResourcesErrors_1.GoneError(RESOURCE_GONE_ERROR);
|
|
76
|
+
}
|
|
77
|
+
return obj;
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
if (e instanceof ResourcesErrors_1.GoneError) {
|
|
81
|
+
throw e;
|
|
82
|
+
}
|
|
83
|
+
if (e instanceof ResourcesErrors_1.NotFoundError) {
|
|
84
|
+
throw new ResourcesErrors_1.NotFoundError('[Repository] ' + e.message);
|
|
85
|
+
}
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
update(obj) {
|
|
91
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
const dataObject = obj.core.dataObject;
|
|
93
|
+
const savedObj = yield this.backendAdapter.update(dataObject);
|
|
94
|
+
return this.model.fromDataObject(savedObj);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
delete(uid) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const dataObject = yield this.getDataObjectFromUid(uid);
|
|
100
|
+
yield this.backendAdapter.delete(dataObject);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
query(query) {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
105
|
+
const items = (yield query.fetchAsInstances(this.backendAdapter));
|
|
106
|
+
const count = yield this.backendAdapter.count(query);
|
|
107
|
+
const meta = {
|
|
108
|
+
count,
|
|
109
|
+
updatedAt: new Date().getTime(),
|
|
110
|
+
};
|
|
111
|
+
const payload = { items, meta };
|
|
112
|
+
return payload;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.default = BaseRepository;
|
|
@@ -3,8 +3,8 @@ import { PropertyClassType } from '../properties/types/PropertyClassType';
|
|
|
3
3
|
import { AbstractObject } from './AbstractObject';
|
|
4
4
|
import { ObjectUri } from './ObjectUri';
|
|
5
5
|
import { DataObjectClass } from './types/DataObjectClass';
|
|
6
|
-
export
|
|
7
|
-
export
|
|
6
|
+
export type CoreObject<T extends AbstractObject> = T;
|
|
7
|
+
export type Properties = {
|
|
8
8
|
[x: string]: PropertyClassType;
|
|
9
9
|
};
|
|
10
10
|
export interface DataObjectFactoryType {
|
|
@@ -41,7 +41,22 @@ export declare class DataObject implements DataObjectClass<any> {
|
|
|
41
41
|
* Populate data object from instant data or backend query
|
|
42
42
|
* @param data
|
|
43
43
|
*/
|
|
44
|
-
populate(data?:
|
|
44
|
+
populate(data?: {
|
|
45
|
+
name: string;
|
|
46
|
+
[x: string]: unknown;
|
|
47
|
+
} | undefined): Promise<DataObject>;
|
|
48
|
+
/**
|
|
49
|
+
* Populate data object from instant data or backend query
|
|
50
|
+
* @param data
|
|
51
|
+
*/
|
|
52
|
+
populateFromData(data: {
|
|
53
|
+
[x: string]: unknown;
|
|
54
|
+
}): this;
|
|
55
|
+
/**
|
|
56
|
+
* Populate data object from backend query
|
|
57
|
+
* @param data
|
|
58
|
+
*/
|
|
59
|
+
populateFromBackend(): Promise<DataObject>;
|
|
45
60
|
isPopulated(): boolean;
|
|
46
61
|
isPersisted(): boolean;
|
|
47
62
|
get properties(): Properties;
|
|
@@ -85,6 +100,6 @@ export declare class DataObject implements DataObjectClass<any> {
|
|
|
85
100
|
* @param param
|
|
86
101
|
* @returns DataObject
|
|
87
102
|
*/
|
|
88
|
-
static factory(param?: DataObjectParams | undefined):
|
|
103
|
+
static factory(param?: DataObjectParams | undefined): DataObject;
|
|
89
104
|
clone(data?: any): Promise<DataObject>;
|
|
90
105
|
}
|
|
@@ -64,13 +64,45 @@ class DataObject {
|
|
|
64
64
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
65
|
if (this._populated === false) {
|
|
66
66
|
if (data) {
|
|
67
|
-
|
|
68
|
-
if (Reflect.get(this._properties, key)) {
|
|
69
|
-
Reflect.get(this._properties, key).set(data[key]);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
67
|
+
this.populateFromData(data);
|
|
72
68
|
}
|
|
73
69
|
else if (this.path !== '/' && this.path !== '') {
|
|
70
|
+
yield this.populateFromBackend();
|
|
71
|
+
}
|
|
72
|
+
this._populated = true;
|
|
73
|
+
if (Reflect.get(this._properties, 'name')) {
|
|
74
|
+
this.uri.label = this.val('name');
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return this;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Populate data object from instant data or backend query
|
|
82
|
+
* @param data
|
|
83
|
+
*/
|
|
84
|
+
populateFromData(data) {
|
|
85
|
+
if (this._populated === false) {
|
|
86
|
+
for (const key in data) {
|
|
87
|
+
if (Reflect.get(this._properties, key)) {
|
|
88
|
+
Reflect.get(this._properties, key).set(data[key]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this._populated = true;
|
|
92
|
+
if (Reflect.get(this._properties, 'name')) {
|
|
93
|
+
this.uri.label = this.val('name');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Populate data object from backend query
|
|
100
|
+
* @param data
|
|
101
|
+
*/
|
|
102
|
+
populateFromBackend() {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
if (this._populated === false) {
|
|
105
|
+
if (this.path !== '/' && this.path !== '') {
|
|
74
106
|
yield Core_1.Core.getBackend(this._objectUri.backend).read(this);
|
|
75
107
|
}
|
|
76
108
|
this._populated = true;
|
|
@@ -176,8 +208,8 @@ class DataObject {
|
|
|
176
208
|
case 'ObjectProperty':
|
|
177
209
|
const value = prop.val();
|
|
178
210
|
Reflect.set(data, key, value
|
|
179
|
-
? objectsAsReferences
|
|
180
|
-
? value.toReference()
|
|
211
|
+
? objectsAsReferences && !(value instanceof ObjectUri_1.ObjectUri)
|
|
212
|
+
? value.core.asReference() //toReference()
|
|
181
213
|
: value.toJSON()
|
|
182
214
|
: null);
|
|
183
215
|
break;
|
|
@@ -190,7 +222,7 @@ class DataObject {
|
|
|
190
222
|
read() {
|
|
191
223
|
return __awaiter(this, void 0, void 0, function* () {
|
|
192
224
|
try {
|
|
193
|
-
return
|
|
225
|
+
return yield Core_1.Core.getBackend().read(this); //this.populate()
|
|
194
226
|
}
|
|
195
227
|
catch (err) {
|
|
196
228
|
console.log(err.message);
|
|
@@ -219,21 +251,22 @@ class DataObject {
|
|
|
219
251
|
* @returns DataObject
|
|
220
252
|
*/
|
|
221
253
|
static factory(param = undefined) {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
}
|
|
230
|
-
});
|
|
254
|
+
try {
|
|
255
|
+
return new this(param);
|
|
256
|
+
}
|
|
257
|
+
catch (err) {
|
|
258
|
+
console.log(err);
|
|
259
|
+
throw new Error(`Unable to build data object: ${err.message}`);
|
|
260
|
+
}
|
|
231
261
|
}
|
|
232
262
|
clone(data = {}) {
|
|
233
263
|
return __awaiter(this, void 0, void 0, function* () {
|
|
234
264
|
const cloned = yield DataObject.factory();
|
|
235
265
|
cloned.uri.class = this.uri.class;
|
|
236
|
-
cloned.
|
|
266
|
+
cloned._populated = false;
|
|
267
|
+
for (let property of Object.keys(this._properties)) {
|
|
268
|
+
cloned._properties[property] = this._properties[property].clone();
|
|
269
|
+
}
|
|
237
270
|
if (data) {
|
|
238
271
|
yield cloned.populate(data);
|
|
239
272
|
}
|