milodb
Version:
Fast encrypted JSON database with AI-friendly API
162 lines (161 loc) • 5.38 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startServer = void 0;
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const milodb_1 = require("./milodb");
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const app = (0, express_1.default)();
app.use((0, cors_1.default)());
app.use(express_1.default.json());
let db;
// Initialize DB from config
const initDB = async () => {
const configPath = path.join(process.cwd(), 'milo.config.json');
const config = await fs.pathExists(configPath)
? await fs.readJson(configPath)
: { encryptData: false };
db = new milodb_1.MiloDB(path.join(process.cwd(), 'db'), config.encryptionKey);
};
// Tables
app.get('/api/tables', async (req, res) => {
try {
const tables = await db.listTables();
res.json({ tables });
}
catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/api/tables', async (req, res) => {
try {
const { name } = req.body;
await db.createTable(name);
res.json({ success: true, table: name });
}
catch (error) {
res.status(400).json({ error: error.message });
}
});
app.delete('/api/tables/:name', async (req, res) => {
try {
await db.dropTable(req.params.name);
res.json({ success: true });
}
catch (error) {
res.status(400).json({ error: error.message });
}
});
// Records
app.get('/api/:table', async (req, res) => {
try {
const { limit, offset, sort, order, ...query } = req.query;
const records = await db.find(req.params.table, query, {
limit: limit ? parseInt(limit) : undefined,
offset: offset ? parseInt(offset) : undefined,
sort: sort,
order: order
});
const total = await db.count(req.params.table, query);
res.json({ records, total });
}
catch (error) {
res.status(500).json({ error: error.message });
}
});
app.get('/api/:table/:id', async (req, res) => {
try {
const record = await db.findById(req.params.table, req.params.id);
if (!record)
return res.status(404).json({ error: 'Not found' });
res.json(record);
}
catch (error) {
res.status(500).json({ error: error.message });
}
});
app.post('/api/:table', async (req, res) => {
try {
const record = await db.insert(req.params.table, req.body);
res.status(201).json(record);
}
catch (error) {
res.status(400).json({ error: error.message });
}
});
app.post('/api/:table/batch', async (req, res) => {
try {
const result = await db.insertMany(req.params.table, req.body.records);
res.json(result);
}
catch (error) {
res.status(400).json({ error: error.message });
}
});
app.put('/api/:table/:id', async (req, res) => {
try {
const record = await db.update(req.params.table, req.params.id, req.body);
if (!record)
return res.status(404).json({ error: 'Not found' });
res.json(record);
}
catch (error) {
res.status(400).json({ error: error.message });
}
});
app.delete('/api/:table/:id', async (req, res) => {
try {
const deleted = await db.delete(req.params.table, req.params.id);
if (!deleted)
return res.status(404).json({ error: 'Not found' });
res.json({ success: true });
}
catch (error) {
res.status(400).json({ error: error.message });
}
});
const startServer = async (port = 3001) => {
await initDB();
app.listen(port, () => {
console.log(`✓ MiloDB API running on http://localhost:${port}`);
});
};
exports.startServer = startServer;
exports.default = app;