Simple, zero-dependency, object pseudo-database for Cloudflare Workers using R2 bucket. ๐งก๐พ
Simple, zero-dependency, object pseudo-database for Cloudflare Workers using R2 buckets, strongly inspired by lowdb ๐ค(https://github.com/typicode/lowdb/).
Become a sponsor and have your company logo here ๐ GitHub Sponsors
While Cloudflare R2 operates on a strongly consistent model (reference), it's important to note that lowstorage
is primarily designed for small, hobby, or personal projects. We advise extreme caution when using lowstorage
for critical applications or production environments, as it may not offer the robustness or features required for such use cases.
import lowstorage from 'lowstorage';
// Initialize object and get users collection
const usersCol = await lowstorage(env, 'MY_TESTING_BUCKET').collection('users');
// Add new user
// you can provide _id or it will be generated as crypto.randomUUID(); -> https://developers.cloudflare.com/workers/runtime-apis/web-crypto/
const newUser = await usersCol.insert({
name: 'Kevin',
gender: 'whatever',
posts: [],
});
// Show all users
const allUsers = usersCol.find({});
// Find users with pagination (e.g., page 2, 10 users per page)
const secondPageUsers = await usersCol.find({}, { skip: 10, limit: 10 });
// Find user by ID and update name
await usersCol.update({ _id: id }, { name: 'Carlos' });
npm install lowstorage
Seamless migration, robust free tier, Nonee gress fees. Dive into the future of data storage with Cloudflare R2 https://developers.cloudflare.com/r2/
- Storage: 10 GB/month
- Class A operations (mutate state): 1,000,000 / month
- Class B operations (read state): 10,000,000 / month
- more details on pricing R2
Check out wrangler.toml from examples
Insctructions with pictures https://github.com/gfodor/p2pcf/blob/master/INSTALL.md#set-up-the-r2-bucket
collection(colName)
Input: A string representing the name of the collection.
Behavior: Creates or accesses a collection with the given name.
Returns: An instance of the Collection class corresponding to the specified collection name.
insert(doc)
insert(doc)
_id
is not provided, a unique identifier is automatically generated using crypto.randomUUID()
._id
property assigned to each if not already present.find(query, options)
query
: A query object to match documents.options
: An optional object for pagination, containing skip
and limit
properties.options
.findOne(query)
find
, but it returns only the first matching document.null
if no match is found.update(query, update)
updateOne(query, update)
1
if a document is updated, otherwise 0
.delete(query)
remove()
count(query)
listCollections()
Check out dummy examples Run:
cd examples
npm install
npm run dev
for testing:
npm run test
It starts local wrangler with ENV and toml config from your /examples folder to run tests.
Feel free to dive in! Open an issue or submit PRs.
Standard Readme follows the Contributor Covenant Code of Conduct.