import { knex } from '../../lib/ormish.js';
import { API } from '../../lib/api.js';

export const get = async (req, res) => {
  const api = new API(req, res);

  if(!api.admin_authenticated) {
    return api.error(401, "Admin rights required.");
  } else {
    // NOTE: in lib/ormish.js the schema is loaded once, but I do it every time
    // here to reflect the current database even if they haven't restarted it yet
    let result = await knex("sqlite_schema").where({"type": "table"}).select(['type', 'name']).orderBy('name');

    for(let table of result) {
      table._columns = await knex(table.name).columnInfo();
    }

    // TODO: flag that the database has changed so they know to restart
    return api.reply(200, result);
  }
}
