215 lines
7.2 KiB
JavaScript
215 lines
7.2 KiB
JavaScript
import { brand } from './util/Brand.mjs';
|
|
import { allow } from './Authorization.mjs';
|
|
import { createValidationBuilder } from './Validate.mjs';
|
|
|
|
/**
|
|
* Used to "attach" auth types to ModelField without exposing them on the builder.
|
|
*/
|
|
const __auth = Symbol('__auth');
|
|
/**
|
|
* Used by `.default()` to represent a generated field (SQL).
|
|
*/
|
|
const __generated = Symbol('__generated');
|
|
const brandName = 'modelField';
|
|
const internal = Symbol('internal');
|
|
var ModelFieldType;
|
|
(function (ModelFieldType) {
|
|
ModelFieldType["Id"] = "ID";
|
|
ModelFieldType["String"] = "String";
|
|
ModelFieldType["Integer"] = "Int";
|
|
ModelFieldType["Float"] = "Float";
|
|
ModelFieldType["Boolean"] = "Boolean";
|
|
ModelFieldType["Date"] = "AWSDate";
|
|
ModelFieldType["Time"] = "AWSTime";
|
|
ModelFieldType["DateTime"] = "AWSDateTime";
|
|
ModelFieldType["Timestamp"] = "AWSTimestamp";
|
|
ModelFieldType["Email"] = "AWSEmail";
|
|
ModelFieldType["JSON"] = "AWSJSON";
|
|
ModelFieldType["Phone"] = "AWSPhone";
|
|
ModelFieldType["Url"] = "AWSURL";
|
|
ModelFieldType["IPAddress"] = "AWSIPAddress";
|
|
})(ModelFieldType || (ModelFieldType = {}));
|
|
var ModelFieldDataType;
|
|
(function (ModelFieldDataType) {
|
|
ModelFieldDataType["String"] = "string";
|
|
ModelFieldDataType["Number"] = "number";
|
|
ModelFieldDataType["Boolean"] = "boolean";
|
|
ModelFieldDataType["Date"] = "Date";
|
|
ModelFieldDataType["JSON"] = "any";
|
|
})(ModelFieldDataType || (ModelFieldDataType = {}));
|
|
/**
|
|
* Model Field Implementation
|
|
*
|
|
* @typeParam T - holds the JS data type of the field; invoking the public methods changes this type accordingly
|
|
* @example
|
|
* string() => T = string | null
|
|
* string().array() => T = Array<string | null> | null
|
|
* string().array().required() => T = Array<string | null>
|
|
* string().required().array().required() => T = Array<string>
|
|
*
|
|
* @param fieldType - stores the GraphQL data type of the field
|
|
*/
|
|
function _field(fieldType) {
|
|
const _meta = {
|
|
lastInvokedMethod: null,
|
|
};
|
|
const data = {
|
|
fieldType,
|
|
required: false,
|
|
array: false,
|
|
arrayRequired: false,
|
|
default: undefined,
|
|
authorization: [],
|
|
validation: [],
|
|
};
|
|
const builder = {
|
|
required() {
|
|
if (_meta.lastInvokedMethod === 'array') {
|
|
data.arrayRequired = true;
|
|
}
|
|
else {
|
|
data.required = true;
|
|
}
|
|
_meta.lastInvokedMethod = 'required';
|
|
return this;
|
|
},
|
|
array() {
|
|
data.array = true;
|
|
_meta.lastInvokedMethod = 'array';
|
|
return this;
|
|
},
|
|
default(val) {
|
|
data.default = typeof val === 'undefined' ? __generated : val;
|
|
_meta.lastInvokedMethod = 'default';
|
|
return this;
|
|
},
|
|
authorization(callback) {
|
|
const { resource: _, ...rest } = allow;
|
|
const rules = callback(rest);
|
|
data.authorization = Array.isArray(rules) ? rules : [rules];
|
|
_meta.lastInvokedMethod = 'authorization';
|
|
return this;
|
|
},
|
|
validate(callback) {
|
|
const { builder, getRules } = createValidationBuilder();
|
|
callback(builder);
|
|
data.validation = getRules();
|
|
_meta.lastInvokedMethod = 'validate';
|
|
return this;
|
|
},
|
|
...brand(brandName),
|
|
[internal]() {
|
|
return this;
|
|
},
|
|
};
|
|
return { ...builder, data };
|
|
}
|
|
/**
|
|
* A unique identifier scalar type. This scalar is serialized like a String but isn't meant to be human-readable.
|
|
* If not specified on create operations, a ULID will be auto-generated service-side.
|
|
* @returns ID field definition
|
|
*/
|
|
function id() {
|
|
return _field(ModelFieldType.Id);
|
|
}
|
|
/**
|
|
* A string scalar type that is represented server-side as a UTF-8 character sequence.
|
|
* @returns string field definition
|
|
*/
|
|
function string() {
|
|
return _field(ModelFieldType.String);
|
|
}
|
|
/**
|
|
* An integer scalar type with a supported value range between -(2^31) and 2^31-1.
|
|
* @returns integer field definition
|
|
*/
|
|
function integer() {
|
|
return _field(ModelFieldType.Integer);
|
|
}
|
|
/**
|
|
* A float scalar type following represented server-side as an IEEE 754 floating point value.
|
|
* @returns float field definition
|
|
*/
|
|
function float() {
|
|
return _field(ModelFieldType.Float);
|
|
}
|
|
/**
|
|
* A boolean scalar type that can be either true or false.
|
|
* @returns boolean field definition
|
|
*/
|
|
function boolean() {
|
|
return _field(ModelFieldType.Boolean);
|
|
}
|
|
/**
|
|
* A date scalar type that is represented server-side as an extended ISO 8601 date string in the format `YYYY-MM-DD`.
|
|
* @returns date field definition
|
|
*/
|
|
function date() {
|
|
return _field(ModelFieldType.Date);
|
|
}
|
|
/**
|
|
* A time scalar type that is represented server-side as an extended ISO 8601 time string in the format `hh:mm:ss.sss`.
|
|
* @returns time field definition
|
|
*/
|
|
function time() {
|
|
return _field(ModelFieldType.Time);
|
|
}
|
|
/**
|
|
* A date time scalar type that is represented server-side as an extended ISO 8601 date and time string in the format `YYYY-MM-DDThh:mm:ss.sssZ`.
|
|
* @returns datetime field definition
|
|
*/
|
|
function datetime() {
|
|
return _field(ModelFieldType.DateTime);
|
|
}
|
|
/**
|
|
* A timestamp scalar type that is represented by an integer value of the number of seconds before or after `1970-01-01-T00:00Z`.
|
|
* @returns timestamp field definition
|
|
*/
|
|
function timestamp() {
|
|
return _field(ModelFieldType.Timestamp);
|
|
}
|
|
/**
|
|
* An email scalar type that is represented server-side in the format `local-part@domain-part` as defined by RFC 822.
|
|
* @returns email field definition
|
|
*/
|
|
function email() {
|
|
return _field(ModelFieldType.Email);
|
|
}
|
|
/**
|
|
* A JSON scalar type that is automatically parsed and loaded server-side as maps, lists, or scalar values
|
|
* rather than as the literal input strings.
|
|
* @returns JSON field definition
|
|
*/
|
|
function json() {
|
|
return _field(ModelFieldType.JSON);
|
|
}
|
|
/**
|
|
* A phone number scalar type thas is stored as a string server-side. Phone numbers can contain either spaces
|
|
* or hyphens to separate digit groups. Phone numbers without a country code are assumed to be US/North American numbers adhering
|
|
* to the North American Numbering Plan.
|
|
* @returns phone number field definition
|
|
*/
|
|
function phone() {
|
|
return _field(ModelFieldType.Phone);
|
|
}
|
|
/**
|
|
* A URL scalar type as defined by RFC 1738. For example, https://www.amazon.com/dp/B000NZW3KC/ or mailto:example@example.com.
|
|
* URLs must contain a schema (http, mailto) and can't contain two forward slashes (//) in the path part.
|
|
* @returns URL field definition
|
|
*/
|
|
function url() {
|
|
return _field(ModelFieldType.Url);
|
|
}
|
|
/**
|
|
* A valid IPv4 or IPv6 address scalar type. IPv4 addresses are expected in quad-dotted notation (123.12.34.56). IPv6 addresses
|
|
* are expected in non-bracketed, colon-separated format (1a2b:3c4b:🔢4567). You can include an optional CIDR suffix (123.45.67.89/16)
|
|
* to indicate subnet mask.
|
|
* @returns IP address field definition
|
|
*/
|
|
function ipAddress() {
|
|
return _field(ModelFieldType.IPAddress);
|
|
}
|
|
|
|
export { ModelFieldDataType, ModelFieldType, __auth, __generated, boolean, date, datetime, email, float, id, integer, ipAddress, json, phone, string, time, timestamp, url };
|
|
//# sourceMappingURL=ModelField.mjs.map
|