Skip to content
Merged
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
71 changes: 71 additions & 0 deletions src/repositories/base.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// src/repositories/base.repository.ts
import { SupabaseService } from '../supabase/supabase.service';

export class BaseRepository<T> {
constructor(
protected readonly supabaseService: SupabaseService,
private readonly tableName: string
) {}

async findAll(): Promise<T[]> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from(this.tableName)
.select('*');
if (error) {
throw error;
}
return data || [];
}

async findById(id: number | string, idColumn = 'id'): Promise<T | null> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from(this.tableName)
.select('*')
.eq(idColumn, id)
.single();
if (error) {
throw error;
}
return data || null;
}

async create(item: Partial<T>): Promise<T> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from(this.tableName)
.insert(item)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async update(id: number | string, item: Partial<T>, idColumn = 'id'): Promise<T> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from(this.tableName)
.update(item)
.eq(idColumn, id)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async delete(id: number | string, idColumn = 'id'): Promise<void> {
const { error } = await this.supabaseService
.getSupabaseClient()
.from(this.tableName)
.delete()
.eq(idColumn, id);
if (error) {
throw error;
}
}
}
70 changes: 5 additions & 65 deletions src/repositories/cw_device_locations.repository.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,13 @@
// src/cw_device_locations/cw_device_locations.repository.ts
import { Injectable } from '@nestjs/common';
import { SupabaseService } from '../supabase/supabase.service';
import { Database } from 'database.types'; // Adjust the path to your generated types
import { Database } from 'database.types';
import { BaseRepository } from './base.repository';

type DeviceLocationRow = Database['public']['Tables']['cw_device_locations']['Row'];

@Injectable()
export class DeviceLocationRepository {
constructor(private readonly supabaseService: SupabaseService) {}

async findAll(): Promise<DeviceLocationRow[]> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_locations')
.select('*');
if (error) {
throw error;
}
return data || [];
}

async findById(id: number): Promise<DeviceLocationRow | null> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_locations')
.select('*')
.eq('id', id)
.single();
if (error) {
throw error;
}
return data || null;
}

async create(deviceLocation: Partial<DeviceLocationRow>): Promise<DeviceLocationRow> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_locations')
.insert(deviceLocation)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async update(id: number, deviceLocation: Partial<DeviceLocationRow>): Promise<DeviceLocationRow> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_locations')
.update(deviceLocation)
.eq('id', id)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async delete(id: number): Promise<void> {
const { error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_locations')
.delete()
.eq('id', id);
if (error) {
throw error;
}
export class DeviceLocationRepository extends BaseRepository<DeviceLocationRow> {
constructor(supabaseService: SupabaseService) {
super(supabaseService, 'cw_device_locations');
}
}
67 changes: 4 additions & 63 deletions src/repositories/cw_device_owners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,13 @@
import { Injectable } from '@nestjs/common';
import { SupabaseService } from '../supabase/supabase.service';
import { Database } from 'database.types';
import { BaseRepository } from './base.repository';

type DeviceOwnerRow = Database['public']['Tables']['cw_device_owners']['Row'];

@Injectable()
export class DeviceOwnerRepository {
constructor(private readonly supabaseService: SupabaseService) {}

async findAll(): Promise<DeviceOwnerRow[]> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_owners')
.select('*');
if (error) {
throw error;
}
return data || [];
}

async findById(id: number): Promise<DeviceOwnerRow | null> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_owners')
.select('*')
.eq('id', id)
.single();
if (error) {
throw error;
}
return data || null;
}

async create(deviceOwner: Partial<DeviceOwnerRow>): Promise<DeviceOwnerRow> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_owners')
.insert(deviceOwner)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async update(id: number, deviceOwner: Partial<DeviceOwnerRow>): Promise<DeviceOwnerRow> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_owners')
.update(deviceOwner)
.eq('id', id)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async delete(id: number): Promise<void> {
const { error } = await this.supabaseService
.getSupabaseClient()
.from('cw_device_owners')
.delete()
.eq('id', id);
if (error) {
throw error;
}
export class DeviceOwnerRepository extends BaseRepository<DeviceOwnerRow> {
constructor(supabaseService: SupabaseService) {
super(supabaseService, 'cw_device_owners');
}
}
70 changes: 5 additions & 65 deletions src/repositories/cw_location.repository.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,13 @@
// src/repositories/cw_locations.repository.ts
import { Injectable } from '@nestjs/common';
import { SupabaseService } from '../supabase/supabase.service';
import { Database } from 'database.types'; // Adjust the path
import { Database } from 'database.types';
import { BaseRepository } from './base.repository';

type LocationRow = Database['public']['Tables']['cw_locations']['Row'];

@Injectable()
export class LocationRepository {
constructor(private readonly supabaseService: SupabaseService) {}

async findAll(): Promise<LocationRow[]> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_locations')
.select('*');
if (error) {
throw error;
}
return data || [];
}

async findById(id: number): Promise<LocationRow | null> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_locations')
.select('*')
.eq('location_id', id)
.single();
if (error) {
throw error;
}
return data || null;
}

async create(location: Partial<LocationRow>): Promise<LocationRow> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_locations')
.insert(location)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async update(id: number, location: Partial<LocationRow>): Promise<LocationRow> {
const { data, error } = await this.supabaseService
.getSupabaseClient()
.from('cw_locations')
.update(location)
.eq('location_id', id)
.select('*')
.single();
if (error) {
throw error;
}
return data;
}

async delete(id: number): Promise<void> {
const { error } = await this.supabaseService
.getSupabaseClient()
.from('cw_locations')
.delete()
.eq('location_id', id);
if (error) {
throw error;
}
export class LocationRepository extends BaseRepository<LocationRow> {
constructor(supabaseService: SupabaseService) {
super(supabaseService, 'cw_locations');
}
}
3 changes: 1 addition & 2 deletions src/repositories/profiles.repositories.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// src/repositories/profiles.repository.ts
import { Injectable, Req } from '@nestjs/common';
import { SupabaseService } from '../supabase/supabase.service';
import { Database } from 'database.types'; // Adjust the path
import { Database } from 'database.types';
import { ApiBearerAuth } from '@nestjs/swagger';

type ProfileRow = Database['public']['Tables']['profiles']['Row'];
Expand Down