[ORM] MongoDB 5.x connection error
See original GitHub issueDocker compose
version: '3.1'
services:
mongo:
image: mongo:5.0.10
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root
MONGO_INITDB_DATABASE: db
volumes:
- ./db/data:/data/db
- ./db/config:/data/configdb
# - ./db/001_users.js:/docker-entrypoint-initdb.d/001_users.js:ro # this just configure a new user
mongo-express:
image: mongo-express:1.0.0-alpha.4
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: root
ME_CONFIG_MONGODB_URL: "mongodb://root:root@mongo:27017/"
code
export class BaseEntity {
id: MongoId & PrimaryKey = '';
created: Date = new Date;
updated: Date = new Date;
static from(data:any){
const entity = new this();
for(const k in data){
entity[k] = data[k];
}
return entity;
}
}
async function test(){
@entity.name('user')
class User extends BaseEntity{
name:string;
}
const database = new Database(new MongoDatabaseAdapter(`mongodb://${"climbo"}:${"climbo"}@${"localhost"}:${27017}/${"climbo"}`), [BaseEntity,User]);
// database.entityRegistry.add(BaseEntity)
// database.entityRegistry.add(User);
await database.migrate(); //create tables
//await database.persist(new BaseEntity()); //insert data
await database.persist(User.from({name:'test'})); //insert data
const allUsers = await database.query(User).find();
console.log('all users', allUsers);
}
test();
crash
/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/command/command.ts:85
this.current.reject(new MongoError(message.errmsg || 'error', message.code));
^
MongoError: error
at HandshakeCommand.handleResponse (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/command/command.ts:85:37)
at MongoConnection.onResponse (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/connection.ts:365:34)
at ResponseParser.feed (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/connection.ts:522:22)
at Socket.<anonymous> (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/connection.ts:301:66)
at Socket.emit (node:events:520:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Sha256ScramAuth.auth (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/command/auth/scram.ts:103:31)
at async HandshakeCommand.doAuth (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/command/handshake.ts:126:9)
at async HandshakeCommand.execute (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/command/handshake.ts:115:13)
at async MongoConnection.execute (/home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/connection.ts:387:20)
at async /home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/mongo/src/client/connection.ts:458:17
at async /home/cristian/IdeaProjects/climbo2/node_modules/@deepkit/core/src/core.ts:506:17
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: undefined
}
command.ts
message has no ok
so i patched the file locally "ok" in message && !message.ok
handleResponse(response: Uint8Array): void {
if (!this.current) throw new Error('Got handleResponse without active command');
const deserializer: BSONDeserializer<BaseResponse> = this.current.responseType ? getBSONDeserializer(mongoBinarySerializer, this.current.responseType) : deserializeBSONWithoutOptimiser;
try {
const message = deserializer(response);
const error = handleErrorResponse(message);
if (error) {
this.current.reject(error);
return;
}
if ("ok" in message && !message.ok) {
this.current.reject(new MongoError(message.errmsg || 'error', message.code));
} else {
this.current.resolve(message);
}
} catch (error: any) {
if (error instanceof ValidationError || error instanceof SerializationError) {
if (this.current.responseType) {
const raw = deserializeBSONWithoutOptimiser(response);
console.log('mongo raw response', inspect(raw, {depth: null}));
if (raw.errmsg && raw.ok === 0) {
const error = handleErrorResponse(raw);
if (error) {
this.current.reject(error);
return;
}
}
this.current.reject(new MongoError(`Could not deserialize type ${stringifyType(this.current.responseType)}: ${error}`));
return;
}
}
this.current.reject(error);
}
}
PS: the following is broken because of webpack #351
PrimaryKey is still not working
This make everything "works" if i assign a `MongoId`to the entity.
Problem 2) Maybe i discover another bug with the PrimaryKey? if i change id to _id everything works as expected.
export class BaseEntity {
id: MongoId & PrimaryKey = "507f1f77bcf86cd799439011";
created: Date = new Date;
updated: Date = new Date;
static from(data:any){
const entity = new this();
for(const k in data){
entity[k] = data[k];
}
return entity;
}
}
PrimaryKey is ignored?
It seems so, if i change the definition to
export class BaseEntity {
id: MongoId & PrimaryKey; // no initializer
created: Date = new Date;
updated: Date = new Date;
static from(data:any){
const entity = new this();
for(const k in data){
entity[k] = data[k];
}
return entity;
}
}
and run a query
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
MongoDB connection error
I am having an error in my node server. I am using mongoose as an ORM with node.js and I am trying to...
Read more >MongoError: connect ECONNREFUSED 127.0.0.1:27017
The error I got was MongoServerSelectionError: connect ECONNREFUSED ::1:27017. The solution was simply replacing localhost by 0.0.0.0 .
Read more >Connection management (Guide) - Prisma
✘ significantly affects database performance, and can result in the database being shut down due to an out of memory error. The way...
Read more >Database integration - Express.js
Adding the capability to connect databases to Express apps is just a matter of loading an ... Cassandra; Couchbase; CouchDB; LevelDB; MySQL; MongoDB;...
Read more >How to Use Go With MongoDB? - GeeksforGeeks
a mongoDB connection. defer func (){. // client.Disconnect method also has deadline. // returns error if any,. if err := client.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@CristianPi thanks, that was the missing piece. Fixed in https://github.com/deepkit/deepkit-framework/commit/c1d6c415199804d966ce17ceb235a050de011da3
@marcj
I see, but you have to try with authentication:
mongo-init.js
then in your code
ps: i think you can create an user without using the mongo-init.js initializer, just try connecting with username & password