{"library":"mongodb-level","title":"MongoDB Abstract-Level Database","description":"mongodb-level is an implementation of the `abstract-level` interface, providing a LevelDB-like key-value store API that is backed by a MongoDB database. This allows developers to interact with MongoDB using familiar operations such as `put`, `get`, `del`, and `iterator`, abstracting away the direct MongoDB Node.js driver API. Currently at version 0.0.4, the library is in an early, pre-stable development phase, indicating that its API may evolve. Given its early stage and specific niche, the release cadence is likely slow and focused on stability. Its key differentiator lies in offering a standardized LevelDB API over MongoDB, which can be advantageous for projects migrating from other LevelDB implementations or those aiming to standardize their data access layer across different storage backends.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mongodb-level"],"cli":null},"imports":["import { MongoDBLevel } from 'mongodb-level'","import type { AbstractLevel } from 'abstract-level'","import { MongoClient } from 'mongodb'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { MongoDBLevel } from 'mongodb-level';\nimport { MongoClient } from 'mongodb';\n\nconst MONGODB_URI = process.env.MONGODB_URI ?? 'mongodb://localhost:27017';\nconst DB_NAME = process.env.DB_NAME ?? 'test_level_db';\nconst COLLECTION_NAME = process.env.COLLECTION_NAME ?? 'level_collection';\n\nasync function run() {\n  let client: MongoClient | undefined;\n  let db: MongoDBLevel | undefined;\n  try {\n    client = await MongoClient.connect(MONGODB_URI);\n    console.log('Connected to MongoDB.');\n\n    db = new MongoDBLevel({\n      dbName: DB_NAME,\n      collectionName: COLLECTION_NAME,\n      mongoClient: client\n    });\n    await db.open();\n    console.log('MongoDBLevel opened.');\n\n    // 1. Put a key-value pair\n    await db.put('hello', 'world');\n    console.log(\"Put 'hello': 'world'\");\n\n    // 2. Get the value for a key\n    const value = await db.get('hello');\n    console.log(`Get 'hello': ${value}`); // Expected: world\n\n    // 3. Try to get a non-existent key\n    try {\n      await db.get('nonExistentKey');\n    } catch (error: any) {\n      if (error.code === 'LEVEL_NOT_FOUND') {\n        console.log('Successfully caught NotFoundError for nonExistentKey.');\n      } else {\n        throw error;\n      }\n    }\n\n    // 4. Delete a key\n    await db.del('hello');\n    console.log(\"Deleted 'hello'\");\n\n    // 5. Verify deletion\n    try {\n      await db.get('hello');\n    } catch (error: any) {\n      if (error.code === 'LEVEL_NOT_FOUND') {\n        console.log(\"Successfully verified 'hello' is deleted.\");\n      } else {\n        throw error;\n      }\n    }\n\n  } catch (err) {\n    console.error('An error occurred:', err);\n  } finally {\n    if (db) await db.close();\n    if (client) await client.close();\n    console.log('Database and MongoDB client closed.');\n  }\n}\n\nrun();","lang":"typescript","description":"Demonstrates basic `mongodb-level` operations (put, get, del) against a local or cloud MongoDB instance, including error handling for non-existent keys. It requires a MongoDB server to be running or a valid connection string to MongoDB Atlas.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}