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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 89x 89x 89x 86x 86x 89x 20x 20x 66x 66x 89x 10x 10x 56x 56x 89x 16x 16x 53x 53x 16x 16x 40x 40x 40x 40x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 57x 57x 55x 57x 23x 23x 15x 15x 8x 8x 32x 57x 7x 7x 25x 57x 11x 11x 29x 29x 11x 11x 14x 14x 14x | /**
* Serialization utilities for converting Prisma objects to Redux-compatible formats
* Handles Date objects and other non-serializable values
*/
/**
* Recursively serializes Prisma objects for Redux state management.
* Converts Date objects to ISO strings and handles nested objects/arrays.
*
* @param obj The object to serialize (typically from Prisma query)
* @returns A serialized version with all Date objects converted to ISO strings
*
* @example
* const product = await prisma.product.findUnique({ where: { id: 1 } });
* const serialized = serializePrismaObject(product);
* dispatch(updateProduct(serialized)); // No Redux serialization warnings
*/
export function serializePrismaObject<T>(obj: T): T {
// Handle null/undefined
if (obj === null || obj === undefined) return obj;
// Convert Date to ISO string
if (obj instanceof Date) {
return obj.toISOString() as unknown as T;
}
// Handle arrays
if (Array.isArray(obj)) {
return obj.map(serializePrismaObject) as unknown as T;
}
// Handle objects
if (typeof obj === "object") {
const serialized: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
serialized[key] = serializePrismaObject(value);
}
return serialized as T;
}
// Return primitives as-is
return obj;
}
/**
* Converts ISO date strings back to Date objects.
* Useful when reading serialized data from Redux state for display/computation.
*
* @param obj The object to deserialize (typically from Redux state)
* @returns A deserialized version with ISO date strings converted to Date objects
*
* @example
* const serialized = useSelector(selectProductDetails);
* const deserialized = deserializePrismaObject(serialized);
* console.log(deserialized.createdAt); // Date object, not string
*/
export function deserializePrismaObject<T>(obj: T): T {
if (obj === null || obj === undefined) return obj;
if (typeof obj === "string") {
// Check if string is ISO date format (simple check)
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/.test(obj)) {
return new Date(obj) as unknown as T;
}
return obj;
}
if (Array.isArray(obj)) {
return obj.map(deserializePrismaObject) as unknown as T;
}
if (typeof obj === "object") {
const deserialized: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
deserialized[key] = deserializePrismaObject(value);
}
return deserialized as T;
}
return obj;
}
|