@@ -6,62 +6,62 @@ var express = require('express'),
66var router = new PromiseRouter ( ) ;
77
88function mongoFieldTypeToApiResponseType ( type ) {
9- if ( type [ 0 ] === '*' ) {
10- return {
11- type : 'Pointer' ,
12- targetClass : type . slice ( 1 ) ,
13- } ;
14- }
15- if ( type . startsWith ( 'relation<' ) ) {
16- return {
17- type : 'Relation' ,
18- targetClass : type . slice ( 'relation<' . length , type . length - 1 ) ,
19- } ;
20- }
21- switch ( type ) {
22- case 'number' : return { type : 'Number' } ;
23- case 'string' : return { type : 'String' } ;
24- case 'boolean' : return { type : 'Boolean' } ;
25- case 'date' : return { type : 'Date' } ;
26- case 'object' : return { type : 'Object' } ;
27- case 'array' : return { type : 'Array' } ;
28- case 'geopoint' : return { type : 'GeoPoint' } ;
29- case 'file' : return { type : 'File' } ;
30- }
9+ if ( type [ 0 ] === '*' ) {
10+ return {
11+ type : 'Pointer' ,
12+ targetClass : type . slice ( 1 ) ,
13+ } ;
14+ }
15+ if ( type . startsWith ( 'relation<' ) ) {
16+ return {
17+ type : 'Relation' ,
18+ targetClass : type . slice ( 'relation<' . length , type . length - 1 ) ,
19+ } ;
20+ }
21+ switch ( type ) {
22+ case 'number' : return { type : 'Number' } ;
23+ case 'string' : return { type : 'String' } ;
24+ case 'boolean' : return { type : 'Boolean' } ;
25+ case 'date' : return { type : 'Date' } ;
26+ case 'object' : return { type : 'Object' } ;
27+ case 'array' : return { type : 'Array' } ;
28+ case 'geopoint' : return { type : 'GeoPoint' } ;
29+ case 'file' : return { type : 'File' } ;
30+ }
3131}
3232
3333function mongoSchemaAPIResponseFields ( schema ) {
34- fieldNames = Object . keys ( schema ) . filter ( key => key !== '_id' ) ;
35- response = { } ;
36- fieldNames . forEach ( fieldName => {
37- response [ fieldName ] = mongoFieldTypeToApiResponseType ( schema [ fieldName ] ) ;
38- } ) ;
39- response . ACL = { type : 'ACL' } ;
40- response . createdAt = { type : 'Date' } ;
41- response . updatedAt = { type : 'Date' } ;
42- response . objectId = { type : 'String' } ;
43- return response ;
34+ fieldNames = Object . keys ( schema ) . filter ( key => key !== '_id' ) ;
35+ response = { } ;
36+ fieldNames . forEach ( fieldName => {
37+ response [ fieldName ] = mongoFieldTypeToApiResponseType ( schema [ fieldName ] ) ;
38+ } ) ;
39+ response . ACL = { type : 'ACL' } ;
40+ response . createdAt = { type : 'Date' } ;
41+ response . updatedAt = { type : 'Date' } ;
42+ response . objectId = { type : 'String' } ;
43+ return response ;
4444}
4545
4646function mongoSchemaToSchemaAPIResponse ( schema ) {
47- return {
48- className : schema . _id ,
49- fields : mongoSchemaAPIResponseFields ( schema ) ,
50- } ;
47+ return {
48+ className : schema . _id ,
49+ fields : mongoSchemaAPIResponseFields ( schema ) ,
50+ } ;
5151}
5252
5353function getAllSchemas ( req ) {
54- if ( ! req . auth . isMaster ) {
55- return Promise . resolve ( {
56- status : 401 ,
57- response : { error : 'unauthorized' } ,
58- } ) ;
59- }
60- return req . config . database . collection ( '_SCHEMA' )
61- . then ( coll => coll . find ( { } ) . toArray ( ) )
62- . then ( schemas => ( { response : {
63- results : schemas . map ( mongoSchemaToSchemaAPIResponse )
64- } } ) ) ;
54+ if ( ! req . auth . isMaster ) {
55+ return Promise . resolve ( {
56+ status : 401 ,
57+ response : { error : 'unauthorized' } ,
58+ } ) ;
59+ }
60+ return req . config . database . collection ( '_SCHEMA' )
61+ . then ( coll => coll . find ( { } ) . toArray ( ) )
62+ . then ( schemas => ( { response : {
63+ results : schemas . map ( mongoSchemaToSchemaAPIResponse )
64+ } } ) ) ;
6565}
6666
6767router . route ( 'GET' , '/schemas' , getAllSchemas ) ;
0 commit comments