Commit
β’
44a299a
1
Parent(s):
4dd693b
π Fix MongoDB transactions
Browse files
src/lib/server/photo.ts
CHANGED
@@ -44,40 +44,43 @@ export async function generatePicture(
|
|
44 |
|
45 |
const _id = generateId(name);
|
46 |
|
47 |
-
await client.withSession(
|
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 |
}
|
|
|
44 |
|
45 |
const _id = generateId(name);
|
46 |
|
47 |
+
await client.withSession(
|
48 |
+
async (session) =>
|
49 |
+
await session.withTransaction(async (session) => {
|
50 |
+
await collections.pictures.insertOne(
|
51 |
+
{
|
52 |
+
_id,
|
53 |
+
name,
|
54 |
+
storage: formats.map((format) => ({
|
55 |
+
_id: `${_id}-${format.width}x${format.height}`,
|
56 |
+
width: format.width,
|
57 |
+
height: format.height,
|
58 |
+
size: format.data.length
|
59 |
+
})),
|
60 |
+
...(opts?.productId && { productId: opts.productId }),
|
61 |
+
createdAt: new Date(),
|
62 |
+
updatedAt: new Date()
|
63 |
+
},
|
64 |
+
{ session }
|
65 |
+
);
|
66 |
|
67 |
+
await collections.picturesFs.insertMany(
|
68 |
+
formats.map(
|
69 |
+
(format) => ({
|
70 |
+
_id: `${_id}-${format.width}x${format.height}`,
|
71 |
+
createdAt: new Date(),
|
72 |
+
updatedAt: new Date(),
|
73 |
+
size: format.data.length,
|
74 |
+
data: format.data,
|
75 |
+
picture: _id
|
76 |
+
}),
|
77 |
+
{ session }
|
78 |
+
)
|
79 |
+
);
|
80 |
|
81 |
+
if (opts?.cb) {
|
82 |
+
await opts.cb(session);
|
83 |
+
}
|
84 |
+
})
|
85 |
+
);
|
86 |
}
|
src/routes/admin/photos/[id]/+page.server.ts
CHANGED
@@ -33,11 +33,14 @@ export const actions: Actions = {
|
|
33 |
delete: async function ({ params }) {
|
34 |
let picture: Picture | null = null;
|
35 |
|
36 |
-
await client.withSession(
|
37 |
-
|
38 |
-
.
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
42 |
if (!picture) {
|
43 |
throw error(404);
|
|
|
33 |
delete: async function ({ params }) {
|
34 |
let picture: Picture | null = null;
|
35 |
|
36 |
+
await client.withSession(
|
37 |
+
async (session) =>
|
38 |
+
await session.withTransaction(async (session) => {
|
39 |
+
picture = (await collections.pictures.findOneAndDelete({ _id: params.id }, { session }))
|
40 |
+
.value;
|
41 |
+
await collections.picturesFs.deleteMany({ picture: params.id }, { session });
|
42 |
+
})
|
43 |
+
);
|
44 |
|
45 |
if (!picture) {
|
46 |
throw error(404);
|
src/routes/admin/produits/[id]/+page.server.ts
CHANGED
@@ -51,28 +51,31 @@ export const actions: Actions = {
|
|
51 |
},
|
52 |
|
53 |
delete: async ({ params }) => {
|
54 |
-
await client.withSession(
|
55 |
-
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
76 |
|
77 |
throw redirect(303, '/admin/produits');
|
78 |
}
|
|
|
51 |
},
|
52 |
|
53 |
delete: async ({ params }) => {
|
54 |
+
await client.withSession(
|
55 |
+
async (session) =>
|
56 |
+
await session.withTransaction(async (session) => {
|
57 |
+
const product = await collections.products.findOneAndDelete({ _id: params.id });
|
58 |
|
59 |
+
if (!product.value) {
|
60 |
+
throw error(404);
|
61 |
+
}
|
62 |
|
63 |
+
const pictures = await collections.pictures
|
64 |
+
.find({ productId: params.id }, { session })
|
65 |
+
.toArray();
|
66 |
|
67 |
+
await collections.pictures.deleteMany(
|
68 |
+
{ _id: { $in: pictures.map((p) => p._id) } },
|
69 |
+
{ session }
|
70 |
+
);
|
71 |
+
await collections.picturesFs.deleteMany(
|
72 |
+
{
|
73 |
+
_id: { $in: pictures.flatMap((p) => p.storage.map((s) => s._id)) }
|
74 |
+
},
|
75 |
+
{ session }
|
76 |
+
);
|
77 |
+
})
|
78 |
+
);
|
79 |
|
80 |
throw redirect(303, '/admin/produits');
|
81 |
}
|