Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[fix] stdlib: Split up utils.opa and removed.

  • Loading branch information...
commit 4db55f9e92db6d776466fd7d28ad36faa02da2a4 1 parent 009065b
@nrs135 nrs135 authored
View
6 stdlib/apis/mongo/collection.opa
@@ -342,6 +342,12 @@ MongoCollection = {{
MongoCommon.reply_to_result("MongoCollection.delete",0,
MongoDriver.deletee(c.db.mongo,c.db.delete_flags,ns,c.db.dbname,select))
+ /** Delete by _id field **/
+ delete_by_id(c:Mongo.collection('value),id) = delete(c,[H.str("_id",id)])
+
+ /** Delete by _id field with error result **/
+ delete_by_id_result(c:Mongo.collection('value),id) = delete_result(c,[H.str("_id",id)])
+
/**
* Return the [Bson.document] representation of a single value selected from
* a collection. This might facilitate more efficient handling of values
View
6 stdlib/apis/mongo/common.opa
@@ -171,6 +171,12 @@ MongoCommon = {{
/** Same as [outcome_map] but coerced to string **/
string_of_outcome = (outcome_map:outcome('s,'f), ('s->string), ('f->string) -> string)
+ /** Turn a result into an option **/
+ result_to_option(result:outcome('a,'b)) : option('a) = match result with | {success=v} -> {some=v} | _ -> {none}
+
+ /** Turn result into a list **/
+ results_to_list(result:outcome(list('a),'b)) : list('a) = match result with | {success=v} -> v | _ -> []
+
/** Turn a result into a [Mongo.error] value **/
error_of_result(result:Mongo.result): Mongo.error = map_success(result, Bson.error_of_document)
View
83 stdlib/apis/mongo/utils.opa
@@ -1,83 +0,0 @@
-/*
- Copyright © 2011 MLstate
-
- This file is part of OPA.
-
- OPA is free software: you can redistribute it and/or modify it under the
- terms of the GNU Affero General Public License, version 3, as published by
- the Free Software Foundation.
-
- OPA is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
- more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with OPA. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-/**
- * MongoDB binding for OPA.
- *
- * @destination public
- * @stabilization work in progress
- **/
-
-/**
- * {1 About this module}
- *
- * [MongoUtils] is a set of utility functions to make programming
- * using the MongoDB driver easier.
- *
- * {1 Where should I start?}
- *
- * {1 What if I need more?}
- *
- **/
-
-MongoUtils = {{
-
- @private ML = MongoLog
-
- /** A safe operation checking the error (still have to check if the
- * last error is really the last error, using eg. findAndModify).
- **/
- @private
- safe_(c:Mongo.collection('value),f:'a->bool,a:'a,msg:string): bool =
- if not(f(a))
- then ML.error("{msg}","Fatal error message not sent to server",false)
- else
- (match MongoConnection.getLastError(c.db) with
- | {~success} ->
- (match Bson.find_string(success, "err") with
- | {some=""} | {none} -> true
- | {some=err} -> ML.error("{msg}","{err}",false))
- | {~failure} -> ML.error("{msg}","fatal error {MongoCommon.string_of_failure(failure)}",false))
-
- safe_insert(c,v) = safe_(c,((c,v) -> MongoCollection.insert(c,v)),(c,v),"Collection.insert")
- safe_insert_batch(c,b) = safe_(c,((c,b) -> MongoCollection.insert_batch(c,b)),(c,b),"Collection.insert_batch")
- safe_update(c,s,v) = safe_(c,((c,s,v) -> MongoCollection.update(c,s,v)),(c,s,v),"Collection.update")
- safe_delete(c,s) = safe_(c,((c,s) -> MongoCollection.delete(c,s)),(c,s),"Collection.delete")
-
- // It's easier to deal with options
- find_result_to_opt(result) : option('a) =
- match result with
- | {success=v} -> {some=v}
- | _ -> {none}
-
- // Idem with list and empty list
- find_all_result_to_list(result) : list('a) =
- match result with
- | {success=v} -> v
- | _ -> []
-
- find(c,r) = find_result_to_opt(MongoCollection.find_one(c,MongoSelect.unsafe_make(r)))
- find_all(c,r) = find_all_result_to_list(MongoCollection.find_all(c,MongoSelect.unsafe_make(r)))
-
- // Delete by id by default
- delete(c,id) = MongoCollection.delete(c,MongoSelect.unsafe_make({_id = id}))
-
-}}
-
-// End of file utils.opa
-
Please sign in to comment.
Something went wrong with that request. Please try again.