Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom authority connector for Airtable #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/authority/connectors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Metagrid } from './metagrid.js';
import { GeoNames } from './geonames.js';
import { Airtable } from './airtable.js';
import { MZP } from './mzp.js';
import { GND } from './gnd.js';
import { KBGA } from './kbga.js';
import { GF } from './gf.js';
Expand All @@ -22,6 +23,9 @@ export function createConnectors(endpoint, root) {
case 'Airtable':
instance = new Airtable(configElem);
break;
case 'MZP':
instance = new MZP(configElem);
break;
case 'KBGA':
instance = new KBGA(configElem);
break;
Expand Down
169 changes: 169 additions & 0 deletions src/authority/mzp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/* Modifies Airtable connector to enable custom URI in TEI Publisher */

import '@lrnwebcomponents/es-global-bridge';
import { Registry } from './registry.js';
import { resolveURL } from '../utils.js';

function expandTemplate(template = '', options) {
return template.replace(/\${([^}]+)}/g, (match, p) => {
let replacement;
if (options[p]) {
replacement = options[p];
}
return replacement || '';
});
}

function getTemplate(configElem, selector) {
const template = configElem.querySelector(selector);
if (template instanceof HTMLTemplateElement) {
const wrapper = document.createElement('div');
wrapper.appendChild(template.content.cloneNode(true));
return wrapper.innerHTML;
}
return '';
}

export class Airtable extends Registry {
Copy link
Member

@wolfgangmm wolfgangmm Jun 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this class be named differently to avoid conflicts with the existing Airtable class? The tests fail due to this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could streamline this by making the ID to use configurable. I'll make a proposal tomorrow...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I would propose the following: instead of creating a new connector, change the existing airtable connector to accept an additional attribute use-as-key in the config element. Right now I can't test this myself, but I'd say the required changes would be:

  1. in line 35 add another variable this.idField = configElem.getAttribute('use-as-key');
  2. in line 101, change the id property to id: this.idField ? record.get(this.idField) : record.id,
  3. in line 120, we also need to take into account that the filterByFormula expression needs to be different, so replace with filterByFormula: this.idField ? ${this.idField}='${key}':RECORD_ID()='${key}'`
  4. then again in line 160 change the id property: id: this.idField ? record.get(this.idField) : record.id,

Could you try this and report if it works?


constructor(configElem) {
super(configElem);
this.apiKey = configElem.getAttribute('api-key');
this.baseKey = configElem.getAttribute('base');
this.table = configElem.getAttribute('table');
this.view = configElem.getAttribute('view');
this.filterExpr = configElem.getAttribute('filter');
this.labelExpr = configElem.getAttribute('label');
const fieldsDef = configElem.getAttribute('fields');
if (fieldsDef) {
this.fields = fieldsDef.split(/\s*,\s*/);
} else {
this.fields = ['ID'];
}
const tokenizeDef = configElem.getAttribute('tokenize');
if (tokenizeDef) {
this.tokenize = tokenizeDef.split(/\s*,\s*/);
} else {
this.tokenize = [this.fields[0]];
}

this.tokenizeChars = configElem.getAttribute('tokenize-regex') || "\\W";

this.infoExpr = getTemplate(configElem, '.info');
this.detailExpr = getTemplate(configElem, '.detail');

this._init();
}

_init() {
window.ESGlobalBridge.requestAvailability();
const path = resolveURL('https://unpkg.com/[email protected]/build/airtable.browser.js');
window.ESGlobalBridge.instance.load('airtable', path);
window.addEventListener(
'es-bridge-airtable-loaded',
this._initAirtable.bind(this),
{ once: true },
);
}

_initAirtable() {
const Airtable = require('airtable');
this.base = new Airtable({ apiKey: this.apiKey }).base(this.baseKey);
}

async query(key) {
key = key.toLowerCase();
const results = [];
const filter = this.filterExpr ? expandTemplate(this.filterExpr, { key }) : null;
const options = {
fields: this.fields,
// Selecting the first 3 records in Grid view:
maxRecords: 100
};
if (this.view) {
options.view = this.view;
}
if (filter) {
options.filterByFormula = filter;
}
return new Promise((resolve, reject) => {
this.base(this.table)
.select(options)
.firstPage((err, records) => {
if (err) {
console.error(err);
reject(err);
return;
}
records.forEach(record => {
const data = {};
this.fields.forEach((field) => { data[field] = record.get(field); });
const result = {
register: this._register,
// Adding field name for custom URI in Airtable
id: record.myURI,
label: expandTemplate(this.labelExpr, data),
details: expandTemplate(this.detailExpr, data),
provider: 'airtable'
};
results.push(result);
});
resolve({
totalItems: 3,
items: results,
});
});
});
}

info(key, container) {
return new Promise((resolve, reject) => {
const options = {
fields: this.fields,
filterByFormula: `RECORD_ID()='${key}'`
};
this.base(this.table)
.select(options)
.firstPage((err, records) => {
if (err) {
switch (err.statusCode) {
case 404:
reject(`No record found for ${key} in table ${this.table}`);
break;
default:
reject(`${err.statusCode}: ${err.message}`);
break;
}
return;
}
const record = records[0];
if (Object.keys(record.fields).length === 0) {
console.warn(`Retrieved an empty record for %s from table %s`, key, this.table);
return;
}
let strings = [];
const data = {};
this.fields.forEach((field) => {
const value = record.get(field);
if (value) {
data[field] = value;
strings.push(value);
}
});
const regex = new RegExp(this.tokenizeChars);
this.tokenize.forEach((key) => {
strings = strings.concat(data[key].split(regex));
});
strings = strings.filter(tok => !/^\d+$/.test(tok));
strings.sort((s1, s2) => s2.length - s1.length);
console.log(strings);
container.innerHTML = expandTemplate(this.infoExpr, data);

resolve({
id: record.id,
strings
});
});
})
}
}