Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,28 @@ export const getJSType = (
return { isNullable, isList, baseType };
};

/** Maximum length of varchar columns created for String/ID fields. */
const VARCHAR_LENGTH = 256;

const isVarcharField = (type: GraphQLType): boolean => {
const nonNullType = type instanceof GraphQLNonNull ? type.ofType : type;

if (nonNullType === GraphQLString || nonNullType === GraphQLID) return true;

if (nonNullType instanceof GraphQLObjectType) {
const idField = nonNullType.getFields()['id'];

return (
idField !== undefined &&
idField.type instanceof GraphQLNonNull &&
idField.type.ofType instanceof GraphQLScalarType &&
['String', 'ID'].includes(idField.type.ofType.name)
);
}

return false;
};

const isPersistedField = (field: GraphQLField<any, any>) => {
if (getComputedDirective(field)) return false;

Expand Down Expand Up @@ -159,6 +181,16 @@ export const codegen = (
? ` constructor(id, indexerName) {\n`
: ` constructor(id: ${idType.baseType}, indexerName: string) {\n`;
contents += ` super(${modelName}.tableName, indexerName);\n\n`;
if (idField && isVarcharField(idField.type)) {
contents += ` if (typeof id === 'string' && [...id].length > ${VARCHAR_LENGTH}) {\n`;
contents += ` throw new Error('Value for ${modelName}.id exceeds ${VARCHAR_LENGTH} characters');\n`;
contents += ` }\n\n`;
}
if (idType.baseType === 'string') {
contents += ` if (typeof id === 'string' && id.includes('\\u0000')) {\n`;
contents += ` throw new Error('Value for ${modelName}.id contains a NUL character');\n`;
contents += ` }\n\n`;
}
persistedFields.forEach(field => {
const rawInitialValue = getInitialValue(field.type, decimalTypes);
const initialValue =
Expand Down Expand Up @@ -223,6 +255,19 @@ export const codegen = (
format === 'javascript'
? ` set ${field.name}(value) {\n`
: ` set ${field.name}(value: ${setterAnnotation}) {\n`;
if (isVarcharField(field.type)) {
contents += ` if (typeof value === 'string' && [...value].length > ${VARCHAR_LENGTH}) {\n`;
contents += ` throw new Error('Value for ${modelName}.${field.name} exceeds ${VARCHAR_LENGTH} characters');\n`;
contents += ` }\n\n`;
}
if (baseType === 'string' || baseType === 'string[]') {
const nulCheck = isList
? `Array.isArray(value) && value.some(item => typeof item === 'string' && item.includes('\\u0000'))`
: `typeof value === 'string' && value.includes('\\u0000')`;
contents += ` if (${nulCheck}) {\n`;
contents += ` throw new Error('Value for ${modelName}.${field.name} contains a NUL character');\n`;
contents += ` }\n\n`;
}
Comment thread
Sekhmet marked this conversation as resolved.
Resolved
contents += ` this.set('${field.name}', ${setterExpression});\n`;
contents += ` }\n\n`;
});
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class GqlEntityController {
column = column.notNullable();
}

if (!['text', 'json'].includes(sqlType.name)) {
if (!['text', 'json', 'jsonb'].includes(sqlType.name)) {
Comment thread
Sekhmet marked this conversation as resolved.
Resolved
column.index();
}
});
Expand Down
Loading
Loading