@jd-data-limited/easy-fm 3.0.5 → 3.0.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/package.json +1 -1
- package/src/index.js +125 -107
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1384,19 +1384,6 @@ export class RecordBase extends EventEmitter {
|
|
|
1384
1384
|
get edited() {
|
|
1385
1385
|
return !!this.fields.find(i => i.edited);
|
|
1386
1386
|
}
|
|
1387
|
-
processPortalData(portalData) {
|
|
1388
|
-
this.portals = [];
|
|
1389
|
-
for (let item of Object.keys(portalData)) {
|
|
1390
|
-
let _portal = new Portal(this, item);
|
|
1391
|
-
_portal.records = portalData[item].map(item => {
|
|
1392
|
-
let fieldData = item;
|
|
1393
|
-
delete fieldData.recordId;
|
|
1394
|
-
delete fieldData.modId;
|
|
1395
|
-
return new PortalRecord(this, _portal, item.recordId, item.modId, fieldData);
|
|
1396
|
-
});
|
|
1397
|
-
this.portals.push(_portal);
|
|
1398
|
-
}
|
|
1399
|
-
}
|
|
1400
1387
|
processFieldData(fieldData) {
|
|
1401
1388
|
return Object.keys(fieldData).map(item => {
|
|
1402
1389
|
let _field = new Field(this, item, fieldData[item]);
|
|
@@ -1429,36 +1416,62 @@ export class RecordBase extends EventEmitter {
|
|
|
1429
1416
|
return _field;
|
|
1430
1417
|
});
|
|
1431
1418
|
}
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
this.
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1419
|
+
_onSave() {
|
|
1420
|
+
this.emit("saved");
|
|
1421
|
+
for (let field of this.fields)
|
|
1422
|
+
field.edited = false;
|
|
1423
|
+
}
|
|
1424
|
+
getField(field) {
|
|
1425
|
+
return this.fields.find(_field => _field.id === field);
|
|
1426
|
+
}
|
|
1427
|
+
toObject(filter = (a) => a.edited, portalFilter = (a) => a.records.find(record => record.edited), portalRowFilter = (a) => a.edited, portalFieldFilter = (a) => a.edited) {
|
|
1428
|
+
let fields_processed = {};
|
|
1429
|
+
for (let field of this.fields.filter(field => filter(field))) {
|
|
1430
|
+
let value = field.value;
|
|
1431
|
+
if (value instanceof Date) {
|
|
1432
|
+
// @ts-ignore
|
|
1433
|
+
let _value = moment.default(value)
|
|
1434
|
+
.utcOffset(this.layout.database.host.timezoneOffset);
|
|
1435
|
+
// @ts-ignore
|
|
1436
|
+
switch (field.metadata.result) {
|
|
1437
|
+
case "time":
|
|
1438
|
+
value = _value.format(this.layout.database.host.metadata.productInfo.timeFormat.replace("dd", "DD"));
|
|
1439
|
+
break;
|
|
1440
|
+
case "date":
|
|
1441
|
+
value = _value.format(this.layout.database.host.metadata.productInfo.dateFormat.replace("dd", "DD"));
|
|
1442
|
+
break;
|
|
1443
|
+
default:
|
|
1444
|
+
value = _value.format(this.layout.database.host.metadata.productInfo.timeStampFormat.replace("dd", "DD"));
|
|
1456
1445
|
}
|
|
1457
|
-
}
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1446
|
+
}
|
|
1447
|
+
fields_processed[field.id] = value;
|
|
1448
|
+
}
|
|
1449
|
+
let obj = {
|
|
1450
|
+
"recordId": this.recordId,
|
|
1451
|
+
"modId": this.modId,
|
|
1452
|
+
"fieldData": fields_processed
|
|
1453
|
+
};
|
|
1454
|
+
// Check if there's been any edited portal information
|
|
1455
|
+
let portals = this.portals.filter(a => portalFilter(a));
|
|
1456
|
+
if (portals) {
|
|
1457
|
+
obj["portalData"] = {};
|
|
1458
|
+
for (let portal of portals) {
|
|
1459
|
+
obj["portalData"][portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
|
|
1460
|
+
return record.toObject(portalFieldFilter);
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
return obj;
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
export class LayoutRecord extends RecordBase {
|
|
1468
|
+
constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
|
|
1469
|
+
super(layout, recordId, modId);
|
|
1470
|
+
this.fields = this.processFieldData(fieldData);
|
|
1471
|
+
this.portals = [];
|
|
1472
|
+
if (portalData) {
|
|
1473
|
+
this.processPortalData(portalData);
|
|
1474
|
+
}
|
|
1462
1475
|
}
|
|
1463
1476
|
commit(extraBody = {}) {
|
|
1464
1477
|
let trace = new Error();
|
|
@@ -1531,31 +1544,40 @@ export class RecordBase extends EventEmitter {
|
|
|
1531
1544
|
});
|
|
1532
1545
|
}));
|
|
1533
1546
|
}
|
|
1534
|
-
|
|
1535
|
-
this.
|
|
1536
|
-
for (let
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1547
|
+
processPortalData(portalData) {
|
|
1548
|
+
this.portals = [];
|
|
1549
|
+
for (let item of Object.keys(portalData)) {
|
|
1550
|
+
let _portal = new Portal(this, item);
|
|
1551
|
+
_portal.records = portalData[item].map(item => {
|
|
1552
|
+
let fieldData = item;
|
|
1553
|
+
delete fieldData.recordId;
|
|
1554
|
+
delete fieldData.modId;
|
|
1555
|
+
return new PortalRecord(this, _portal, item.recordId, item.modId, fieldData);
|
|
1556
|
+
});
|
|
1557
|
+
this.portals.push(_portal);
|
|
1558
|
+
}
|
|
1544
1559
|
}
|
|
1545
|
-
|
|
1560
|
+
get() {
|
|
1546
1561
|
let trace = new Error();
|
|
1562
|
+
if (this.recordId === -1) {
|
|
1563
|
+
throw "Cannot get this RecordBase until a commit() is done.";
|
|
1564
|
+
}
|
|
1547
1565
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
1566
|
+
if (!this.layout.metadata)
|
|
1567
|
+
yield this.layout.getLayoutMeta();
|
|
1548
1568
|
this.layout.database.apiRequest(this.endpoint, {
|
|
1549
1569
|
port: 443,
|
|
1550
|
-
method: "
|
|
1570
|
+
method: "GET"
|
|
1551
1571
|
})
|
|
1552
1572
|
.then(res => {
|
|
1553
|
-
if (
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
this.
|
|
1558
|
-
|
|
1573
|
+
if (res.messages[0].code === "0") {
|
|
1574
|
+
// console.log(res, res.response.data)
|
|
1575
|
+
this.modId = res.response.data[0].modId;
|
|
1576
|
+
this.fields = this.processFieldData(res.response.data[0].fieldData);
|
|
1577
|
+
this.portalData = [];
|
|
1578
|
+
if (res.response.data[0].portalData)
|
|
1579
|
+
this.processPortalData(res.response.data[0].portalData);
|
|
1580
|
+
resolve(this);
|
|
1559
1581
|
}
|
|
1560
1582
|
else {
|
|
1561
1583
|
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
@@ -1566,6 +1588,9 @@ export class RecordBase extends EventEmitter {
|
|
|
1566
1588
|
});
|
|
1567
1589
|
}));
|
|
1568
1590
|
}
|
|
1591
|
+
getPortal(portal) {
|
|
1592
|
+
return this.portals.find(p => p.name === portal);
|
|
1593
|
+
}
|
|
1569
1594
|
duplicate() {
|
|
1570
1595
|
let trace = new Error();
|
|
1571
1596
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1592,54 +1617,29 @@ export class RecordBase extends EventEmitter {
|
|
|
1592
1617
|
});
|
|
1593
1618
|
}));
|
|
1594
1619
|
}
|
|
1595
|
-
|
|
1596
|
-
let
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
case "time":
|
|
1606
|
-
value = _value.format(this.layout.database.host.metadata.productInfo.timeFormat.replace("dd", "DD"));
|
|
1607
|
-
break;
|
|
1608
|
-
case "date":
|
|
1609
|
-
value = _value.format(this.layout.database.host.metadata.productInfo.dateFormat.replace("dd", "DD"));
|
|
1610
|
-
break;
|
|
1611
|
-
default:
|
|
1612
|
-
value = _value.format(this.layout.database.host.metadata.productInfo.timeStampFormat.replace("dd", "DD"));
|
|
1620
|
+
delete() {
|
|
1621
|
+
let trace = new Error();
|
|
1622
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
1623
|
+
this.layout.database.apiRequest(this.endpoint, {
|
|
1624
|
+
port: 443,
|
|
1625
|
+
method: "DELETE"
|
|
1626
|
+
})
|
|
1627
|
+
.then(res => {
|
|
1628
|
+
if (typeof res.response.scriptError !== "undefined" && res.response.scriptError !== '0') {
|
|
1629
|
+
reject(new FMError(res.response.scriptError, res.status, res, trace));
|
|
1613
1630
|
}
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
for (let portal of portals) {
|
|
1627
|
-
obj["portalData"][portal.name] = portal.records.filter(a => portalRowFilter(a)).map(record => {
|
|
1628
|
-
return record.toObject(portalFieldFilter);
|
|
1629
|
-
});
|
|
1630
|
-
}
|
|
1631
|
-
}
|
|
1632
|
-
return obj;
|
|
1633
|
-
}
|
|
1634
|
-
}
|
|
1635
|
-
export class LayoutRecord extends RecordBase {
|
|
1636
|
-
constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
|
|
1637
|
-
super(layout, recordId, modId);
|
|
1638
|
-
this.fields = this.processFieldData(fieldData);
|
|
1639
|
-
this.portals = [];
|
|
1640
|
-
if (portalData) {
|
|
1641
|
-
this.processPortalData(portalData);
|
|
1642
|
-
}
|
|
1631
|
+
else if (res.messages[0].code === "0") {
|
|
1632
|
+
this.emit("deleted");
|
|
1633
|
+
resolve();
|
|
1634
|
+
}
|
|
1635
|
+
else {
|
|
1636
|
+
reject(new FMError(res.messages[0].code, res.status, res, trace));
|
|
1637
|
+
}
|
|
1638
|
+
})
|
|
1639
|
+
.catch(e => {
|
|
1640
|
+
reject(e);
|
|
1641
|
+
});
|
|
1642
|
+
}));
|
|
1643
1643
|
}
|
|
1644
1644
|
}
|
|
1645
1645
|
export class Field {
|
|
@@ -1722,6 +1722,24 @@ export class Field {
|
|
|
1722
1722
|
// if (this.metadata.result === "container") throw "Use await field.stream() to get the contents of a container field, instead of field.value"
|
|
1723
1723
|
return this._value;
|
|
1724
1724
|
}
|
|
1725
|
+
get string() {
|
|
1726
|
+
if (typeof this._value === "string") {
|
|
1727
|
+
return this._value;
|
|
1728
|
+
}
|
|
1729
|
+
throw "Field value is not a string";
|
|
1730
|
+
}
|
|
1731
|
+
get date() {
|
|
1732
|
+
if (this._value instanceof Date) {
|
|
1733
|
+
return this._value;
|
|
1734
|
+
}
|
|
1735
|
+
throw "Field value is not a date";
|
|
1736
|
+
}
|
|
1737
|
+
get number() {
|
|
1738
|
+
if (typeof this._value === "number") {
|
|
1739
|
+
return this._value;
|
|
1740
|
+
}
|
|
1741
|
+
throw "Field value is not a number";
|
|
1742
|
+
}
|
|
1725
1743
|
upload(buffer, filename, mime) {
|
|
1726
1744
|
let trace = new Error();
|
|
1727
1745
|
if (this.metadata.result !== "container")
|