Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Proxy
✓ const { Proxy } = require('database-proxy')
✗ import { Proxy } from 'database-proxy'
This package is explicitly CommonJS (`"type": "commonjs"`) and does not support ESM imports directly. Use `require()`.
MongoAccessor
✓ const { MongoAccessor } = require('database-proxy')
✗ import MongoAccessor from 'database-proxy'
MongoAccessor is a named export for database interaction. Ensure correct destructuring and CommonJS syntax.
Policy
✓ const { Policy } = require('database-proxy')
✗ const Policy = require('database-proxy').Policy
Policy is a named export for defining access control rules. Destructure it directly for clarity.
Sets up an Express.js server to expose a secure HTTP endpoint for MongoDB operations using `database-proxy`. It demonstrates how to configure access control rules with `Policy`, inject user context (like `uid` and `admin` status), and handle incoming client database requests, ensuring all operations comply with predefined security policies. This example assumes a MongoDB instance is running locally or accessible via `MONGO_URI`.
const express = require('express');
const { Proxy, MongoAccessor, Policy } = require('database-proxy');
const { MongoClient } = require('mongodb');
const app = express();
app.use(express.json());
// Dummy function for token parsing (replace with actual implementation)
function parseToken(authorizationHeader) {
// In a real application, this would validate a JWT and extract user info
if (authorizationHeader && authorizationHeader.startsWith('Bearer ')) {
const token = authorizationHeader.substring(7);
// For demonstration, let's assume a valid token means uid is 'testuser'
// In production, decode and verify the token properly.
return { uid: 'testuser', admin: true }; // Example: always admin for demo
}
return { uid: null, admin: false };
}
// Design the access control policy rules
const rules = {
categories: {
"read": true,
"update": "$admin === true", // Only admin can update
"add": "$admin === true", // Only admin can add
"remove": "$admin === true"
},
articles: {
"read": true,
"update": "$userid && $userid === query.createdBy",
"add": "$userid && data.createdBy === $userid",
"remove": "$userid === query.createdBy || $admin === true"
}
};
const mongoUri = process.env.MONGO_URI ?? 'mongodb://localhost:27017';
const client = new MongoClient(mongoUri);
async function setupDatabaseProxy() {
try {
await client.connect();
console.log('Connected to MongoDB');
const accessor = new MongoAccessor(client);
const policy = new Policy(accessor);
policy.load(rules);
const proxy = new Proxy(accessor, policy);
app.post('/proxy', async (req, res) => {
const { uid, admin } = parseToken(req.headers['authorization']);
const injections = {
uid: uid,
admin: admin
};
const params = proxy.parseParams(req.body);
const result = await proxy.validate(params, injections);
if (result.errors) {
return res.status(403).send({
code: 1,
error: result.errors
});
}
const data = await proxy.execute(params);
return res.send({
code: 0,
data
});
});
const port = 8080;
app.listen(port, () => console.log(`Database Proxy listening on http://localhost:${port}/proxy`));
} catch (error) {
console.error('Failed to connect to MongoDB or start server:', error);
process.exit(1);
}
}
setupDatabaseProxy();
Errors
Common errors & fixes
TypeError: (0 , database_proxy_1.Proxy) is not a constructor
Attempting to use ES module `import` syntax for a CommonJS package.
fixChange your import statements from `import { Proxy } from 'database-proxy'` to `const { Proxy } = require('database-proxy')`. Error: MongoParseError: options is not a function
An incorrect MongoDB connection string format or attempting to pass connection options in an incompatible way to `MongoClient`.
fixEnsure your MongoDB connection string (`MONGO_URI`) is correctly formatted, e.g., `mongodb://localhost:27017` or `mongodb+srv://user:pass@cluster.mongodb.net/`.
code: 1, error: 'Access denied: operation not permitted by policy'
The incoming database operation (read, update, add, remove) violates the defined `Policy` rules for the collection or document, given the injected user context.
fixReview your `rules` configuration for the affected collection and the `injections` provided to `proxy.validate()`. Ensure the user's role and data context satisfy the policy conditions for the requested action.
Audit
Dependencies
mongodbrequiredRequired for MongoDB database connectivity and operations as demonstrated in the server-side integration example.