@snowtop/ent 0.1.26 → 0.2.0-alpha.1

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.
@@ -0,0 +1,10 @@
1
+ export declare function toDBColumnOrTable(...strs: string[]): string;
2
+ export declare function toFilePath(s: string): string;
3
+ export declare function toFieldName(...strs: string[]): string;
4
+ export declare function toClassName(str: string): string;
5
+ interface splitResult {
6
+ s: string;
7
+ type: "lower" | "upper" | "digit" | "other";
8
+ }
9
+ export declare function _splitCamelCase(s: string): splitResult[];
10
+ export {};
package/names/names.js ADDED
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._splitCamelCase = exports.toClassName = exports.toFieldName = exports.toFilePath = exports.toDBColumnOrTable = void 0;
4
+ const camel_case_1 = require("camel-case");
5
+ const pascal_case_1 = require("pascal-case");
6
+ const snake_case_1 = require("snake-case");
7
+ // TODO tests and update functionality to match golang
8
+ function toDBColumnOrTable(...strs) {
9
+ let name = "";
10
+ for (let i = 0; i < strs.length; i++) {
11
+ const s = strs[i];
12
+ // this is to handle userIDs -> user_ids
13
+ const split = _splitCamelCase(s);
14
+ if (split.length > 2) {
15
+ const last = split[split.length - 1];
16
+ const nextLast = split[split.length - 2];
17
+ if (last.s === "s" && nextLast.type === "upper") {
18
+ // get the first n-2 words
19
+ name += (0, snake_case_1.snakeCase)(split
20
+ .map((v) => v.s)
21
+ .slice(0, split.length - 2)
22
+ .join(""));
23
+ name += "_";
24
+ // combine the last two
25
+ name += (0, snake_case_1.snakeCase)(nextLast.s);
26
+ name += last.s;
27
+ continue;
28
+ }
29
+ }
30
+ name += (0, snake_case_1.snakeCase)(s);
31
+ if (i !== strs.length - 1) {
32
+ name += "_";
33
+ }
34
+ }
35
+ return name;
36
+ }
37
+ exports.toDBColumnOrTable = toDBColumnOrTable;
38
+ function toFilePath(s) {
39
+ return (0, snake_case_1.snakeCase)(s);
40
+ }
41
+ exports.toFilePath = toFilePath;
42
+ function toFieldName(...strs) {
43
+ let name = "";
44
+ let hasDoneLower = false;
45
+ for (const s of strs) {
46
+ // same logic in toDBColumnOrTable
47
+ const split = _splitCamelCase(s);
48
+ if (split.length > 2) {
49
+ const last = split[split.length - 1];
50
+ const nextLast = split[split.length - 2];
51
+ if (last.s === "s" && nextLast.type === "upper") {
52
+ // get the first n-2 words
53
+ name += (0, camel_case_1.camelCase)(split
54
+ .map((v) => v.s)
55
+ .slice(0, split.length - 2)
56
+ .join(""));
57
+ // combine the last two
58
+ name += (0, pascal_case_1.pascalCase)(nextLast.s);
59
+ name += last.s;
60
+ hasDoneLower = true;
61
+ continue;
62
+ }
63
+ }
64
+ if (!hasDoneLower) {
65
+ name += (0, camel_case_1.camelCase)(s);
66
+ hasDoneLower = true;
67
+ }
68
+ else {
69
+ name += (0, pascal_case_1.pascalCase)(s);
70
+ }
71
+ }
72
+ return name;
73
+ }
74
+ exports.toFieldName = toFieldName;
75
+ function toClassName(str) {
76
+ return (0, pascal_case_1.pascalCase)(str);
77
+ }
78
+ exports.toClassName = toClassName;
79
+ function isUpper(s) {
80
+ for (const c of s) {
81
+ if (!(c >= "A" && c <= "Z")) {
82
+ return false;
83
+ }
84
+ }
85
+ return true;
86
+ }
87
+ function isLower(s) {
88
+ for (const c of s) {
89
+ if (!(c >= "a" && c <= "z")) {
90
+ return false;
91
+ }
92
+ }
93
+ return true;
94
+ }
95
+ function isDigit(s) {
96
+ return /^\d+$/.test(s);
97
+ }
98
+ // ported from golang in internal/names/names.go
99
+ function _splitCamelCase(s) {
100
+ const results = [];
101
+ let lastType;
102
+ let type;
103
+ for (let i = 0; i < s.length; i++) {
104
+ const c = s[i];
105
+ if (isLower(c)) {
106
+ type = "lower";
107
+ }
108
+ else if (isUpper(c)) {
109
+ type = "upper";
110
+ }
111
+ else if (isDigit(c)) {
112
+ type = "digit";
113
+ }
114
+ else {
115
+ type = "other";
116
+ }
117
+ if (lastType == type) {
118
+ results[results.length - 1].s += c;
119
+ }
120
+ else {
121
+ results.push({ s: c, type });
122
+ }
123
+ lastType = type;
124
+ }
125
+ // this is for handling the userIDs -> "user", "ID", "s" case
126
+ const isPlural = function (curr, next) {
127
+ return curr.length > 1 && next === "s";
128
+ };
129
+ for (let i = 0; i < results.length - 1; i++) {
130
+ if (isUpper(results[i].s[0]) &&
131
+ isLower(results[i + 1].s[0]) &&
132
+ !isPlural(results[i].s, results[i + 1].s)) {
133
+ // get last item from results[i] and add to front of results[i+1]
134
+ results[i + 1].s =
135
+ results[i].s[results[i].s.length - 1] + results[i + 1].s;
136
+ // now has a prefix and is not all of one case
137
+ results[i + 1].type = "other";
138
+ // remove last character from results[i]
139
+ results[i].s = results[i].s.slice(0, results[i].s.length - 1);
140
+ }
141
+ else if (isDigit(results[i].s[0]) && isLower(results[i + 1].s[0])) {
142
+ results[i].s = results[i].s + results[i + 1].s;
143
+ results[i].type = "other";
144
+ // remove
145
+ results[i + 1].s = "";
146
+ i++;
147
+ }
148
+ }
149
+ for (let i = 0; i < results.length; i++) {
150
+ if (results[i].s === "") {
151
+ results.splice(i, 1);
152
+ i--;
153
+ }
154
+ }
155
+ return results;
156
+ }
157
+ exports._splitCamelCase = _splitCamelCase;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.26",
3
+ "version": "0.2.0-alpha.1",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/schema/field.d.ts CHANGED
@@ -100,7 +100,7 @@ interface PolymorphicStringOptions extends StringOptions {
100
100
  }
