69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
/**
|
|
* Model for managing sites table in the database.
|
|
*/
|
|
|
|
const { supabase } = require('../auth');
|
|
|
|
class SiteModel {
|
|
|
|
static tableName = 'sites';
|
|
|
|
/**
|
|
* Inserts a new site into the database.
|
|
*
|
|
* @param {string} name - The name of the site.
|
|
* @param {string} domainName - The domain name of the site.
|
|
*
|
|
* @returns {Promise<Object>} - The result of the insert operation.
|
|
*/
|
|
async insert(name, domainName) {
|
|
const { error } = await supabase.from(siteModel.tableName).insert({
|
|
name: name,
|
|
domain_name: domainName,
|
|
});
|
|
|
|
if (error) {
|
|
console.error('Error inserting site:', error);
|
|
throw error;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* Retrieves all sites from the database.
|
|
*
|
|
* @returns {Promise<Array>} - An array of site objects.
|
|
*/
|
|
async getAll() {
|
|
const { data, error } = await supabase.from(SiteModel.tableName).select('*');
|
|
|
|
if (error) {
|
|
console.error('Error fetching sites:', error);
|
|
throw error;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* Retrieves a site by its ID.
|
|
*
|
|
* @param {number} id - The ID of the site.
|
|
*
|
|
* @returns {Promise<Object>} - The site object.
|
|
*/
|
|
async getById(id) {
|
|
const { data, error } = await supabase.from(SiteModel.tableName).select('*').eq('id', id).single();
|
|
|
|
if (error) {
|
|
console.error('Error fetching site by ID:', error);
|
|
throw error;
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = SiteModel; |