Skip to content

Commit 35cf9a6

Browse files
committed
Updates mongodb driver to support DBRef and change primary key settings
1 parent a15d4e9 commit 35cf9a6

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

lib/Drivers/DML/mongodb.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ function Driver(config, connection, opts) {
66
this.client = new mongodb.MongoClient();
77
this.db = null;
88
this.config = config || {};
9-
this.opts = opts || {};
9+
this.opts = opts;
10+
11+
this.opts.settings.set("properties.primary_key", "_id");
1012
}
1113

1214
Driver.prototype.sync = function (opts, cb) {
@@ -57,6 +59,9 @@ Driver.prototype.close = function (cb) {
5759

5860
Driver.prototype.find = function (fields, table, conditions, opts, cb) {
5961
var collection = this.db.collection(table);
62+
63+
convertToDB(conditions);
64+
6065
var cursor = collection.find(conditions, fields);
6166

6267
if (opts.order) {
@@ -76,7 +81,8 @@ Driver.prototype.find = function (fields, table, conditions, opts, cb) {
7681

7782
return cursor.toArray(function (err, docs) {
7883
for (var i = 0; i < docs.length; i++) {
79-
docs[i]._id = docs[i]._id.toString();
84+
convertFromDB(docs[i]);
85+
// docs[i]._id = docs[i]._id.toString();
8086
}
8187
return cb(err, docs);
8288
});
@@ -163,7 +169,11 @@ Driver.prototype.clear = function (table, cb) {
163169
function convertToDB(obj) {
164170
for (var k in obj) {
165171
if (k == "_id") {
166-
obj[k] = new mongodb.ObjectID(obj[k]);
172+
if (obj[k] instanceof mongodb.DBRef) {
173+
obj[k] = new mongodb.ObjectID(obj[k].oid.toString());
174+
} else if (!(obj[k] instanceof mongodb.ObjectID)) {
175+
obj[k] = new mongodb.ObjectID(obj[k]);
176+
}
167177
}
168178
}
169179
}

0 commit comments

Comments
 (0)