101
101
  export declare class PolymorphicStringField extends StringField {
102
102
  private opts;
103
- private camelCaseVals;
103
+ private fieldNameValues;
104
104
  constructor(opts: PolymorphicStringOptions);
105
105
  validateWithFullData(val: any, b: Builder<any>): boolean;
106
106
  valid(val: any): boolean;
package/schema/field.js CHANGED
@@ -25,7 +25,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.UUIDListType = exports.IntegerEnumListType = exports.EnumListType = exports.DateListType = exports.TimetzListType = exports.TimeListType = exports.TimestamptzListType = exports.TimestampListType = exports.BooleanListType = exports.BigIntegerListType = exports.FloatListType = exports.IntegerListType = exports.IntListType = exports.StringListType = exports.ListField = exports.IntegerEnumType = exports.IntegerEnumField = exports.EnumType = exports.StringEnumField = exports.EnumField = exports.DateType = exports.DateField = exports.TimetzType = exports.TimeType = exports.TimeField = exports.leftPad = exports.TimestamptzType = exports.TimestampType = exports.TimestampField = exports.StringType = exports.PolymorphicStringField = exports.StringField = exports.BooleanType = exports.BooleanField = exports.FloatType = exports.FloatField = exports.BigIntegerType = exports.BigIntegerField = exports.IntegerType = exports.IntegerField = exports.NumberField = exports.UUIDType = exports.UUIDField = exports.BaseField = void 0;
27
27
  const luxon_1 = require("luxon");
28
- const camel_case_1 = require("camel-case");
29
28
  const types_1 = require("util/types");
30
29
  const uuid_1 = require("uuid");
31
30
  const base_1 = require("../core/base");
@@ -33,6 +32,7 @@ const db_1 = __importStar(require("../core/db"));
33
32
  const schema_1 = require("./schema");
34
33
  const global_schema_1 = require("../core/global_schema");
35
34
  const logger_1 = require("../core/logger");
35
+ const names_1 = require("../names/names");
36
36
  class BaseField {
37
37
  logValue(val) {
38
38
  if (this.sensitive) {
@@ -328,7 +328,7 @@ class PolymorphicStringField extends StringField {
328
328
  super(opts);
329
329
  this.opts = opts;
330
330
  if (opts.types) {
331
- this.camelCaseVals = opts.types.map((v) => (0, camel_case_1.camelCase)(v));
331
+ this.fieldNameValues = opts.types.map((v) => (0, names_1.toFieldName)(v));
332
332
  }
333
333
  }
334
334
  validateWithFullData(val, b) {
@@ -347,19 +347,19 @@ class PolymorphicStringField extends StringField {
347
347
  return true;
348
348
  }
349
349
  valid(val) {
350
- if (!this.camelCaseVals) {
350
+ if (!this.fieldNameValues) {
351
351
  return true;
352
352
  }
353
- let str = (0, camel_case_1.camelCase)(String(val));
353
+ let str = (0, names_1.toFieldName)(String(val));
354
354
  // allow different cases because it could be coming from different clients who don't have strong typing
355
- return this.camelCaseVals.some((value) => value === str);
355
+ return this.fieldNameValues.some((value) => value === str);
356
356
  }
357
357
  format(val) {
358
- if (!this.camelCaseVals) {
358
+ if (!this.fieldNameValues) {
359
359
  return val;
360
360
  }
361
- const converted = (0, camel_case_1.camelCase)(String(val));
362
- for (const v of this.camelCaseVals) {
361
+ const converted = (0, names_1.toFieldName)(String(val));
362
+ for (const v of this.fieldNameValues) {
363
363
  if (v === val) {
364
364
  return val;
365
365
  }
@@ -270,9 +270,6 @@ export interface SchemaConstructor {
270
270
  export type SchemaInputType = Schema | SchemaConstructor;
271
271
  export declare function getSchema(value: SchemaInputType): Schema;
272
272
  export declare function getFields(value: SchemaInputType): Map<string, Field>;
273
- /**
274
- * @deprecated should only be used by tests
275
- */
276
273
  export declare function getStorageKey(field: Field, fieldName: string): string;
277
274
  export declare function getFieldsWithPrivacy(value: SchemaInputType, fieldInfoMap: FieldInfoMap): Map<string, PrivacyPolicy>;
278
275
  export declare function getFieldsWithEditPrivacy(value: SchemaInputType, fieldInfoMap: FieldInfoMap): Map<string, PrivacyPolicy>;
package/schema/schema.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getTransformedUpdateOp = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.getFieldsForCreateAction = exports.getFieldsWithEditPrivacy = exports.getFieldsWithPrivacy = exports.getStorageKey = exports.getFields = exports.getSchema = exports.DBType = exports.SQLStatementOperation = void 0;
4
- const snake_case_1 = require("snake-case");
4
+ const names_1 = require("../names/names");
5
5
  // we also want this transformation to exist on a per-action basis
6
6
  // if it exists on an action, we don't do the global schema transformation
7
7
  var SQLStatementOperation;
@@ -79,11 +79,8 @@ function getFields(value) {
79
79
  return m;
80
80
  }
81
81
  exports.getFields = getFields;
82
- /**
83
- * @deprecated should only be used by tests
84
- */
85
82
  function getStorageKey(field, fieldName) {
86
- return field.storageKey || (0, snake_case_1.snakeCase)(fieldName);
83
+ return field.storageKey || (0, names_1.toDBColumnOrTable)(fieldName);
87
84
  }
88
85
  exports.getStorageKey = getStorageKey;
89
86
  // returns a mapping of storage key to field privacy
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StructTypeAsList = exports.StructListType = exports.StructType = exports.StructField = void 0;
4
- const camel_case_1 = require("camel-case");
5
4
  const field_1 = require("./field");
6
5
  const schema_1 = require("./schema");
7
6
  const global_schema_1 = require("../core/global_schema");
8
7
  const logger_1 = require("../core/logger");
8
+ const names_1 = require("../names/names");
9
9
  class StructField extends field_1.BaseField {
10
10
  constructor(options, jsonAsList) {
11
11
  super();
@@ -40,10 +40,14 @@ class StructField extends field_1.BaseField {
40
40
  const field = this.options.fields[k];
41
41
  // check two values
42
42
  // store in dbKey format
43
- // TODO more #510
43
+ // check both fieldName and dbKey and store in dbKey for
44
+ // serialization to db
45
+ // we should only have if it in fieldName format for non-tests
46
+ // but checking for backwards compatibility and to make
47
+ // sure we don't break anything
44
48
  let dbKey = (0, schema_1.getStorageKey)(field, k);
45
- let camelKey = (0, camel_case_1.camelCase)(k);
46
- let val = obj[camelKey];
49
+ let fieldName = (0, names_1.toFieldName)(k);
50
+ let val = obj[fieldName];
47
51
  if (val === undefined && obj[dbKey] !== undefined) {
48
52
  val = obj[dbKey];
49
53
  }
@@ -114,20 +118,19 @@ class StructField extends field_1.BaseField {
114
118
  let valid = true;
115
119
  for (const k in this.options.fields) {
116
120
  const field = this.options.fields[k];
117
- // TODO more #510
118
121
  let dbKey = (0, schema_1.getStorageKey)(field, k);
119
- let camelKey = (0, camel_case_1.camelCase)(k);
120
- let val = obj[camelKey];
122
+ let fieldName = (0, names_1.toFieldName)(k);
123
+ let val = obj[fieldName];
121
124
  let uniqueKeyField = false;
122
125
  if (this.validateUniqueKey !== undefined &&
123
- (camelKey === this.validateUniqueKey ||
126
+ (fieldName === this.validateUniqueKey ||
124
127
  k === this.validateUniqueKey ||
125
128
  dbKey === this.validateUniqueKey)) {
126
129
  // this.validateUniqueKey = camelKey;
127
130
  uniqueKeyField = true;
128
131
  }
129
132
  if (uniqueKeyField && this.checkUniqueKey) {
130
- this.validateUniqueKey = camelKey;
133
+ this.validateUniqueKey = fieldName;
131
134
  }
132
135
  if (val === undefined && obj[dbKey] !== undefined) {
133
136
  if (uniqueKeyField && this.checkUniqueKey) {
@@ -28,10 +28,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const glob = __importStar(require("glob"));
30
30
  const path = __importStar(require("path"));
31
- const pascal_case_1 = require("pascal-case");
32
31
  const minimist_1 = __importDefault(require("minimist"));
33
32
  const parse_1 = require("../parse_schema/parse");
34
33
  const ast_1 = require("../tsc/ast");
34
+ const names_1 = require("../names/names");
35
35
  function main() {
36
36
  const options = (0, minimist_1.default)(process.argv.slice(2));
37
37
  if (!options.path) {
@@ -70,7 +70,7 @@ function main() {
70
70
  if (relativePath !== undefined) {
71
71
  s.schemaPath = relativePath;
72
72
  }
73
- potentialSchemas[(0, pascal_case_1.pascalCase)(schema)] = s;
73
+ potentialSchemas[(0, names_1.toClassName)(schema)] = s;
74
74
  }
75
75
  // console.log(potentialSchemas);
76
76
  // NB: do not change this to async/await
@@ -10,12 +10,11 @@ const action_1 = require("../action");
10
10
  const schema_1 = require("../schema");
11
11
  const db_mock_1 = require("./db_mock");
12
12
  const pluralize_1 = __importDefault(require("pluralize"));
13
- const snake_case_1 = require("snake-case");
14
13
  const loaders_1 = require("../core/loaders");
15
14
  const convert_1 = require("../core/convert");
16
- const camel_case_1 = require("camel-case");
17
15
  const base_schema_1 = require("../schema/base_schema");
18
16
  const schema_2 = require("../schema/schema");
17
+ const names_1 = require("../names/names");
19
18
  class BaseEnt {
20
19
  constructor(viewer, data) {
21
20
  this.viewer = viewer;
@@ -157,7 +156,7 @@ function getSchemaName(value) {
157
156
  }
158
157
  exports.getSchemaName = getSchemaName;
159
158
  function getTableName(value) {
160
- return (0, pluralize_1.default)((0, snake_case_1.snakeCase)(value.ent.name)).toLowerCase();
159
+ return (0, pluralize_1.default)((0, names_1.toDBColumnOrTable)(value.ent.name)).toLowerCase();
161
160
  }
162
161
  exports.getTableName = getTableName;
163
162
  const getDbFields = (schema) => {
@@ -217,12 +216,12 @@ class SimpleBuilder {
217
216
  throw new Error(`no id field and multiple fields so can't deduce key. add an id field to schema`);
218
217
  }
219
218
  for (const [name, _] of fields) {
220
- key = (0, snake_case_1.snakeCase)(name);
219
+ key = (0, names_1.toDBColumnOrTable)(name);
221
220
  }
222
221
  }
223
222
  this.ent = schema.ent;
224
223
  const tableName = getTableName(schema);
225
- this.nodeType = (0, camel_case_1.camelCase)(schema.ent.name);
224
+ this.nodeType = (0, names_1.toFieldName)(schema.ent.name);
226
225
  const fieldInfo = getFieldInfo(schema);
227
226
  this.orchestrator = new orchestrator_1.Orchestrator({
228
227
  viewer: this.viewer,
@@ -270,9 +269,8 @@ class SimpleBuilder {
270
269
  this.fields.set(k, input[k]);
271
270
  }
272
271
  else {
273
- // related to #510. we do camelCase to pass fields in here but fields may be snakeCase and we want that to pass in tests
274
- // we do camelCase in
275
- const sc = (0, snake_case_1.snakeCase)(k);
272
+ // TODO: ola 2/18/2024. we may not need both anymore?
273
+ const sc = (0, names_1.toDBColumnOrTable)(k);
276
274
  if (knownFields.has(sc)) {
277
275
  this.fields.set(sc, input[k]);
278
276
  }
@@ -34,9 +34,9 @@ const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
34
34
  const config_1 = require("../../core/config");
35
35
  const fs = __importStar(require("fs"));
36
36
  const schema_1 = require("../../schema");
37
- const snake_case_1 = require("snake-case");
38
37
  const builder_1 = require("../builder");
39
38
  const test_edge_global_schema_1 = require("../test_edge_global_schema");
39
+ const names_1 = require("../../names/names");
40
40
  function primaryKey(name, cols) {
41
41
  return {
42
42
  name: name,
@@ -790,7 +790,7 @@ function storageKey(fieldName, f) {
790
790
  if (f.storageKey) {
791
791
  return f.storageKey;
792
792
  }
793
- return (0, snake_case_1.snakeCase)(fieldName);
793
+ return (0, names_1.toDBColumnOrTable)(fieldName);
794
794
  }
795
795
  function isSyncClient(client) {
796
796
  return client.execSync !== undefined;
@@ -4,7 +4,6 @@ exports.createAllEvents = exports.tempDBTables = exports.setupTempDB = exports.c
4
4
  const jest_date_mock_1 = require("jest-date-mock");
5
5
  const viewer_1 = require("../../core/viewer");
6
6
  const ent_1 = require("../../core/ent");
7
- const snake_case_1 = require("snake-case");
8
7
  const write_1 = require("../write");
9
8
  const temp_db_1 = require("../db/temp_db");
10
9
  const _1 = require(".");
@@ -15,6 +14,7 @@ const builder_1 = require("../builder");
15
14
  const action_1 = require("../../action");
16
15
  const fake_tag_1 = require("./fake_tag");
17
16
  const db_1 = require("../../core/db");
17
+ const names_1 = require("../../names/names");
18
18
  function getContactInput(user, input) {
19
19
  return {
20
20
  firstName: "Jon",
@@ -190,7 +190,7 @@ async function createEdges() {
190
190
  await (0, write_1.createRowForTest)({
191
191
  tableName: "assoc_edge_config",
192
192
  fields: {
193
- edge_table: (0, snake_case_1.snakeCase)(`${edge}_table`),
193
+ edge_table: (0, names_1.toDBColumnOrTable)(edge, "table"),
194
194
  symmetric_edge: _1.SymmetricEdges.has(edge),
195
195
  inverse_edge_type: _1.InverseEdges.get(edge) || null,
196
196
  edge_type: edge,
@@ -206,7 +206,7 @@ async function createEdges() {
206
206
  exports.createEdges = createEdges;
207
207
  function edgeTableNames() {
208
208
  const edges = Object.values(_1.EdgeType);
209
- return edges.map((edge) => (0, snake_case_1.snakeCase)(`${edge}_table`));
209
+ return edges.map((edge) => (0, names_1.toDBColumnOrTable)(edge, "table"));
210
210
  }
211
211
  exports.edgeTableNames = edgeTableNames;
212
212
  async function createTestEvent(user, input) {
@@ -33,8 +33,6 @@ class DeletedAtPattern {
33
33
  constructor() {
34
34
  this.name = "deleted_at";
35
35
  this.fields = {
36
- // need this to be lowerCamelCase because we do this based on field name
37
- // #510
38
36
  deletedAt: (0, field_1.TimestampType)({
39
37
  nullable: true,
40
38
  index: true,
@@ -95,8 +93,6 @@ class DeletedAtPatternWithExtraWrites {
95
93
  constructor() {
96
94
  this.name = "deleted_at";
97
95
  this.fields = {
98
- // need this to be lowerCamelCase because we do this based on field name
99
- // #510
100
96
  deletedAt: (0, field_1.TimestampType)({
101
97
  nullable: true,
102
98
  index: true,
@@ -36,8 +36,6 @@ class EdgeWithDeletedAt extends ent_1.AssocEdge {
36
36
  exports.EdgeWithDeletedAt = EdgeWithDeletedAt;
37
37
  exports.testEdgeGlobalSchema = {
38
38
  extraEdgeFields: {
39
- // need this to be lowerCamelCase because we do this based on field name
40
- // #510
41
39
  deletedAt: (0, schema_1.TimestampType)({
42
40
  nullable: true,
43
41
  index: true,
@@ -31,7 +31,7 @@ const typescript_1 = __importDefault(require("typescript"));
31
31
  const ast_1 = require("../tsc/ast");
32
32
  const viewer_1 = require("../core/viewer");
33
33
  const path = __importStar(require("path"));
34
- const snake_case_1 = require("snake-case");
34
+ const names_1 = require("../names/names");
35
35
  // returns input and importPath
36
36
  function getBaseFileInfo(file, classInfo, sourceFile) {
37
37
  // @ts-ignore
@@ -155,7 +155,7 @@ class TransformAction {
155
155
  klassContents += mm.getFullText(sourceFile);
156
156
  }
157
157
  }
158
- const builderPath = `src/ent/generated/${(0, snake_case_1.snakeCase)(nodeName)}/actions/${(0, snake_case_1.snakeCase)(builder)}`;
158
+ const builderPath = `src/ent/generated/${(0, names_1.toFilePath)(nodeName)}/actions/${(0, names_1.toFilePath)(builder)}`;
159
159
  let imports = new Map([
160
160
  [
161
161
  (0, ast_1.transformRelative)(file, this.customInfo.viewerInfo.path, this.customInfo.relativeImports),