@snowtop/ent 0.1.19 → 0.1.20

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,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildQuery = exports.getJoinPhrase = exports.reverseOrderBy = exports.getOrderByPhrase = void 0;
3
+ exports.buildQuery = exports.getJoinInfo = exports.reverseOrderBy = exports.getOrderByPhrase = void 0;
4
4
  function getOrderByPhrase(orderby, alias) {
5
5
  return orderby
6
6
  .map((v) => {
@@ -27,34 +27,51 @@ function reverseOrderBy(orderby) {
27
27
  });
28
28
  }
29
29
  exports.reverseOrderBy = reverseOrderBy;
30
- function getJoinPhrase(join, clauseIdx = 1) {
31
- const joinTable = join.alias
32
- ? `${join.tableName} ${join.alias}`
33
- : join.tableName;
34
- return `${joinTable} ON ${join.clause.clause(clauseIdx)}`;
30
+ function getJoinInfo(join, clauseIdx = 1) {
31
+ let valuesUsed = 0;
32
+ const str = join
33
+ .map((join) => {
34
+ const joinTable = join.alias
35
+ ? `${join.tableName} ${join.alias}`
36
+ : join.tableName;
37
+ valuesUsed += join.clause.values().length;
38
+ return `JOIN ${joinTable} ON ${join.clause.clause(clauseIdx)}`;
39
+ })
40
+ .join(" ");
41
+ return {
42
+ phrase: str,
43
+ valuesUsed,
44
+ };
35
45
  }
36
- exports.getJoinPhrase = getJoinPhrase;
46
+ exports.getJoinInfo = getJoinInfo;
37
47
  function buildQuery(options) {
38
- const fields = options.alias
39
- ? options.fields.map((f) => `${options.alias}.${f}`).join(", ")
48
+ const fieldsAlias = options.fieldsAlias ?? options.alias;
49
+ const fields = fieldsAlias && !options.disableFieldsAlias
50
+ ? options.fields.map((f) => `${fieldsAlias}.${f}`).join(", ")
40
51
  : options.fields.join(", ");
41
52
  // always start at 1
42
53
  const parts = [];
43
54
  const tableName = options.alias
44
55
  ? `${options.tableName} AS ${options.alias}`
45
56
  : options.tableName;
46
- parts.push(`SELECT ${fields} FROM ${tableName}`);
57
+ if (options.distinct) {
58
+ parts.push(`SELECT DISTINCT ${fields} FROM ${tableName}`);
59
+ }
60
+ else {
61
+ parts.push(`SELECT ${fields} FROM ${tableName}`);
62
+ }
47
63
  let whereStart = 1;
48
64
  if (options.join) {
49
- parts.push(`JOIN ${getJoinPhrase(options.join, 1)}`);
50
- whereStart += options.join.clause.values().length;
65
+ const { phrase, valuesUsed } = getJoinInfo(options.join);
66
+ parts.push(phrase);
67
+ whereStart += valuesUsed;
51
68
  }
52
69
  parts.push(`WHERE ${options.clause.clause(whereStart, options.alias)}`);
53
70
  if (options.groupby) {
54
71
  parts.push(`GROUP BY ${options.groupby}`);
55
72
  }
56
73
  if (options.orderby) {
57
- parts.push(`ORDER BY ${getOrderByPhrase(options.orderby, options.alias)}`);
74
+ parts.push(`ORDER BY ${getOrderByPhrase(options.orderby, fieldsAlias)}`);
58
75
  }
59
76
  if (options.limit) {
60
77
  parts.push(`LIMIT ${options.limit}`);
@@ -57,8 +57,7 @@ const commonTests = (opts) => {
57
57
  if (isCustomQuery(q)) {
58
58
  opts = {
59
59
  row: contacts[idx],
60
- col: "id",
61
- // keys: ["id"],
60
+ keys: ["id"],
62
61
  };
63
62
  }
64
63
  else {
@@ -66,10 +65,8 @@ const commonTests = (opts) => {
66
65
  // is from assoc_edge table id2 field and so cursor takes it from there
67
66
  opts = {
68
67
  row: contacts[idx],
69
- col: "id",
70
- cursorKey: "id2",
71
- // keys: ["id2"],
72
- // cursorKeys: ["id"],
68
+ keys: ["id2"],
69
+ cursorKeys: ["id"],
73
70
  };
74
71
  }
75
72
  return (0, ent_1.getCursor)(opts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -32,7 +32,7 @@
32
32
  "peerDependencies": {
33
33
  "@swc-node/register": "^1.6.5",
34
34
  "better-sqlite3": "^8.4.0",
35
- "graphql": "^16.5.0"
35
+ "graphql": "^16.8.1"
36
36
  },
37
37
  "peerDependenciesMeta": {
38
38
  "better-sqlite3": {
@@ -1,4 +1,4 @@
1
- import { Ent, ID, Viewer, Data, EntConstructor, PrivacyPolicy } from "../core/base";
1
+ import { Ent, ID, Viewer, Data, EntConstructor, PrivacyPolicy, LoadEntOptions } from "../core/base";
2
2
  import { Orchestrator } from "../action/orchestrator";
3
3
  import { Action, Builder, Changeset, WriteOperation, Validator, Trigger, Observer } from "../action";
4
4
  import { FieldMap, Schema } from "../schema";
@@ -15,6 +15,17 @@ export declare class BaseEnt {
15
15
  getPrivacyPolicy(): PrivacyPolicy;
16
16
  __setRawDBData(data: Data): void;
17
17
  }
18
+ export declare class AnyEnt implements Ent {
19
+ viewer: Viewer;
20
+ readonly data: Data;
21
+ nodeType: string;
22
+ readonly id: ID;
23
+ constructor(viewer: Viewer, data: Data);
24
+ getKey(): string;
25
+ getPrivacyPolicy(): PrivacyPolicy;
26
+ __setRawDBData(data: Data): void;
27
+ static loaderOptions(tableName: string, fields: string[], opts?: Partial<LoadEntOptions<AnyEnt>>): LoadEntOptions<AnyEnt>;
28
+ }
18
29
  export declare class User extends BaseEnt implements Ent {
19
30
  viewer: Viewer;
20
31
  data: Data;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SimpleAction = exports.SimpleBuilder = exports.getFieldInfo = exports.getDbFields = exports.getTableName = exports.getSchemaName = exports.getBuilderSchemaTZFromFields = exports.getBuilderSchemaFromFields = exports.getBuilderSchema = exports.EntBuilderSchema = exports.Address = exports.Message = exports.Group = exports.Contact = exports.Event = exports.User = exports.BaseEnt = void 0;
6
+ exports.SimpleAction = exports.SimpleBuilder = exports.getFieldInfo = exports.getDbFields = exports.getTableName = exports.getSchemaName = exports.getBuilderSchemaTZFromFields = exports.getBuilderSchemaFromFields = exports.getBuilderSchema = exports.EntBuilderSchema = exports.Address = exports.Message = exports.Group = exports.Contact = exports.Event = exports.User = exports.AnyEnt = exports.BaseEnt = void 0;
7
7
  const privacy_1 = require("../core/privacy");
8
8
  const orchestrator_1 = require("../action/orchestrator");
9
9
  const action_1 = require("../action");
@@ -35,6 +35,38 @@ class BaseEnt {
35
35
  }
36
36
  }
37
37
  exports.BaseEnt = BaseEnt;
38
+ class AnyEnt {
39
+ constructor(viewer, data) {
40
+ this.viewer = viewer;
41
+ this.data = data;
42
+ this.nodeType = "Any";
43
+ this.id = data[this.getKey()];
44
+ }
45
+ getKey() {
46
+ return "id";
47
+ }
48
+ getPrivacyPolicy() {
49
+ return privacy_1.AlwaysAllowPrivacyPolicy;
50
+ }
51
+ __setRawDBData(data) {
52
+ // doesn't apply here so ignore...
53
+ }
54
+ static loaderOptions(tableName, fields, opts) {
55
+ return {
56
+ tableName,
57
+ fields,
58
+ ent: AnyEnt,
59
+ loaderFactory: new loaders_1.ObjectLoaderFactory({
60
+ tableName,
61
+ fields,
62
+ key: "id",
63
+ }),
64
+ alias: tableName[0],
65
+ ...opts,
66
+ };
67
+ }
68
+ }
69
+ exports.AnyEnt = AnyEnt;
38
70
  class User extends BaseEnt {
39
71
  constructor(viewer, data) {
40
72
  super(viewer, data);
@@ -164,7 +196,8 @@ class SimpleBuilder {
164
196
  // create dynamic placeholder
165
197
  // TODO: do we need to use this as the node when there's an existingEnt
166
198
  // same for generated builders.
167
- this.placeholderID = `$ent.idPlaceholderID$ ${randomNum()}-${schema.ent?.name}`;
199
+ this.placeholderID = `$ent.idPlaceholderID$ ${randomNum()}-${schema.ent
200
+ ?.name}`;
168
201
  if (this.operation === action_1.WriteOperation.Insert) {
169
202
  for (const [key, value] of fields) {
170
203
  if (key === "id" && value === "{id}") {
@@ -523,7 +523,7 @@ class TempDB {
523
523
  await db_1.default.getInstance().endPool();
524
524
  // drop db
525
525
  await this.client.query(`DROP DATABASE ${this.db}`);
526
- // console.log(this.db);
526
+ // console.debug(this.db);
527
527
  await this.client.end();
528
528
  }
529
529
  getDB() {