Changeset View
Changeset View
Standalone View
Standalone View
lib/datastore/mongo-api-abstract.js
"use strict"; | const Promise = require("bluebird"); | ||||
var Promise = require("bluebird"); | |||||
const merge = require("merge"); | const merge = require("merge"); | ||||
function createIndex(db_collection, index) { | function createIndex(db_collection, index) { | ||||
return Promise.promisify(db_collection.createIndex).bind(db_collection)( | return Promise.promisify(db_collection.createIndex).bind(db_collection)( | ||||
index, | index, | ||||
{ background: true } | { background: true } | ||||
); | ); | ||||
} | } | ||||
var DatabasesCommonPart = function(app, datastore, _private) { | const DatabasesCommonPart = function(app, datastore, _private) { | ||||
datastore.post_start = function() { | datastore.post_start = function() { | ||||
datastore.client = _private.db; | datastore.client = _private.db; | ||||
const collection_names = app.ChipManager.get_all_collections(); | const collection_names = app.ChipManager.get_all_collections(); | ||||
const collections = collection_names.map(name => | const collections = collection_names.map(name => | ||||
app.ChipManager.get_chip("collection", name) | app.ChipManager.get_chip("collection", name) | ||||
); | ); | ||||
return Promise.map(collections, function(collection) { | return Promise.map(collections, collection => { | ||||
let indexes = [["sealious_id", 1]]; | const indexes = [["sealious_id", 1]]; | ||||
for (var field_name in collection.fields) { | for (const field_name in collection.fields) { | ||||
indexes.push( | indexes.push( | ||||
Promise.all([ | Promise.all([ | ||||
field_name, | field_name, | ||||
collection.fields[field_name].has_index(), | collection.fields[field_name].has_index(), | ||||
]) | ]) | ||||
); | ); | ||||
} | } | ||||
const db_collection = _private.db.collection(collection.name); | const db_collection = _private.db.collection(collection.name); | ||||
return Promise.all(indexes) | return Promise.all(indexes) | ||||
.then(function(collection_indexes) { | .then(collection_indexes => { | ||||
const all_indexes = collection_indexes | const all_indexes = collection_indexes | ||||
.filter(e => e[1] !== false) | .filter(e => e[1] !== false) | ||||
.map(function(index) { | .map(index => { | ||||
if (index[1] instanceof Object) { | if (index[1] instanceof Object) { | ||||
const ret = []; | const ret = []; | ||||
for (const i in index[1]) { | for (const i in index[1]) { | ||||
ret.push([index[0] + "." + i, index[1][i]]); | ret.push([`${index[0]}.${i}`, index[1][i]]); | ||||
} | } | ||||
return ret; | return ret; | ||||
} else { | |||||
return [index]; | |||||
} | } | ||||
return [index]; | |||||
}) | }) | ||||
.reduce((a, b) => a.concat(b), []); | .reduce((a, b) => a.concat(b), []); | ||||
const non_text_indexes = all_indexes | const non_text_indexes = all_indexes | ||||
.filter(e => e[1] !== "text") | .filter(e => e[1] !== "text") | ||||
.map(e => { | .map(e => { | ||||
return { [e[0]]: e[1] }; | return { [e[0]]: e[1] }; | ||||
}); | }); | ||||
// if multiple fields take part in full text search, we need to combine them into a single index. | // if multiple fields take part in full text search, we need to combine them into a single index. | ||||
let text_indexes = [ | let text_indexes = [ | ||||
all_indexes | all_indexes | ||||
.filter(e => e[1] === "text") | .filter(e => e[1] === "text") | ||||
.reduce( | .reduce( | ||||
(a, b) => merge(true, a, { [b[0]]: b[1] }), | (a, b) => merge(true, a, { [b[0]]: b[1] }), | ||||
{} | {} | ||||
), | ), | ||||
]; | ]; | ||||
if (Object.keys(text_indexes[0]).length == 0) { | if (!Object.keys(text_indexes[0]).length) { | ||||
text_indexes = []; | text_indexes = []; | ||||
} | } | ||||
const merged_indexes = text_indexes.concat( | const merged_indexes = text_indexes.concat( | ||||
non_text_indexes | non_text_indexes | ||||
); | ); | ||||
return merged_indexes; | return merged_indexes; | ||||
}) | }) | ||||
.each(function(index) { | .each(index => { | ||||
return createIndex(db_collection, index).catch( | return createIndex(db_collection, index).catch( | ||||
e => e.code == 85, | e => e.code === 85, | ||||
function(error) { | error => { | ||||
const index_name = error.message | const index_name = error.message | ||||
.match(/name: \"([^\"]+)\"/g)[1] | .match(/name: "([^"]+)"/g)[1] | ||||
.replace('name: "', "") | .replace('name: "', "") | ||||
.replace('"', ""); | .replace('"', ""); | ||||
return Promise.promisify(db_collection.dropIndex) | return Promise.promisify(db_collection.dropIndex) | ||||
.bind(db_collection)(index_name) | .bind(db_collection)(index_name) | ||||
.then(() => createIndex(db_collection, index)); | .then(() => createIndex(db_collection, index)); | ||||
} | } | ||||
); | ); | ||||
}); | }); | ||||
}); | }); | ||||
}; | }; | ||||
function process_query(query) { | function process_query(query) { | ||||
if (!query) { | if (!query) { | ||||
return {}; | return {}; | ||||
} | } | ||||
var new_query = {}; | let new_query = {}; | ||||
for (var attribute_name in query) { | for (let attribute_name in query) { | ||||
if (attribute_name == "sealious_id") { | if (attribute_name === "sealious_id") { | ||||
new_query[attribute_name] = query[attribute_name]; | new_query[attribute_name] = query[attribute_name]; | ||||
} else { | } else if (query[attribute_name] instanceof Object) { | ||||
if (query[attribute_name] instanceof Object) { | |||||
if (attribute_name[0] === "$") { | if (attribute_name[0] === "$") { | ||||
new_query[attribute_name] = query[attribute_name]; | new_query[attribute_name] = query[attribute_name]; | ||||
} else { | } else { | ||||
for (var i in query[attribute_name]) { | for (let i in query[attribute_name]) { | ||||
new_query[attribute_name + "." + i] = | new_query[`${attribute_name}.${i}`] = | ||||
query[attribute_name][i]; | query[attribute_name][i]; | ||||
} | } | ||||
} | } | ||||
} else { | } else { | ||||
new_query[attribute_name] = query[attribute_name]; | new_query[attribute_name] = query[attribute_name]; | ||||
} | } | ||||
} | } | ||||
} | |||||
return new_query; | return new_query; | ||||
} | } | ||||
datastore.find = function(collection_name, query, options, output_options) { | datastore.find = function( | ||||
collection_name, | |||||
query, | |||||
_options, | |||||
_output_options | |||||
) { | |||||
//console.log("FIND", collection_name, query); | //console.log("FIND", collection_name, query); | ||||
//query = process_query(query); // - needed, ResourceCollection subject handles that now | //query = process_query(query); // - needed, ResourceCollection subject handles that now | ||||
options = options || {}; | const options = _options || {}; | ||||
output_options = output_options || {}; | const output_options = _output_options || {}; | ||||
var cursor = _private.db | let cursor = _private.db | ||||
.collection(collection_name) | .collection(collection_name) | ||||
.find(query, options); | .find(query, options); | ||||
if (output_options.sort) { | if (output_options.sort) { | ||||
cursor.sort(output_options.sort); | cursor.sort(output_options.sort); | ||||
} | } | ||||
if (output_options.skip) { | if (output_options.skip) { | ||||
cursor.skip(output_options.skip); | cursor.skip(output_options.skip); | ||||
} | } | ||||
if (output_options.amount) { | if (output_options.amount) { | ||||
cursor.limit(output_options.amount); | cursor.limit(output_options.amount); | ||||
} | } | ||||
return Promise.promisify(cursor.toArray).bind(cursor)(); | return Promise.promisify(cursor.toArray).bind(cursor)(); | ||||
}; | }; | ||||
datastore.aggregate = function( | datastore.aggregate = function( | ||||
collection_name, | collection_name, | ||||
pipeline, | pipeline, | ||||
options, | options, | ||||
output_options | _output_options | ||||
) { | ) { | ||||
//console.log("aggregate", collection_name, JSON.stringify(pipeline)); | //console.log("aggregate", collection_name, JSON.stringify(pipeline)); | ||||
options = options || {}; | const output_options = _output_options || {}; | ||||
output_options = output_options || {}; | |||||
const cursor = _private.db | const cursor = _private.db | ||||
.collection(collection_name) | .collection(collection_name) | ||||
.aggregate(pipeline); | .aggregate(pipeline); | ||||
if (output_options.sort) { | if (output_options.sort) { | ||||
cursor.sort(output_options.sort); | cursor.sort(output_options.sort); | ||||
} | } | ||||
if (output_options.skip) { | if (output_options.skip) { | ||||
cursor.skip(output_options.skip); | cursor.skip(output_options.skip); | ||||
} | } | ||||
if (output_options.amount) { | if (output_options.amount) { | ||||
cursor.limit(output_options.amount); | cursor.limit(output_options.amount); | ||||
} | } | ||||
return Promise.promisify(cursor.toArray).bind(cursor)(); | return Promise.promisify(cursor.toArray).bind(cursor)(); | ||||
}; | }; | ||||
datastore.insert = function(collection_name, to_insert, options) { | datastore.insert = function(collection_name, to_insert, options) { | ||||
return Promise.promisify(_private.db.collection(collection_name).insert) | return Promise.promisify(_private.db.collection(collection_name).insert) | ||||
.bind(_private.db.collection(collection_name))(to_insert, options) | .bind(_private.db.collection(collection_name))(to_insert, options) | ||||
.then(function(result) { | .then(result => { | ||||
return result.ops[0]; | return result.ops[0]; | ||||
}); | }); | ||||
}; | }; | ||||
datastore.update = function(collection_name, query, new_value) { | datastore.update = function(collection_name, _query, new_value) { | ||||
query = process_query(query); | const query = process_query(_query); | ||||
return new Promise(function(resolve, reject) { | return new Promise((resolve, reject) => { | ||||
_private.db | _private.db | ||||
.collection(collection_name) | .collection(collection_name) | ||||
.update(query, new_value, function(err, WriteResult) { | .update(query, new_value, (err, WriteResult) => { | ||||
if (err) { | if (err) { | ||||
reject(err); | reject(err); | ||||
} else { | } else { | ||||
resolve(WriteResult); | resolve(WriteResult); | ||||
} | } | ||||
}); | }); | ||||
}); | }); | ||||
}; | }; | ||||
datastore.remove = function(collection_name, query, just_one) { | datastore.remove = function(collection_name, _query, _just_one) { | ||||
query = process_query(query); | const query = process_query(_query); | ||||
return new Promise(function(resolve, reject) { | return new Promise((resolve, reject) => { | ||||
if (just_one === undefined) { | let just_one = _just_one === undefined ? 0 : _just_one; | ||||
just_one = 0; | |||||
} | |||||
just_one = just_one ? 1 : 0; | just_one = just_one ? 1 : 0; | ||||
_private.db | _private.db | ||||
.collection(collection_name) | .collection(collection_name) | ||||
.remove(query, just_one, function(err, delete_response) { | .remove(query, just_one, (err, delete_response) => { | ||||
if (err) { | if (err) { | ||||
reject(err); | reject(err); | ||||
} else { | } else { | ||||
resolve(delete_response); | resolve(delete_response); | ||||
} | } | ||||
}); | }); | ||||
}); | }); | ||||
}; | }; | ||||
Show All 11 Lines |