-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert-data.js
More file actions
80 lines (64 loc) · 2.19 KB
/
insert-data.js
File metadata and controls
80 lines (64 loc) · 2.19 KB
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
const Airtable = require('airtable');
const BASE_ID = 'apppvmEYnXEXlBqtp';
const API_KEY = 'patOvmYCU09qjXnbD.11b59cf62e82b545ee7d0a4d082094fa04368a6dc56147f05c94e7f13a199e3e'; // FYI Nope, it's not valid anymore!
const TABLE_NAME = 'People';
// Static data as dataset
const dataset = Array.from({ length: 301 }, (_, i) => ({
id: `rec${i + 1}`,
name: `Person ${i + 1}`,
email: `person${i + 1}@example.com`,
age: Math.floor(Math.random() * 50), // Random age between 0 and 49
}));
// Initialize Airtable
const base = new Airtable({ apiKey: API_KEY }).base(BASE_ID);
async function upsertRecords() {
const existingRecords = await base(TABLE_NAME).select({
fields: ['id'],
}).all();
const existingRecordMap = existingRecords.reduce((acc, record) => {
acc[record.fields.id] = record.id;
return acc;
}, {});
// Process records in batches of 10
for (let i = 0; i < dataset.length; i += 10) {
const batch = dataset.slice(i, i + 10);
const createPromises = [];
const updatePromises = [];
for (const record of batch) {
const { id, ...fields } = record;
if (existingRecordMap[id]) {
// Update existing record
updatePromises.push(base(TABLE_NAME).update(existingRecordMap[id], fields));
} else {
// Create a new record
createPromises.push(base(TABLE_NAME).create({ id, ...fields }));
}
}
// Await all batch operations
await Promise.all([...createPromises, ...updatePromises]);
}
}
async function deleteUnderageRecords() {
const recordsToDelete = await base(TABLE_NAME)
.select({
filterByFormula: 'age < 1',
})
.all();
const deletePromises = [];
for (let i = 0; i < recordsToDelete.length; i += 10) {
const batch = recordsToDelete.slice(i, i + 10);
const idsToDelete = batch.map((record) => record.id);
deletePromises.push(base(TABLE_NAME).destroy(idsToDelete));
}
await Promise.all(deletePromises);
}
(async () => {
try {
await upsertRecords();
console.log('Records processed successfully.');
await deleteUnderageRecords();
console.log('Underage records deleted successfully.');
} catch (error) {
console.error('Error processing records:', error);
}
})();