@graffy/pg 0.16.3-alpha.2 → 0.16.3-alpha.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.
package/index.cjs CHANGED
@@ -442,7 +442,12 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
442
442
  if (!common.isEmpty(filter))
443
443
  where.push(getSql(filter, options));
444
444
  if (!hasRangeArg)
445
- return { meta: meta(baseKey), where, limit: 1 };
445
+ return {
446
+ meta: meta(baseKey),
447
+ where,
448
+ limit: 1,
449
+ ensureSingleRow: $group === true
450
+ };
446
451
  const groupCols = Array.isArray($group) && $group.length && $group.map((prop) => lookup(prop, options));
447
452
  const group = groupCols ? join(groupCols, ", ") : void 0;
448
453
  const orderCols = ($order || [idCol]).map(
@@ -469,7 +474,8 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
469
474
  where,
470
475
  order,
471
476
  group,
472
- limit: $first || $last
477
+ limit: $first || $last,
478
+ ensureSingleRow: true
473
479
  };
474
480
  }
475
481
  function getBoundCond(orderCols, bound, kind) {
@@ -503,9 +509,23 @@ function getBoundCond(orderCols, bound, kind) {
503
509
  }
504
510
  const MAX_LIMIT = 4096;
505
511
  function selectByArgs(args, projection, options) {
506
- const { table } = options;
507
- const { where, order, group, limit, meta } = getArgSql(args, options);
512
+ const { table, idCol } = options;
513
+ const { where, order, group, limit, meta, ensureSingleRow } = getArgSql(
514
+ args,
515
+ options
516
+ );
508
517
  const clampedLimit = Math.min(MAX_LIMIT, limit || MAX_LIMIT);
518
+ if (!ensureSingleRow) {
519
+ return sql`
520
+ SELECT
521
+ ${getSelectCols(options, projection)}, ${meta}
522
+ FROM "${raw(table)}" WHERE "${raw(idCol)}" = (
523
+ SELECT "${raw(idCol)}" FROM "${raw(table)}"
524
+ ${where.length ? sql`WHERE ${join(where, " AND ")}` : empty}
525
+ LIMIT 2
526
+ )
527
+ `;
528
+ }
509
529
  return sql`
510
530
  SELECT
511
531
  ${getSelectCols(options, projection)}, ${meta}
@@ -541,7 +561,7 @@ function getSingleSql(arg, options) {
541
561
  SELECT "${raw(idCol)}"
542
562
  FROM "${raw(table)}"
543
563
  WHERE ${join(where, " AND ")}
544
- LIMIT 1
564
+ LIMIT 2
545
565
  )`,
546
566
  meta
547
567
  };
package/index.mjs CHANGED
@@ -440,7 +440,12 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
440
440
  if (!isEmpty(filter))
441
441
  where.push(getSql(filter, options));
442
442
  if (!hasRangeArg)
443
- return { meta: meta(baseKey), where, limit: 1 };
443
+ return {
444
+ meta: meta(baseKey),
445
+ where,
446
+ limit: 1,
447
+ ensureSingleRow: $group === true
448
+ };
444
449
  const groupCols = Array.isArray($group) && $group.length && $group.map((prop) => lookup(prop, options));
445
450
  const group = groupCols ? join(groupCols, ", ") : void 0;
446
451
  const orderCols = ($order || [idCol]).map(
@@ -467,7 +472,8 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
467
472
  where,
468
473
  order,
469
474
  group,
470
- limit: $first || $last
475
+ limit: $first || $last,
476
+ ensureSingleRow: true
471
477
  };
472
478
  }
473
479
  function getBoundCond(orderCols, bound, kind) {
@@ -501,9 +507,23 @@ function getBoundCond(orderCols, bound, kind) {
501
507
  }
502
508
  const MAX_LIMIT = 4096;
503
509
  function selectByArgs(args, projection, options) {
504
- const { table } = options;
505
- const { where, order, group, limit, meta } = getArgSql(args, options);
510
+ const { table, idCol } = options;
511
+ const { where, order, group, limit, meta, ensureSingleRow } = getArgSql(
512
+ args,
513
+ options
514
+ );
506
515
  const clampedLimit = Math.min(MAX_LIMIT, limit || MAX_LIMIT);
516
+ if (!ensureSingleRow) {
517
+ return sql`
518
+ SELECT
519
+ ${getSelectCols(options, projection)}, ${meta}
520
+ FROM "${raw(table)}" WHERE "${raw(idCol)}" = (
521
+ SELECT "${raw(idCol)}" FROM "${raw(table)}"
522
+ ${where.length ? sql`WHERE ${join(where, " AND ")}` : empty}
523
+ LIMIT 2
524
+ )
525
+ `;
526
+ }
507
527
  return sql`
508
528
  SELECT
509
529
  ${getSelectCols(options, projection)}, ${meta}
@@ -539,7 +559,7 @@ function getSingleSql(arg, options) {
539
559
  SELECT "${raw(idCol)}"
540
560
  FROM "${raw(table)}"
541
561
  WHERE ${join(where, " AND ")}
542
- LIMIT 1
562
+ LIMIT 2
543
563
  )`,
544
564
  meta
545
565
  };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/pg",
3
3
  "description": "The standard Postgres module for Graffy. Each instance this module mounts a Postgres table as a Graffy subtree.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.16.3-alpha.2",
5
+ "version": "0.16.3-alpha.3",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.16.3-alpha.2",
19
+ "@graffy/common": "0.16.3-alpha.3",
20
20
  "debug": "^4.3.3"
21
21
  },
22
22
  "peerDependencies": {
@@ -5,7 +5,7 @@
5
5
  @param {{prefix: string, idCol: string, verDefault: string}} options
6
6
 
7
7
  @typedef { import('sql-template-tag').Sql } Sql
8
- @return {{ meta: Sql, where: Sql[], order?: Sql, group?: Sql, limit: number }}
8
+ @return {{ meta: Sql, where: Sql[], order?: Sql, group?: Sql, limit: number, ensureSingleRow: boolean }}
9
9
  */
10
10
  export default function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $cursor: _, ...rest }: object, options: {
11
11
  prefix: string;
@@ -17,6 +17,7 @@ export default function getArgSql({ $first, $last, $after, $before, $since, $unt
17
17
  order?: Sql;
18
18
  group?: Sql;
19
19
  limit: number;
20
+ ensureSingleRow: boolean;
20
21
  };
21
22
  /**
22
23
  * Uses the args object (typically passed in the $key attribute)