Umbau auf postgres 2. step

This commit is contained in:
2024-04-22 22:26:44 +02:00
parent c90d6b72b7
commit 7f0f21b598
77 changed files with 3325 additions and 3066 deletions

View File

@@ -11,10 +11,11 @@ import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
import { EntityData, EntityId, Schema, SchemaDefinition } from 'redis-om';
import { SQL, eq, gte, ilike, lte, sql, and} from 'drizzle-orm';
import { BusinessListing, CommercialPropertyListing, PG_CONNECTION, businesses, commercials, } from '../drizzle/schema.js';
import { PG_CONNECTION, businesses, commercials, } from '../drizzle/schema.js';
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
import * as schema from '../drizzle/schema.js';
import { PgTableFn, PgTableWithColumns, QueryBuilder } from 'drizzle-orm/pg-core';
import { BusinessListing, CommercialPropertyListing } from 'src/models/db.model.js';
@Injectable()
export class ListingsService {
@@ -22,17 +23,6 @@ export class ListingsService {
constructor(@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger,
@Inject(PG_CONNECTION) private conn: NodePgDatabase<typeof schema>,) {
}
// private buildWhereClause(criteria: ListingCriteria): SQL {
// const finalSql = sql`1=1`;
// finalSql.append(criteria.type ? sql` AND 'type' = ${criteria.type}` : sql``)
// finalSql.append(criteria.state ? sql` AND data->>'state' = ${criteria.state}` : sql``)
// finalSql.append(criteria.minPrice ? sql` AND CAST(data->>'price' AS NUMERIC) >= ${parseFloat(criteria.minPrice)}` : sql``)
// finalSql.append(criteria.maxPrice ? sql` AND CAST(data->>'price' AS NUMERIC) < ${parseFloat(criteria.maxPrice)}` : sql``)
// finalSql.append(criteria.realEstateChecked !== undefined ? sql` AND CAST(data->>'realEstateIncluded' AS BOOLEAN) = ${criteria.realEstateChecked}` : sql``)
// finalSql.append(criteria.title ? sql` AND LOWER(data->>'title') LIKE LOWER('%' || ${criteria.title} || '%')` : sql``)
// return finalSql
// }
private getConditions(criteria: ListingCriteria): any[] {
const conditions = [];
if (criteria.type) {
@@ -76,20 +66,10 @@ export class ListingsService {
return result[0] as BusinessListing | CommercialPropertyListing
}
// async findByPriceRange(minPrice: number, maxPrice: number, table: typeof businesses | typeof commercials): Promise<BusinessesJson[]> {
// return this.conn.select().from(table).where(sql`${table}->>'price' BETWEEN ${minPrice} AND ${maxPrice}`);
// }
// async findByState(state: string, table: typeof businesses | typeof commercials): Promise<BusinessesJson[]> {
// return this.conn.select().from(table).where(sql`${table}->>'state' = ${state}`);
// }
async findByUserId(userId: string, table: typeof businesses | typeof commercials): Promise<BusinessListing[] | CommercialPropertyListing[]> {
return await this.conn.select().from(table).where(eq(table.userId, userId)) as BusinessListing[] | CommercialPropertyListing[]
}
// async findByTitleContains(title: string, table: typeof businesses | typeof commercials): Promise<BusinessesJson[]> {
// return this.conn.select().from(table).where(sql`${table}->>'title' ILIKE '%' || ${title} || '%'`);
// }
async createListing(data: BusinessListing | CommercialPropertyListing, table: typeof businesses | typeof commercials): Promise<BusinessListing | CommercialPropertyListing> {
const newListing = { data, created: data.created, updated: data.updated, visits: 0, last_visit: null }
const [createdListing] = await this.conn.insert(table).values(newListing).returning();