Changeset View
Changeset View
Standalone View
Standalone View
lib/datastore/mongo-api-abstract.js
"use strict"; | "use strict"; | ||||
var 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) { | var DatabasesCommonPart = function(app, datastore, _private) { | ||||
datastore.post_start = function() { | datastore.post_start = function() { | ||||
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, function(collection) { | ||||
let indexes = [["sealious_id", 1]]; | let indexes = [["sealious_id", 1]]; | ||||
for (var field_name in collection.fields) { | for (var field_name in collection.fields) { | ||||
indexes.push( | indexes.push( | ||||
Show All 27 Lines | return Promise.map(collections, function(collection) { | ||||
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 == 0) { | ||||
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(function(index) { | ||||
return createIndex(db_collection, index).catch( | return createIndex(db_collection, index).catch( | ||||
e => e.code == 85, | e => e.code == 85, | ||||
function(error) { | function(error) { | ||||
const index_name = error.message | const index_name = error.message | ||||
Show All 18 Lines | for (var 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 (var 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 || {}; | options = options || {}; | ||||
output_options = output_options || {}; | output_options = output_options || {}; | ||||
var cursor = _private.db | var cursor = _private.db.collection(collection_name).find(query, options); | ||||
.collection(collection_name) | |||||
.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 || {}; | options = options || {}; | ||||
output_options = output_options || {}; | output_options = output_options || {}; | ||||
const cursor = _private.db | const cursor = _private.db.collection(collection_name).aggregate(pipeline); | ||||
.collection(collection_name) | |||||
.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) { | ||||
Show All 23 Lines | return new Promise(function(resolve, reject) { | ||||
resolve(WriteResult); | resolve(WriteResult); | ||||
} | } | ||||
}); | }); | ||||
}); | }); | ||||
}; | }; | ||||
datastore.remove = function(collection_name, query, just_one) { | datastore.remove = function(collection_name, query, just_one) { | ||||
query = process_query(query); | query = process_query(query); | ||||
//console.log("Removing!", query); | |||||
return new Promise(function(resolve, reject) { | return new Promise(function(resolve, reject) { | ||||
if (just_one === undefined) { | if (just_one === undefined) { | ||||
just_one = 0; | 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, function(err, delete_response) { | ||||
Show All 19 Lines |