All files / server storage.ts

100% Statements 21/21
100% Branches 2/2
100% Functions 8/8
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91  1x                                   28x     28x                             28x       5x       3x 4x         20x 20x 20x           20x 20x             7x 7x   6x           6x 6x       5x       15x       1x  
import { type Sketch, type InsertSketch } from "@shared/schema";
import { randomUUID } from "crypto";
 
export interface IStorage {
  getSketch(id: string): Promise<Sketch | undefined>;
  getSketchByName(name: string): Promise<Sketch | undefined>;
  createSketch(sketch: InsertSketch): Promise<Sketch>;
  updateSketch(
    id: string,
    sketch: Partial<InsertSketch>,
  ): Promise<Sketch | undefined>;
  deleteSketch(id: string): Promise<boolean>;
  getAllSketches(): Promise<Sketch[]>;
}
 
export class MemStorage implements IStorage {
  private sketches: Map<string, Sketch>;
 
  constructor() {
    this.sketches = new Map();
 
    // Initialize with default blink sketch
    const defaultSketch: Sketch = {
      id: randomUUID(),
      name: "sketch.ino",
      content: `
void setup() {
  // put your setup code here, to run once
}
 
void loop() {
  // put your main code here, to run repeatedly
}`,
      createdAt: new Date(),
      updatedAt: new Date(),
    };
 
    this.sketches.set(defaultSketch.id, defaultSketch);
  }
 
  async getSketch(id: string): Promise<Sketch | undefined> {
    return this.sketches.get(id);
  }
 
  async getSketchByName(name: string): Promise<Sketch | undefined> {
    return Array.from(this.sketches.values()).find(
      (sketch) => sketch.name === name,
    );
  }
 
  async createSketch(insertSketch: InsertSketch): Promise<Sketch> {
    const id = randomUUID();
    const now = new Date();
    const sketch: Sketch = {
      ...insertSketch,
      id,
      createdAt: now,
      updatedAt: now,
    };
    this.sketches.set(id, sketch);
    return sketch;
  }
 
  async updateSketch(
    id: string,
    updateData: Partial<InsertSketch>,
  ): Promise<Sketch | undefined> {
    const existing = this.sketches.get(id);
    if (!existing) return undefined;
 
    const updated: Sketch = {
      ...existing,
      ...updateData,
      updatedAt: new Date(),
    };
 
    this.sketches.set(id, updated);
    return updated;
  }
 
  async deleteSketch(id: string): Promise<boolean> {
    return this.sketches.delete(id);
  }
 
  async getAllSketches(): Promise<Sketch[]> {
    return Array.from(this.sketches.values());
  }
}
 
export const storage = new MemStorage();