@journeyapps/cloudcode-build
Advanced tools
Comparing version 1.9.6 to 1.9.7-dev.00b908a.e8c9b44
@@ -1,2 +0,8 @@ | ||
import { Schema } from '@journeyapps/parser-schema'; | ||
import { Type, Schema } from '@journeyapps/parser-schema'; | ||
export declare function mapType(type: Type): any; | ||
export declare function schemaToDefinitions(schema: Schema): string; | ||
export declare function typeRef(modelName: string): string; | ||
export declare function modelInterfaceName(name: string): string; | ||
export declare function isValidProperty(name: string): boolean; | ||
export declare function safeProperty(name: string): string; | ||
export declare const CONTEXTUAL_KEYWORDS: string[]; |
@@ -19,3 +19,6 @@ "use strict"; | ||
function mapType(type) { | ||
if (type.name in TYPE_MAP) { | ||
if (type == null) { | ||
return 'any'; | ||
} | ||
else if (type.name in TYPE_MAP) { | ||
return TYPE_MAP[type.name]; | ||
@@ -27,2 +30,3 @@ } | ||
} | ||
exports.mapType = mapType; | ||
function indentation(levels) { | ||
@@ -62,3 +66,3 @@ let r = ''; | ||
let attribute = model.attributes[attributeName]; | ||
attrs.push(`/** ${attribute.label} (${attribute.sourceTypeName}) */`, `${attributeName}: ${mapType(attribute.type)};`); | ||
attrs.push(`/** ${attribute.label} (${attribute.sourceTypeName}) */`, `${safeProperty(attributeName)}: ${mapType(attribute.type)};`); | ||
if (attribute.type.name == 'attachment') { | ||
@@ -70,10 +74,10 @@ attrs.push(`/** ID of ${attribute.label}. */`, `readonly ${attributeName}_id: string;`); | ||
let rel = model.belongsTo[relName]; | ||
attrs.push(`/** Lookup related ${rel.foreignType.label}. */`, `${relName}(): Promise<DB.${rel.foreignType.name}>;`, `/** Set related ${rel.foreignType.label}. */`, `${relName}(value: DB.${rel.foreignType.name}): void;`, `/** ID of related ${rel.foreignType.label}. */`, `${relName}_id: string;`); | ||
attrs.push(`/** Lookup related ${rel.foreignType.label}. */`, `${safeProperty(relName)}(): Promise<${typeRef(rel.foreignType.name)}>;`, `/** Set related ${rel.foreignType.label}. */`, `${safeProperty(relName)}(value: ${typeRef(rel.foreignType.name)}): void;`, `/** ID of related ${rel.foreignType.label}. */`, `${safeProperty(relName + '_id')}: string;`); | ||
} | ||
for (let relName in model.hasMany) { | ||
let rel = model.hasMany[relName]; | ||
attrs.push(`/** Query for related ${rel.objectType.label}. */`, `readonly ${relName}: Query<DB.${rel.objectType.name}>;`); | ||
attrs.push(`/** Query for related ${rel.objectType.label}. */`, `readonly ${safeProperty(relName)}: Query<${typeRef(rel.objectType.name)}>;`); | ||
} | ||
const modelTs = ` | ||
interface ${modelName} extends DatabaseObject { | ||
interface ${modelInterfaceName(modelName)} extends DatabaseObject { | ||
${indent(attrs)} | ||
@@ -92,3 +96,3 @@ } | ||
interface DB extends Database { | ||
${indent(modelNames.map(name => `${name}: Collection<DB.${name}>;`))} | ||
${indent(modelNames.map(name => `${JSON.stringify(name)}: Collection<${typeRef(name)}>;`))} | ||
} | ||
@@ -109,2 +113,109 @@ | ||
exports.schemaToDefinitions = schemaToDefinitions; | ||
function typeRef(modelName) { | ||
return `DB.${modelInterfaceName(modelName)}`; | ||
} | ||
exports.typeRef = typeRef; | ||
function modelInterfaceName(name) { | ||
// If the name is a reserved word, we prepend an underscore to the type name. | ||
// This only applies to the type, not the model. | ||
// Effectively we'd get `var c: DB._class = DB.class.create()` | ||
if (TYPE_BLACKLIST[name]) { | ||
return `_${name}`; | ||
} | ||
return name; | ||
} | ||
exports.modelInterfaceName = modelInterfaceName; | ||
const PROPERTY_REGEXP = /^(?![0-9])[a-zA-Z0-9$_]+$/; | ||
function isValidProperty(name) { | ||
return PROPERTY_REGEXP.test(name); | ||
} | ||
exports.isValidProperty = isValidProperty; | ||
function safeProperty(name) { | ||
// We may get invalid property names. In that case the datamodel should have validation errors, but we still | ||
// attempt to handle them gracefully here, by wrapping them in quotes. | ||
if (isValidProperty(name)) { | ||
return name; | ||
} | ||
else { | ||
return JSON.stringify(name); | ||
} | ||
} | ||
exports.safeProperty = safeProperty; | ||
// From: https://github.com/Microsoft/TypeScript/issues/2536 | ||
const RESERVED_WORDS = [ | ||
'break', | ||
'case', | ||
'catch', | ||
'class', | ||
'const', | ||
'continue', | ||
'debugger', | ||
'default', | ||
'delete', | ||
'do', | ||
'else', | ||
'enum', | ||
'export', | ||
'extends', | ||
'false', | ||
'finally', | ||
'for', | ||
'function', | ||
'if', | ||
'import', | ||
'in', | ||
'instanceof', | ||
'new', | ||
'null', | ||
'return', | ||
'super', | ||
'switch', | ||
'this', | ||
'throw', | ||
'true', | ||
'try', | ||
'typeof', | ||
'var', | ||
'void', | ||
'while', | ||
'with' | ||
]; | ||
// These currently appear to work fine as interface names. | ||
// Listed here for reference only | ||
const STRICT_RESERVED_WORDS = [ | ||
'as', | ||
'implements', | ||
'interface', | ||
'let', | ||
'package', | ||
'private', | ||
'protected', | ||
'public', | ||
'static', | ||
'yield' | ||
]; | ||
// Some of these appear to work as interface names, but we blacklist them all. | ||
exports.CONTEXTUAL_KEYWORDS = [ | ||
'any', | ||
'boolean', | ||
'constructor', | ||
'declare', | ||
'get', | ||
'module', | ||
'require', | ||
'number', | ||
'set', | ||
'string', | ||
'symbol', | ||
'type', | ||
'from', | ||
'of' | ||
]; | ||
const TYPE_BLACKLIST = {}; | ||
for (let key of exports.CONTEXTUAL_KEYWORDS) { | ||
TYPE_BLACKLIST[key] = true; | ||
} | ||
for (let key of RESERVED_WORDS) { | ||
TYPE_BLACKLIST[key] = true; | ||
} | ||
//# sourceMappingURL=datamodel.js.map |
{ | ||
"name": "@journeyapps/cloudcode-build", | ||
"version": "1.9.6", | ||
"version": "1.9.7-dev.00b908a.e8c9b44", | ||
"main": "./lib/index.js", | ||
@@ -32,3 +32,3 @@ "license": "MIT", | ||
], | ||
"gitHead": "53b2dae1865d28acf340ec097768c5eb19980464" | ||
"gitHead": "2b895f3c167e7d944d635ef06f096eadb4ec2be8" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20963
366
2