-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
140 lines (126 loc) · 4.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Auto increment support for mongodb and mongoose
* Created by Alexey Chistyakov <[email protected]> on 11.10.2014.
*/
var settings = {};
var defaultSettings = {
collection: "counters",
field: "_id",
step: 1
};
/**
* Get next sequence id for the given collection in the given database
*
* @param {MongodbNativeDriver} db Connection to mongodb native driver
* @param {String} collectionName Current collection name to get auto increment field for
* @param {String} [fieldName] Auto increment field in collection collectionName
* @param {Function} callback callback(err, number)
*/
exports.getNextSequence = function (db, collectionName, fieldName, callback) {
if (typeof fieldName == "function") {
callback = fieldName;
fieldName = null;
}
if (db._state == "connecting") {
db.on("open", function (err, db) {
getNextId(db, collectionName, fieldName, callback);
});
} else {
getNextId(db, collectionName, fieldName, callback);
}
};
/**
* Plugin for Mongoose
* Usage:
* 1) Global scope. It will be applied to all schemas with _id option field set
* var mongoose = require("mongoose");
* var autoincrement = require("mongodb-autoincrement");
* mongoose.plugin(autoincrement.mongoosePlugin);
* 2) Schema scope
* var Schema = require("mongoose").Schema;
* var autoincrement = require("mongodb-autoincrement");
* var schema = new Schema(...);
* schema.plugin(autoincrement.mongoosePlugin);
*
* @param {MongooseSchema} schema
* @param {Object} [options]
*/
exports.mongoosePlugin = function (schema, options) {
options = options || {};
var fieldName = options.field || defaultSettings.field;
if (schema.options._id) {
var schemaField = {};
schemaField[fieldName] = {type: Number, unique: true, required: true};
schema.add(schemaField);
}
schema.pre('save', function (next) {
var doc = this;
if (doc.isNew && doc.collection) {
if (!settings[doc.collection.name]) {
settings[doc.collection.name] = options;
}
exports.getNextSequence(doc.db.db, doc.collection.name, fieldName, function (err, result) {
doc[fieldName] = result;
next(err);
});
} else {
next();
}
});
};
/**
* Redefine default options
* - collection - name of the collection in db to hold sequence counters (default: counters)
* - field - name of the field to auto increment (default: _id)
* - step - auto increment step (default: 1)
* @param {Object} options
*/
exports.setDefaults = function (options) {
for (var key in options) {
if (options.hasOwnProperty(key)) {
defaultSettings[key] = options[key];
}
}
};
/**
* Get next auto increment index for the given collection
* @param {MongodbNativeDriver} db
* @param {String} collectionName
* @param {String} [fieldName]
* @param {Function} callback
*/
function getNextId(db, collectionName, fieldName, callback) {
if (typeof fieldName == "function") {
callback = fieldName;
fieldName = null;
}
fieldName = fieldName || getOption(collectionName, "field");
var collection = db.collection(defaultSettings.collection);
var step = getOption(collectionName, "step");
collection.findAndModify(
{_id: collectionName, field: fieldName},
null,
{$inc: {seq: step}},
{upsert: true, new: true},
function (err, result) {
if (err) {
if (err.code == 11000) {
process.nextTick(getNextId.bind(null, db, collectionName, fieldName, callback));
} else {
callback(err);
}
} else {
if (result.value && result.value.seq) {
callback(null, result.value.seq);
} else {
callback(null, result.seq);
}
}
}
);
}
function getOption(collectionName, optionName) {
return settings[collectionName] && settings[collectionName][optionName] !== undefined ?
settings[collectionName][optionName] :
defaultSettings[optionName];
}