Files
task-manager/server/storage.ts
T
paul-nothaft 35c0889e05 Fix module resolution errors in Node.js production deployment
Update import statements and file paths to correctly resolve modules in the production Node.js environment by adding `.js` extensions and adjusting relative paths for compiled ES modules.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ceced2fc-aa46-458d-ba87-ddd4b7bb1518
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/659922a9-0087-461c-90dd-6d9a58b81d4d/ceced2fc-aa46-458d-ba87-ddd4b7bb1518/SBF5OKZ
2025-10-23 16:25:00 +00:00

226 lines
6.9 KiB
TypeScript

import { type User, type InsertUser, type Label, type InsertLabel, type Task, type InsertTask } from "../shared/schema.js";
import { randomUUID } from "crypto";
// modify the interface with any CRUD methods
// you might need
export interface IStorage {
getUser(id: string): Promise<User | undefined>;
getUserByUsername(username: string): Promise<User | undefined>;
createUser(user: InsertUser): Promise<User>;
// Labels
getAllLabels(): Promise<Label[]>;
getLabel(id: string): Promise<Label | undefined>;
createLabel(label: InsertLabel): Promise<Label>;
updateLabel(id: string, updates: Partial<InsertLabel>): Promise<Label | undefined>;
deleteLabel(id: string): Promise<boolean>;
// Tasks
getAllTasks(): Promise<Task[]>;
getTask(id: string): Promise<Task | undefined>;
createTask(task: InsertTask): Promise<Task>;
updateTask(id: string, updates: Partial<InsertTask>): Promise<Task | undefined>;
deleteTask(id: string): Promise<boolean>;
}
export class MemStorage implements IStorage {
private users: Map<string, User>;
private labels: Map<string, Label>;
private tasks: Map<string, Task>;
constructor() {
this.users = new Map();
this.labels = new Map();
this.tasks = new Map();
// Create some default labels
this.createDefaultLabels();
}
private async createDefaultLabels() {
// Use fixed IDs to prevent ID churn on server restarts
const defaultLabels = [
{ id: 'cb44bed1-8ba3-43fe-9498-bb28e483ed1f', name: "Work", color: "#3B82F6" },
{ id: '274f0ba4-a133-471a-bbe9-8189aa3b0106', name: "Personal", color: "#10B981" },
{ id: '2e8c3aa0-278f-4220-b2c5-aa109b4279b7', name: "Urgent", color: "#EF4444" },
{ id: 'c5eccac9-7919-4eb1-bb50-badacad6b1c1', name: "Study", color: "#8B5CF6" },
];
for (const label of defaultLabels) {
this.labels.set(label.id, label);
}
}
async getUser(id: string): Promise<User | undefined> {
return this.users.get(id);
}
async getUserByUsername(username: string): Promise<User | undefined> {
return Array.from(this.users.values()).find(
(user) => user.username === username,
);
}
async createUser(insertUser: InsertUser): Promise<User> {
const id = randomUUID();
const user: User = { ...insertUser, id };
this.users.set(id, user);
return user;
}
// Labels
async getAllLabels(): Promise<Label[]> {
return Array.from(this.labels.values());
}
async getLabel(id: string): Promise<Label | undefined> {
return this.labels.get(id);
}
async createLabel(insertLabel: InsertLabel): Promise<Label> {
const id = randomUUID();
const label: Label = { ...insertLabel, id };
this.labels.set(id, label);
return label;
}
async updateLabel(id: string, updates: Partial<InsertLabel>): Promise<Label | undefined> {
const existing = this.labels.get(id);
if (!existing) return undefined;
const updated: Label = { ...existing, ...updates };
this.labels.set(id, updated);
return updated;
}
async deleteLabel(id: string): Promise<boolean> {
return this.labels.delete(id);
}
// Tasks
async getAllTasks(): Promise<Task[]> {
return Array.from(this.tasks.values());
}
async getTask(id: string): Promise<Task | undefined> {
return this.tasks.get(id);
}
async createTask(insertTask: InsertTask): Promise<Task> {
const id = randomUUID();
const task: Task = {
id,
title: insertTask.title,
description: insertTask.description || null,
status: insertTask.status || "todo",
priority: insertTask.priority || "medium",
dueDate: insertTask.dueDate || null,
timeTracked: insertTask.timeTracked || 0,
isTracking: insertTask.isTracking || false,
projectId: insertTask.projectId || null,
notes: insertTask.notes || null,
labelId: insertTask.labelId || null,
};
this.tasks.set(id, task);
return task;
}
async updateTask(id: string, updates: Partial<InsertTask>): Promise<Task | undefined> {
const existing = this.tasks.get(id);
if (!existing) return undefined;
const updated: Task = { ...existing, ...updates };
this.tasks.set(id, updated);
return updated;
}
async deleteTask(id: string): Promise<boolean> {
return this.tasks.delete(id);
}
}
import { getDatabase } from './db.js';
import { eq } from 'drizzle-orm';
import * as schema from '../shared/schema.js';
export class DbStorage implements IStorage {
private db = getDatabase();
async getUser(id: string): Promise<User | undefined> {
const result = await this.db.select().from(schema.users).where(eq(schema.users.id, id));
return result[0];
}
async getUserByUsername(username: string): Promise<User | undefined> {
const result = await this.db.select().from(schema.users).where(eq(schema.users.username, username));
return result[0];
}
async createUser(insertUser: InsertUser): Promise<User> {
const result = await this.db.insert(schema.users).values(insertUser).returning();
return result[0];
}
async getAllLabels(): Promise<Label[]> {
return await this.db.select().from(schema.labels);
}
async getLabel(id: string): Promise<Label | undefined> {
const result = await this.db.select().from(schema.labels).where(eq(schema.labels.id, id));
return result[0];
}
async createLabel(insertLabel: InsertLabel): Promise<Label> {
const result = await this.db.insert(schema.labels).values(insertLabel).returning();
return result[0];
}
async updateLabel(id: string, updates: Partial<InsertLabel>): Promise<Label | undefined> {
const result = await this.db
.update(schema.labels)
.set(updates)
.where(eq(schema.labels.id, id))
.returning();
return result[0];
}
async deleteLabel(id: string): Promise<boolean> {
const result = await this.db.delete(schema.labels).where(eq(schema.labels.id, id)).returning();
return result.length > 0;
}
async getAllTasks(): Promise<Task[]> {
return await this.db.select().from(schema.tasks);
}
async getTask(id: string): Promise<Task | undefined> {
const result = await this.db.select().from(schema.tasks).where(eq(schema.tasks.id, id));
return result[0];
}
async createTask(insertTask: InsertTask): Promise<Task> {
const result = await this.db.insert(schema.tasks).values(insertTask).returning();
return result[0];
}
async updateTask(id: string, updates: Partial<InsertTask>): Promise<Task | undefined> {
const result = await this.db
.update(schema.tasks)
.set(updates)
.where(eq(schema.tasks.id, id))
.returning();
return result[0];
}
async deleteTask(id: string): Promise<boolean> {
const result = await this.db.delete(schema.tasks).where(eq(schema.tasks.id, id)).returning();
return result.length > 0;
}
}
// Export storage based on environment
export const storage = process.env.NODE_ENV === 'production' || process.env.USE_DB === 'true'
? new DbStorage()
: new MemStorage();