Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mongo-db adaptor #1427

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,7 @@ PINATA_JWT= # Pinata JWT for uploading files to IPFS
# Cronos zkEVM
CRONOSZKEVM_ADDRESS=
CRONOSZKEVM_PRIVATE_KEY=

# MongoDB
MONGODB_CONNECTION_STRING= #mongodb connection string
MONGODB_DATABASE= #name of the database in mongoDB atlas
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 31 additions & 2 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,37 @@ export function getTokenForProvider(
}

function initializeDatabase(dataDir: string) {
if (process.env.POSTGRES_URL) {
if (process.env.MONGODB_CONNECTION_STRING) {
elizaLogger.log("Initializing database on MongoDB Atlas");
const client = new MongoClient(process.env.MONGODB_CONNECTION_STRING, {
maxPoolSize: 100,
minPoolSize: 5,
maxIdleTimeMS: 60000,
connectTimeoutMS: 10000,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
compressors: ['zlib'],
retryWrites: true,
retryReads: true
});

const dbName = process.env.MONGODB_DATABASE_NAME || 'CumulusAiAgent'; // Default database name
const db = new MongoDBDatabaseAdapter(client, dbName);

// Test the connection
db.init()
.then(() => {
elizaLogger.success(
"Successfully connected to MongoDB Atlas"
);
})
.catch((error) => {
elizaLogger.error("Failed to connect to MongoDB Atlas:", error);
throw error; // Re-throw to handle it in the calling code
});

return db;
} else if (process.env.POSTGRES_URL) {
elizaLogger.info("Initializing PostgreSQL connection...");
const db = new PostgresDatabaseAdapter({
connectionString: process.env.POSTGRES_URL,
Expand All @@ -373,7 +403,6 @@ function initializeDatabase(dataDir: string) {
} else {
const filePath =
process.env.SQLITE_FILE ?? path.resolve(dataDir, "db.sqlite");
// ":memory:";
const db = new SqliteDatabaseAdapter(new Database(filePath));
return db;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/adapter-mongodb/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
3 changes: 3 additions & 0 deletions packages/adapter-mongodb/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
26 changes: 26 additions & 0 deletions packages/adapter-mongodb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@ai16z/adapter-mongodb",
"version": "0.1.0",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@ai16z/eliza": "workspace:*",
"mongodb": "^6.3.0",
"uuid": "^9.0.1"
},
"devDependencies": {
"@types/node": "^20.10.0",
"@types/uuid": "^10.0.0",
"tsup": "8.3.5",
"typescript": "^5.0.0"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
},
"engines": {
"node": ">=16.0.0"
}
}
Loading
Loading