Skip to content

Commit 2cafd69

Browse files
committed
Passing postgres test with user
1 parent f75c8b3 commit 2cafd69

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ const parseTypeToPostgresType = type => {
99
case 'Date': return 'timestamp';
1010
case 'Object': return 'jsonb';
1111
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+
}
1219
default: throw `no type for ${JSON.stringify(type)} yet`;
1320
}
1421
};
@@ -50,7 +57,11 @@ export class PostgresStorageAdapter {
5057
let patternsArray = [];
5158
Object.keys(schema.fields).forEach((fieldName, index) => {
5259
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));
5465
patternsArray.push(`$${index * 2 + 2}:name $${index * 2 + 3}:raw`);
5566
});
5667
return this._client.query(`CREATE TABLE $1:name (${patternsArray.join(',')})`, [className, ...valuesArray])
@@ -128,7 +139,7 @@ export class PostgresStorageAdapter {
128139
}
129140

130141
// 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
132143
// rejection reason are TBD.
133144
getAllClasses() {
134145
return this._ensureSchemaCollectionExists()
@@ -143,7 +154,7 @@ export class PostgresStorageAdapter {
143154
return this._client.query('SELECT * FROM "_SCHEMA" WHERE "className"=$<className>', { className })
144155
.then(result => {
145156
if (result.length === 1) {
146-
return result;
157+
return result[0];
147158
} else {
148159
throw undefined;
149160
}
@@ -152,24 +163,40 @@ export class PostgresStorageAdapter {
152163

153164
// TODO: remove the mongo format dependency
154165
createObject(className, schema, object) {
155-
console.log(object);
156166
let columnsArray = [];
157167
let valuesArray = [];
158168
Object.keys(object).forEach(fieldName => {
159169
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+
}
161181
});
162-
let columnsPattern = columnsArray.map((col, index) => `$${index + 2}~`).join(',');
182+
let columnsPattern = columnsArray.map((col, index) => `$${index + 2}:name`).join(',');
163183
let valuesPattern = valuesArray.map((val, index) => `$${index + 2 + columnsArray.length}`).join(',');
164184
return this._client.query(`INSERT INTO $1~ (${columnsPattern}) VALUES (${valuesPattern})`, [className, ...columnsArray, ...valuesArray])
165-
.then(() => ({ ops: [object] }));
185+
.then(() => ({ ops: [object] }))
166186
}
167187

168188
// Remove all objects that match the given Parse Query.
169189
// If no objects match, reject with OBJECT_NOT_FOUND. If objects are found and deleted, resolve with undefined.
170190
// If there is some other error, reject with INTERNAL_SERVER_ERROR.
171191
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+
});
173200
}
174201

175202
// Apply the update to all objects that match the given Parse Query.
@@ -190,6 +217,12 @@ export class PostgresStorageAdapter {
190217
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
191218
find(className, schema, query, { skip, limit, sort }) {
192219
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+
}))
193226
}
194227

195228
// Create a unique index. Unique indexes on nullable fields are not allowed. Since we don't

src/Controllers/DatabaseController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ DatabaseController.prototype.create = function(className, object, { acl } = {})
410410
.then(() => this.handleRelationUpdates(className, null, object))
411411
.then(() => schemaController.enforceClassExists(className))
412412
.then(() => schemaController.getOneSchema(className, true))
413-
.then(schema => this.adapter.createObject(className, schema, object))
413+
.then(schema => this.adapter.createObject(className, SchemaController.convertSchemaToAdapterSchema(schema), object))
414414
.then(result => sanitizeDatabaseResult(originalObject, result.ops[0]));
415415
})
416416
};

src/Controllers/SchemaController.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ const fieldTypeIsInvalid = ({ type, targetClass }) => {
223223
const convertSchemaToAdapterSchema = schema => {
224224
schema = injectDefaultSchema(schema);
225225
delete schema.fields.ACL;
226+
schema.fields._rperm = { type: 'Array' };
227+
schema.fields._wperm = { type: 'Array' };
226228

227229
if (schema.className === '_User') {
228230
delete schema.fields.password;
@@ -837,4 +839,5 @@ export {
837839
buildMergedSchemaObject,
838840
systemClasses,
839841
defaultColumns,
842+
convertSchemaToAdapterSchema,
840843
};

src/RestQuery.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,16 +526,14 @@ function findPointers(object, path) {
526526
}
527527

528528
if (typeof object !== 'object') {
529-
throw new Parse.Error(Parse.Error.INVALID_QUERY,
530-
'can only include pointer fields');
529+
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'can only include pointer fields');
531530
}
532531

533532
if (path.length == 0) {
534533
if (object.__type == 'Pointer') {
535534
return [object];
536535
}
537-
throw new Parse.Error(Parse.Error.INVALID_QUERY,
538-
'can only include pointer fields');
536+
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'can only include pointer fields');
539537
}
540538

541539
var subobject = object[path[0]];

0 commit comments

Comments
 (0)