@jd-data-limited/easy-fm 3.2.8 → 4.0.1

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.
Files changed (75) hide show
  1. package/README.md +36 -24
  2. package/package.json +12 -9
  3. package/src/FMError.js +2 -2
  4. package/src/FMError.ts +2 -2
  5. package/src/connection/FMHost.d.ts +2 -1
  6. package/src/connection/FMHost.js +1 -2
  7. package/src/connection/FMHost.ts +4 -4
  8. package/src/connection/HostBase.d.ts +1 -1
  9. package/src/connection/HostBase.js +1 -1
  10. package/src/connection/HostBase.ts +2 -4
  11. package/src/connection/database.d.ts +10 -10
  12. package/src/connection/database.js +6 -17
  13. package/src/connection/database.ts +17 -27
  14. package/src/connection/databaseBase.d.ts +4 -4
  15. package/src/connection/databaseBase.js +1 -1
  16. package/src/connection/databaseBase.ts +4 -6
  17. package/src/databaseStructure.d.ts +6 -0
  18. package/src/databaseStructure.js +4 -0
  19. package/src/databaseStructure.ts +12 -0
  20. package/src/index.d.ts +3 -6
  21. package/src/index.js +3 -5
  22. package/src/index.ts +4 -9
  23. package/src/layouts/layout.d.ts +3 -17
  24. package/src/layouts/layout.js +2 -20
  25. package/src/layouts/layout.ts +5 -27
  26. package/src/layouts/layoutBase.d.ts +4 -4
  27. package/src/layouts/layoutBase.js +1 -1
  28. package/src/layouts/layoutBase.ts +5 -6
  29. package/src/layouts/layoutInterface.d.ts +1 -1
  30. package/src/layouts/layoutInterface.js +1 -1
  31. package/src/layouts/layoutInterface.ts +2 -2
  32. package/src/layouts/layoutRecordManager.d.ts +5 -6
  33. package/src/layouts/layoutRecordManager.js +5 -9
  34. package/src/layouts/layoutRecordManager.ts +12 -16
  35. package/src/layouts/recordFieldsMap.d.ts +2 -2
  36. package/src/layouts/recordFieldsMap.js +1 -1
  37. package/src/layouts/recordFieldsMap.ts +3 -3
  38. package/src/models/apiResults.d.ts +3 -3
  39. package/src/models/apiResults.js +1 -1
  40. package/src/models/apiResults.ts +2 -4
  41. package/src/records/field.d.ts +3 -23
  42. package/src/records/field.js +3 -152
  43. package/src/records/field.ts +4 -169
  44. package/src/records/fieldBase.d.ts +30 -0
  45. package/src/records/fieldBase.js +154 -0
  46. package/src/records/fieldBase.ts +174 -0
  47. package/src/records/getOperations/recordGetOperation.d.ts +47 -11
  48. package/src/records/getOperations/recordGetOperation.js +142 -31
  49. package/src/records/getOperations/recordGetOperation.ts +172 -30
  50. package/src/records/layoutRecord.d.ts +14 -11
  51. package/src/records/layoutRecord.js +7 -8
  52. package/src/records/layoutRecord.ts +32 -22
  53. package/src/records/portal.d.ts +6 -5
  54. package/src/records/portal.js +1 -1
  55. package/src/records/portal.ts +10 -8
  56. package/src/records/portalBase.d.ts +5 -4
  57. package/src/records/portalBase.js +1 -1
  58. package/src/records/portalBase.ts +6 -6
  59. package/src/records/portalRecord.d.ts +12 -8
  60. package/src/records/portalRecord.js +6 -8
  61. package/src/records/portalRecord.ts +17 -16
  62. package/src/records/recordBase.d.ts +11 -15
  63. package/src/records/recordBase.js +9 -13
  64. package/src/records/recordBase.ts +17 -25
  65. package/src/types.d.ts +18 -15
  66. package/src/types.js +7 -1
  67. package/src/types.ts +20 -16
  68. package/src/records/getOperations/find.d.ts +0 -15
  69. package/src/records/getOperations/find.js +0 -70
  70. package/src/records/getOperations/find.ts +0 -77
  71. package/src/records/getOperations/recordGetRange.d.ts +0 -13
  72. package/src/records/getOperations/recordGetRange.js +0 -77
  73. package/src/records/getOperations/recordGetRange.ts +0 -77
  74. package/src/records/portalRecordBase.d.ts +0 -4
  75. package/src/records/portalRecordBase.js +0 -6
