Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extract meta information from airtable base
/*****
* Title: Post Meta Data
* License: MIT
* Author: Openside (Team behind On2Air products and BuiltOnAir community)
* Sites:
* https://openside.com - Openside Consulting Services
* https://openside.com/#products - On2Air Products
* https://builtonair.com - All things Airtable Community
*
* Reach out for all your Airtable needs
*
* Description: Retrieve the meta information regarding a base and optionally
* post it to an external URL.
*
* This is useful to share your base information with 3rd party tools or
* for other other internal purposes.
*
* Instructions: Configure the 3 fields below and execute script.
*
*/
//-------------START CONFIGURATION ------------------//
//set to true to display the Meta Data as Json; false to not display
const renderJson = true
//set to true to POST metadata to a URL; false to not post anywhere
const postJson = false
//if blank, it will ask for the URL; add it here to avoid asking each time
let postUrl = ''
//-------------END CONFIGURATION ------------------//
if(postJson && !postUrl){
output.text('***POST TO URL***')
output.text('Be sure this URL supports [CORS](https://medium.com/@baphemot/understanding-cors-18ad6b478e2b)')
postUrl = await input.text('Enter the URL of where to Post the Meta Data')
postUrl = postUrl.trim()
}
//get base meta data
const fda = ( fields ) => {
let data = []
for(const f of fields ){
data.push(fd(f))
}
return data
}
const fd = ( field ) => {
let data = {
id: field.id, name: field.name, type: field.type, isComputed: field.isComputed, options:field.options, description: field.description
}
return data
}
let tables = []
for (const table of base.tables ){
let views = []
for (const view of table.views ){
views.push({
id: view.id,
name: view.name,
type: view.type,
url: view.url
})
}
let fields = fda(table.fields)
tables.push({
id: table.id,
name: table.name,
description: table.description,
url: table.url,
views, fields
})
}
let baseData = {
source: 'script',
version: '0.0',
meta: {
id: base.id, name: base.name, activeCollaborators: base.activeCollaborators,
tables
}
}
if(renderJson){
output.markdown('## BASE META DATA ##')
output.inspect(baseData)
output.markdown('### START JSON ###')
output.text(JSON.stringify(baseData))
output.markdown('### END JSON ###')
}
if(postJson && postUrl){
// @ts-ignore
let response = await fetch(postUrl,{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({base:baseData})
})
output.text('**POST RESPONSE**' + ' - ' + response.status)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.