Skip to content

Instantly share code, notes, and snippets.

@prantlf
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prantlf/ad26565233f76854eb4e to your computer and use it in GitHub Desktop.
Save prantlf/ad26565233f76854eb4e to your computer and use it in GitHub Desktop.
Exposing meta-data from a simple DMS stored in MongoDB by OData based on JayData

How to expose meta-data from a simple DMS (Document Management System) stored in the MongoDB database by an OData service implemented with the help of JayData.

This is a minimum sample showing how to build a tree of folders and documents, where folders can be nested and every folder and document points to its parent. A full DMS would include more properties.

The actual folder+document hierarchy:

/--- Projects         (root folder)
  |
  +--- Mollie         (sub-folder)
    |
    +--- Summary.txt  (document)

Representation of the folder+document hierarchy in the database:

> db.folders.find()
  {
    "_id" : 2000, "name" : "Projects", "parent_id" : -1,
    "parent" : null, "folders" : null, "documents" : null
  }
  {
    "_id" : 2001, "name" : "Mollie", "parent_id" : 2000,
    "parent" : DBRef("folders", 2000), "folders" : null, "documents" : null
  }
> db.documents.find()
  {
    "_id" : 3000, "name" : "Summary.txt", "parent_id" : 2001,
    "parent" : DBRef("folders", 2001)
  }

Description of the folder-document relations for JayData:

(function($data, undefined) {

  $data.Entity.extend('OllieModel.Node', {
    _id: {
      key: true,
      type: 'Edm.Int32',
      nullable: false,
      computed: true
    },
    name: {
      type: 'Edm.String',
      nullable: false,
      required: true,
      maxLength: 512
    },
    parent_id: {
      type: 'Edm.Int32'
    }
  });

  OllieModel.Node.extend('OllieModel.Folder', {
    documents: {
      type: 'Array',
      elementType: 'OllieModel.Document',
      inverseProperty: 'parent'
    },
    folders: {
      type: 'Array',
      elementType: 'OllieModel.Folder',
      inverseProperty: 'parent'
    },
    parent: {
      type: 'OllieModel.Folder',
      inverseProperty: 'folders'
    }
  });

  OllieModel.Node.extend('OllieModel.Document', {
    parent: {
      type: 'OllieModel.Folder',
      inverseProperty: 'documents'
    }
  });

  $data.EntityContext.extend('OllieContext', {
    'folders': { type: $data.EntitySet, elementType: OllieModel.Folder},
    'documents': { type: $data.EntitySet, elementType: OllieModel.Document}
  });

  $data.generatedContexts = $data.generatedContexts || [];
  $data.generatedContexts.push(OllieContext);

})($data);

JayData cannot populate the database if the context contains entities with recursive associations - inverse (navigation) properties pointing back to the same entity. Exposing such model by the ODataServer to perform queries works.

Also, JayData builds the constraint key of an association by inserting two underscores between the source and target properties. If you cannot rename properties in your database, you can force it to use just one underscore, as it is usual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment