json-fs-query 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -749,8 +749,13 @@
749
749
  const _readEntity = require('./readEntity')
750
750
  const _useJoinClause = require('../utilities/useJoinClause')
751
751
  function _interopRequireDefault (e) { return e && e.__esModule ? e : { default: e } }
752
- const joinEntity = async (entity, joinEntity, dataSets = {}, joinClauses = []) => {
753
- dataSets = await (0, _readEntity.readEntity)(entity, dataSets)
752
+ const joinEntity = async (entity, joinEntity, dataSets = {}, joinClauses = [], entityAlreadyResolved = false) => {
753
+ // Same ambiguity readEntity() always has (can't tell "never loaded" from "loaded, zero rows
754
+ // matched") - runQuery already resolved entity's rows via an indexed condition in that second
755
+ // case, so entityAlreadyResolved tells this call not to reload and silently discard that result.
756
+ if (!entityAlreadyResolved) {
757
+ dataSets = await (0, _readEntity.readEntity)(entity, dataSets)
758
+ }
754
759
  if (!joinClauses.length) {
755
760
  return dataSets
756
761
  }
@@ -1385,6 +1390,13 @@
1385
1390
  // was actually parsed - silently discarding every join clause whenever the query had no WHERE.
1386
1391
  return parsed
1387
1392
  }
1393
+ if (!joinClauses.length) {
1394
+ // A WHERE condition DID index entity's own rows above, but this query has no join at all -
1395
+ // joinEntity is undefined. The loop below writes dataSets[entity][anEntity][joinEntity]
1396
+ // unconditionally; left unguarded, that assigns a literal "undefined" key (object[undefined]
1397
+ // coerces to the string "undefined") onto every row, since there's no join to actually attach.
1398
+ return parsed
1399
+ }
1388
1400
  for (const anEntity in dataSets[entity]) {
1389
1401
  const currentEntity = dataSets[entity][anEntity]
1390
1402
  // Loop over each of the main entities
@@ -2400,14 +2412,23 @@
2400
2412
  let dataSet = {
2401
2413
  [parsed.entity]: []
2402
2414
  }
2415
+ // A condition that's indexed but genuinely matches nothing leaves dataSet[entity] at [] - the
2416
+ // same state it starts in before anything has looked at it at all. readEntity() (below) can't
2417
+ // tell those two states apart on its own (it reloads whenever the array is empty, regardless of
2418
+ // why), so capture here, before that ambiguity can happen, whether every condition actually got
2419
+ // resolved this way - if so, dataSet[entity] is already correct and must not be reloaded.
2420
+ const hadConditions = parsed.conditions.length > 0
2403
2421
  parsed = await (0, _getConditionIndexedList.getConditionIndexedList)(parsed, recordData, dataSet)
2422
+ const conditionsFullyIndexed = hadConditions && parsed.conditions.length === 0
2404
2423
  parsed = await (0, _getJoinIndexedList.getJoinIndexedList)(parsed, recordData, dataSet)
2405
2424
  // delete/update act on whatever is already in dataSet[entity] - unlike read, which loads
2406
2425
  // everything and lets the where-reduce below narrow it down afterward, they never get a second
2407
2426
  // pass. So the matching rows have to be fully resolved (indexed conditions already are, via
2408
2427
  // getConditionIndexedList above; anything left in parsed.conditions is not) before either runs.
2409
2428
  if (parsed.command === 'delete' || parsed.command === 'update') {
2410
- dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2429
+ if (!conditionsFullyIndexed) {
2430
+ dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2431
+ }
2411
2432
  dataSet[parsed.entity] = parsed.conditions.reduce((remainingData, condition) => (0, _where.where)(remainingData, condition), dataSet[parsed.entity])
2412
2433
  }
2413
2434
  switch (parsed.command) {
@@ -2418,7 +2439,9 @@
2418
2439
  dataSet = await (0, _insertEntity.insertEntity)(parsed.entity, parsed.insertValues, dataSet)
2419
2440
  break
2420
2441
  case 'read':
2421
- dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2442
+ if (!conditionsFullyIndexed) {
2443
+ dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2444
+ }
2422
2445
  break
2423
2446
  case 'update':
2424
2447
  dataSet = await (0, _updateEntity.updateEntity)(parsed.entity, parsed.updateValues, dataSet)
@@ -2426,7 +2449,7 @@
2426
2449
  default:
2427
2450
  throw new Error(`Unknown command: ${parsed.command}`)
2428
2451
  }
2429
- dataSet = await (0, _joinEntity.joinEntity)(parsed.entity, parsed.joinEntity, dataSet, parsed.joinClauses)
2452
+ dataSet = await (0, _joinEntity.joinEntity)(parsed.entity, parsed.joinEntity, dataSet, parsed.joinClauses, conditionsFullyIndexed)
2430
2453
  dataSet = await (0, _mergeJoins.mergeJoins)(dataSet, parsed.mergeJoins)
2431
2454
  // delete/update already had parsed.conditions applied above, before they ran. Re-applying the
2432
2455
  // same conditions here would be wrong, not just redundant, for update specifically: if the