Commit 8117084c authored by Grant's avatar Grant
Browse files

update queries

parent 6f1a5e12
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -7,20 +7,21 @@ export const client = new Client({
const makeQuery = (cl: Client) => ({
  pixelsWithinTimeframe: (start: Date, end: Date) => {
    return cl.query<DBPixel>(
      `SELECT * FROM "Pixel" WHERE ("createdAt" >= $1 AND "createdAt" < $2) or ("deletedAt" >= $1 AND "deletedAt" < $2) ORDER BY "createdAt" ASC`,
      [start, end]
      `SELECT x, y, color, "createdAt", "deletedAt" FROM "Pixel" WHERE ("createdAt" >= $1 AND "createdAt" < $2) or ("deletedAt" >= $1 AND "deletedAt" < $2) ORDER BY "createdAt" ASC`,
      [start, end],
    );
  },
  existingPixelsBeforeTime: (end: Date) => {
    return cl.query<DBPixel>(
      `SELECT * FROM "Pixel" WHERE "createdAt" < $1 AND "deletedAt" IS NULL ORDER BY "createdAt" ASC`,
      [end]
      `SELECT DISTINCT ON (x, y) x, y, color, "createdAt" FROM "Pixel" WHERE "createdAt" < $1 AND "deletedAt" IS NULL ORDER BY x, y, "createdAt" DESC`,
      [end],
    );
  },
  /** Batch: get all pixel history for a set of (x,y) positions, ordered by createdAt ASC per position.
   *  Used to avoid N+1 queries when resolving covering pixels for deleted pixels. */
  batchPixelHistory: (positions: { x: number; y: number }[]) => {
    if (positions.length === 0) return Promise.resolve({ rows: [] as DBPixel[] });
    if (positions.length === 0)
      return Promise.resolve({ rows: [] as DBPixel[] });
    const params: number[] = [];
    const placeholders = positions
      .map((p) => {
@@ -30,7 +31,7 @@ const makeQuery = (cl: Client) => ({
      .join(", ");
    return cl.query<DBPixel>(
      `SELECT * FROM "Pixel" WHERE (x, y) IN (${placeholders}) ORDER BY x, y, "createdAt" ASC`,
      params
      params,
    );
  },
});