@snowtop/ent 0.1.24 → 0.1.26

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.
@@ -1,7 +1,7 @@
1
- import { Queryer, SyncQueryer } from "../core/db";
2
- import { Viewer, Ent, ID, Data, DataOptions, EditRowOptions, LoadEntOptions, Context, CreateRowOptions } from "../core/base";
1
+ import { Builder, WriteOperation } from "../action";
3
2
  import { Executor } from "../action/action";
4
- import { WriteOperation, Builder } from "../action";
3
+ import { Context, CreateRowOptions, Data, DataOptions, EditRowOptions, Ent, ID, LoadEntOptions, Viewer } from "../core/base";
4
+ import { Queryer, SyncQueryer } from "../core/db";
5
5
  import { AssocEdgeData, parameterizedQueryOptions } from "../core/ent";
6
6
  export interface UpdatedOperation<TEnt extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
7
7
  operation: WriteOperation;
@@ -24,12 +24,12 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.ConditionalNodeOperation = exports.ConditionalOperation = exports.EdgeOperation = exports.EditNodeOperation = exports.NoOperation = exports.RawQueryOperation = exports.DeleteNodeOperation = void 0;
27
- const clause = __importStar(require("../core/clause"));
28
27
  const action_1 = require("../action");
29
- const schema_1 = require("../schema/schema");
30
- const global_schema_1 = require("../core/global_schema");
28
+ const clause = __importStar(require("../core/clause"));
31
29
  const ent_1 = require("../core/ent");
30
+ const global_schema_1 = require("../core/global_schema");
32
31
  const query_impl_1 = require("../core/query_impl");
