Browse Source

fetch linked records

Signed-off-by: Raju Udava <86527202+dstala@users.noreply.github.com>
sync
Raju Udava 3 years ago
parent
commit
d4156f9dcd
  1. 131
      packages/nocodb/tests/sync/sync.js

131
packages/nocodb/tests/sync/sync.js

@ -428,19 +428,38 @@ let base = new Airtable({ apiKey: syncDB.airtable.apiKey }).base(
syncDB.airtable.baseId syncDB.airtable.baseId
); );
async function nocoReadData(table) { let aTblDataLinks = [];
base(table.title) let aTblNcRecordMappingTable = {};
.select({
pageSize: 25,
// maxRecords: 100,
view: 'Grid view'
})
.eachPage(
function page(records, fetchNextPage) {
console.log(JSON.stringify(records, null, 2));
// This function (`page`) will get called for each page of records. function nocoLinkProcessing(table, record) {
records.forEach(function(record) { (async () => {
let rec = record.fields;
const value = Object.values(rec);
let srcRow = aTblNcRecordMappingTable[`${record.id}`];
if (value.length) {
for (let i = 0; i < value[0].length; i++) {
let dstRow = aTblNcRecordMappingTable[`${value[0][i]}`];
await api.dbTableRow.nestedAdd(
'noco',
syncDB.projectName,
table.title,
`${srcRow[1]}`,
'hm', // fix me
`${dstRow[0]}`,
`${dstRow[1]}`
);
}
}
})().catch(e => {
console.log(e);
});
}
// fix me:
// instead of skipping data after retrieval, use select fields option in airtable API
function nocoBaseDataProcessing(table, record) {
(async () => { (async () => {
let rec = record.fields; let rec = record.fields;
@ -465,6 +484,16 @@ async function nocoReadData(table) {
// if currency, set decimal place to 2 // if currency, set decimal place to 2
// //
if (dt === 'Currency') rec[key] = value.toFixed(2); if (dt === 'Currency') rec[key] = value.toFixed(2);
// we will pick up LTAR once all table data's are in place
if (dt === 'LinkToAnotherRecord') {
aTblDataLinks.push(JSON.parse(JSON.stringify(rec)));
delete rec[key];
}
// these will be automatically populated depending on schema configuration
if (dt === 'Lookup') delete rec[key];
if (dt === 'Rollup') delete rec[key];
} }
// bulk Insert // bulk Insert
@ -474,10 +503,54 @@ async function nocoReadData(table) {
table.title, table.title,
[rec] [rec]
); );
aTblNcRecordMappingTable[record.id] = [table.title, returnValue[0]];
})().catch(e => { })().catch(e => {
console.log(e); console.log(e);
}); });
}); }
async function nocoReadData(table, callback) {
base(table.title)
.select({
pageSize: 25,
// maxRecords: 100,
view: 'Grid view' // fix me
})
.eachPage(
function page(records, fetchNextPage) {
// console.log(JSON.stringify(records, null, 2));
// This function (`page`) will get called for each page of records.
records.forEach(record => callback(table, record));
// To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again.
// If there are no more records, `done` will get called.
fetchNextPage();
},
function done(err) {
if (err) {
console.error(err);
}
}
);
}
async function nocoReadDataSelected(table, callback, fields) {
base(table.title)
.select({
pageSize: 25,
// maxRecords: 100,
view: 'Grid view', // fix me
fields: [fields]
})
.eachPage(
function page(records, fetchNextPage) {
// console.log(JSON.stringify(records, null, 2));
// This function (`page`) will get called for each page of records.
records.forEach(record => callback(table, record));
// To fetch the next page of records, call `fetchNextPage`. // To fetch the next page of records, call `fetchNextPage`.
// If there are more records, `page` will get called again. // If there are more records, `page` will get called again.
@ -507,7 +580,7 @@ function nc_isLinkExists(atblFieldId) {
} }
// start function // start function
(async () => { async function nc_migrateATbl() {
// read schema file // read schema file
const schema = getAtableSchema(); const schema = getAtableSchema();
let aTblSchema = schema.tableSchemas; let aTblSchema = schema.tableSchemas;
@ -523,22 +596,38 @@ function nc_isLinkExists(atblFieldId) {
// add LTAR // add LTAR
await nocoCreateLinkToAnotherRecord(aTblSchema); await nocoCreateLinkToAnotherRecord(aTblSchema);
// add lookup's // add look-ups
await nocoCreateLookups(aTblSchema); await nocoCreateLookups(aTblSchema);
// add rollups // add roll-ups
await nocoCreateRollups(aTblSchema); await nocoCreateRollups(aTblSchema);
// await nc_DumpTableSchema(); // await nc_DumpTableSchema();
let ncTblList = await api.dbTable.list(ncCreatedProjectSchema.id);
for (let i = 0; i < ncTblList.list.length; i++) {
let ncTbl = await api.dbTable.read(ncTblList.list[i].id);
await nocoReadData(ncTbl, nocoBaseDataProcessing);
}
// let ncTblList = await api.dbTable.list(ncCreatedProjectSchema.id); // kludge
// for (let i = 0; i < ncTblList.list.length; i++) { // wait till above operations are completed instead of static timeout
// let ncTbl = await api.dbTable.read(ncTblList.list[i].id); setTimeout(() => {
// await nocoReadData(ncTbl); (async () => {
// } // console.log(ncLinkMappingTable)
for (let idx = 0; idx < ncLinkMappingTable.length; idx++) {
let x = ncLinkMappingTable[idx];
let ncTbl = await nc_getTableSchema(aTbl_getTableName(x.aTbl.tblId).tn);
await nocoReadDataSelected(ncTbl, nocoLinkProcessing, x.aTbl.name);
}
})().catch(e => { })().catch(e => {
console.log(e); console.log(e);
}); });
}, 5000);
}
nc_migrateATbl().catch(e => {
console.log(e);
});
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

Loading…
Cancel
Save