UNPKG

@windingtree/wt-search-api

Version:

NodeJS app that enables quick search over data from Winding Tree platform

56 lines (49 loc) 1.28 kB
/* eslint-disable camelcase */ const { db } = require('../../../config'); const TABLE = 'hotel_default_locale'; const createTable = async () => { await db.schema.createTable(TABLE, (table) => { table.string('hotel_address').primary(); table.string('locale', 2).notNullable(); table.index(['locale']); }); }; const dropTable = async () => { await db.schema.dropTableIfExists(TABLE); }; /** * Create or insert hotel location data. * * @param {String} hotelAddress * @param {String} locale in ISO 639-1 * @return {Promise<void>} */ const upsert = async (hotelAddress, locale) => { const existing = await db(TABLE).where({ hotel_address: hotelAddress, }).select('hotel_address'), data = { locale: locale.toLowerCase(), }; if (existing.length === 0) { await db(TABLE).insert(Object.assign({ hotel_address: hotelAddress }, data)); } else { await db(TABLE).where('hotel_address', hotelAddress).update(data); } }; /** * Delete hotel location data * * @param {String} hotelAddress * @return {Promise<void>} */ const delete_ = async (hotelAddress) => { await db(TABLE).where({ hotel_address: hotelAddress }).delete(); }; module.exports = { createTable, dropTable, upsert, delete: delete_, TABLE, };