Skip to content

JSON Path Stored Procedures - A tiny JSON processing tunnel tier library, to query and filter results with stored JS procedures

Notifications You must be signed in to change notification settings

mglazer-cengage/praetor.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#Praetor.js ####JSON Path Stored Procedures A tiny JSON processing tunnel tier library, to query and filter results with stored JS procedures

Getting started

Download Praetor.JS

<script src="//rawgit.com/magnumjs/praetor.js/master/praetor.min.js"></script>

Includes the (bundled) dependency of JSONPath

DEMO site (using Mithril.js!)

####Simple Example

JSON:

{"movies":[{"title":"American Sniper"},{"title":"Terminator"}]}
p.proc (
  getMovies (),
  ['$..title'],
  'this.results[0].reverse()'
)

Results:

  ["Terminator","American Sniper"]

####API methods Add a data store.

p.setDataStore( name, data )

Create a named Query on a given dataStore

p.setJsonQuery ( name, JSONPathQuery, storeName )

Execute JSONPath Query by name and get result

getJsonQueryResult ( name, options )

Stored Procedure

Pure JavaScript code block with query results

Is given its own context of the named query results and optional params to override defaults

Create a stored proc with a given name, named existing queries, the code to be executed and its default properties (added to the executing context)

p.setStoredProc ( name,  namedQueries , codeBody , parms )

Execute a given named stored procedure and pass in overriding default properties and it will return the results.

getStoredProcResult ( name, params )

Full Example

p.setDataStore('books',{books:[{title:'kids', author:'adams'},{title:'action', author:'johns'}]});
p.setJsonQuery('getBookTitles','$..title', 'books');
console.log(p.getJsonQueryResult('getBookTitles'))
console.log(p.getState())

var code='console.log(this.params, this.results[0]["getBookTitles"]); \
 if(this.params.upperCase){ \
 this.results[0]["getBookTitles"]=this.results[0]["getBookTitles"].map(function(x) { return x.toUpperCase(); }); \
  }';

p.setStoredProc('convertBookTitles',['getBookTitles'],code,{upperCase : false})
console.log(p.getStoredProcResult('convertBookTitles', {upperCase : true })[0]['getBookTitles'])

##Jasmine Specs http://rawgit.com/magnumjs/praetor.js/master/tests/specRunner.html

Created by Michael Glazer 3/1/2015

About

JSON Path Stored Procedures - A tiny JSON processing tunnel tier library, to query and filter results with stored JS procedures

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 94.7%
  • HTML 5.3%