CortexObject

The class that represents built-in/custom object definitions and represents the upcoming standard for cursor-based instance access within the Cortex scripting environment.

script.principal and script.context (when it exists) are CortexObject instances. This makes the following valid code:

  • script.principal.update('name.first', 'Tim') (update the name of the current principal)

  • script.context.cancel('not a good time') (deployment.before script)

org auto-global, the accounts and view modules extend CortexObject. This makes the following true:

  • require('accounts') === org.objects.accounts

  • script.org === org

  • typeof require('views').find === 'function'

  • typeof org.objects.accounts.createAuthToken === 'function'

Obtaining an instance cursor is as easy as org.objects.c_custom.find().

CortexObject can be easily extended in order to provide custom functionality to all your objects through as() or simply by extending it directly.

// new base custom object -------

class CustomBase extends CortexObject {
    static findOne(filter, ...paths) {
        const cursor = this.find(filter).skipAcl().limit(1).grant(8);
        if (paths.length) {
            cursor.paths(...paths);
        }
        return cursor.hasNext() ? cursor.next() : null;
    }
};
CustomBase.as('c_my_object').findOne();

const obj = CustomBase.as('Account').findOne({email: 'chazz@reinhold.com'}, 'name');

// or we can just name the class -------

class c_My_Object extends CortexObject {

   static findAllFoos(skip = 0, limit = 100) {
        return this.find({c_foo: true}).skip(skip).limit(limit).toList()          
    }
}
return c_My_Object.findAllFoos();

Static Methods

as(objectName)

Extends CortexObject and returns a class with a constructor name matching objectName.

Arguments

  • objectName (String)

Returns

Class

const Nums = CortexObject.as('c_num');
const _id = Nums.insertOne({c_val: [1, 2, 3]}).execute();

from(context)

Returns an instance of a CortexObject based on the passed in context argument, which should have at least _id and object properties.

Arguments

  • context (Object)

Returns

CortexObject instance

aggregate(pipeline=[])

Returns an aggregation cursor based on the optional passed in pipeline argument. The pipeline must be in the form of an array and named aggregation steps ([{$match: {...}}, {$project: {...}}, ...]). A pipeline can also be built later using aggregation cursor chaining.

Arguments

  • pipeline (Object[])

Returns

count(match)

Returns a count of matching documents of the current CortexObject.

Arguments

  • match (Object) Optional match filter document.

Returns

Number

deleteOne(match)

Returns a delete operation based on the passed in match filter.

Arguments

  • match (Object) Optional match filter document.

Returns

find(match)

Returns a cursor based on the passed in match filter.

Arguments

  • match (Object) Match filter document.

Returns

insertOne(doc)

Returns an insert operation.

Arguments

  • doc (Object) The document to insert.

Returns

setOwner(id, to)

Transfers ownership of a context object to another account.

Arguments

  • id (ObjectID) The identifier of the instance to update.

  • to (ObjectID|String) The identifier or email of the new owner account.

Returns

Boolean true is modified or false if already owned by to.

🚧Warning

setOwner is a low-level operation and does not implement any access controls, though an Audit log record is produced.

updateOne(match, doc)

Returns an update operation based on the passed in match filter.

$set and $push operations support updating existing document array elements by including their identifiers (e.g. push 2 items into c_arr of a specific document in the c_docs document array: { $push: {c_docs: {_id: '599284e01c9e955ff7526793', c_arr: [1, 2] }}}).

$unset operations can remove properties buried in specific documents

Arguments

  • match (Object) Optional match filter document.

  • doc (Object) An object containing the changes to effect. Update supports $set, $push, $unset and $remove.

    • $set Contains properties to update. (e.g. {$set: {name: {first: 'Auric', last: 'Goldfinger'}}})

    • $push Contains items to push into arrays and document arrays (e.g. {$push: {c_names: ['Jill Masterson']}})

    • $unset Unset one or more deletable properties (e.g. {$unset: {'c_doc.59928b74341a65ef3f03842b.c_deletable': 1, c_remove_me: 1}}).

    • $remove Removes elements from arrays, by full path (e.g. remove 2 documents from c_doc_array by identifier and all 1 and 2 values from c_num_arr in the c_doc_array document with an identifier of 59928b74341a65ef3f03842b: $remove: {'c_doc_array': ['59928aed341a65ef3f03836a', '59928af9341a65ef3f038380'], 'c_doc_array.59928b74341a65ef3f03842b.c_num_arr': [1,2] })

Returns

JavaScript

const Nums = CortexObject.as('c_nums');

Nums.updateOne({_id: '5992879ef4e03e6c3f682c5e'}, {
  $push: {    
    c_doc: {
      c_arr: [1, 2, 3]
    }
  },
  $set: {
    c_doc: [{
      _id: '59928b74341a65ef3f03842b',
      c_del: 420,
      c_arr: [1, 2, 3, 4]
    }]
  },
  $unset: {
    'c_doc.59928b74341a65ef3f03842b.c_del': 1
  }, 
  $remove: {
    c_doc: ['59928aed341a65ef3f03836a', '59928af9341a65ef3f038380']
    'c_doc.59928b74341a65ef3f03842b.c_arr': [1,2]
  }
}).execute();

Last updated