Ivan Asmer 5 tahun lalu
induk
melakukan
681935fad9
1 mengubah file dengan 32 tambahan dan 21 penghapusan
  1. 32 21
      index.js

+ 32 - 21
index.js

@@ -283,16 +283,19 @@ const mm = db => {
                         return obj[_class]
                     }
 
+                    const applyCursorCalls = (cursor, calls) =>{
+                        for (let [method, params] of Object.entries(calls)){
+                            if (typeof cursor[method] !== "function"){
+                                throw new SyntaxError(`Wrong cursor method ${method}`)
+                            }
+                            cursor = cursor[method](...params)
+                        }
+                        return cursor;
+                    }
+
                     return  obj[_class] = {
                         * find(query, projection, cursorCalls={}){
-                            let cursor = db.collection(_class).find(query, projection)
-                            for (let [method, params] of Object.entries(cursorCalls)){
-                                if (typeof cursor[method] !== "function"){
-                                    throw new SyntaxError(`Wrong cursor method ${method}`)
-                                }
-
-                                cursor = cursor[method](...params)
-                            }
+                            let cursor = applyCursorCalls(db.collection(_class).find(query, projection), cursorCalls)
                             let cursorGen = asynchronize({s: cursor.stream(), 
                                                           chunkEventName: 'data', 
                                                           endEventName: 'close',
@@ -305,6 +308,10 @@ const mm = db => {
                                               err => fail(err)))
                             }
                         },
+                        async count(query, cursorCalls={}){
+                            let cursor = applyCursorCalls(db.collection(_class).find(query, projection), cursorCalls)
+                            return await cursor.count(true)
+                        },
                         async findOne(query, projection){
                             let result = await db.collection(_class).findOne(query, projection)
                             if (result)
@@ -435,6 +442,10 @@ const mm = db => {
                                         Savable.addClass(originalClass)
                                         yield* iter;
                                     },
+                                    async count(query, cursorCalls={}){
+                                        let permittedQuery = {$and: [SlicedSavable.___permissionQuery('read') ,query]}
+                                        return await Savable.m[_class].count(permittedQuery, cursorCalls)
+                                    },
                                     async findOne(query, projection){
                                         const originalClass = Savable.classes[_class]
                                         Savable.addClass(SlicedSavable.classes[_class])
@@ -453,20 +464,20 @@ const mm = db => {
                 })
             }
 
-                static get defaultPermissions(){
-                        return {
-                                //savable refs, objectid's, words like 'tags' or 'roles'
-                                read: ['owner', 'user'],
-                                write: ['owner', 'admin'],
-                                create: ['user'],
-                                delete: ['admin'],
-
-                                /*permission
-                                 * TODO: permissions for read and write permissions
-                                 *
-                                 */
-                        }
+            static get defaultPermissions(){
+                return {
+                    //savable refs, objectid's, words like 'tags' or 'roles'
+                    read: ['owner', 'user'],
+                    write: ['owner', 'admin'],
+                    create: ['user'],
+                    delete: ['admin'],
+
+                    /*permission
+                     * TODO: permissions for read and write permissions
+                     *
+                     */
                 }
+            }
         }
 
         return SlicedSavable