g-drive-database
v1.0.1
Published
A universal, NoSQL-like database that uses Google Drive as its storage backend.
Readme
G-Drive Database
A universal, NoSQL-like database that uses Google Drive as its storage backend.
Features
- Universal: Works in both Browser and Node.js environments.
- Serverless: No traditional database server required, uses users' Google Drive.
- NoSQL Interface: Familiar MongoDB-like syntax for collections (
insertOne,find,updateOne,deleteOne). - TypeScript Ready: Strongly typed for professional development.
Installation
npm install g-drive-databaseAuthentication
Since this package interacts with Google Drive, you will need a valid Google Drive API Access Token. You can obtain this token in your application using Google Identity Services (for React/Frontend) or Google OAuth2 Client (for Node.js).
Make sure the token has the https://www.googleapis.com/auth/drive.file scope (or drive full scope).
Usage
import { GDriveDatabase } from "g-drive-database";
// Initialize the database with your access token
const db = new GDriveDatabase({
accessToken: "YOUR_GOOGLE_ACCESS_TOKEN", // Keep this token updated!
folderName: "My-App-Database", // Optional: Custom folder name in Drive (defaults to GDrive-DB)
});
async function run() {
// Connect to Google Drive (creates folder and metadata file if they don't exist)
await db.connect();
// Get or create a collection (this creates a users.json file in the folder)
const users = db.collection("users");
// Insert a document
const newUser = await users.insertOne({ name: "Ali", role: "admin" });
console.log("Inserted:", newUser);
// Find documents
const allUsers = await users.find();
console.log("All users:", allUsers);
const admins = await users.find({ role: "admin" });
console.log("Admins:", admins);
// Update a document
const updatedUser = await users.updateOne(
{ name: "Ali" },
{ role: "superadmin" },
);
console.log("Updated:", updatedUser);
// Delete a document
await users.deleteOne({ name: "Ali" });
console.log("Deleted successfully.");
}
run();Environment Variables (.env)
If you are developing this package or using it in a Node.js project, make sure to keep your secrets safe.
Create a .env file in your project root:
GOOGLE_CLIENT_ID=your_client_id_here
GOOGLE_CLIENT_SECRET=your_client_secret_here
GOOGLE_REFRESH_TOKEN=your_refresh_token_hereNote: Do not commit
.envto version control. An.env.exampleis provided for reference.
How it Works
- Folder: On
connect(), the package checks if the database folder exists in the user's Drive. If not, it creates it. - Metadata: A
metadata.jsonfile is automatically created inside the folder to describe the storage schema. - Collections: Each collection (e.g.,
db.collection('users')) corresponds to a.jsonfile inside the database folder. - Operations: CRUD operations read and update the corresponding
.jsonfile.
Publishing
To publish this package:
- Run
npm run build - Run
npm publish
