ISO 639-3 (Sequelize)

Introduction

Eg: gsm

Using Sequelize ORM, let's create a database table populated with all the ISO 639-3 language codes.

Both, the table definition and its records, are taken form the official site, see reference [1] ISO 639-3 Code Set.

ISO 639-3 table creation and records loading

src/resources/iso-639-3.tab

Records in TSV format to be loaded into the database, previously downloaded from reference [1] ISO 639-3 Code Set


src/models/myModel.mjs

import { Sequelize, DataTypes, Model } from 'sequelize';

import fs from 'fs';

import path from 'path';

import readline from 'readline';

import { fileURLToPath } from 'url';


const G_CREATED_INSTANT = "created_instant"; // technical field name

const G_UPDATED_INSTANT = "modified_instant"; // technical field name

const G_DELETED_INSTANT = "deleted_instant"; // technical field name



/** ISO 639-3 MODEL. Langage table and records from https://iso639-3.sil.org/code_tables/download_tables#639-3%20Code%20Set */

class Iso639p3 extends Model {}


/**

 * Initialize all db models with their associations.

 * @param {Sequelize} sequelize

 */

async function initModels(sequelize) {

  await Promise.all([

    initIso639p3(sequelize),

  ]);

}


/**

 * Sync all model (alter metadata).

 * @param {Sequelize} sequelize

 * @returns {Promise<Sequelize>} -

 */

async function initAndSyncModels(sequelize){

  await initModels(sequelize);

  return sequelize.sync();

}


/**

 * Iso639p3 model init.

 * 

 * @param {Sequelize} sequelize Sequelize instance

 * @returns {Iso639p3} The model

 */

function initIso639p3(sequelize) {

  const modelName= 'Iso639p3';

  const tableName= 'iso639p3';


  Iso639p3.init(

    {

      id: {

        type: DataTypes.CHAR(3),

        allowNull: false,

        primaryKey: true,

      },

      part2B: {

        type: DataTypes.CHAR(3),

        allowNull: true,

      },

      part2T: {

        type: DataTypes.CHAR(3),

        allowNull: true,

      },

      part1: {

        type: DataTypes.CHAR(2),

        allowNull: true,

      },

      scope: {

        type: DataTypes.CHAR(1),

        allowNull: false,

      },

      type: {

        type: DataTypes.CHAR(1),

        allowNull: false,

      },

      ref_name: {

        type: DataTypes.STRING(150),

        allowNull: false,

      },

      comment: {

        type: DataTypes.STRING(150),

        allowNull: true,

      }

    },

    {

      sequelize: sequelize, // connection instance

      modelName: modelName,

      tableName: tableName,

      timestamps: true,

      createdAt: G_CREATED_INSTANT,

      updatedAt: G_UPDATED_INSTANT,

      paranoid: true,

      deletedAt: G_DELETED_INSTANT

    }

  );


  return Iso639p3;

};



/**

 * Upsert the TSV file 'iso-639-3.tab' into table iso639p3.

 */

async function loadIso639p3() {

  const __filename = fileURLToPath(import.meta.url);

  const __dirname = path.dirname(__filename);

  const filePath = path.join(__dirname, '../resources/iso-639-3.tab');


  const fileStream = fs.createReadStream(filePath);

  const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity });

  let isFirstLine = true;


  for await (const line of rl) {

    if (isFirstLine) {

      isFirstLine = false; // Skip header

      continue;

    }


    const [id, part2B, part2T, part1, scope, type, ref_name, comment] = line.split('\t').map(col => col.trim());


    try {

      await Iso639p3.upsert({

        id,

        part2B: part2B || null,

        part2T: part2T || null,

        part1: part1 || null,

        scope,

        type,

        ref_name,

        comment: comment || null

      });

    } catch (error) {

      console.error(`Error upserting record with ID ${id}: ${error.message}`);

    }

  }


  console.log("Upsert operation complete.");

}


export { Iso639p3, initAndSyncModels, loadIso639p3 }


src/models/myFunction.mjs

import { DataTypes, Sequelize } from 'sequelize';

import { initAndSyncModels, loadIso639p3 } from '../models/myModel.mjs';


let _sequelize = null; // See the Sequelize page to know how to initialize it


// ==== ENTRY POINT (Exporting the Lambda handler function) ======================

export const handler = async (event, context) => {

    await initAndSyncModels(_sequelize);

    await loadIso639p3(_sequelize);

}