@snowtop/ent 0.1.19 → 0.1.21-test1

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.21-test1",
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": {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ const ast_1 = require("../tsc/ast");
27
+ const transform_1 = require("../tsc/transform");
28
+ const typescript_1 = __importStar(require("typescript"));
29
+ class FixActionExports {
30
+ constructor() {
31
+ this.glob = "src/ent/**/actions/*_action.ts";
32
+ this.prettierGlob = "src/ent/**/actions/*_action.ts";
33
+ }
34
+ traverseChild(sourceFile, contents, file, node) {
35
+ if (!(0, typescript_1.isClassDeclaration)(node)) {
36
+ return { node };
37
+ }
38
+ // only Action classes
39
+ if (!node.name?.text.endsWith("Action")) {
40
+ return { node };
41
+ }
42
+ const modifiers = typescript_1.default.canHaveModifiers(node)
43
+ ? typescript_1.default.getModifiers(node)
44
+ : undefined;
45
+ const exported = modifiers?.filter((mod) => mod.getText(sourceFile) === "export");
46
+ const defaultExport = modifiers?.filter((mod) => mod.getText(sourceFile) === "default");
47
+ // for now, we're only supporting changing from default -> non-default
48
+ // trivial to support the other way around but don't need it yet
49
+ if (!exported?.length || !defaultExport?.length) {
50
+ return { node };
51
+ }
52
+ let classInfo = (0, ast_1.getClassInfo)(contents, sourceFile, node, {
53
+ hasExport: true,
54
+ hasDefault: false,
55
+ });
56
+ if (!classInfo) {
57
+ // somehow don't have classInfo, bye
58
+ return { node };
59
+ }
60
+ let klassContents = "";
61
+ for (const mm of node.members) {
62
+ klassContents += mm.getFullText(sourceFile);
63
+ }
64
+ return {
65
+ traversed: true,
66
+ rawString: classInfo.wrapClassContents(klassContents),
67
+ };
68
+ }
69
+ }
70
+ // ts-node-script --swc --project ./tsconfig.json -r tsconfig-paths/register ../../ts/src/scripts/fix_action_exports.ts
71
+ function main() {
72
+ const f = new FixActionExports();
73
+ (0, transform_1.transform)(f);
74
+ }
75
+ main();
@@ -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() {
package/tsc/ast.d.ts CHANGED
@@ -9,7 +9,10 @@ export interface ClassInfo {
9
9
  implements?: string[];
10
10
  wrapClassContents(inner: string): string;
11
11
  }
12
- export declare function getClassInfo(fileContents: string, sourceFile: ts.SourceFile, node: ts.ClassDeclaration): ClassInfo | undefined;
12
+ export declare function getClassInfo(fileContents: string, sourceFile: ts.SourceFile, node: ts.ClassDeclaration, override?: {
13
+ hasExport: boolean;
14
+ hasDefault: boolean;
15
+ }): ClassInfo | undefined;
13
16
  type transformImportFn = (imp: string) => string;
14
17
  interface transformOpts {
15
18
  removeImports?: string[];
package/tsc/ast.js CHANGED
@@ -36,7 +36,7 @@ function getPreText(fileContents, node, sourceFile) {
36
36
  return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
37
37
  }
38
38
  exports.getPreText = getPreText;
39
- function getClassInfo(fileContents, sourceFile, node) {
39
+ function getClassInfo(fileContents, sourceFile, node, override) {
40
40
  let className = node.name?.text;
41
41
  let classExtends;
42
42
  let impl = [];
@@ -83,7 +83,11 @@ function getClassInfo(fileContents, sourceFile, node) {
83
83
  ${inner}
84
84
  }`;
85
85
  };
86
- if (node.modifiers) {
86
+ if (override) {
87
+ hasExport = override.hasExport;
88
+ hasDefault = override.hasDefault;
89
+ }
90
+ else if (node.modifiers) {
87
91
  for (const mod of node.modifiers) {
88
92
  const text = mod.getText(sourceFile);
89
93
  if (text === "export") {
package/tsc/transform.js CHANGED
@@ -168,7 +168,7 @@ function transform(transform) {
168
168
  }
169
169
  });
170
170
  if (transform.prettierGlob) {
171
- (0, child_process_1.execSync)(`prettier ${transform.prettierGlob} --write`);
171
+ (0, child_process_1.spawnSync)(`prettier`, [transform.prettierGlob, "--write"]);
172
172
  }
173
173
  }
174
174
  exports.transform = transform;