32
+ const schema_1 = require("../schema/schema");
33
33
  class DeleteNodeOperation {
34
34
  constructor(id, builder, options) {
35
35
  this.id = id;
@@ -224,7 +224,14 @@ class EditNodeOperation {
224
224
  }
225
225
  getReturning() {
226
226
  if (this.options.loadEntOptions.fields.length) {
227
- return `RETURNING ${this.options.loadEntOptions.fields.join(",")}`;
227
+ return `RETURNING ${this.options.loadEntOptions.fields
228
+ .map((f) => {
229
+ if (typeof f === "object") {
230
+ return `${f.alias}.${f.column}`;
231
+ }
232
+ return f;
233
+ })
234
+ .join(",")}`;
228
235
  }
229
236
  return `RETURNING *`;
230
237
  }
package/core/base.d.ts CHANGED
@@ -67,7 +67,10 @@ export interface DataOptions {
67
67
  context?: Context;
68
68
  }
69
69
  export interface SelectBaseDataOptions extends DataOptions {
70
- fields: string[];
70
+ fields: (string | {
71
+ alias: string;
72
+ column: string;
73
+ })[];
71
74
  fieldsAlias?: string;
72
75
  disableFieldsAlias?: boolean;
73
76
  disableDefaultOrderByAlias?: boolean;
package/core/clause.js CHANGED
@@ -591,7 +591,7 @@ function ArrayNotEq(col, value) {
591
591
  exports.ArrayNotEq = ArrayNotEq;
592
592
  function Eq(col, value, overrideAlias) {
593
593
  return new simpleClause(col, value, "=", {
594
- handleNull: new isNullClause(col),
594
+ handleNull: new isNullClause(col, overrideAlias),
595
595
  overrideAlias,
596
596
  });
597
597
  }
@@ -870,7 +870,7 @@ exports.JSONPathValuePredicate = JSONPathValuePredicate;
870
870
  function JSONKeyExists(dbCol, val, overrideAlias) {
871
871
  return new simpleClause(dbCol, val, "?", {
872
872
  // TODO ola: does isNullClause make sense here???
873
- handleNull: new isNullClause(dbCol),
873
+ handleNull: new isNullClause(dbCol, overrideAlias),
874
874
  overrideAlias,
875
875
  });
876
876
  }
@@ -952,35 +952,35 @@ exports.PaginationMultipleColsQuery = PaginationMultipleColsQuery;
952
952
  // These 5 are used on the RHS of an expression
953
953
  function Add(col, value, overrideAlias) {
954
954
  return new simpleClause(col, value, "+", {
955
- handleNull: new isNullClause(col),
955
+ handleNull: new isNullClause(col, overrideAlias),
956
956
  overrideAlias,
957
957
  });
958
958
  }
959
959
  exports.Add = Add;
960
960
  function Subtract(col, value, overrideAlias) {
961
961
  return new simpleClause(col, value, "-", {
962
- handleNull: new isNullClause(col),
962
+ handleNull: new isNullClause(col, overrideAlias),
963
963
  overrideAlias,
964
964
  });
965
965
  }
966
966
  exports.Subtract = Subtract;
967
967
  function Multiply(col, value, overrideAlias) {
968
968
  return new simpleClause(col, value, "*", {
969
- handleNull: new isNullClause(col),
969
+ handleNull: new isNullClause(col, overrideAlias),
970
970
  overrideAlias,
971
971
  });
972
972
  }
973
973
  exports.Multiply = Multiply;
974
974
  function Divide(col, value, overrideAlias) {
975
975
  return new simpleClause(col, value, "/", {
976
- handleNull: new isNullClause(col),
976
+ handleNull: new isNullClause(col, overrideAlias),
977
977
  overrideAlias,
978
978
  });
979
979
  }
980
980
  exports.Divide = Divide;
981
981
  function Modulo(col, value, overrideAlias) {
982
982
  return new simpleClause(col, value, "%", {
983
- handleNull: new isNullClause(col),
983
+ handleNull: new isNullClause(col, overrideAlias),
984
984
  overrideAlias,
985
985
  });
986
986
  }
package/core/context.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import { Viewer, Data, Loader, LoaderWithLoadMany, QueryOptions } from "./base";
3
2
  import { IncomingMessage, ServerResponse } from "http";
3
+ import { Data, Loader, LoaderWithLoadMany, QueryOptions, Viewer } from "./base";
4
4
  import { Context } from "./base";
5
5
  export interface RequestContext<TViewer extends Viewer = Viewer> extends Context<TViewer> {
6
6
  authViewer(viewer: TViewer): Promise<void>;
package/core/context.js CHANGED
@@ -39,7 +39,14 @@ class ContextCache {
39
39
  // maybe we just want to store sql queries???
40
40
  getkey(options) {
41
41
  let parts = [
42
- options.fields.join(","),
42
+ options.fields
43
+ .map((f) => {
44
+ if (typeof f === "object") {
45
+ return `${f.alias}.${f.column}`;
46
+ }
47
+ return f;
48
+ })
49
+ .join(","),
43
50
  options.clause.instanceKey(),
44
51
  ];
45
52
  if (options.orderby) {
package/core/ent.d.ts CHANGED
@@ -1,7 +1,7 @@
1
+ import { Context, CreateRowOptions, Data, DataOptions, EdgeQueryableDataOptions, EditRowOptions, Ent, ID, LoadCustomEntOptions, LoadEntOptions, LoadRowOptions, LoadRowsOptions, LoaderWithLoadMany, QueryDataOptions, SelectCustomDataOptions, Viewer } from "./base";
1
2
  import { Queryer, SyncQueryer } from "./db";
2
- import { Viewer, Ent, ID, LoadRowsOptions, LoadRowOptions, Data, DataOptions, EditRowOptions, LoadEntOptions, LoadCustomEntOptions, EdgeQueryableDataOptions, Context, CreateRowOptions, QueryDataOptions, SelectCustomDataOptions, LoaderWithLoadMany } from "./base";
3
- import * as clause from "./clause";
4
3
  import DataLoader from "dataloader";
4
+ import * as clause from "./clause";
5
5
  import { OrderBy } from "./query_impl";
6
6
  declare class entCacheMap<TViewer extends Viewer, TEnt extends Ent<TViewer>> {
7
7
  private viewer;
@@ -103,7 +103,10 @@ interface GroupQueryOptions<T extends Data, K = keyof T> {
103
103
  tableName: string;
104
104
  clause?: clause.Clause<T, K>;
105
105
  groupColumn: K;
106
- fields: K[];
106
+ fields: (K | {
107
+ alias: string;
108
+ column: K;
109
+ })[];
107
110
  values: any[];
108
111
  orderby?: OrderBy;
109
112
  limit: number;
package/core/ent.js CHANGED
@@ -30,12 +30,12 @@ exports.applyPrivacyPolicyForRow = exports.loadNodesByEdge = exports.loadTwoWayE
30
30
  exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = void 0;
31
31
  const db_1 = __importStar(require("./db"));
32
32
  const privacy_1 = require("./privacy");
33
- const clause = __importStar(require("./clause"));
34
- const logger_1 = require("./logger");
35
33
  const dataloader_1 = __importDefault(require("dataloader"));
34
+ const clause = __importStar(require("./clause"));
36
35
  const global_schema_1 = require("./global_schema");
37
- const query_impl_1 = require("./query_impl");
38
36
  const loader_1 = require("./loaders/loader");
37
+ const logger_1 = require("./logger");
38
+ const query_impl_1 = require("./query_impl");
39
39
  class entCacheMap {
40
40
  constructor(viewer, options) {
41
41
  this.viewer = viewer;
@@ -638,8 +638,19 @@ function buildGroupQuery(options) {
638
638
  }
639
639
  // window functions work in sqlite!
640
640
  // https://www.sqlite.org/windowfunctions.html
641
+ const fieldString = fields
642
+ .map((f) => {
643
+ if (typeof f === "object") {
644
+ // TS doesn't understand that K can only be a string, so we need
645
+ // for it to treat f as the object we know it is.
646
+ const fObj = f;
647
+ return `${fObj.alias}.${fObj.column}`;
648
+ }
649
+ return f;
650
+ })
651
+ .join(",");
641
652
  return [
642
- `SELECT * FROM (SELECT ${fields.join(",")} OVER (PARTITION BY ${options.groupColumn} ${orderby}) as row_num FROM ${options.tableName} WHERE ${cls.clause(1)}) t WHERE row_num <= ${options.limit}`,
653
+ `SELECT * FROM (SELECT ${fieldString} OVER (PARTITION BY ${options.groupColumn} ${orderby}) as row_num FROM ${options.tableName} WHERE ${cls.clause(1)}) t WHERE row_num <= ${options.limit}`,
643
654
  cls,
644
655
  ];
645
656
  }
@@ -1,7 +1,7 @@
1
- import { Context, EdgeQueryableDataOptions, Loader, LoaderFactory, Data } from "../base";
1
+ import { Context, Data, EdgeQueryableDataOptions, Loader, LoaderFactory } from "../base";
2
2
  import * as clause from "../clause";
3
- import { ObjectLoaderFactory } from "./object_loader";
4
3
  import { OrderBy } from "../query_impl";
4
+ import { ObjectLoaderFactory } from "./object_loader";
5
5
  declare class QueryDirectLoader<K extends any> implements Loader<K, Data[]> {
6
6
  private options;
7
7
  private queryOptions?;
@@ -14,7 +14,10 @@ declare class QueryDirectLoader<K extends any> implements Loader<K, Data[]> {
14
14
  clearAll(): void;
15
15
  }
16
16
  interface QueryOptions {
17
- fields: string[];
17
+ fields: (string | {
18
+ alias: string;
19
+ column: string;
20
+ })[];
18
21
  tableName: string;
19
22
  groupCol?: string;
20
23
  clause?: clause.Clause;
@@ -28,11 +28,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.QueryLoaderFactory = void 0;
30
30
  const dataloader_1 = __importDefault(require("dataloader"));
31
- const ent_1 = require("../ent");
31
+ const memoizee_1 = __importDefault(require("memoizee"));
32
32
  const clause = __importStar(require("../clause"));
33
+ const ent_1 = require("../ent");
33
34
  const logger_1 = require("../logger");
34
35
  const loader_1 = require("./loader");
35
- const memoizee_1 = __importDefault(require("memoizee"));
36
36
  function getOrderByLocal(options, queryOptions) {
37
37
  return (options.orderby ??
38
38
  queryOptions?.orderby ?? [
@@ -67,15 +67,15 @@ class CustomClauseQuery extends query_1.BaseEdgeQuery {
67
67
  // sqlite needs as count otherwise it returns count(1)
68
68
  let fields = ["count(1) as count"];
69
69
  if (this.options.joinBETA) {
70
- const requestedFields = this.options.loadEntOptions.fields;
70
+ const firstRequestedField = this.options.loadEntOptions.fields[0];
71
71
  const alias = this.options.loadEntOptions.fieldsAlias ??
72
72
  this.options.loadEntOptions.alias;
73
- if (alias) {
74
- fields = [`count(distinct ${alias}.${requestedFields[0]}) as count`];
75
- }
76
- else {
77
- fields = [`count(distinct ${requestedFields[0]}) as count`];
78
- }
73
+ const fieldString = typeof firstRequestedField === "object"
74
+ ? `${firstRequestedField.alias}.${firstRequestedField.column}`
75
+ : alias
76
+ ? `${alias}.${firstRequestedField}`
77
+ : firstRequestedField;
78
+ fields = [`count(distinct ${fieldString}) as count`];
79
79
  }
80
80
  const row = await (0, ent_1.loadRow)({
81
81
  ...this.options.loadEntOptions,
@@ -47,9 +47,20 @@ function getJoinInfo(join, clauseIdx = 1) {
47
47
  exports.getJoinInfo = getJoinInfo;
48
48
  function buildQuery(options) {
49
49
  const fieldsAlias = options.fieldsAlias ?? options.alias;
50
- const fields = fieldsAlias && !options.disableFieldsAlias
51
- ? options.fields.map((f) => `${fieldsAlias}.${f}`).join(", ")
52
- : options.fields.join(", ");
50
+ const fields = options.fields
51
+ .map((f) => {
52
+ if (typeof f === "object") {
53
+ if (!options.disableFieldsAlias) {
54
+ return `${f.alias}.${f.column}`;
55
+ }
56
+ return f.column;
57
+ }
58
+ if (fieldsAlias && !options.disableFieldsAlias) {
59
+ return `${fieldsAlias}.${f}`;
60
+ }
61
+ return f;
62
+ })
63
+ .join(", ");
53
64
  // always start at 1
54
65
  const parts = [];
55
66
  const tableName = options.alias
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",