@quatrain/core 0.1.6
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/Core.d.ts +16 -0
- package/lib/Core.js +30 -0
- package/lib/backends/AbstractAdapter.d.ts +49 -0
- package/lib/backends/AbstractAdapter.js +30 -0
- package/lib/backends/BackendError.d.ts +2 -0
- package/lib/backends/BackendError.js +6 -0
- package/lib/backends/Filter.d.ts +15 -0
- package/lib/backends/Filter.js +14 -0
- package/lib/backends/FilterOperators.d.ts +13 -0
- package/lib/backends/FilterOperators.js +16 -0
- package/lib/backends/Filters.d.ts +10 -0
- package/lib/backends/Filters.js +14 -0
- package/lib/backends/Limits.d.ts +9 -0
- package/lib/backends/Limits.js +10 -0
- package/lib/backends/MockAdapter.d.ts +24 -0
- package/lib/backends/MockAdapter.js +81 -0
- package/lib/backends/Query.d.ts +24 -0
- package/lib/backends/Query.js +72 -0
- package/lib/backends/SortAndLimit.d.ts +11 -0
- package/lib/backends/SortAndLimit.js +11 -0
- package/lib/backends/Sorting.d.ts +9 -0
- package/lib/backends/Sorting.js +10 -0
- package/lib/backends/index.d.ts +10 -0
- package/lib/backends/index.js +21 -0
- package/lib/common/statuses.d.ts +11 -0
- package/lib/common/statuses.js +14 -0
- package/lib/components/AbstractObject.d.ts +23 -0
- package/lib/components/AbstractObject.js +64 -0
- package/lib/components/BaseObject.d.ts +16 -0
- package/lib/components/BaseObject.js +73 -0
- package/lib/components/BaseObjectProperties.d.ts +15 -0
- package/lib/components/BaseObjectProperties.js +82 -0
- package/lib/components/DataObject.d.ts +83 -0
- package/lib/components/DataObject.js +209 -0
- package/lib/components/ObjectUri.d.ts +42 -0
- package/lib/components/ObjectUri.js +112 -0
- package/lib/components/User.d.ts +6 -0
- package/lib/components/User.js +56 -0
- package/lib/components/index.d.ts +7 -0
- package/lib/components/index.js +16 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.js +66 -0
- package/lib/properties/BaseProperty.d.ts +34 -0
- package/lib/properties/BaseProperty.js +57 -0
- package/lib/properties/BooleanProperty.d.ts +6 -0
- package/lib/properties/BooleanProperty.js +10 -0
- package/lib/properties/DateTimeProperty.d.ts +10 -0
- package/lib/properties/DateTimeProperty.js +17 -0
- package/lib/properties/EnumProperty.d.ts +9 -0
- package/lib/properties/EnumProperty.js +18 -0
- package/lib/properties/HashProperty.d.ts +20 -0
- package/lib/properties/HashProperty.js +48 -0
- package/lib/properties/NumberProperty.d.ts +36 -0
- package/lib/properties/NumberProperty.js +75 -0
- package/lib/properties/ObjectProperty.d.ts +16 -0
- package/lib/properties/ObjectProperty.js +25 -0
- package/lib/properties/Property.d.ts +16 -0
- package/lib/properties/Property.js +49 -0
- package/lib/properties/StringProperty.d.ts +27 -0
- package/lib/properties/StringProperty.js +73 -0
- package/lib/properties/index.d.ts +15 -0
- package/lib/properties/index.js +21 -0
- package/lib/properties/types/AbstractPropertyType.d.ts +5 -0
- package/lib/properties/types/AbstractPropertyType.js +2 -0
- package/lib/properties/types/PropertyClassType.d.ts +4 -0
- package/lib/properties/types/PropertyClassType.js +2 -0
- package/lib/properties/types/PropertyTypes.d.ts +2 -0
- package/lib/properties/types/PropertyTypes.js +2 -0
- package/lib/statuses.d.ts +10 -0
- package/lib/statuses.js +13 -0
- package/package.json +28 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ObjectUri } from './ObjectUri';
|
|
2
|
+
import { AbstractObject } from './AbstractObject';
|
|
3
|
+
import { DataObjectProperties } from '../properties';
|
|
4
|
+
import { Query } from '../backends';
|
|
5
|
+
export declare class BaseObject extends AbstractObject {
|
|
6
|
+
static PROPS_DEFINITION: DataObjectProperties;
|
|
7
|
+
get status(): string;
|
|
8
|
+
set status(status: string);
|
|
9
|
+
static factory<T extends BaseObject>(src?: string | ObjectUri | object | undefined): Promise<T | BaseObject>;
|
|
10
|
+
asReference(): {
|
|
11
|
+
label: any;
|
|
12
|
+
ref: string;
|
|
13
|
+
uri: string;
|
|
14
|
+
};
|
|
15
|
+
query(): Query<this>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
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.BaseObject = void 0;
|
|
13
|
+
const DataObject_1 = require("./DataObject");
|
|
14
|
+
const ObjectUri_1 = require("./ObjectUri");
|
|
15
|
+
const AbstractObject_1 = require("./AbstractObject");
|
|
16
|
+
const BaseObjectProperties_1 = require("./BaseObjectProperties");
|
|
17
|
+
const backends_1 = require("../backends");
|
|
18
|
+
class BaseObject extends AbstractObject_1.AbstractObject {
|
|
19
|
+
get status() {
|
|
20
|
+
return this._dataObject.val('status');
|
|
21
|
+
}
|
|
22
|
+
set status(status) {
|
|
23
|
+
this._dataObject.set('status', status);
|
|
24
|
+
}
|
|
25
|
+
static factory(src = undefined) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
try {
|
|
28
|
+
// merge base properties with additional or redefined ones
|
|
29
|
+
const base = BaseObjectProperties_1.BaseObjectProperties;
|
|
30
|
+
// this.PROPS_DEFINITION &&
|
|
31
|
+
this.PROPS_DEFINITION.forEach((property) => {
|
|
32
|
+
// manage parent properties potential redeclaration
|
|
33
|
+
const found = base.findIndex((el) => el.name === property.name);
|
|
34
|
+
if (found !== -1) {
|
|
35
|
+
base[found] = Object.assign(base[found], property);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
base.push(property);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
//console.log('props to add', JSON.stringify(base))
|
|
42
|
+
// create data object
|
|
43
|
+
const dao = yield DataObject_1.DataObject.factory(this.prototype, base);
|
|
44
|
+
if (src instanceof ObjectUri_1.ObjectUri) {
|
|
45
|
+
dao.uri = src;
|
|
46
|
+
yield dao.populate();
|
|
47
|
+
}
|
|
48
|
+
else if (typeof src === 'string') {
|
|
49
|
+
dao.uri.path = src;
|
|
50
|
+
yield dao.populate();
|
|
51
|
+
}
|
|
52
|
+
else if (src instanceof Object) {
|
|
53
|
+
dao.uri = new ObjectUri_1.ObjectUri(`${this.COLLECTION}${ObjectUri_1.ObjectUri.DEFAULT}`, Reflect.get(src, 'name'));
|
|
54
|
+
dao.uri.collection = this.COLLECTION;
|
|
55
|
+
yield dao.populate(src);
|
|
56
|
+
}
|
|
57
|
+
return new this(dao);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
console.log(err.message);
|
|
61
|
+
throw new Error(`Unable to build instance for '${this.constructor.name}': ${err.message}`);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
asReference() {
|
|
66
|
+
return this._dataObject.toReference();
|
|
67
|
+
}
|
|
68
|
+
query() {
|
|
69
|
+
return new backends_1.Query(this);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.BaseObject = BaseObject;
|
|
73
|
+
BaseObject.PROPS_DEFINITION = BaseObjectProperties_1.BaseObjectProperties;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DataObjectProperties } from '../properties';
|
|
2
|
+
import * as statuses from '../statuses';
|
|
3
|
+
import { User } from './User';
|
|
4
|
+
import { ObjectUri } from './ObjectUri';
|
|
5
|
+
export interface BaseObjectType {
|
|
6
|
+
name: string;
|
|
7
|
+
status: typeof statuses.CREATED | typeof statuses.PENDING | typeof statuses.ACTIVE | typeof statuses.DELETED;
|
|
8
|
+
createdBy?: User | ObjectUri;
|
|
9
|
+
createdAt?: number;
|
|
10
|
+
updatedBy?: User | ObjectUri;
|
|
11
|
+
updatedAt?: number;
|
|
12
|
+
deletedBy?: User | ObjectUri;
|
|
13
|
+
deletedAt?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare const BaseObjectProperties: DataObjectProperties;
|
|
@@ -0,0 +1,82 @@
|
|
|
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.BaseObjectProperties = void 0;
|
|
27
|
+
const Property_1 = require("../properties/Property");
|
|
28
|
+
const statuses = __importStar(require("../statuses"));
|
|
29
|
+
exports.BaseObjectProperties = [
|
|
30
|
+
{
|
|
31
|
+
name: 'name',
|
|
32
|
+
mandatory: true,
|
|
33
|
+
type: Property_1.Property.TYPE_STRING,
|
|
34
|
+
minLength: 1,
|
|
35
|
+
maxLength: 100,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: 'status',
|
|
39
|
+
mandatory: true,
|
|
40
|
+
type: Property_1.Property.TYPE_ENUM,
|
|
41
|
+
values: [
|
|
42
|
+
statuses.CREATED,
|
|
43
|
+
statuses.PENDING,
|
|
44
|
+
statuses.ACTIVE,
|
|
45
|
+
statuses.DELETED,
|
|
46
|
+
],
|
|
47
|
+
defaultValue: statuses.CREATED,
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'createdBy',
|
|
51
|
+
type: Property_1.Property.TYPE_OBJECT,
|
|
52
|
+
instanceOf: 'User',
|
|
53
|
+
mandatory: true,
|
|
54
|
+
protected: true,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'createdAt',
|
|
58
|
+
type: Property_1.Property.TYPE_DATETIME,
|
|
59
|
+
mandatory: true,
|
|
60
|
+
protected: true,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'updatedBy',
|
|
64
|
+
type: Property_1.Property.TYPE_OBJECT,
|
|
65
|
+
instanceOf: 'User',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'updatedAt',
|
|
69
|
+
type: Property_1.Property.TYPE_DATETIME,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'deletedBy',
|
|
73
|
+
type: Property_1.Property.TYPE_OBJECT,
|
|
74
|
+
instanceOf: 'User',
|
|
75
|
+
protected: true,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: 'deletedAt',
|
|
79
|
+
type: Property_1.Property.TYPE_DATETIME,
|
|
80
|
+
protected: true,
|
|
81
|
+
},
|
|
82
|
+
];
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { AbstractObject } from './AbstractObject';
|
|
2
|
+
import { ObjectUri } from './ObjectUri';
|
|
3
|
+
export interface DataObjectFactoryType {
|
|
4
|
+
path: string;
|
|
5
|
+
[x: string]: any;
|
|
6
|
+
}
|
|
7
|
+
export declare type CoreObject<T extends AbstractObject> = T;
|
|
8
|
+
/**
|
|
9
|
+
* Data objects constitute the agnostic glue between objects and backends.
|
|
10
|
+
* They handle data and identifiers in a protected registry
|
|
11
|
+
* This is what backends and objects manipulate, oblivious of the other.
|
|
12
|
+
*/
|
|
13
|
+
export declare class DataObject {
|
|
14
|
+
protected _class: CoreObject<any>;
|
|
15
|
+
protected _objectUri: ObjectUri;
|
|
16
|
+
protected _obj: any;
|
|
17
|
+
protected _uid: string | undefined;
|
|
18
|
+
protected _properties: {
|
|
19
|
+
[x: string]: any;
|
|
20
|
+
};
|
|
21
|
+
protected _persisted: boolean;
|
|
22
|
+
protected _populated: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Has data been modified since last backend operation?
|
|
25
|
+
*/
|
|
26
|
+
protected _modified: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Constructor is protected, use factory() instead
|
|
29
|
+
* @param objClass AbstractObject
|
|
30
|
+
* @param properties array of properties instances
|
|
31
|
+
*/
|
|
32
|
+
protected constructor(objClass: AbstractObject, properties: any[] | undefined);
|
|
33
|
+
protected _init(properties: any[]): void;
|
|
34
|
+
/**
|
|
35
|
+
* Populate data object from instant data or backend query
|
|
36
|
+
* @param data
|
|
37
|
+
*/
|
|
38
|
+
populate(data?: any): Promise<DataObject>;
|
|
39
|
+
isPopulated(): boolean;
|
|
40
|
+
isPersisted(): boolean;
|
|
41
|
+
get properties(): {
|
|
42
|
+
[x: string]: any;
|
|
43
|
+
};
|
|
44
|
+
get backend(): string | undefined;
|
|
45
|
+
get class(): any;
|
|
46
|
+
get path(): string;
|
|
47
|
+
set uid(uid: string | undefined);
|
|
48
|
+
get uid(): string | undefined;
|
|
49
|
+
get data(): any;
|
|
50
|
+
set uri(uri: string | ObjectUri | undefined);
|
|
51
|
+
get uri(): ObjectUri;
|
|
52
|
+
/**
|
|
53
|
+
* Returns property matching key or throw
|
|
54
|
+
* @param key string
|
|
55
|
+
* @returns BaseProperty
|
|
56
|
+
*/
|
|
57
|
+
get(key: string): any;
|
|
58
|
+
set(key: string, val: any): this;
|
|
59
|
+
/**
|
|
60
|
+
* Get value of given property
|
|
61
|
+
* @param key string
|
|
62
|
+
* @returns any
|
|
63
|
+
*/
|
|
64
|
+
val(key: string): any;
|
|
65
|
+
toJSON(): {
|
|
66
|
+
[x: string]: any;
|
|
67
|
+
};
|
|
68
|
+
toReference(): {
|
|
69
|
+
label: any;
|
|
70
|
+
ref: string;
|
|
71
|
+
uri: string;
|
|
72
|
+
};
|
|
73
|
+
protected _dataToJSON(): {};
|
|
74
|
+
read(): Promise<DataObject>;
|
|
75
|
+
save(desiredUid?: string | undefined): Promise<DataObject>;
|
|
76
|
+
/**
|
|
77
|
+
* Data object must be created from factory in order for async-loaded data to be available
|
|
78
|
+
* @param className
|
|
79
|
+
* @param param
|
|
80
|
+
* @returns DataObject
|
|
81
|
+
*/
|
|
82
|
+
static factory(className: any, param?: any[] | undefined): Promise<DataObject>;
|
|
83
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
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.DataObject = void 0;
|
|
13
|
+
const Core_1 = require("../Core");
|
|
14
|
+
const Property_1 = require("../properties/Property");
|
|
15
|
+
const ObjectUri_1 = require("./ObjectUri");
|
|
16
|
+
/**
|
|
17
|
+
* Data objects constitute the agnostic glue between objects and backends.
|
|
18
|
+
* They handle data and identifiers in a protected registry
|
|
19
|
+
* This is what backends and objects manipulate, oblivious of the other.
|
|
20
|
+
*/
|
|
21
|
+
class DataObject {
|
|
22
|
+
/**
|
|
23
|
+
* Constructor is protected, use factory() instead
|
|
24
|
+
* @param objClass AbstractObject
|
|
25
|
+
* @param properties array of properties instances
|
|
26
|
+
*/
|
|
27
|
+
constructor(objClass, properties) {
|
|
28
|
+
this._uid = undefined;
|
|
29
|
+
this._properties = {};
|
|
30
|
+
this._persisted = false;
|
|
31
|
+
this._populated = false;
|
|
32
|
+
/**
|
|
33
|
+
* Has data been modified since last backend operation?
|
|
34
|
+
*/
|
|
35
|
+
this._modified = false;
|
|
36
|
+
this._class = objClass;
|
|
37
|
+
this._objectUri = new ObjectUri_1.ObjectUri();
|
|
38
|
+
if (Array.isArray(properties)) {
|
|
39
|
+
this._init(properties);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
_init(properties) {
|
|
43
|
+
// console.log(
|
|
44
|
+
// `Preparing properties for instance of ${this._class.constructor.name}`
|
|
45
|
+
// )
|
|
46
|
+
properties.forEach((prop) => {
|
|
47
|
+
this._properties[prop.name] = Property_1.Property.factory(prop, this);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Populate data object from instant data or backend query
|
|
52
|
+
* @param data
|
|
53
|
+
*/
|
|
54
|
+
populate(data = undefined) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
if (this._populated === false) {
|
|
57
|
+
if (data) {
|
|
58
|
+
for (const key in data) {
|
|
59
|
+
if (Reflect.get(this._properties, key)) {
|
|
60
|
+
Reflect.get(this._properties, key).set(data[key]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else if (this.path !== '/' && this.path !== '') {
|
|
65
|
+
yield Core_1.Core.getBackend(this._objectUri.backend).read(this);
|
|
66
|
+
}
|
|
67
|
+
this._populated = true;
|
|
68
|
+
if (Reflect.get(this._properties, 'name')) {
|
|
69
|
+
this.uri.label = this.val('name');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
isPopulated() {
|
|
76
|
+
return this._populated;
|
|
77
|
+
}
|
|
78
|
+
isPersisted() {
|
|
79
|
+
return this._persisted;
|
|
80
|
+
}
|
|
81
|
+
get properties() {
|
|
82
|
+
return this._properties;
|
|
83
|
+
}
|
|
84
|
+
get backend() {
|
|
85
|
+
return this._objectUri ? this._objectUri.backend : undefined;
|
|
86
|
+
}
|
|
87
|
+
get class() {
|
|
88
|
+
return this._class;
|
|
89
|
+
}
|
|
90
|
+
get path() {
|
|
91
|
+
return this._objectUri ? this._objectUri.path : '';
|
|
92
|
+
}
|
|
93
|
+
set uid(uid) {
|
|
94
|
+
if (this._uid !== undefined) {
|
|
95
|
+
throw new Error(`DataObject already has an uid`);
|
|
96
|
+
}
|
|
97
|
+
this._uid = uid;
|
|
98
|
+
}
|
|
99
|
+
get uid() {
|
|
100
|
+
return this._objectUri ? this._objectUri.uid : undefined;
|
|
101
|
+
}
|
|
102
|
+
get data() {
|
|
103
|
+
return this._properties;
|
|
104
|
+
}
|
|
105
|
+
set uri(uri) {
|
|
106
|
+
this._objectUri = uri instanceof ObjectUri_1.ObjectUri ? uri : new ObjectUri_1.ObjectUri(uri);
|
|
107
|
+
}
|
|
108
|
+
get uri() {
|
|
109
|
+
return this._objectUri;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Returns property matching key or throw
|
|
113
|
+
* @param key string
|
|
114
|
+
* @returns BaseProperty
|
|
115
|
+
*/
|
|
116
|
+
get(key) {
|
|
117
|
+
try {
|
|
118
|
+
return this._properties[key];
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
throw new Error(err.message);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
set(key, val) {
|
|
125
|
+
if (!Reflect.has(this._properties, key)) {
|
|
126
|
+
throw new Error(`Unknown property in data object: ${key}`);
|
|
127
|
+
}
|
|
128
|
+
this._properties[key].set(val);
|
|
129
|
+
this._populated = true;
|
|
130
|
+
this._modified = true;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get value of given property
|
|
135
|
+
* @param key string
|
|
136
|
+
* @returns any
|
|
137
|
+
*/
|
|
138
|
+
val(key) {
|
|
139
|
+
if (Reflect.get(this._properties, key)) {
|
|
140
|
+
return Reflect.get(this._properties, key).val();
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
throw new Error(`Unknown property '${key}'`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
toJSON() {
|
|
147
|
+
return Object.assign(Object.assign({}, (this._uid && { uid: this._uid })), this._dataToJSON());
|
|
148
|
+
}
|
|
149
|
+
toReference() {
|
|
150
|
+
var _a;
|
|
151
|
+
return Object.assign(Object.assign({}, (_a = this._objectUri) === null || _a === void 0 ? void 0 : _a.toReference()), { label: this.val('name') });
|
|
152
|
+
}
|
|
153
|
+
_dataToJSON() {
|
|
154
|
+
const data = {};
|
|
155
|
+
Object.keys(this._properties).forEach((key) => {
|
|
156
|
+
const prop = Reflect.get(this._properties, key);
|
|
157
|
+
if (typeof prop.val() === 'object' &&
|
|
158
|
+
Reflect.has(prop.val(), 'toJSON')) {
|
|
159
|
+
Reflect.set(data, key, prop.val().toJSON());
|
|
160
|
+
}
|
|
161
|
+
else if (prop !== undefined) {
|
|
162
|
+
Reflect.set(data, key, prop.val());
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return data;
|
|
166
|
+
}
|
|
167
|
+
read() {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
try {
|
|
170
|
+
return this.populate(Core_1.Core.getBackend().read(this));
|
|
171
|
+
}
|
|
172
|
+
catch (err) {
|
|
173
|
+
console.log(err.message);
|
|
174
|
+
throw new Error(err.message);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
save(desiredUid = undefined) {
|
|
179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
+
const backend = Core_1.Core.getBackend(this.backend || Core_1.Core.defaultBackend);
|
|
181
|
+
this._persisted = true;
|
|
182
|
+
this._modified = false;
|
|
183
|
+
return this._uid
|
|
184
|
+
? yield backend.update(this)
|
|
185
|
+
: yield backend.create(this, desiredUid);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Data object must be created from factory in order for async-loaded data to be available
|
|
190
|
+
* @param className
|
|
191
|
+
* @param param
|
|
192
|
+
* @returns DataObject
|
|
193
|
+
*/
|
|
194
|
+
static factory(className, param = undefined) {
|
|
195
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
196
|
+
if (className === undefined) {
|
|
197
|
+
throw new Error(`className is a require value`);
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
return new this(className, param);
|
|
201
|
+
}
|
|
202
|
+
catch (err) {
|
|
203
|
+
console.log(err);
|
|
204
|
+
throw new Error(`Unable to build data object: ${err.message}`);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.DataObject = DataObject;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export declare class ObjectUri {
|
|
2
|
+
static DEFAULT: string;
|
|
3
|
+
static MISSING_COLLECTION: string;
|
|
4
|
+
protected _str: string;
|
|
5
|
+
protected _pairs: Array<string>;
|
|
6
|
+
protected _literal: string;
|
|
7
|
+
protected _backend: string;
|
|
8
|
+
protected _path: string;
|
|
9
|
+
protected _uid: string | undefined;
|
|
10
|
+
protected _collection: string | undefined;
|
|
11
|
+
protected _label: string | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* create object uri from string:
|
|
14
|
+
* ex: 'xyz', '@backend:xyz', 'collection/xyz', '@backend:collection/xyz'
|
|
15
|
+
* some backends will need a collection or table name, some may deduct it from object
|
|
16
|
+
* @param str
|
|
17
|
+
*/
|
|
18
|
+
constructor(str?: string, label?: string | undefined);
|
|
19
|
+
get backend(): string;
|
|
20
|
+
get path(): string;
|
|
21
|
+
set label(label: string | undefined);
|
|
22
|
+
get label(): string | undefined;
|
|
23
|
+
get literal(): string;
|
|
24
|
+
set path(path: string);
|
|
25
|
+
get uid(): string | undefined;
|
|
26
|
+
get collection(): string | undefined;
|
|
27
|
+
set collection(collection: string | undefined);
|
|
28
|
+
/**
|
|
29
|
+
* Return references to find object locally and remotely
|
|
30
|
+
* @returns object
|
|
31
|
+
*/
|
|
32
|
+
toReference(): {
|
|
33
|
+
ref: string;
|
|
34
|
+
uri: string;
|
|
35
|
+
label: string | undefined;
|
|
36
|
+
};
|
|
37
|
+
toJSON(): {
|
|
38
|
+
literal: string;
|
|
39
|
+
ref: string;
|
|
40
|
+
label: string | undefined;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObjectUri = void 0;
|
|
4
|
+
const Core_1 = require("../Core");
|
|
5
|
+
class ObjectUri {
|
|
6
|
+
/**
|
|
7
|
+
* create object uri from string:
|
|
8
|
+
* ex: 'xyz', '@backend:xyz', 'collection/xyz', '@backend:collection/xyz'
|
|
9
|
+
* some backends will need a collection or table name, some may deduct it from object
|
|
10
|
+
* @param str
|
|
11
|
+
*/
|
|
12
|
+
constructor(str = '', label = undefined) {
|
|
13
|
+
this._pairs = [];
|
|
14
|
+
this._literal = '';
|
|
15
|
+
this._backend = Core_1.Core.defaultBackend;
|
|
16
|
+
this._path = ObjectUri.DEFAULT;
|
|
17
|
+
this._uid = undefined;
|
|
18
|
+
this._collection = undefined;
|
|
19
|
+
this._str = str;
|
|
20
|
+
this._label = label || this._uid;
|
|
21
|
+
if (str.indexOf(':') !== -1) {
|
|
22
|
+
const [backend, path] = str.split(':');
|
|
23
|
+
this._backend = backend || Core_1.Core.defaultBackend;
|
|
24
|
+
this._path = path;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this._path = str;
|
|
28
|
+
}
|
|
29
|
+
const parts = this._path.split(ObjectUri.DEFAULT);
|
|
30
|
+
if (parts.length === 1) {
|
|
31
|
+
// It is allowed to only give the uid part of the uri
|
|
32
|
+
// provided that collection will be injected by another mean
|
|
33
|
+
this._uid = parts[0].length > 0 ? parts[0] : undefined;
|
|
34
|
+
this._collection = ObjectUri.MISSING_COLLECTION;
|
|
35
|
+
this._pairs.push(`${ObjectUri.MISSING_COLLECTION}${ObjectUri.DEFAULT}${this._uid}`);
|
|
36
|
+
}
|
|
37
|
+
else if (parts.length % 2 === 0) {
|
|
38
|
+
// General case is a path containing pairs of collection/uid
|
|
39
|
+
let i = 0;
|
|
40
|
+
while (i < parts.length) {
|
|
41
|
+
this._collection = parts[i];
|
|
42
|
+
this._uid = parts[i + 1];
|
|
43
|
+
this._pairs.push(`${parts[i]}${ObjectUri.DEFAULT}${parts[i + 1]}`);
|
|
44
|
+
i += 2;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
throw new Error('Path parts number must be 1 or even');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
get backend() {
|
|
52
|
+
return this._backend;
|
|
53
|
+
}
|
|
54
|
+
get path() {
|
|
55
|
+
return this._path;
|
|
56
|
+
}
|
|
57
|
+
set label(label) {
|
|
58
|
+
this._label = label;
|
|
59
|
+
}
|
|
60
|
+
get label() {
|
|
61
|
+
return this._label;
|
|
62
|
+
}
|
|
63
|
+
get literal() {
|
|
64
|
+
// return `${this._backend}:${
|
|
65
|
+
// this._collection || ObjectUri.MISSING_COLLECTION
|
|
66
|
+
// }${ObjectUri.DEFAULT}${this._uid}`
|
|
67
|
+
return `${this._backend}:${this._pairs.join(ObjectUri.DEFAULT)}`;
|
|
68
|
+
}
|
|
69
|
+
set path(path) {
|
|
70
|
+
if (this._path && this._path !== ObjectUri.DEFAULT) {
|
|
71
|
+
throw new Error(`Path value already set with '${this._path}'`);
|
|
72
|
+
}
|
|
73
|
+
this._path = path;
|
|
74
|
+
this._collection = path.split('/').pop();
|
|
75
|
+
this._literal = `${this._backend}:${this._path}`;
|
|
76
|
+
}
|
|
77
|
+
get uid() {
|
|
78
|
+
return this._uid;
|
|
79
|
+
}
|
|
80
|
+
get collection() {
|
|
81
|
+
return this._collection === ObjectUri.MISSING_COLLECTION
|
|
82
|
+
? undefined
|
|
83
|
+
: this._collection;
|
|
84
|
+
}
|
|
85
|
+
set collection(collection) {
|
|
86
|
+
if (this.collection !== undefined) {
|
|
87
|
+
throw new Error('Collection value already set');
|
|
88
|
+
}
|
|
89
|
+
this._collection = collection;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Return references to find object locally and remotely
|
|
93
|
+
* @returns object
|
|
94
|
+
*/
|
|
95
|
+
toReference() {
|
|
96
|
+
return {
|
|
97
|
+
ref: this._path,
|
|
98
|
+
uri: this._literal,
|
|
99
|
+
label: this._label,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
toJSON() {
|
|
103
|
+
return {
|
|
104
|
+
literal: this._literal,
|
|
105
|
+
ref: this._path,
|
|
106
|
+
label: this._label,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.ObjectUri = ObjectUri;
|
|
111
|
+
ObjectUri.DEFAULT = '/';
|
|
112
|
+
ObjectUri.MISSING_COLLECTION = '_?_';
|