Skip to content

Commit d4e3382

Browse files
committed
fix issue in listing all collections and update examples
1 parent 7047d03 commit d4e3382

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

examples/src/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ app.post('/inserdata', async (c) => {
1818
return c.json(insertedData);
1919
});
2020

21+
app.post('/update/:id', async (c) => {
22+
const id = c.req.param('id');
23+
const jsonGeneratedData = await c.req.json();
24+
const userCol = new lowstorage(c.env, BUCKET_NAME).collection(USER_COL);
25+
const updatedDataRespo = await userCol.update({ _id: id }, jsonGeneratedData);
26+
return c.json(updatedDataRespo);
27+
});
28+
29+
// list all "collections"
30+
app.get('/list-collections', async (c) => {
31+
const ls = new lowstorage(c.env, BUCKET_NAME);
32+
const allCols = await ls.listCollections();
33+
return c.json({ allCols });
34+
});
35+
2136
// list all users
2237
app.get('/users', async (c) => {
2338
const requestStartTime = Date.now();

lib/lowstorage.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lowstorage.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ class lowstorage {
151151

152152
// this is similar to list all files unfortunatelly -
153153
// it returns a list of "collections" but expect to have a .json file for each collection
154-
// this is a workaround for the list method not returning all the keys (default limit is 1000)
155154
// return names of collections
156155
async listCollections() {
157156
const listed = await this._store.list();
@@ -166,7 +165,9 @@ class lowstorage {
166165
truncated = next.truncated;
167166
cursor = next.cursor;
168167
}
169-
return listed.objects.filter((key) => key.endsWith('.json')).map((key) => key.split('/')[0]);
168+
const collections = listed.objects.filter((entry) => entry.key.endsWith('.json'));
169+
// return only the collection name
170+
return collections.map((entry) => entry.key.split('/')[0]);
170171
}
171172
}
172173

0 commit comments

Comments
 (0)