fix(db): replace crypto.randomUUID with custom UUID generator
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { db } from "./client";
|
||||
import { generateUUID } from "../utils/uuid";
|
||||
import { workouts, sets, laps, templates } from "./schema";
|
||||
import { eq, desc, sql } from "drizzle-orm";
|
||||
import type { WorkoutSet, LapData } from "@types/workout";
|
||||
@@ -6,7 +7,7 @@ import type { WorkoutSet, LapData } from "@types/workout";
|
||||
// ─── Templates ───────────────────────────────────────────────
|
||||
|
||||
export async function saveTemplate(name: string, poolLength: number, setsData: WorkoutSet[]) {
|
||||
const id = crypto.randomUUID();
|
||||
const id = generateUUID();
|
||||
await db.insert(templates).values({
|
||||
id,
|
||||
name,
|
||||
@@ -37,7 +38,7 @@ export async function saveWorkout(
|
||||
setsData: WorkoutSet[],
|
||||
templateId?: string
|
||||
) {
|
||||
const workoutId = crypto.randomUUID();
|
||||
const workoutId = generateUUID();
|
||||
const totalDistance = setsData.reduce((acc, s) => acc + s.distancePerRep * s.repCount, 0);
|
||||
|
||||
await db.insert(workouts).values({
|
||||
@@ -53,7 +54,7 @@ export async function saveWorkout(
|
||||
for (let i = 0; i < setsData.length; i++) {
|
||||
const s = setsData[i];
|
||||
await db.insert(sets).values({
|
||||
id: crypto.randomUUID(),
|
||||
id: generateUUID(),
|
||||
workoutId,
|
||||
position: i + 1,
|
||||
stroke: s.stroke,
|
||||
@@ -112,7 +113,7 @@ export async function saveLap(workoutId: string, lap: LapData) {
|
||||
lap.strokeCount != null ? lap.strokeCount + Math.round(lap.duration) : null;
|
||||
|
||||
await db.insert(laps).values({
|
||||
id: crypto.randomUUID(),
|
||||
id: generateUUID(),
|
||||
workoutId,
|
||||
setPosition: lap.setPosition,
|
||||
repNumber: lap.repNumber,
|
||||
|
||||
@@ -29,7 +29,8 @@ export function BuilderScreen() {
|
||||
const workoutId = await save(poolLength);
|
||||
navigation.navigate("PreSwim", { workoutId });
|
||||
} catch (e) {
|
||||
Alert.alert("Error", "Failed to save workout");
|
||||
console.error("Failed to save workout:", e);
|
||||
Alert.alert("Error", `Failed to save workout: ${e}`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
export function generateUUID(): string {
|
||||
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
||||
const r = (Math.random() * 16) | 0;
|
||||
const v = c === "x" ? r : (r & 0x3) | 0x8;
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user