UNPKG

@darwino/darwino

Version:

A set of Javascript classes and utilities

173 lines (145 loc) 4.05 kB
/* * (c) Copyright Darwino Inc. 2014-2017. */ import {fetchJson,makeQueryString,addQueryString} from './Fetch'; import DEV_OPTIONS from "./dev"; const USE_POST = false; // For mostly debug purposes /* * JSON store cursor. */ export default class JstoreCursor { constructor() { this._params = {} this._queryString = {}; this._process = this._process.bind(this) } _stringify(s) { return typeof s==='object' ? JSON.stringify(s) : s; } queryString() { return this._queryString; } queryParams(params) { if(params) { for(let k in params) { if(params.hasOwnProperty(k)) { if(params[k]) { this._params[k] = this._stringify(params[k]) } else { delete this._params[k]; } } } } return this; } database(database) { this.database = database; return this; } store(store) { this.store = store; return this; } jsonTree(t) { if(t) this._queryString.jsontree=true else delete this._queryString.jsontree; } key(key) { return this.queryParams({key}); } query(query) { return this.queryParams({query}); } categoryCount(categorycount) { return this.queryParams({categorycount}); } params(params) { return this.queryParams({params}); } orderby(orderBy,descending) { if(Array.isArray(orderBy)) { orderBy = orderBy.join(',') } let o = {orderby:orderBy} if(descending) o.descending=true return this.queryParams(o); } ftsearch(ftsearch) { return this.queryParams({ftsearch}); } extract(extract) { return this.queryParams({extract}); } aggregator(aggregator) { return this.queryParams({aggregator}); } skip(skip) { return this.queryParams({skip}); } limit(limit) { return this.queryParams({limit}); } name(name) { return this.queryParams({name}); } _process(e) { // This might be done on the server... let r = e.json; delete e.json r.__meta = e if(r.__meta.children) { r.__meta.children = r.__meta.children.map(this._process); } return r; } fetch() { // Use POST to not have length constraints const url = this.computeUrl('/entries',USE_POST,true) const opts = this.computeOptions(); return fetchJson( url, opts ).then(json => { return json.map(this._process); }) } fetchCount() { // Use POST to not have length constraints const url = this.computeUrl('/count',USE_POST,true) const opts = this.computeOptions(); return fetchJson( url, opts ).then(json => { return json.count; }) } computeUrl(urlPart,post,queryString) { let url = `${DEV_OPTIONS.serverPrefix}$darwino-jstore/databases/${encodeURIComponent(this.database)}` if(this.store) { url += `/stores/${encodeURIComponent(this.store)}` } url += urlPart if(!post) { url = addQueryString(url,makeQueryString(this._params)) } if(queryString) { url = addQueryString(url,makeQueryString(this._queryString)) } return url; } computeOptions() { // MS Edge is picky about the presence of a body param in GET, even if empty if(USE_POST) { return { method: 'POST', body: JSON.stringify(this._params||{})}; } else { return { method: 'GET' }; } } getDataLoader() { return (num,pagesize) => { this.skip(num*pagesize) this.limit(pagesize) return this.fetch(); } } }