coyotte508 HF staff commited on
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(async (session) => {
48
- await collections.pictures.insertOne(
49
- {
50
- _id,
51
- name,
52
- storage: formats.map((format) => ({
53
- _id: `${_id}-${format.width}x${format.height}`,
54
- width: format.width,
55
- height: format.height,
56
- size: format.data.length
57
- })),
58
- ...(opts?.productId && { productId: opts.productId }),
59
- createdAt: new Date(),
60
- updatedAt: new Date()
61
- },
62
- { session }
63
- );
 
 
64
 
65
- await collections.picturesFs.insertMany(
66
- formats.map(
67
- (format) => ({
68
- _id: `${_id}-${format.width}x${format.height}`,
69
- createdAt: new Date(),
70
- updatedAt: new Date(),
71
- size: format.data.length,
72
- data: format.data,
73
- picture: _id
74
- }),
75
- { session }
76
- )
77
- );
78
 
79
- if (opts?.cb) {
80
- await opts.cb(session);
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(async (session) => {
37
- picture = (await collections.pictures.findOneAndDelete({ _id: params.id }, { session }))
38
- .value;
39
- await collections.picturesFs.deleteMany({ picture: params.id }, { session });
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(async (session) => {
55
- const product = await collections.products.findOneAndDelete({ _id: params.id });
 
 
56
 
57
- if (!product.value) {
58
- throw error(404);
59
- }
60
 
61
- const pictures = await collections.pictures
62
- .find({ productId: params.id }, { session })
63
- .toArray();
64
 
65
- await collections.pictures.deleteMany(
66
- { _id: { $in: pictures.map((p) => p._id) } },
67
- { session }
68
- );
69
- await collections.picturesFs.deleteMany(
70
- {
71
- _id: { $in: pictures.flatMap((p) => p.storage.map((s) => s._id)) }
72
- },
73
- { session }
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
  }