@@ -9,6 +9,13 @@ const parseTypeToPostgresType = type => {
9
9
case 'Date' : return 'timestamp' ;
10
10
case 'Object' : return 'jsonb' ;
11
11
case 'Boolean' : return 'boolean' ;
12
+ case 'Pointer' : return 'char(10)' ;
13
+ case 'Array' :
14
+ if ( type . contents && type . contents . type === 'String' ) {
15
+ return 'text[]' ;
16
+ } else {
17
+ throw `no type for ${ JSON . stringify ( type ) } yet` ;
18
+ }
12
19
default : throw `no type for ${ JSON . stringify ( type ) } yet` ;
13
20
}
14
21
} ;
@@ -50,7 +57,11 @@ export class PostgresStorageAdapter {
50
57
let patternsArray = [ ] ;
51
58
Object . keys ( schema . fields ) . forEach ( ( fieldName , index ) => {
52
59
valuesArray . push ( fieldName ) ;
53
- valuesArray . push ( parseTypeToPostgresType ( schema . fields [ fieldName ] ) ) ;
60
+ let parseType = schema . fields [ fieldName ] ;
61
+ if ( [ '_rperm' , '_wperm' ] . includes ( fieldName ) ) {
62
+ parseType . contents = { type : 'String' } ;
63
+ }
64
+ valuesArray . push ( parseTypeToPostgresType ( parseType ) ) ;
54
65
patternsArray . push ( `$${ index * 2 + 2 } :name $${ index * 2 + 3 } :raw` ) ;
55
66
} ) ;
56
67
return this . _client . query ( `CREATE TABLE $1:name (${ patternsArray . join ( ',' ) } )` , [ className , ...valuesArray ] )
@@ -128,7 +139,7 @@ export class PostgresStorageAdapter {
128
139
}
129
140
130
141
// Return a promise for all schemas known to this adapter, in Parse format. In case the
131
- // schemas cannot be retrieved, returns a promise that rejects. Requirements for the
142
+ // schemas cannot be retrieved, returns a promise that rejects. Rquirements for the
132
143
// rejection reason are TBD.
133
144
getAllClasses ( ) {
134
145
return this . _ensureSchemaCollectionExists ( )
@@ -143,7 +154,7 @@ export class PostgresStorageAdapter {
143
154
return this . _client . query ( 'SELECT * FROM "_SCHEMA" WHERE "className"=$<className>' , { className } )
144
155
. then ( result => {
145
156
if ( result . length === 1 ) {
146
- return result ;
157
+ return result [ 0 ] ;
147
158
} else {
148
159
throw undefined ;
149
160
}
@@ -152,24 +163,40 @@ export class PostgresStorageAdapter {
152
163
153
164
// TODO: remove the mongo format dependency
154
165
createObject ( className , schema , object ) {
155
- console . log ( object ) ;
156
166
let columnsArray = [ ] ;
157
167
let valuesArray = [ ] ;
158
168
Object . keys ( object ) . forEach ( fieldName => {
159
169
columnsArray . push ( fieldName ) ;
160
- valuesArray . push ( object [ fieldName ] ) ;
170
+ switch ( schema . fields [ fieldName ] . type ) {
171
+ case 'Date' :
172
+ valuesArray . push ( object [ fieldName ] . iso ) ;
173
+ break ;
174
+ case 'Pointer' :
175
+ valuesArray . push ( object [ fieldName ] . objectId ) ;
176
+ break ;
177
+ default :
178
+ valuesArray . push ( object [ fieldName ] ) ;
179
+ break ;
180
+ }
161
181
} ) ;
162
- let columnsPattern = columnsArray . map ( ( col , index ) => `$${ index + 2 } ~ ` ) . join ( ',' ) ;
182
+ let columnsPattern = columnsArray . map ( ( col , index ) => `$${ index + 2 } :name ` ) . join ( ',' ) ;
163
183
let valuesPattern = valuesArray . map ( ( val , index ) => `$${ index + 2 + columnsArray . length } ` ) . join ( ',' ) ;
164
184
return this . _client . query ( `INSERT INTO $1~ (${ columnsPattern } ) VALUES (${ valuesPattern } )` , [ className , ...columnsArray , ...valuesArray ] )
165
- . then ( ( ) => ( { ops : [ object ] } ) ) ;
185
+ . then ( ( ) => ( { ops : [ object ] } ) )
166
186
}
167
187
168
188
// Remove all objects that match the given Parse Query.
169
189
// If no objects match, reject with OBJECT_NOT_FOUND. If objects are found and deleted, resolve with undefined.
170
190
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
171
191
deleteObjectsByQuery ( className , schema , query ) {
172
- return Promise . reject ( 'Not implented yet.' )
192
+ return this . _client . query ( `WITH deleted AS (DELETE FROM $<className:name> RETURNING *) SELECT count(*) FROM deleted` , { className } )
193
+ . then ( result => {
194
+ if ( result [ 0 ] . count === 0 ) {
195
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Object not found.' ) ;
196
+ } else {
197
+ return result [ 0 ] . count ;
198
+ }
199
+ } ) ;
173
200
}
174
201
175
202
// Apply the update to all objects that match the given Parse Query.
@@ -190,6 +217,12 @@ export class PostgresStorageAdapter {
190
217
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
191
218
find ( className , schema , query , { skip, limit, sort } ) {
192
219
return this . _client . query ( "SELECT * FROM $<className:name>" , { className } )
220
+ . then ( results => results . map ( object => {
221
+ Object . keys ( schema . fields ) . filter ( field => schema . fields [ field ] . type === 'Pointer' ) . forEach ( fieldName => {
222
+ object [ fieldName ] = { objectId : object [ fieldName ] , __type : 'Pointer' , className : schema . fields [ fieldName ] . targetClass } ;
223
+ } ) ;
224
+ return object ;
225
+ } ) )
193
226
}
194
227
195
228
// Create a unique index. Unique indexes on nullable fields are not allowed. Since we don't
0 commit comments