µDB or (uDB) is a tiny document database for browsers, Node.js, and Google Apps Script (GAS), but for micro projects only.
Note! On GAS it is extreeeemly slow.
console.log(`Existing stores: ${uDB.getStores()}`);
const db = new uDB("my-docs-store"); // For Node.js and GAS, a corresponding JSON file is created in a data/ folder located next to the (uDB) script.
const doc = { foo: "bar" };
let insertedDoc = db.put(doc);
console.log(`Inserted: ${db.get(insertedDoc._id)}`);
insertedDoc.foo = "bazz";
console.log(`Updated: ${db.put(insertedDoc)}`);
console.log(`All documents: ${db.getAll()}`);
console.log(
`All documents after deleting ${insertedDoc._id}: ${db.delete(insertedDoc._id)}`
);
The GAS API and Node.js API responses comply with the JSend specification and can be used with the provided client.
const apiUrl = `${baseUrl}?api_key=${apiKey}`;
console.log(`Existing stores: ${await new uDBClient(apiUrl).getStores()}`);
const db = new uDBClient(apiUrl).store("my-docs-store");
const doc = { foo: "bar" };
let insertedDoc = await db.put(doc);
console.log(`Inserted: ${await db.get(insertedDoc._id)}`);
insertedDoc.foo = "bazz";
console.log(`Updated: ${await db.put(insertedDoc)}`);
console.log(`All documents: ${await db.getAll()}`);
console.log(
`All documents after deleting ${insertedDoc._id}: ${await db.delete(insertedDoc._id)}`
);
To deploy on GAS, create a new project using appsscript.json and add the code uDB.js and api.gs.
Node.js: Run npm install && npm test
. To test the API, use npm run testApi
, which starts both the server using npm start
and runs the client tests.
Browser: Launch uDB.test.html and open the console or use client.test.html for the API client instead.
GAS: Add uDB.js and uDB.test.gs to the project and run the runTests
function.
The code is simple enough to not need documentation.
Although I am planning to add some more helpful comments. 😉