@@ -1,12 +1,12 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  import { LayoutRecordManager } from "./layoutRecordManager.js";
5
5
  import { FMError } from "../FMError.js";
6
6
  export class Layout {
7
7
  database;
8
- records = new LayoutRecordManager(this);
9
8
  name;
9
+ records = new LayoutRecordManager(this);
10
10
  metadata;
11
11
  constructor(database, name) {
12
12
  this.database = database;
@@ -15,24 +15,6 @@ export class Layout {
15
15
  get endpoint() {
16
16
  return `${this.database.endpoint}/layouts/${this.name}`;
17
17
  }
18
- /**
19
- * @deprecated use layout.records.create() instead
20
- */
21
- createRecord() {
22
- return this.records.create();
23
- }
24
- /**
25
- * @deprecated use layout.records.get() instead
26
- */
27
- getRecord(recordId) {
28
- return this.records.get(recordId);
29
- }
30
- /**
31
- * @deprecated use layout.records.find() instead
32
- */
33
- newFind() {
34
- return this.records.find();
35
- }
36
18
  async runScript(script) {
37
19
  let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`;
38
20
  if (script.parameter)
@@ -1,21 +1,19 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
5
  import {LayoutRecordManager} from "./layoutRecordManager.js";
6
- import {LayoutRecord} from "../records/layoutRecord.js";
7
6
  import {Script, ScriptResult} from "../types.js";
8
7
  import {LayoutInterface} from "./layoutInterface.js";
9
- import {Find} from "../records/getOperations/find.js";
10
8
  import {FMError} from "../FMError.js";
11
9
  import {LayoutBase} from "./layoutBase.js"
12
- import {DatabaseBase} from "../connection/databaseBase";
13
- import {ApiLayoutMetadata, ApiScriptResult} from "../models/apiResults";
10
+ import {DatabaseBase} from "../connection/databaseBase.js";
11
+ import {ApiLayoutMetadata, ApiScriptResult} from "../models/apiResults.js";
14
12
 
15
13
  export class Layout<T extends LayoutInterface> implements LayoutBase {
16
14
  readonly database: DatabaseBase;
17
- readonly records = new LayoutRecordManager<T>(this)
18
15
  readonly name: string;
16
+ readonly records = new LayoutRecordManager<T>(this)
19
17
  metadata: ApiLayoutMetadata;
20
18
 
21
19
  constructor(database: DatabaseBase, name: string) {
@@ -23,31 +21,11 @@ export class Layout<T extends LayoutInterface> implements LayoutBase {
23
21
  this.name = name
24
22
  }
25
23
 
24
+
26
25
  get endpoint() {
27
26
  return `${this.database.endpoint}/layouts/${this.name}`
28
27
  }
29
28
 
30
- /**
31
- * @deprecated use layout.records.create() instead
32
- */
33
- createRecord(): Promise<LayoutRecord<T["fields"], T["portals"]>> {
34
- return this.records.create()
35
- }
36
-
37
- /**
38
- * @deprecated use layout.records.get() instead
39
- */
40
- getRecord(recordId): Promise<LayoutRecord<T["fields"], T["portals"]>> {
41
- return this.records.get(recordId)
42
- }
43
-
44
- /**
45
- * @deprecated use layout.records.find() instead
46
- */
47
- newFind(): Find<T> {
48
- return this.records.find()
49
- }
50
-
51
29
  async runScript(script: Script): Promise<ScriptResult> {
52
30
  let url = `${this.endpoint}/script/${encodeURIComponent(script.name)}`
53
31
  if (script.parameter) url += "?script.param=" + encodeURIComponent(script.parameter)
@@ -1,9 +1,9 @@
1
- import { Script, ScriptResult } from "../types";
2
- import { DatabaseBase } from "../connection/databaseBase";
3
- import { ApiLayoutMetadata } from "../models/apiResults";
1
+ import { Script, ScriptResult } from "../types.js";
2
+ import { DatabaseBase } from "../connection/databaseBase.js";
3
+ import { ApiLayoutMetadata } from "../models/apiResults.js";
4
4
  export interface LayoutBase {
5
5
  readonly name: string;
6
- metadata: any;
6
+ metadata: ApiLayoutMetadata | null;
7
7
  endpoint: string;
8
8
  runScript(script: Script): Promise<ScriptResult>;
9
9
  getLayoutMeta(): Promise<ApiLayoutMetadata>;
@@ -1,4 +1,4 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  export {};
@@ -1,15 +1,14 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
- import {Script, ScriptResult} from "../types";
6
- import {FMError} from "../FMError";
7
- import {DatabaseBase} from "../connection/databaseBase";
8
- import {ApiLayoutMetadata} from "../models/apiResults";
5
+ import {Script, ScriptResult} from "../types.js";
6
+ import {DatabaseBase} from "../connection/databaseBase.js";
7
+ import {ApiLayoutMetadata} from "../models/apiResults.js";
9
8
 
10
9
  export interface LayoutBase {
11
10
  readonly name: string
12
- metadata: any
11
+ metadata: ApiLayoutMetadata | null
13
12
  endpoint: string
14
13
  runScript(script: Script): Promise<ScriptResult>
15
14
  getLayoutMeta(): Promise<ApiLayoutMetadata>
@@ -1,5 +1,5 @@
1
1
  import { Portal } from "../records/portal.js";
2
- import { RecordFieldsMap } from "./recordFieldsMap";
2
+ import { RecordFieldsMap } from "./recordFieldsMap.js";
3
3
  export interface LayoutInterface {
4
4
  fields: RecordFieldsMap;
5
5
  portals: PortalInterface;
@@ -1,4 +1,4 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  export {};
@@ -1,9 +1,9 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
5
  import {Portal} from "../records/portal.js";
6
- import {RecordFieldsMap} from "./recordFieldsMap";
6
+ import {RecordFieldsMap} from "./recordFieldsMap.js";
7
7
 
8
8
  export interface LayoutInterface {
9
9
  fields: RecordFieldsMap,
@@ -1,13 +1,12 @@
1
1
  import { LayoutRecord } from "../records/layoutRecord.js";
2
2
  import { LayoutInterface } from "./layoutInterface.js";
3
- import { RecordGetRange } from "../records/getOperations/recordGetRange.js";
4
- import { Find } from "../records/getOperations/find.js";
5
3
  import { LayoutBase } from "./layoutBase.js";
4
+ import { GetOperationOptions, RecordGetOperation } from "../records/getOperations/recordGetOperation.js";
5
+ import { RecordFetchOptions } from "../types.js";
6
6
  export declare class LayoutRecordManager<T extends LayoutInterface> {
7
7
  readonly layout: LayoutBase;
8
8
  constructor(layout: LayoutBase);
9
- create(): Promise<LayoutRecord<T["fields"], T["portals"]>>;
10
- get(recordId: number): Promise<LayoutRecord<T["fields"], T["portals"]>>;
11
- range(start?: number, limit?: number): RecordGetRange<T>;
12
- find(start?: number, limit?: number): Find<T>;
9
+ create<OPTIONS extends RecordFetchOptions>(options: OPTIONS): Promise<LayoutRecord<T, OPTIONS["portals"][number]>>;
10
+ get(recordId: number): Promise<LayoutRecord<T>>;
11
+ list<OPTIONS extends GetOperationOptions<T>>(options: OPTIONS): RecordGetOperation<T, OPTIONS>;
13
12
  }
@@ -1,15 +1,14 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  import { LayoutRecord } from "../records/layoutRecord.js";
5
- import { RecordGetRange } from "../records/getOperations/recordGetRange.js";
6
- import { Find } from "../records/getOperations/find.js";
5
+ import { RecordGetOperation } from "../records/getOperations/recordGetOperation.js";
7
6
  export class LayoutRecordManager {
8
7
  layout;
9
8
  constructor(layout) {
10
9
  this.layout = layout;
11
10
  }
12
- async create() {
11
+ async create(options) {
13
12
  await this.layout.getLayoutMeta();
14
13
  let fields = {};
15
14
  for (let _field of this.layout.metadata.fieldMetaData) {
@@ -26,10 +25,7 @@ export class LayoutRecordManager {
26
25
  await record.get();
27
26
  return record;
28
27
  }
29
- range(start = 0, limit = 100) {
30
- return new RecordGetRange(this.layout, start, limit);
31
- }
32
- find(start = 0, limit = 100) {
33
- return new Find(this.layout, start, limit);
28
+ list(options) {
29
+ return new RecordGetOperation(this.layout, options);
34
30
  }
35
31
  }
@@ -1,43 +1,39 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
5
  import {LayoutRecord} from "../records/layoutRecord.js";
6
6
  import {LayoutInterface} from "./layoutInterface.js";
7
- import {RecordGetRange} from "../records/getOperations/recordGetRange.js";
8
- import {Find} from "../records/getOperations/find.js";
9
7
  import {LayoutBase} from "./layoutBase.js"
8
+ import {GetOperationOptions, RecordGetOperation} from "../records/getOperations/recordGetOperation.js";
9
+ import {RecordFetchOptions} from "../types.js";
10
+ import {ApiFieldData} from "../models/apiResults.js";
10
11
 
11
12
  export class LayoutRecordManager<T extends LayoutInterface> {
12
- readonly layout: LayoutBase
13
-
13
+ readonly layout: LayoutBase;
14
14
  constructor(layout: LayoutBase) {
15
15
  this.layout = layout
16
16
  }
17
17
 
18
- async create(): Promise<LayoutRecord<T["fields"], T["portals"]>> {
18
+ async create<OPTIONS extends RecordFetchOptions>(options: OPTIONS): Promise<LayoutRecord<T, OPTIONS["portals"][number]>> {
19
19
  await this.layout.getLayoutMeta()
20
- let fields = {}
20
+ let fields: ApiFieldData = {}
21
21
  for (let _field of this.layout.metadata.fieldMetaData) {
22
22
  fields[_field.name] = ""
23
23
  }
24
- let portals = {}
24
+ let portals: {[key: string]: []} = {}
25
25
  for (let _portal of Object.keys(this.layout.metadata.portalMetaData)) portals[_portal] = []
26
26
  return new LayoutRecord(this.layout, -1, 0, fields, portals)
27
27
  }
28
28
 
29
- async get(recordId: number): Promise<LayoutRecord<T["fields"], T["portals"]>> {
29
+ async get(recordId: number): Promise<LayoutRecord<T>> {
30
30
  await this.layout.getLayoutMeta()
31
- let record = new LayoutRecord(this.layout, recordId)
31
+ let record = new LayoutRecord<T>(this.layout, recordId)
32
32
  await record.get()
33
33
  return record
34
34
  }
35
35
 
36
- range(start = 0, limit = 100) {
37
- return new RecordGetRange<T>(this.layout, start, limit)
38
- }
39
-
40
- find(start = 0, limit = 100): Find<T> {
41
- return new Find<T>(this.layout, start, limit)
36
+ list<OPTIONS extends GetOperationOptions<T>>(options: OPTIONS) {
37
+ return new RecordGetOperation<T, OPTIONS>(this.layout, options)
42
38
  }
43
39
  }
@@ -1,4 +1,4 @@
1
- import { Field, FieldValue } from "../records/field";
1
+ import { FieldBase, FieldValue } from "../records/fieldBase.js";
2
2
  export interface RecordFieldsMap {
3
- [fieldName: string]: Field<FieldValue>;
3
+ [fieldName: string]: FieldBase<FieldValue>;
4
4
  }
@@ -1,4 +1,4 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  export {};
@@ -1,9 +1,9 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
- import {Field, FieldValue} from "../records/field";
5
+ import {FieldBase, FieldValue} from "../records/fieldBase.js";
6
6
 
7
7
  export interface RecordFieldsMap {
8
- [fieldName: string]: Field<FieldValue>
8
+ [fieldName: string]: FieldBase<FieldValue>
9
9
  }
@@ -75,9 +75,9 @@ export interface ApiRecordResponseObj {
75
75
  data: ApiRowDataDef[];
76
76
  dataInfo: ApiResultSetObj;
77
77
  }
78
- export interface ApiFieldData {
79
- [key: string]: string;
80
- }
78
+ export type ApiFieldData = {
79
+ [key: string]: string | number;
80
+ };
81
81
  export interface ApiPortalData {
82
82
  [key: string]: ApiFieldData[];
83
83
  }
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
  export var ApiFieldTypes;
5
5
  (function (ApiFieldTypes) {
@@ -1,9 +1,7 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2023-2024. See LICENSE file for more information
3
3
  */
4
4
 
5
- import * as stream from "stream";
6
-
7
5
  export enum ApiFieldTypes {
8
6
  NORMAL = "normal",
9
7
  CALCULATION = "calculation",
@@ -85,7 +83,7 @@ export interface ApiRecordResponseObj {
85
83
  dataInfo: ApiResultSetObj
86
84
  }
87
85
 
88
- export interface ApiFieldData {[key: string]: string}
86
+ export type ApiFieldData = {[key: string]: string | number}
89
87
 
90
88
  export interface ApiPortalData {[key: string]: ApiFieldData[]}
91
89
 
@@ -1,25 +1,5 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { RecordBase } from "./recordBase.js";
4
- import * as http from "http";
5
- import { ContainerBufferResult, DOWNLOAD_MODES } from "../types.js";
6
- import { ApiFieldMetadata } from "../models/apiResults.js";
7
- export type FieldValue = string | number | Date | Container;
8
- export type Container = null;
9
- export declare class Field<T extends FieldValue> {
1
+ import { RecordBase } from "./recordBase";
2
+ import { FieldBase, FieldValue } from "./fieldBase";
3
+ export declare class Field<T extends FieldValue> extends FieldBase<T> {
10
4
  record: RecordBase<any>;
11
- id: string;
12
- protected _value: T;
13
- edited: boolean;
14
- constructor(record: any, id: any, contents: any);
15
- set(content: string | number | Date | undefined | null): void;
16
- get metadata(): ApiFieldMetadata;
17
- get value(): T;
18
- set value(value: T);
19
- get string(): string;
20
- get date(): Date;
21
- get number(): number;
22
- upload(buffer: Buffer, filename: string, mime: string): Promise<void>;
23
- download(mode: DOWNLOAD_MODES.Stream): Promise<http.IncomingMessage>;
24
- download(mode: DOWNLOAD_MODES.Buffer): Promise<ContainerBufferResult>;
25
5
  }
@@ -1,156 +1,7 @@
1
1
  /*
2
- * Copyright (c) 2023. See LICENSE file for more information
2
+ * Copyright (c) 2024. See LICENSE file for more information
3
3
  */
4
- import { RecordTypes } from "./recordBase.js";
5
- import { File, FormData } from "node-fetch";
6
- import { DOWNLOAD_MODES } from "../types.js";
7
- import { ApiFieldDisplayTypes, ApiFieldResultTypes, ApiFieldTypes } from "../models/apiResults.js";
8
- export class Field {
4
+ import { FieldBase } from "./fieldBase";
5
+ export class Field extends FieldBase {
9
6
  record;
10
- id;
11
- _value;
12
- edited;
13
- constructor(record, id, contents) {
14
- this.record = record;
15
- this.id = id;
16
- this._value = contents;
17
- this.edited = false;
18
- }
19
- set(content) {
20
- if (this.metadata.result === "container")
21
- throw "Cannot set container value using set(). Use upload() instead.";
22
- if ((this.metadata.result === "timeStamp" ||
23
- this.metadata.result === "date" ||
24
- this.metadata.result === "time")
25
- && !(content instanceof Date) && !!content) {
26
- throw "Value was not an instance of Date: " + content;
27
- }
28
- // @ts-ignore
29
- if (!content)
30
- this._value = "";
31
- // @ts-ignore
32
- else
33
- this._value = content;
34
- this.edited = true;
35
- }
36
- get metadata() {
37
- if (!this.record.layout.metadata) {
38
- // Default to a regular text field
39
- return {
40
- name: this.id.toString(),
41
- type: ApiFieldTypes.NORMAL,
42
- displayType: ApiFieldDisplayTypes.EDIT_TEXT,
43
- result: ApiFieldResultTypes.TEXT,
44
- global: false,
45
- autoEnter: true,
46
- fourDigitYear: false,
47
- maxRepeat: 1,
48
- maxCharacters: 0,
49
- notEmpty: false,
50
- numeric: false,
51
- timeOfDay: false,
52
- repetitions: 1,
53
- valueList: ""
54
- };
55
- }
56
- if (this.record.type === RecordTypes.PORTAL) {
57
- // @ts-ignore
58
- return this.record.layout.metadata.portalMetaData[this.record.portal.name || "portal not attached"].find(i => i.name === this.id) || {
59
- name: this.id.toString(),
60
- type: 'normal',
61
- displayType: 'editText',
62
- result: 'text',
63
- global: false,
64
- autoEnter: true,
65
- fourDigitYear: false,
66
- maxRepeat: 1,
67
- maxCharacters: 0,
68
- notEmpty: false,
69
- numeric: false,
70
- timeOfDay: false,
71
- repetitionStart: 1,
72
- repetitionEnd: 1
73
- };
74
- }
75
- else {
76
- return this.record.layout.metadata.fieldMetaData.find(i => i.name === this.id) || {
77
- name: this.id.toString(),
78
- type: ApiFieldTypes.NORMAL,
79
- displayType: ApiFieldDisplayTypes.EDIT_TEXT,
80
- result: ApiFieldResultTypes.TEXT,
81
- global: false,
82
- autoEnter: true,
83
- fourDigitYear: false,
84
- maxRepeat: 1,
85
- maxCharacters: 0,
86
- notEmpty: false,
87
- numeric: false,
88
- timeOfDay: false,
89
- repetitions: 1,
90
- valueList: ""
91
- };
92
- }
93
- }
94
- get value() {
95
- // if (this.metadata.result === "container") throw "Use await field.stream() to get the contents of a container field, instead of field.value"
96
- return this._value;
97
- }
98
- set value(value) {
99
- this._value = value;
100
- }
101
- get string() {
102
- if (typeof this._value === "string") {
103
- return this._value;
104
- }
105
- throw "Field value is not a string";
106
- }
107
- get date() {
108
- if (this._value instanceof Date) {
109
- return this._value;
110
- }
111
- throw "Field value is not a date";
112
- }
113
- get number() {
114
- if (typeof this._value === "number") {
115
- return this._value;
116
- }
117
- throw "Field value is not a number";
118
- }
119
- async upload(buffer, filename, mime) {
120
- let trace = new Error();
121
- if (this.metadata.result !== "container")
122
- throw "Cannot upload a file to the field; " + this.id + " (not a container field)";
123
- let form = new FormData();
124
- form.append("upload", new File([buffer], filename, { type: mime }));
125
- await this.record.layout.database.apiRequest(`${this.record.endpoint}/containers/${this.id}/1`, {
126
- method: "POST",
127
- // @ts-ignore
128
- headers: { "Authorization": "Bearer " + this.record.layout.database.token },
129
- body: form
130
- });
131
- }
132
- async download(mode = DOWNLOAD_MODES.Stream) {
133
- if (this.metadata.result !== ApiFieldResultTypes.CONTAINER)
134
- throw new Error("Cannot perform download() on a non-container field");
135
- let stream = await this.record.layout.database.streamURL(this.string);
136
- if (mode === DOWNLOAD_MODES.Stream) {
137
- return stream;
138
- }
139
- let body = [];
140
- return new Promise((resolve, reject) => {
141
- stream.on("data", chunk => {
142
- body.push(chunk);
143
- });
144
- stream.on("error", (e) => {
145
- reject(e);
146
- });
147
- stream.on("end", () => {
148
- resolve({
149
- buffer: Buffer.concat(body),
150
- mime: stream.headers["content-type"],
151
- request: stream
152
- });
153
- });
154
- });
155
- }
156
7
  }