@snowtop/ent 0.2.2 → 0.2.3-alpha1

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/core/db.js CHANGED
@@ -205,6 +205,22 @@ exports.defaultTimestampParser = pg_1.default.types.getTypeParser(pg_1.default.t
205
205
  pg_1.default.types.setTypeParser(pg_1.default.types.builtins.TIMESTAMP, function (val) {
206
206
  return luxon_1.DateTime.fromSQL(val + "Z").toJSDate();
207
207
  });
208
+ pg_1.default.types.setTypeParser(pg_1.default.types.builtins.DATE, function (val) {
209
+ return val;
210
+ });
211
+ // pg-types only exposes scalar OIDs, so we have to hard-code the array
212
+ // OIDs we care about (DATE[] and TEXT[]). If these ever change upstream,
213
+ // update the constants below.
214
+ const TEXT_ARRAY_OID = 1009;
215
+ const DATE_ARRAY_OID = 1182;
216
+ // TEXT[] already parses to string[], so reuse that parser for DATE[]
217
+ const parseTextArray = pg_1.default.types.getTypeParser(TEXT_ARRAY_OID);
218
+ pg_1.default.types.setTypeParser(DATE_ARRAY_OID, function (val) {
219
+ if (val === null) {
220
+ return null;
221
+ }
222
+ return parseTextArray(val);
223
+ });
208
224
  class Sqlite {
209
225
  constructor(db) {
210
226
  this.db = db;
@@ -1,5 +1,6 @@
1
1
  export { gqlFieldOptions, gqlObjectOptions, gqlField, gqlArgType, gqlInputObjectType, gqlObjectType, gqlQuery, gqlMutation, gqlContextType, gqlConnection, GraphQLConnection, GQLCapture, gqlFileUpload, CustomType, gqlInterfaceType, gqlUnionType, } from "./graphql";
2
2
  export { GraphQLTime } from "./scalars/time";
3
+ export { GraphQLDate } from "./scalars/date";
3
4
  export { GraphQLOrderByDirection } from "./scalars/orderby_direction";
4
5
  export { GraphQLPageInfo } from "./query/page_info";
5
6
  export { GraphQLEdge, GraphQLEdgeConnection } from "./query/edge_connection";
package/graphql/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformUnionTypes = exports.encodeGQLID = exports.mustDecodeNullableIDFromGQLID = exports.mustDecodeIDFromGQLID = exports.nodeIDEncoder = exports.resolveID = exports.clearResolvers = exports.registerResolver = exports.EntNodeResolver = exports.GraphQLEdgeInterface = exports.GraphQLConnectionInterface = exports.GraphQLNodeInterface = exports.GraphQLConnectionType = exports.GraphQLEdgeType = exports.GraphQLEdgeConnection = exports.GraphQLPageInfo = exports.GraphQLOrderByDirection = exports.GraphQLTime = exports.gqlUnionType = exports.gqlInterfaceType = exports.gqlFileUpload = exports.GQLCapture = exports.gqlConnection = exports.gqlContextType = exports.gqlMutation = exports.gqlQuery = exports.gqlObjectType = exports.gqlInputObjectType = exports.gqlArgType = exports.gqlField = void 0;
3
+ exports.transformUnionTypes = exports.encodeGQLID = exports.mustDecodeNullableIDFromGQLID = exports.mustDecodeIDFromGQLID = exports.nodeIDEncoder = exports.resolveID = exports.clearResolvers = exports.registerResolver = exports.EntNodeResolver = exports.GraphQLEdgeInterface = exports.GraphQLConnectionInterface = exports.GraphQLNodeInterface = exports.GraphQLConnectionType = exports.GraphQLEdgeType = exports.GraphQLEdgeConnection = exports.GraphQLPageInfo = exports.GraphQLOrderByDirection = exports.GraphQLDate = exports.GraphQLTime = exports.gqlUnionType = exports.gqlInterfaceType = exports.gqlFileUpload = exports.GQLCapture = exports.gqlConnection = exports.gqlContextType = exports.gqlMutation = exports.gqlQuery = exports.gqlObjectType = exports.gqlInputObjectType = exports.gqlArgType = exports.gqlField = void 0;
4
4
  var graphql_1 = require("./graphql");
5
5
  Object.defineProperty(exports, "gqlField", { enumerable: true, get: function () { return graphql_1.gqlField; } });
6
6
  Object.defineProperty(exports, "gqlArgType", { enumerable: true, get: function () { return graphql_1.gqlArgType; } });
@@ -16,6 +16,8 @@ Object.defineProperty(exports, "gqlInterfaceType", { enumerable: true, get: func
16
16
  Object.defineProperty(exports, "gqlUnionType", { enumerable: true, get: function () { return graphql_1.gqlUnionType; } });
17
17
  var time_1 = require("./scalars/time");
18
18
  Object.defineProperty(exports, "GraphQLTime", { enumerable: true, get: function () { return time_1.GraphQLTime; } });
19
+ var date_1 = require("./scalars/date");
20
+ Object.defineProperty(exports, "GraphQLDate", { enumerable: true, get: function () { return date_1.GraphQLDate; } });
19
21
  var orderby_direction_1 = require("./scalars/orderby_direction");
20
22
  Object.defineProperty(exports, "GraphQLOrderByDirection", { enumerable: true, get: function () { return orderby_direction_1.GraphQLOrderByDirection; } });
21
23
  var page_info_1 = require("./query/page_info");
@@ -0,0 +1,2 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+ export declare const GraphQLDate: GraphQLScalarType<string, string>;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GraphQLDate = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const language_1 = require("graphql/language");
6
+ const luxon_1 = require("luxon");
7
+ const DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;
8
+ function normalizeDate(input, throwErr) {
9
+ if (typeof input === "string") {
10
+ if (!DATE_REGEX.test(input)) {
11
+ throw throwErr(`Date must be in YYYY-MM-DD format: ${input}`);
12
+ }
13
+ const dt = luxon_1.DateTime.fromISO(input, { zone: "UTC" });
14
+ if (!dt.isValid) {
15
+ throw throwErr(`Invalid Date value ${input}`);
16
+ }
17
+ return dt.toISODate();
18
+ }
19
+ if (input instanceof Date) {
20
+ return luxon_1.DateTime.fromJSDate(input).toISODate();
21
+ }
22
+ if (input instanceof luxon_1.DateTime) {
23
+ if (!input.isValid) {
24
+ throw throwErr(`Invalid Date value ${input.toISO()}`);
25
+ }
26
+ return input.toISODate();
27
+ }
28
+ throw throwErr(`Date cannot represent value: ${input}`);
29
+ }
30
+ exports.GraphQLDate = new graphql_1.GraphQLScalarType({
31
+ name: "Date",
32
+ description: "Date scalar type with format YYYY-MM-DD",
33
+ serialize(value) {
34
+ return normalizeDate(value, (msg) => new graphql_1.GraphQLError(msg));
35
+ },
36
+ parseValue(value) {
37
+ return normalizeDate(value, (msg) => new graphql_1.GraphQLError(msg));
38
+ },
39
+ parseLiteral(ast) {
40
+ if (ast.kind !== language_1.Kind.STRING) {
41
+ throw new graphql_1.GraphQLError(`Date cannot represent literal: ${ast.kind}`);
42
+ }
43
+ return normalizeDate(ast.value, (msg) => new graphql_1.GraphQLError(msg));
44
+ },
45
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.2.2",
3
+ "version": "0.2.3-alpha1",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/schema/field.js CHANGED
@@ -499,11 +499,13 @@ class DateField extends BaseField {
499
499
  return val;
500
500
  }
501
501
  val = new Date(val);
502
- let yy = (0, exports.leftPad)(val.getFullYear());
502
+ // interpret as UTC to avoid timezone drift when the Date
503
+ // represents a midnight UTC value
504
+ let yy = (0, exports.leftPad)(val.getUTCFullYear());
503
505
  // lol this API
504
506
  // for some reason this is 0-index
505
- let mm = (0, exports.leftPad)(val.getMonth() + 1);
506
- let dd = (0, exports.leftPad)(val.getDate());
507
+ let mm = (0, exports.leftPad)(val.getUTCMonth() + 1);
508
+ let dd = (0, exports.leftPad)(val.getUTCDate());
507
509
  let ret = `${yy}-${mm}-${dd}`;
508
510
  return ret;
509
511
  }
@@ -359,6 +359,11 @@ async function main() {
359
359
  secondaryImportPath: "../graphql/scalars/time",
360
360
  type: "GraphQLTime",
361
361
  }, gqlCapture);
362
+ (0, graphql_1.addCustomType)({
363
+ importPath: MODULE_PATH,
364
+ secondaryImportPath: "../graphql/scalars/date",
365
+ type: "GraphQLDate",
366
+ }, gqlCapture);
362
367
  (0, graphql_1.addCustomType)({
363
368
  importPath: "graphql-type-json",
364
369
  type: "GraphQLJSON",
@@ -4,6 +4,7 @@ exports.getDefaultValue = exports.randomPhoneNumber = exports.randomEmail = void
4
4
  const uuid_1 = require("uuid");
5
5
  const schema_1 = require("../../schema");
6
6
  const schema_2 = require("../../schema");
7
+ const crypto_1 = require("crypto");
7
8
  function random() {
8
9
  return Math.random().toString(16).substring(2);
9
10
  }
@@ -13,7 +14,12 @@ function randomEmail(domain) {
13
14
  }
14
15
  exports.randomEmail = randomEmail;
15
16
  function randomPhoneNumber() {
16
- return `+1${Math.random().toString(10).substring(2, 11)}`;
17
+ // generate 12 digits from random bytes so we can safely carve out a NANP number
18
+ const raw = (0, crypto_1.randomBytes)(5).readUIntBE(0, 5).toString().padStart(12, "0");
19
+ const first = (parseInt(raw[0], 10) % 8) + 2; // ensure 2-9 for NANP area codes
20
+ const areaCode = `${first}${raw[1]}${raw[2]}`;
21
+ const localNumber = raw.slice(3, 10);
22
+ return `+1${areaCode}${localNumber}`;
17
23
  }
18
24
  exports.randomPhoneNumber = randomPhoneNumber;
19
25
  function coinFlip() {