-
Notifications
You must be signed in to change notification settings - Fork 4
/
Record.js
745 lines (686 loc) · 23.6 KB
/
Record.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
const CreatedTime = require('./fields/CreatedTime');
const DateField = require('./fields/DateField');
const Currency = require('./fields/Currency');
const Field = require('./fields/Field');
const LinkToAnotherRecord = require('./fields/LinkToAnotherRecord');
const Percent = require('./fields/Percent');
const UnknownField = require('./fields/UnknownField');
const Request = require('./Request');
/* Record
* Parameters:
* table: <Table>
* The Table that this record belongs to.
* id: <String>
* The ID of the Record from Airtable.com
* createdTime: <String> <Date>
* The time this Record was created.
* fields: <Object>
* A key-value Object of Field Definitions.
*/
class Record {
/* static get printRecordChanges
* Return:
* A Boolean representing whether or not Record changes will be printed to the console
* during a save operation.
*/
static get printRecordChanges() {
return this._printRecordChanges;
}
/* static set printRecordChanges
* Parameters:
* value:
* A Boolean representing whether or not Record changes should be printed to the console
* during a save operation.
*/
static set printRecordChanges(value) {
this._printRecordChanges = value;
}
constructor(table, id, createdTime, fields = {}) {
this.createdTime = new CreatedTime('createdTime', createdTime, {
dateFormat: 'ISO',
includeTime: true,
timeFormat: 24,
includeSeconds: true
});
this.id = id;
this._cache_ = [];
this.fields = fields;
this.table = table;
}
/* _knownFields
* Do not use this!
* This is used to track known Fields so that invalid fields to not
* get sent during a save request which would throw an error from Airtable.com
*/
get _knownFields() {
return this._knownFields_ || {};
}
/* get createdTime
* Return:
* A CreatedTime Field with a value representing the time this Record was created.
*/
get createdTime() {
return this._createdTime;
}
/* get fields
* Return:
* A key-value Object of { key: Field Object } Where the key is the same key used
* in the Field Definition. If no key was used in the Field Definition (ie a Field
* that wasn't defined but came in from Airtable) then the key will be the name of
* the Field as it is on Airtable.com
*/
get fields() {
return this._fields;
}
/* get id
* Return:
* A String of the ID of the Field from Airtable.com.
*/
get id() {
return this._id;
}
/* get primaryField
* Return:
* Returns the value of the Field that is listed as the Primary Field
* in the Table Definition.
*/
get primaryField() {
return this.getPrimaryField().value;
}
/* get table
* Return:
* Returns that Table that this Record is a part of.
*/
get table() {
return this._table;
}
/* _knownFields
* Do not use this!
* This is used to track known Fields so that invalid fields to not
* get sent during a save request which would throw an error from Airtable.com
*/
set _knownFields(fields) {
const knownFields = {};
Object.entries(fields).forEach(([key, value]) => {
knownFields[key] = true;
})
this._knownFields_ = knownFields;
}
/* set createdTime
* Parameters:
* createdTime: <CreatedTime>
* A CreatedTime Field with a value representing the time this Record was created.
* This cannot be changed once it is set.
*/
set createdTime(createdTime) {
if (this.createdTime !== undefined)
throw new Error('RecordError: createdTime cannot be changed!');
this._createdTime = createdTime;
}
/* set fields
* Parameters:
* fields: <Object>
* A key-value Object of initialized Fields with keys matching the Table Definition.
*/
set fields(fields) {
if (fields === undefined)
return;
Object.entries(fields).forEach(([key, field]) => {
if (!(field instanceof Field))
throw new Error(`RecordError: A field '${key}' was not an instance of a Field object. Received: '${JSON.stringify(field, null, 2)}'`);
});
if (this._cache_ !== undefined)
this._cache_.forEach((key) => {
Object.defineProperty(this, key, {
get: undefined,
set: undefined,
enumerable: true,
configurable: true
});
});
this._cache_ = Object.keys(fields);
this._cache_.forEach((key) => {
if (Record._checkField(key))
Object.defineProperty(this, key, {
get: () => this.fields[key].value,
set: (value) => this.fields[key].value = value,
enumerable: true,
configurable: true
});
});
this._fields = fields;
this._knownFields = fields;
}
/* set id
* Parameters:
* id: <String>
* The ID of this Record from Airtable.com
* This cannot be changed once it is set.
*/
set id(id) {
if (this.id !== undefined)
throw new Error('RecordError: id cannot be changed!');
this._id = id;
}
/* set primaryField
* Parameters:
* value: <Anything>
* The value that the primary Field defined in the Table Definition will accept.
* This just calls the `set value` from whatever Field is marked aas being the primary Field.
*/
set primaryField(value) {
this.getPrimaryField().value = value;
}
/* set table
* Parameters:
* table: <Table>
* The Table that this Record belongs to.
*/
set table(table) {
if (this.table !== undefined)
throw new Error('RecordError: table cannot be changed!');
if (typeof table !== 'object' || table._airtable_ === undefined)
throw new Error("RecordError: Hmm... It doesn't look like you passed in a Table Object.");
this._table = table;
}
/* getFieldByKey(key)
* Parameters:
* key: <String>
* The key the was assigned in the Field Definition.
* Return:
* Returns a Field Object or undefined.
*/
getFieldByKey(key) {
if (this.fields === undefined || typeof key !== 'string')
return;
return this.fields[key];
}
/* getFieldByName(name)
* Parameters:
* name: <String>
* The name of the Field as it is on Airtable.com
* Return:
* Returns a Field Object or undefined.
*/
getFieldByName(name) {
if (this.fields === undefined || typeof name !== 'string')
return;
const values = Object.values(this.fields);
for (let i = 0; i < values.length; i++) {
const field = values[i];
if (field.name === name)
return field;
}
}
/* getPrimaryField()
* Return:
* Returns a Field Object of the primary key defined in the Table Definition or undefined.
*/
getPrimaryField() {
if (this.fields === undefined)
return;
const entries = Object.entries(this.fields);
for (let i = 0; i < entries.length; i++) {
const [key, field] = entries[i];
if (field.isPrimary())
return field;
}
}
/* fget(name)
* Parameters:
* name: <String>
* The name of the Field as it is on Airtable.com
* Return:
* Returns a Field Object or undefined.
* This is an alias for Record.getFieldByName
*/
fget(name) {
return this.getFieldByName(name);
}
/* fgetk(key)
* Parameters:
* key: <String>
* The key the was assigned in the Field Definition.
* Return:
* Returns a Field Object or undefined.
* This is an alias for Record.getFieldByKey
*/
fgetk(key) {
return this.getFieldByKey(key);
}
/* get(name)
* Parameters:
* name: <String>
* The name of the Field as it is on Airtable.com
* Return:
* Returns the value of the Field or undefined.
*/
get(name) {
const field = this.getFieldByName(name);
if (field === undefined)
return;
return field.value;
}
/* getk(key)
* Parameters:
* key: <String>
* The key the was assigned in the Field Definition.
* Return:
* Returns a Field Object or undefined.
*/
getk(key) {
const field = this.getFieldByKey(key);
if (field === undefined)
return;
return field.value;
}
/* has(name)
* Parameters:
* name: <String>
* The name of the Field as it is on Airtable.com
* Return:
* Returns a boolean representing whether or not a Field with the given name exists.
*/
has(name) {
return this.getFieldByName(name) !== undefined;
}
/* hask(key)
* Parameters:
* key: <String>
* The key the was assigned in the Field Definition.
* Return:
* Returns a boolean representing whether or not a Field referenced by the given key exists.
*/
hask(key) {
return this.getFieldByKey(key) !== undefined;
}
/* put(name, value)
* Parameters:
* name: <String>
* The name of the Field as it is on Airtable.com
* value: <Anything>
* The value of the Field.
* Sets the value of the field with the given name.
* Return:
* Returns false if the Field did not exist, otherwise it will return true.
*/
put(name, value) {
const field = this.getFieldByName(name);
if (field === undefined)
return false;
// throw new Error(`TableError: Field '${key}' does not exist!`);
field.value = value;
return true;
}
/* putk(key, value)
* Parameters:
* key: <String>
* The key the was assigned in the Field Definition.
* value: <Anything>
* The value of the Field.
* Sets the value of the field with the given name.
* Return:
* Returns false if the Field did not exist, otherwise it will return true.
*/
putk(key, value) {
const field = this.getFieldByKey(key);
if (field === undefined)
return false;
// throw new Error(`TableError: Field '${key}' does not exist!`);
field.value = value;
return true;
}
/* createRecord(setupLinks)
* Parameters:
* [setupLinks: <Boolean>]
* A Boolean representing whether or not to set up LinkToAnotherRecord Fields
* on the Record that will be resolved. Setting it to anything other than false
* will result in it being set to true.
* Return:
* Returns a Promise which will resolve either a newly created Record or undefined
* if the Record failed to be created. It will reject an error from Airtable.com on
* a failed create request.
*/
create(setupLinks = true) {
setupLinks = setupLinks === true;
return this.table.createRecord(this, setupLinks);
}
/* save(deepSave)
* Parameters:
* [deepSave: <Boolean>]
* Whether or not to also run the save operation on any Records stored in a
* LinkToAnotherRecord Field on this Record and any Records in those Fields,
* should they exist.
* Return:
* Returns a Promise which will resolve this Record after the save operation.
* It will reject an error from Airtable.com on a failed save Request.
* This is an alias for Record.update
*/
save(deepSave = true) {
return this.update(deepSave);
}
/* update(deepUpdate)
* Parameters:
* [deepUpdate: <Boolean>]
* Whether or not to also run the update operation on any Records stored in a
* LinkToAnotherRecord Field on this Record and any Records in those Fields,
* should they exist.
* Return:
* Returns a Promise which will resolve this Record after the update operation.
* It will reject an error from Airtable.com on a failed update Request.
*/
update(deepUpdate = true) {
return new Promise((resolve, reject) => {
// surrounding everything in a try-catch so Promise doesn't get mad.
const tryCB = (cb) => {
return (...args) => {
try {
cb(...args);
} catch(error) {
reject(error);
}
};
};
resolve = tryCB(resolve);
reject = tryCB(reject);
try {
const changed = [];
Object.entries(this.fields).forEach(([key, field]) => {
if (!(field instanceof Field)) {
//this will get triggered if someone does <record>.fields.<key> = <value> rather than <record>.fields.<key>.value = <value>
if (this.table.fields[key] !== undefined) {
console.error(new Error(
`RecordError: The field '${key}' in table '${this.table.name}' was improperly set. ` +
'Attempting to fix the error....'
).toString());
this.fields[key] = this.table._blankFields[key];
this.fields[key].config.__record__ = this;
this.fields[key].value = field;
if (this.fields[key].value === undefined)
this.fields[key]._originalValue = null; // they were likely trying to clear the field so this will make sure it clears
field = this.fields[key];
}
}
if (this._knownFields[key] !== true) {
console.error(new Error(
`RecordError: A field '${key}' was attempted to be saved to a record in table '${this.table.name}'. ` +
`This field did not exist until the save operation. ` +
`New fields must be created on the Airtable website as there is no support for this through their API. ` +
`Deleting '${key}' from the record and continuing operation...`
).toString());
delete this.fields[key];
return;
} else if (field._changed) {
changed.push(key);
}
});
if (changed.length > 0 && Record._printRecordChanges) {
console.log('Changed fields:');
console.log(JSON.stringify(changed.map((key) => {
return {
key: key,
originalValue: this.fields[key]._originalValue,
newValue: this.fields[key]._saveValue
};
}), null, 2));
}
let remainingPromises = 0;
const saveError = (reject, error, ...args) => {
remainingPromises--;
if (typeof error.response !== 'undefined') {
if (error.response.status === 429) {
// it'll just return because a 429 error references the API Request Limit being exceeded.
// the then/success and catch/fail methods will still work because the request is scheduled to re-run automatically
// after a cooldown.
return;
}
}
Object.entries(this.fields).forEach(([key, field]) => {
// skip over LinkToAnotherRecords because their save handlers will also do this,
// provided deepUpdate was set to true. Their save handlers are on seperate requests
// so failing here shouldn't effect them.
if (!(field instanceof LinkToAnotherRecord) && field.value !== field._originalValue)
field.value = field._originalValue; // reset fields on an error
});
reject(error, ...args);
};
if (deepUpdate === true) {
const cache = { [this.id]: true };
const searchFields = (record) => {
Object.entries(this.fields).forEach(([key, field]) => {
if (field instanceof LinkToAnotherRecord) {
const records = field.isMulti ? field.value : [field.value];
records.forEach((record) => {
if (!(record instanceof Record))
return;
if (cache[record.id] !== true) {
cache[record.id] = true;
remainingPromises++;
record.save(false)
.then(() => remainingPromises--)
.catch((...args) => saveError(reject, ...args));
}
});
}
});
};
searchFields(this);
}
const waitForPromises = (resolve) => {
if (remainingPromises > 0)
setTimeout(() => waitForPromises(resolve), 250);
else
resolve(this);
};
// this will trigger if they saved but didn't make any changes. If they didn't do .save(false)
// then it will wait for any linked records to save before resolving.
if (changed.length === 0)
return waitForPromises(resolve);
this.table._airtable.sendRequest(new Request(
Request.types.patch,
{
base: this.table.base,
tableName: this.table.name,
appendID: this.id,
data: {
fields: changed.reduce((obj, item) => {
obj[this.fields[item].name] = this.fields[item]._saveValue;
return obj;
}, {})
}
},
(res) => {
Object.entries(this.fields).forEach(([key, field]) => {
field._originalValue = field._saveValue;
this._fields = this.fields;
});
waitForPromises(resolve);
},
(...args) => saveError(reject, ...args)
));
} catch (error) {
reject(error);
}
});
}
dangerouslyReplace() {
}
dangerouslyDelete() {
}
/* stringify(replacer, space)
* Parameters:
* [repalcer: <Function> <Array>]
* A function that alters the behavior of the stringification
* process, or an array of String and Number objects that serve
* as a whitelist for selecting/filtering the properties of the
* value object to be included in the JSON string. If this value
* is null or not provided, all properties of the object are included
* in the resulting JSON string.
* [spacer: <String> <Number>]
* A String or Number object that's used to insert white space into the
* output JSON string for readability purposes. If this is a Number, it
* indicates the number of space characters to use as white space; this
* number is capped at 10 (if it is greater, the value is just 10). Values
* less than 1 indicate that no space should be used. If this is a String,
* the string (or the first 10 characters of the string, if it's longer
* than that) is used as white space. If this parameter is not provided (or
* is null), no white space is used.
* Return:
* A String of all the Fields in a mostly JSON format. LinkToAnotherRecord values will
* will be converted to a string like 'Record [TableName : RecordID : PrimaryField Value]'
* This function just calls JSON.stringify on everything but also handles things that would
* format incorrectly or cause an error like the loop created in LinkToAnotherRecord Fields.
*/
stringify(replacer, space) {
// I don't want to toString() every value
// because I don't want to return numbers as strings.
// shouldToString is used to indentify fields that should be
// printed a certain way such as Currency with the specified
// symbol or Dates using the specified formatting.
const shouldToString = (field) => {
if (field instanceof Field) {
switch (field.constructor) {
case CreatedTime:
case DateField:
case Currency:
case Percent:
return true;
default:
return false;
}
}
return false;
};
return `${this.table.name} Record ` + JSON.stringify({
id: this.id,
fields: {
...Object.entries(this.fields).reduce((fields, [key, field]) => {
let value = field.value;
if (value instanceof Record)
value = `Record [${value.table.name} : ${value.id}]`;
if (shouldToString(field))
value = field.toString(false);
if (Array.isArray(value))
value = value.map((value) => {
if (value instanceof Record)
return `Record [${value.table.name} : ${value.id} : ${shouldToString(value.getPrimaryField()) ? value.getPrimaryField().toString(false) : value.primaryField}]`;
else
return value;
});
fields[key] = value === undefined ? null : value
return fields;
}, {})
},
createdTime: this.createdTime.toString(false)
}, replacer, space);
}
/* toString()
* Return:
* Returns a String represention of the Record.
*/
toString() {
return this.stringify(null, 2);
}
/* static _checkField(key)
* Parameters:
* key: <String>
* Returns:
* True if the key for a Field definined in the Table Definition does
* match something that is blacklisted. The blacklist exists so that
* Fields do not overwrite something that this class is using, as all
* Fields get set as propeties on this Object in order to make
* 'record.fields.age.value' be that same as 'record.age'.
* If the key exists on the blacklist then an error will be thrown.
*/
static _checkField(key) {
if (Record._blacklist_.indexOf(key) >= 0) {
throw new Error(`FieldError: Oops! Looks like you managed to pick the same name as an Object that's being used in the Record class. Please pick a name other than '${key}'.`)
}
return true;
}
/* static _configureBlacklist()
* This function is used to configure the blacklist when the module
* is first imported.
*/
static _configureBlacklist() {
Record._blacklistVariables_ = [
'_cache_',
'__fields__',
'_fields',
'_table'
];
Record._blacklistVariablesNoWrite_ = [
'_blacklist_',
'_blacklistVariables_',
'_blacklistVariablesNoWrite_',
'_blacklistGettersSetters_',
'_blacklistFunctions_',
'_createdTime',
'_id',
];
Record._blacklistGettersSetters_ = [
'_fields_',
'_printRecordChanges',
'createdTime',
'fields',
'id',
'primaryField',
'table'
];
Record._blacklistFunctions_ = [
'getFieldByName',
'getFieldByKey',
'getPrimaryField',
'has',
'fget',
'fgetk',
'get',
'put',
'hask',
'getk',
'putk',
'save',
'update',
'dangerouslyReplace',
'dangerouslyDelete'
];
Record._blacklist_ = []
.concat(Record._blacklistVariables_)
.concat(Record._blacklistVariablesNoWrite_)
.concat(Record._blacklistGettersSetters_)
.concat(Record._blacklistFunctions_);
Record._blacklistVariables_.forEach((key) => {
Object.defineProperty(Record, key, {
value: Record[key],
enumerable: false,
configureable: false,
writable: true
});
});
Record._blacklistVariablesNoWrite_.forEach((key) => {
Object.defineProperty(Record, key, {
value: Record[key],
enumerable: false,
configureable: false,
writable: false
});
});
Record._blacklistFunctions_.forEach((key) => {
Object.defineProperty(Record, key, {
value: Record[key],
enumerable: false,
configureable: false,
writable: false
});
});
Object.defineProperty(Record, '_blacklistConfigured', {
value: true,
enumerable: false,
configureable: false,
writable: false
});
}
}
if (Record._blacklistConfigured !== true)
Record._configureBlacklist();
module.exports = Record;