@jd-data-limited/easy-fm 1.2.2-alpha1 → 1.2.2-alpha3
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/index.js +36 -27
- package/index.ts +54 -41
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -284,7 +284,7 @@ class layoutRecordManager {
|
|
|
284
284
|
for (let _field of this.layout.metadata.fieldMetaData) {
|
|
285
285
|
fields[_field.name] = "";
|
|
286
286
|
}
|
|
287
|
-
resolve(new
|
|
287
|
+
resolve(new LayoutRecord(this, -1, 0, fields));
|
|
288
288
|
}).catch(e => {
|
|
289
289
|
reject(e);
|
|
290
290
|
});
|
|
@@ -295,7 +295,7 @@ class layoutRecordManager {
|
|
|
295
295
|
let record;
|
|
296
296
|
this.layout.getLayoutMeta()
|
|
297
297
|
.then(layout => {
|
|
298
|
-
record = new
|
|
298
|
+
record = new LayoutRecord(this.layout, recordId);
|
|
299
299
|
return record.get();
|
|
300
300
|
})
|
|
301
301
|
.then(() => {
|
|
@@ -313,21 +313,13 @@ class layoutRecordManager {
|
|
|
313
313
|
return new find(this.layout);
|
|
314
314
|
}
|
|
315
315
|
}
|
|
316
|
-
class
|
|
317
|
-
constructor(layout, recordId, modId = recordId
|
|
316
|
+
class RecordBase extends EventEmitter {
|
|
317
|
+
constructor(layout, recordId, modId = recordId) {
|
|
318
318
|
super();
|
|
319
|
+
this.portals = [];
|
|
319
320
|
this.layout = layout;
|
|
320
321
|
this.recordId = recordId;
|
|
321
322
|
this.modId = modId;
|
|
322
|
-
this.fields = this.processFieldData(fieldData);
|
|
323
|
-
this.portals = [];
|
|
324
|
-
if (portalData) {
|
|
325
|
-
this.processPortalData(portalData);
|
|
326
|
-
}
|
|
327
|
-
this.on("saved", () => {
|
|
328
|
-
for (let field of this.fields)
|
|
329
|
-
field.edited = false;
|
|
330
|
-
});
|
|
331
323
|
}
|
|
332
324
|
get endpoint() {
|
|
333
325
|
return `${this.layout.endpoint}/records/${this.recordId}`;
|
|
@@ -342,9 +334,9 @@ class record extends EventEmitter {
|
|
|
342
334
|
let fieldData = item;
|
|
343
335
|
delete fieldData.recordId;
|
|
344
336
|
delete fieldData.modId;
|
|
345
|
-
return new
|
|
337
|
+
return new PortalRecord(this, _portal, item.recordId, item.modId, fieldData);
|
|
346
338
|
});
|
|
347
|
-
this.portals.push();
|
|
339
|
+
this.portals.push(_portal);
|
|
348
340
|
}
|
|
349
341
|
}
|
|
350
342
|
processFieldData(fieldData) {
|
|
@@ -381,7 +373,7 @@ class record extends EventEmitter {
|
|
|
381
373
|
}
|
|
382
374
|
get() {
|
|
383
375
|
if (this.recordId === -1) {
|
|
384
|
-
throw "Cannot get this
|
|
376
|
+
throw "Cannot get this RecordBase until a commit() is done.";
|
|
385
377
|
}
|
|
386
378
|
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
387
379
|
if (!this.layout.metadata)
|
|
@@ -417,7 +409,7 @@ class record extends EventEmitter {
|
|
|
417
409
|
delete data.recordId;
|
|
418
410
|
delete data.modId;
|
|
419
411
|
if (this.recordId === -1) {
|
|
420
|
-
// This is a new
|
|
412
|
+
// This is a new LayoutRecord
|
|
421
413
|
extraBody.fieldData = data.fieldData;
|
|
422
414
|
this.layout.database.apiRequest(`${this.layout.endpoint}/records`, {
|
|
423
415
|
port: 443,
|
|
@@ -455,7 +447,7 @@ class record extends EventEmitter {
|
|
|
455
447
|
}
|
|
456
448
|
else if (res.messages[0].code === "0") {
|
|
457
449
|
this.modId = res.response.modId;
|
|
458
|
-
this.
|
|
450
|
+
this._onSave();
|
|
459
451
|
resolve(this);
|
|
460
452
|
}
|
|
461
453
|
else {
|
|
@@ -467,6 +459,11 @@ class record extends EventEmitter {
|
|
|
467
459
|
});
|
|
468
460
|
}));
|
|
469
461
|
}
|
|
462
|
+
_onSave() {
|
|
463
|
+
this.emit("saved");
|
|
464
|
+
for (let field of this.fields)
|
|
465
|
+
field.edited = false;
|
|
466
|
+
}
|
|
470
467
|
getField(field) {
|
|
471
468
|
return this.fields.find(_field => _field.id === field);
|
|
472
469
|
}
|
|
@@ -508,7 +505,7 @@ class record extends EventEmitter {
|
|
|
508
505
|
}
|
|
509
506
|
else if (res.messages[0].code === "0") {
|
|
510
507
|
let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false);
|
|
511
|
-
let _res = new
|
|
508
|
+
let _res = new LayoutRecord(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData);
|
|
512
509
|
this.emit("duplicated");
|
|
513
510
|
resolve(_res);
|
|
514
511
|
}
|
|
@@ -561,6 +558,16 @@ class record extends EventEmitter {
|
|
|
561
558
|
return obj;
|
|
562
559
|
}
|
|
563
560
|
}
|
|
561
|
+
class LayoutRecord extends RecordBase {
|
|
562
|
+
constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
|
|
563
|
+
super(layout, recordId, modId);
|
|
564
|
+
this.fields = this.processFieldData(fieldData);
|
|
565
|
+
this.portals = [];
|
|
566
|
+
if (portalData) {
|
|
567
|
+
this.processPortalData(portalData);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
564
571
|
class field {
|
|
565
572
|
constructor(record, id, contents) {
|
|
566
573
|
this.record = record;
|
|
@@ -600,7 +607,7 @@ class field {
|
|
|
600
607
|
repetitionEnd: 1
|
|
601
608
|
};
|
|
602
609
|
}
|
|
603
|
-
if (this.record instanceof
|
|
610
|
+
if (this.record instanceof PortalRecord) {
|
|
604
611
|
return this.record.layout.metadata.portalMetaData[this.record.portal.name || "portal not attached"].find(i => i.name === this.id) || {
|
|
605
612
|
name: this.id.toString(),
|
|
606
613
|
type: 'normal',
|
|
@@ -679,13 +686,15 @@ class portal {
|
|
|
679
686
|
this.name = name;
|
|
680
687
|
}
|
|
681
688
|
}
|
|
682
|
-
class
|
|
689
|
+
class PortalRecord extends RecordBase {
|
|
683
690
|
constructor(record, portal, recordId, modId = recordId, fieldData = {}) {
|
|
684
|
-
super(record.layout, recordId, modId
|
|
691
|
+
super(record.layout, recordId, modId);
|
|
685
692
|
this.portal = portal;
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
693
|
+
this.fields = this.processFieldData(fieldData);
|
|
694
|
+
}
|
|
695
|
+
_onSave() {
|
|
696
|
+
super._onSave();
|
|
697
|
+
this.portal.record._onSave();
|
|
689
698
|
}
|
|
690
699
|
commit(extraBody = { fieldData: undefined }) {
|
|
691
700
|
return this.portal.record.commit(extraBody);
|
|
@@ -778,7 +787,7 @@ class recordGetRange extends recordGetOperation {
|
|
|
778
787
|
if (!this.layout.metadata)
|
|
779
788
|
yield this.layout.getLayoutMeta();
|
|
780
789
|
let data = res.response.data.map(item => {
|
|
781
|
-
return new
|
|
790
|
+
return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData);
|
|
782
791
|
});
|
|
783
792
|
resolve(data);
|
|
784
793
|
}
|
|
@@ -847,7 +856,7 @@ class find extends recordGetOperation {
|
|
|
847
856
|
if (!this.layout.metadata)
|
|
848
857
|
yield this.layout.getLayoutMeta();
|
|
849
858
|
let data = res.response.data.map(item => {
|
|
850
|
-
return new
|
|
859
|
+
return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData);
|
|
851
860
|
});
|
|
852
861
|
resolve(data);
|
|
853
862
|
}
|
package/index.ts
CHANGED
|
@@ -338,14 +338,14 @@ class layout {
|
|
|
338
338
|
/**
|
|
339
339
|
* @deprecated use layout.records.create() instead
|
|
340
340
|
*/
|
|
341
|
-
createRecord(): Promise<
|
|
341
|
+
createRecord(): Promise<RecordBase | Error | void> {
|
|
342
342
|
return this.records.create()
|
|
343
343
|
}
|
|
344
344
|
|
|
345
345
|
/**
|
|
346
346
|
* @deprecated use layout.records.get() instead
|
|
347
347
|
*/
|
|
348
|
-
getRecord(recordId): Promise<
|
|
348
|
+
getRecord(recordId): Promise<RecordBase> {
|
|
349
349
|
return this.records.get(recordId)
|
|
350
350
|
}
|
|
351
351
|
|
|
@@ -401,7 +401,7 @@ class layoutRecordManager {
|
|
|
401
401
|
this.layout = layout
|
|
402
402
|
}
|
|
403
403
|
|
|
404
|
-
create(): Promise<
|
|
404
|
+
create(): Promise<RecordBase | Error | void> {
|
|
405
405
|
return new Promise((resolve, reject) => {
|
|
406
406
|
// Get the layout's metadata
|
|
407
407
|
this.layout.getLayoutMeta().then(layout => {
|
|
@@ -409,19 +409,19 @@ class layoutRecordManager {
|
|
|
409
409
|
for (let _field of this.layout.metadata.fieldMetaData) {
|
|
410
410
|
fields[_field.name] = ""
|
|
411
411
|
}
|
|
412
|
-
resolve(new
|
|
412
|
+
resolve(new LayoutRecord(this, -1, 0, fields))
|
|
413
413
|
}).catch(e => {
|
|
414
414
|
reject(e)
|
|
415
415
|
})
|
|
416
416
|
})
|
|
417
417
|
}
|
|
418
418
|
|
|
419
|
-
get(recordId): Promise<
|
|
419
|
+
get(recordId): Promise<RecordBase> {
|
|
420
420
|
return new Promise((resolve, reject) => {
|
|
421
421
|
let record
|
|
422
422
|
this.layout.getLayoutMeta()
|
|
423
423
|
.then(layout => {
|
|
424
|
-
record = new
|
|
424
|
+
record = new LayoutRecord(this.layout, recordId)
|
|
425
425
|
return record.get()
|
|
426
426
|
})
|
|
427
427
|
.then(() => {
|
|
@@ -442,27 +442,19 @@ class layoutRecordManager {
|
|
|
442
442
|
}
|
|
443
443
|
}
|
|
444
444
|
|
|
445
|
-
class
|
|
445
|
+
class RecordBase extends EventEmitter {
|
|
446
446
|
readonly layout: layout;
|
|
447
447
|
public recordId: number;
|
|
448
448
|
modId: number;
|
|
449
449
|
fields: field[];
|
|
450
|
-
|
|
451
|
-
|
|
450
|
+
portals: portal[] = [];
|
|
451
|
+
protected portalData: any[];
|
|
452
452
|
|
|
453
|
-
constructor(layout, recordId, modId = recordId
|
|
453
|
+
constructor(layout, recordId, modId = recordId) {
|
|
454
454
|
super();
|
|
455
455
|
this.layout = layout
|
|
456
456
|
this.recordId = recordId
|
|
457
457
|
this.modId = modId
|
|
458
|
-
this.fields = this.processFieldData(fieldData)
|
|
459
|
-
this.portals = []
|
|
460
|
-
if (portalData) {
|
|
461
|
-
this.processPortalData(portalData)
|
|
462
|
-
}
|
|
463
|
-
this.on("saved", () => {
|
|
464
|
-
for (let field of this.fields) field.edited = false
|
|
465
|
-
})
|
|
466
458
|
}
|
|
467
459
|
|
|
468
460
|
get endpoint(): string {
|
|
@@ -473,16 +465,16 @@ class record extends EventEmitter {
|
|
|
473
465
|
return !!this.fields.find(i => i.edited)
|
|
474
466
|
}
|
|
475
467
|
|
|
476
|
-
|
|
468
|
+
protected processPortalData(portalData): void {
|
|
477
469
|
for (let item of Object.keys(portalData)) {
|
|
478
470
|
let _portal = new portal(this, item)
|
|
479
471
|
_portal.records = portalData[item].map(item => {
|
|
480
472
|
let fieldData = item;
|
|
481
473
|
delete fieldData.recordId;
|
|
482
474
|
delete fieldData.modId;
|
|
483
|
-
return new
|
|
475
|
+
return new PortalRecord(this, _portal, item.recordId, item.modId, fieldData);
|
|
484
476
|
})
|
|
485
|
-
this.portals.push();
|
|
477
|
+
this.portals.push(_portal);
|
|
486
478
|
}
|
|
487
479
|
}
|
|
488
480
|
|
|
@@ -520,7 +512,7 @@ class record extends EventEmitter {
|
|
|
520
512
|
|
|
521
513
|
get() {
|
|
522
514
|
if (this.recordId === -1) {
|
|
523
|
-
throw "Cannot get this
|
|
515
|
+
throw "Cannot get this RecordBase until a commit() is done."
|
|
524
516
|
}
|
|
525
517
|
return new Promise(async (resolve, reject) => {
|
|
526
518
|
if (!this.layout.metadata) await this.layout.getLayoutMeta()
|
|
@@ -549,14 +541,14 @@ class record extends EventEmitter {
|
|
|
549
541
|
|
|
550
542
|
commit(extraBody = {
|
|
551
543
|
fieldData: undefined
|
|
552
|
-
}): Promise<
|
|
544
|
+
}): Promise<RecordBase | FMError> {
|
|
553
545
|
return new Promise(async (resolve, reject) => {
|
|
554
546
|
let data = this.toObject()
|
|
555
547
|
delete data.recordId
|
|
556
548
|
delete data.modId
|
|
557
549
|
|
|
558
550
|
if (this.recordId === -1) {
|
|
559
|
-
// This is a new
|
|
551
|
+
// This is a new LayoutRecord
|
|
560
552
|
extraBody.fieldData = data.fieldData
|
|
561
553
|
|
|
562
554
|
this.layout.database.apiRequest(`${this.layout.endpoint}/records`, {
|
|
@@ -593,7 +585,7 @@ class record extends EventEmitter {
|
|
|
593
585
|
reject(new FMError(res.response.scriptError, res.status, res))
|
|
594
586
|
} else if (res.messages[0].code === "0") {
|
|
595
587
|
this.modId = res.response.modId
|
|
596
|
-
this.
|
|
588
|
+
this._onSave()
|
|
597
589
|
resolve(this)
|
|
598
590
|
} else {
|
|
599
591
|
reject(new FMError(res.messages[0].code, res.status, res))
|
|
@@ -604,6 +596,11 @@ class record extends EventEmitter {
|
|
|
604
596
|
})
|
|
605
597
|
})
|
|
606
598
|
}
|
|
599
|
+
|
|
600
|
+
_onSave() {
|
|
601
|
+
this.emit("saved")
|
|
602
|
+
for (let field of this.fields) field.edited = false
|
|
603
|
+
}
|
|
607
604
|
|
|
608
605
|
getField(field) {
|
|
609
606
|
return this.fields.find(_field => _field.id === field)
|
|
@@ -635,7 +632,7 @@ class record extends EventEmitter {
|
|
|
635
632
|
})
|
|
636
633
|
}
|
|
637
634
|
|
|
638
|
-
duplicate(): Promise<
|
|
635
|
+
duplicate(): Promise<RecordBase> {
|
|
639
636
|
return new Promise(async (resolve, reject) => {
|
|
640
637
|
this.layout.database.apiRequest(this.endpoint, {
|
|
641
638
|
port: 443,
|
|
@@ -646,7 +643,7 @@ class record extends EventEmitter {
|
|
|
646
643
|
reject(new FMError(res.response.scriptError, res.status, res))
|
|
647
644
|
} else if (res.messages[0].code === "0") {
|
|
648
645
|
let data = this.toObject((a) => true, (a) => true, (a) => false, (a) => false)
|
|
649
|
-
let _res = new
|
|
646
|
+
let _res = new LayoutRecord(this.layout, res.response.recordId, res.response.modId, data.fieldData, data.portalData)
|
|
650
647
|
|
|
651
648
|
this.emit("duplicated")
|
|
652
649
|
resolve(_res)
|
|
@@ -708,9 +705,21 @@ class record extends EventEmitter {
|
|
|
708
705
|
return obj
|
|
709
706
|
}
|
|
710
707
|
}
|
|
708
|
+
class LayoutRecord extends RecordBase {
|
|
709
|
+
portals: portal[];
|
|
710
|
+
|
|
711
|
+
constructor(layout, recordId, modId = recordId, fieldData = {}, portalData = null) {
|
|
712
|
+
super(layout, recordId, modId);
|
|
713
|
+
this.fields = this.processFieldData(fieldData)
|
|
714
|
+
this.portals = []
|
|
715
|
+
if (portalData) {
|
|
716
|
+
this.processPortalData(portalData)
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
711
720
|
|
|
712
721
|
class field {
|
|
713
|
-
record:
|
|
722
|
+
record: RecordBase;
|
|
714
723
|
id: number;
|
|
715
724
|
private _value: string | number | Date;
|
|
716
725
|
edited: boolean;
|
|
@@ -756,7 +765,7 @@ class field {
|
|
|
756
765
|
repetitionEnd: 1
|
|
757
766
|
} as fieldMetaData
|
|
758
767
|
}
|
|
759
|
-
if (this.record instanceof
|
|
768
|
+
if (this.record instanceof PortalRecord) {
|
|
760
769
|
return this.record.layout.metadata.portalMetaData[this.record.portal.name || "portal not attached"].find(i => i.name === this.id) as fieldMetaData || {
|
|
761
770
|
name: this.id.toString(),
|
|
762
771
|
type: 'normal',
|
|
@@ -832,25 +841,29 @@ class field {
|
|
|
832
841
|
}
|
|
833
842
|
|
|
834
843
|
class portal {
|
|
835
|
-
readonly record:
|
|
844
|
+
readonly record: RecordBase;
|
|
836
845
|
readonly name: string;
|
|
837
|
-
public records:
|
|
846
|
+
public records: PortalRecord[];
|
|
838
847
|
|
|
839
|
-
constructor(record:
|
|
848
|
+
constructor(record: RecordBase, name: string) {
|
|
840
849
|
this.record = record
|
|
841
850
|
this.name = name
|
|
842
851
|
}
|
|
843
852
|
}
|
|
844
853
|
|
|
845
|
-
class
|
|
854
|
+
class PortalRecord extends RecordBase {
|
|
846
855
|
readonly portal: portal;
|
|
847
856
|
|
|
848
857
|
constructor(record, portal, recordId, modId = recordId, fieldData = {}) {
|
|
849
|
-
super(record.layout, recordId, modId
|
|
858
|
+
super(record.layout, recordId, modId);
|
|
859
|
+
|
|
850
860
|
this.portal = portal
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
861
|
+
this.fields = this.processFieldData(fieldData)
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
_onSave() {
|
|
865
|
+
super._onSave();
|
|
866
|
+
this.portal.record._onSave()
|
|
854
867
|
}
|
|
855
868
|
|
|
856
869
|
commit(extraBody = {fieldData: undefined}) {
|
|
@@ -933,7 +946,7 @@ class recordGetRange extends recordGetOperation {
|
|
|
933
946
|
return "?" + params.join("&")
|
|
934
947
|
}
|
|
935
948
|
|
|
936
|
-
run(): Promise<
|
|
949
|
+
run(): Promise<RecordBase[]> {
|
|
937
950
|
return new Promise((resolve, reject) => {
|
|
938
951
|
// console.log(this.#toObject())
|
|
939
952
|
this.layout.getLayoutMeta().then(() => {
|
|
@@ -947,7 +960,7 @@ class recordGetRange extends recordGetOperation {
|
|
|
947
960
|
// console.log("RESOLVING")
|
|
948
961
|
if (!this.layout.metadata) await this.layout.getLayoutMeta()
|
|
949
962
|
let data = res.response.data.map(item => {
|
|
950
|
-
return new
|
|
963
|
+
return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData)
|
|
951
964
|
})
|
|
952
965
|
resolve(data)
|
|
953
966
|
} else {
|
|
@@ -1002,7 +1015,7 @@ class find extends recordGetOperation {
|
|
|
1002
1015
|
return this.run()
|
|
1003
1016
|
}
|
|
1004
1017
|
|
|
1005
|
-
run(): Promise<
|
|
1018
|
+
run(): Promise<RecordBase[]> {
|
|
1006
1019
|
return new Promise((resolve, reject) => {
|
|
1007
1020
|
// console.log(this.#toObject())
|
|
1008
1021
|
this.layout.getLayoutMeta()
|
|
@@ -1019,7 +1032,7 @@ class find extends recordGetOperation {
|
|
|
1019
1032
|
// console.log("RESOLVING")
|
|
1020
1033
|
if (!this.layout.metadata) await this.layout.getLayoutMeta()
|
|
1021
1034
|
let data = res.response.data.map(item => {
|
|
1022
|
-
return new
|
|
1035
|
+
return new LayoutRecord(this.layout, item.recordId, item.modId, item.fieldData, item.portalData)
|
|
1023
1036
|
})
|
|
1024
1037
|
resolve(data)
|
|
1025
1038
|
} else {
|