rads-db 3.0.75 → 3.0.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -114,18 +114,41 @@ var _default = options => (schema, entity) => {
114
114
  };
115
115
  },
116
116
  async putMany(items, ctx) {
117
+ const itemsToPut = [];
117
118
  for (const item of items) {
118
119
  const id = item?.id;
119
120
  if (!id) throw new Error(`You must provide an id`);
120
- const itemToPut = {
121
+ itemsToPut.push({
121
122
  _partition: entity,
122
123
  id,
123
124
  ...item
124
- };
125
- const response = await client.items.upsert(itemToPut);
125
+ });
126
+ }
127
+ for (const chunk of _lodash.default.chunk(itemsToPut, 100)) {
128
+ let chunkToSend = chunk.map(x => ({
129
+ operationType: "Upsert",
130
+ resourceBody: x
131
+ }));
132
+ const responses = [];
133
+ for (let i = 0; i < 20; i++) {
134
+ const response = await client.items.bulk(chunkToSend);
135
+ const newChunkToSend = [];
136
+ for (let i2 = 0; i2 < chunkToSend.length; i2++) {
137
+ if (response[i2].statusCode !== 429) {
138
+ responses.push(response[i2]);
139
+ } else {
140
+ newChunkToSend.push(chunkToSend[i2]);
141
+ }
142
+ }
143
+ chunkToSend = newChunkToSend;
144
+ if (chunkToSend.length === 0) break;
145
+ const delay = 50 + i * 100;
146
+ console.warn(`Db overloaded. Retrying after ${delay}ms...`);
147
+ await new Promise(resolve => setTimeout(resolve, delay));
148
+ }
126
149
  ctx?.log?.({
127
- charge: response.requestCharge,
128
- request: `put#${itemToPut._partition}|${itemToPut.id}`
150
+ charge: _lodash.default.sumBy(responses, r => r.requestCharge),
151
+ request: `put#${chunk[0]._partition}[${chunk.length}]`
129
152
  });
130
153
  }
131
154
  }
@@ -73,13 +73,40 @@ export default (options) => (schema, entity) => {
73
73
  return { nodes, cursor };
74
74
  },
75
75
  async putMany(items, ctx) {
76
+ const itemsToPut = [];
76
77
  for (const item of items) {
77
78
  const id = item?.id;
78
79
  if (!id)
79
80
  throw new Error(`You must provide an id`);
80
- const itemToPut = { _partition: entity, id, ...item };
81
- const response = await client.items.upsert(itemToPut);
82
- ctx?.log?.({ charge: response.requestCharge, request: `put#${itemToPut._partition}|${itemToPut.id}` });
81
+ itemsToPut.push({ _partition: entity, id, ...item });
82
+ }
83
+ for (const chunk of _.chunk(itemsToPut, 100)) {
84
+ let chunkToSend = chunk.map((x) => ({
85
+ operationType: "Upsert",
86
+ resourceBody: x
87
+ }));
88
+ const responses = [];
89
+ for (let i = 0; i < 20; i++) {
90
+ const response = await client.items.bulk(chunkToSend);
91
+ const newChunkToSend = [];
92
+ for (let i2 = 0; i2 < chunkToSend.length; i2++) {
93
+ if (response[i2].statusCode !== 429) {
94
+ responses.push(response[i2]);
95
+ } else {
96
+ newChunkToSend.push(chunkToSend[i2]);
97
+ }
98
+ }
99
+ chunkToSend = newChunkToSend;
100
+ if (chunkToSend.length === 0)
101
+ break;
102
+ const delay = 50 + i * 100;
103
+ console.warn(`Db overloaded. Retrying after ${delay}ms...`);
104
+ await new Promise((resolve) => setTimeout(resolve, delay));
105
+ }
106
+ ctx?.log?.({
107
+ charge: _.sumBy(responses, (r) => r.requestCharge),
108
+ request: `put#${chunk[0]._partition}[${chunk.length}]`
109
+ });
83
110
  }
84
111
  }
85
112
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rads-db",
3
- "version": "3.0.75",
3
+ "version": "3.0.77",
4
4
  "packageManager": "pnpm@8.6.1",
5
5
  "description": "Say goodbye to boilerplate code and hello to efficient and elegant syntax.",
6
6
  "author": "",