Skip to content

Commit a2a10b9

Browse files
committed
fix: do not delegate to select on read like ever that is really dumb
1 parent 9b39309 commit a2a10b9

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

packages/backend/src/drivers/EntityStoreImplementation.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,13 @@ const _fetch_based_on_complex_id = async (self, id) => {
6767
let predicate = new And({ children: key_eqs });
6868

6969
// Perform a select
70-
const entities = await svc_es.select({ predicate });
71-
if ( entities.length === 0 ) {
70+
const entity = await svc_es.read({ predicate });
71+
if ( ! entity ) {
7272
return null;
7373
}
7474

75-
console.log('WHAT ISAGERSGAREWHGwr', entities)
76-
7775
// Ensure there is only one result
78-
return entities[0];
76+
return entity;
7977
}
8078

8179
const _fetch_based_on_either_id = async (self, uid, id) => {

packages/backend/src/om/entitystorage/SQLES.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,41 @@ class SQLES extends BaseES {
4444
}
4545
},
4646
async read (uid) {
47-
const id_prop = this.om.properties[this.om.primary_identifier];
48-
let id_col =
49-
id_prop.descriptor.sql?.column_name ?? id_prop.name;
5047

51-
console.log('CALLING READ WITH UID ', uid);
52-
// Temporary hack until multiple identifiers are supported
53-
// (allows us to query using an internal ID; users can't do this)
54-
if ( typeof uid === 'number' ) {
55-
id_col = 'id';
56-
}
48+
const [stmt_where, where_vals] = await (async () => {
49+
if ( typeof uid !== 'object' ) {
50+
const id_prop =
51+
this.om.properties[this.om.primary_identifier];
52+
let id_col =
53+
id_prop.descriptor.sql?.column_name ?? id_prop.name;
54+
// Temporary hack until multiple identifiers are supported
55+
// (allows us to query using an internal ID; users can't do this)
56+
if ( typeof uid === 'number' ) {
57+
id_col = 'id';
58+
}
59+
return [` WHERE ${id_col} = ?`, [uid]]
60+
}
61+
62+
if ( ! uid.hasOwnProperty('predicate') ) {
63+
throw new Error(
64+
'SQLES.read does not understand this input: ' +
65+
'object with no predicate property',
66+
);
67+
}
68+
let predicate = uid.predicate; // uid is actually a predicate
69+
if ( predicate instanceof Predicate ) {
70+
predicate = await this.om_to_sql_condition_(predicate);
71+
}
72+
const stmt_where = ` WHERE ${predicate.sql} LIMIT 1` ;
73+
const where_vals = predicate.values;
74+
return [stmt_where, where_vals];
75+
})();
5776

5877
const stmt =
59-
`SELECT * FROM ${this.om.sql.table_name} WHERE ${id_col} = ?`;
78+
`SELECT * FROM ${this.om.sql.table_name}${stmt_where}`;
6079

6180
const rows = await this.db.read(
62-
stmt, [uid]
81+
stmt, where_vals
6382
);
6483

6584
if ( rows.length === 0 ) {

0 commit comments

Comments
 (0)