json-fs-query 1.0.1 → 1.0.3

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
  }
@@ -1375,6 +1380,16 @@
1375
1380
  const entity = parsed.entity
1376
1381
  const joinEntity = parsed.joinEntity
1377
1382
  const joinClauses = parsed.joinClauses
1383
+ if (!dataSets[entity].length) {
1384
+ // Nothing indexed entity's own rows yet (no WHERE condition narrowed them down ahead of this -
1385
+ // that only happens via getConditionIndexedList, called just before this). The loop below can't
1386
+ // pre-resolve anything against zero rows, so leave parsed.joinClauses exactly as parsed: the
1387
+ // later, unconditional joinEntity() call (once this command's own read actually runs) still
1388
+ // needs the real clauses to do the join itself. Previously this fell through to the loop,
1389
+ // which never ran, leaving reducedJoins (and so parsed.joinClauses) empty regardless of what
1390
+ // was actually parsed - silently discarding every join clause whenever the query had no WHERE.
1391
+ return parsed
1392
+ }
1378
1393
  for (const anEntity in dataSets[entity]) {
1379
1394
  const currentEntity = dataSets[entity][anEntity]
1380
1395
  // Loop over each of the main entities
@@ -2390,14 +2405,23 @@
2390
2405
  let dataSet = {
2391
2406
  [parsed.entity]: []
2392
2407
  }
2408
+ // A condition that's indexed but genuinely matches nothing leaves dataSet[entity] at [] - the
2409
+ // same state it starts in before anything has looked at it at all. readEntity() (below) can't
2410
+ // tell those two states apart on its own (it reloads whenever the array is empty, regardless of
2411
+ // why), so capture here, before that ambiguity can happen, whether every condition actually got
2412
+ // resolved this way - if so, dataSet[entity] is already correct and must not be reloaded.
2413
+ const hadConditions = parsed.conditions.length > 0
2393
2414
  parsed = await (0, _getConditionIndexedList.getConditionIndexedList)(parsed, recordData, dataSet)
2415
+ const conditionsFullyIndexed = hadConditions && parsed.conditions.length === 0
2394
2416
  parsed = await (0, _getJoinIndexedList.getJoinIndexedList)(parsed, recordData, dataSet)
2395
2417
  // delete/update act on whatever is already in dataSet[entity] - unlike read, which loads
2396
2418
  // everything and lets the where-reduce below narrow it down afterward, they never get a second
2397
2419
  // pass. So the matching rows have to be fully resolved (indexed conditions already are, via
2398
2420
  // getConditionIndexedList above; anything left in parsed.conditions is not) before either runs.
2399
2421
  if (parsed.command === 'delete' || parsed.command === 'update') {
2400
- dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2422
+ if (!conditionsFullyIndexed) {
2423
+ dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2424
+ }
2401
2425
  dataSet[parsed.entity] = parsed.conditions.reduce((remainingData, condition) => (0, _where.where)(remainingData, condition), dataSet[parsed.entity])
2402
2426
  }
2403
2427
  switch (parsed.command) {
@@ -2408,7 +2432,9 @@
2408
2432
  dataSet = await (0, _insertEntity.insertEntity)(parsed.entity, parsed.insertValues, dataSet)
2409
2433
  break
2410
2434
  case 'read':
2411
- dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2435
+ if (!conditionsFullyIndexed) {
2436
+ dataSet = await (0, _readEntity.readEntity)(parsed.entity, dataSet)
2437
+ }
2412
2438
  break
2413
2439
  case 'update':
2414
2440
  dataSet = await (0, _updateEntity.updateEntity)(parsed.entity, parsed.updateValues, dataSet)
@@ -2416,7 +2442,7 @@
2416
2442
  default:
2417
2443
  throw new Error(`Unknown command: ${parsed.command}`)
2418
2444
  }
2419
- dataSet = await (0, _joinEntity.joinEntity)(parsed.entity, parsed.joinEntity, dataSet, parsed.joinClauses)
2445
+ dataSet = await (0, _joinEntity.joinEntity)(parsed.entity, parsed.joinEntity, dataSet, parsed.joinClauses, conditionsFullyIndexed)
2420
2446
  dataSet = await (0, _mergeJoins.mergeJoins)(dataSet, parsed.mergeJoins)
2421
2447
  // delete/update already had parsed.conditions applied above, before they ran. Re-applying the
2422
2448
  // same conditions here would be wrong, not just redundant, for update specifically: if the