DB API Reads
dfcaf046
garrettmills
committed
succeeded
17 changed files
DatabaseAPI.controller.js
/app/controllers/api/v1/DatabaseAPI.controller.js+122
/app/controllers/api/v1/DatabaseAPI.controller.js
Add comment 1 Plus  const Controller = require('libflitter/controller/Controller')
Add comment 2 Plus  
Add comment 3 Plus  /*
Add comment 4 Plus   * DatabaseAPI Controller
Add comment 5 Plus   * -------------------------------------------------------------
Add comment 6 Plus   * Put some description here!
Add comment 7 Plus   */
Add comment 8 Plus  class DatabaseAPI extends Controller {
Add comment 9 Plus   static get services() {
Add comment 10 Plus   return [...super.services, 'models', 'utility']
Add comment 11 Plus   }
Add comment 12 Plus  
Add comment 13 Plus   async databases(req, res, next) {
Add comment 14 Plus   const Database = this.models.get('api:db:Database')
Add comment 15 Plus   const dbs = await Database.visible_by_user(req.user)
Add comment 16 Plus   return res.api(dbs.map(x => x.to_api_object()))
Add comment 17 Plus   }
Add comment 18 Plus  
Add comment 19 Plus   async get_database(req, res, next) {
Add comment 20 Plus   return res.api(req.form.database.to_api_object())
Add comment 21 Plus   }
Add comment 22 Plus  
Add comment 23 Plus   async get_columns(req, res, next) {
Add comment 24 Plus   const db = req.form.database
Add comment 25 Plus   const cols = (await db.get_columns()).map(x => x.to_api_object())
Add comment 26 Plus   return res.api(cols)
Add comment 27 Plus   }
Add comment 28 Plus  
Add comment 29 Plus   async get_columns_order(req, res, next) {
Add comment 30 Plus   return res.api(req.form.database.ColumnIds)
Add comment 31 Plus   }
Add comment 32 Plus  
Add comment 33 Plus   async get_data(req, res, next) {
Add comment 34 Plus   const DBEntry = this.models.get('api:db:DBEntry')
Add comment 35 Plus   const db = req.form.database
Add comment 36 Plus   const cursor = await DBEntry.cursor({DatabaseId: db.UUID})
Add comment 37 Plus  
Add comment 38 Plus   if ( req.query.sort ) {
Add comment 39 Plus   if ( this.utility.is_json(req.query.sort) ) {
Add comment 40 Plus   const sort = JSON.parse(req.query.sort)
Add comment 41 Plus   if ( typeof sort !== 'object' ) return res.status(400).message('Invalid sort field. Should be JSON object.').api()
Add comment 42 Plus   const sort_obj = {}
Add comment 43 Plus   for ( const field in sort ) {
Add comment 44 Plus   if ( !sort.hasOwnProperty(field) ) continue
Add comment 45 Plus   sort_obj[`RowData.${field}`] = (Number(sort[field]) < 0 ? -1 : 1)
Add comment 46 Plus   }
Add comment 47 Plus   cursor.sort(sort_obj)
Add comment 48 Plus   } else if ( req.query.sort ) {
Add comment 49 Plus   const sort_obj = {}
Add comment 50 Plus   sort_obj[`RowData.${req.query.sort}`] = (req.query.reverse ? -1 : 1)
Add comment 51 Plus   cursor.sort(sort_obj)
Add comment 52 Plus   }
Add comment 53 Plus   }
Add comment 54 Plus  
Add comment 55 Plus   if ( req.query.limit ) {
Add comment 56 Plus   const limit = Number(req.query.limit)
Add comment 57 Plus   if ( !isNaN(limit) ) cursor.limit(limit)
Add comment 58 Plus   }
Add comment 59 Plus  
Add comment 60 Plus   if ( req.query.skip ) {
Add comment 61 Plus   const skip = Number(req.query.skip)
Add comment 62 Plus   if ( !isNaN(skip) ) cursor.skip(skip)
Add comment 63 Plus   }
Add comment 64 Plus  
Add comment 65 Plus   if ( req.query.where ) {
Add comment 66 Plus   if ( !this.utility.is_json(req.query.where) ) {
Add comment 67 Plus   return res.status(400).message('Invalid where field. Should be JSON object.').api()
Add comment 68 Plus   }
Add comment 69 Plus  
Add comment 70 Plus   const wheres = JSON.parse(req.query.where)
Add comment 71 Plus  
Add comment 72 Plus   if ( typeof wheres !== 'object' ) {
Add comment 73 Plus   return res.status(400).message('Invalid where field. Should be JSON object.').api()
Add comment 74 Plus   }
Add comment 75 Plus  
Add comment 76 Plus   const wheres_object = {}
Add comment 77 Plus   for ( const field in wheres ) {
Add comment 78 Plus   if ( !wheres.hasOwnProperty(field) ) continue
Add comment 79 Plus   const value = wheres[field]
Add comment 80 Plus   if ( typeof value !== 'object' ) {
Add comment 81 Plus   wheres_object[`RowData.${field}`] = value
Add comment 82 Plus   } else {
Add comment 83 Plus   const sub_where = {}
Add comment 84 Plus   }
Add comment 85 Plus   }
Add comment 86 Plus   }
Add comment 87 Plus  
Add comment 88 Plus   const data = (await DBEntry.from_cursor(cursor)).map(x => {
Add comment 89 Plus   x = x.to_api_object()
Add comment 90 Plus   if ( req.query.flatten ) {
Add comment 91 Plus   x = {
Add comment 92 Plus   uuid: x.uuid,
Add comment 93 Plus   database_id: x.database_id,
Add comment 94 Plus   ...x.data,
Add comment 95 Plus   }
Add comment 96 Plus   }
Add comment 97 Plus   return x
Add comment 98 Plus   })
Add comment 99 Plus   return res.api(data)
Add comment 100 Plus   }
Add comment 101 Plus  
Add comment 102 Plus   async get_record(req, res, next) {
Add comment 103 Plus   const DBEntry = this.models.get('api:db:DBEntry')
Add comment 104 Plus   const db = req.form.database
Add comment 105 Plus   const record = await DBEntry.findOne({UUID: req.params.record_id, DatabaseId: db.UUID})
Add comment 106 Plus   if ( record ) {
Add comment 107 Plus   const api_obj = record.to_api_object()
Add comment 108 Plus   if ( req.query.flatten ) {
Add comment 109 Plus   return res.api({
Add comment 110 Plus   uuid: api_obj.uuid,
Add comment 111 Plus   database_id: api_obj.database_id,
Add comment 112 Plus   ...api_obj.data,
Add comment 113 Plus   })
Add comment 114 Plus   }
Add comment 115 Plus   return res.api(api_obj)
Add comment 116 Plus   }
Add comment 117 Plus   else return res.status(404).message('Database record not found with that ID.').api()
Add comment 118 Plus   }
Add comment 119 Plus  }
Add comment 120 Plus  
Add comment 121 Plus  module.exports = exports = DatabaseAPI
Add comment 122 Plus  
Misc.controller.js
/app/controllers/api/v1/Misc.controller.js
/app/controllers/api/v1/Misc.controller.js
ColumnDef.model.js
/app/models/api/db/ColumnDef.model.js
/app/models/api/db/ColumnDef.model.js
Database.model.js
/app/models/api/db/Database.model.js
/app/models/api/db/Database.model.js
DBEntry.model.js
/app/models/api/db/DBEntry.model.js
/app/models/api/db/DBEntry.model.js
Node.model.js
/app/models/api/Node.model.js
/app/models/api/Node.model.js
Page.model.js
/app/models/api/Page.model.js
/app/models/api/Page.model.js
Token.model.js
/app/models/api/Token.model.js
/app/models/api/Token.model.js
Valid.scope.js
/app/models/scopes/Valid.scope.js
/app/models/scopes/Valid.scope.js
BearerToken.middleware.js
/app/routing/middleware/api/auth/BearerToken.middleware.js
/app/routing/middleware/api/auth/BearerToken.middleware.js
DatabaseRoute.middleware.js
/app/routing/middleware/api/DatabaseRoute.middleware.js
/app/routing/middleware/api/DatabaseRoute.middleware.js
database.routes.js
/app/routing/routers/api/v1/database.routes.js
/app/routing/routers/api/v1/database.routes.js
v1.routes.js
/app/routing/routers/api/v1.routes.js
/app/routing/routers/api/v1.routes.js
auth.config.js
/config/auth.config.js
/config/auth.config.js
example.env
/example.env
/example.env
package.json
/package.json
/package.json
yarn.lock
/yarn.lock
/yarn.lock