Skip to content

Commit

Permalink
break: Use kind and type (#50)
Browse files Browse the repository at this point in the history
* break: Use kind and type

* update
  • Loading branch information
nicola-smartive authored Aug 31, 2023
1 parent e6c1798 commit 1d5b728
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 287 deletions.
1 change: 1 addition & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// created from 'create-ts-index'

export * from './gql';
export * from './models';
export * from './mutations';
export * from './queries';
1 change: 1 addition & 0 deletions src/client/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type ClientEntity = Record<string, unknown>;
10 changes: 5 additions & 5 deletions src/client/queries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import upperFirst from 'lodash/upperFirst';
import { Field } from '..';
import { ModelField } from '..';
import { Model, Models, Relation, ReverseRelation } from '../models/models';
import {
actionableRelations,
Expand Down Expand Up @@ -53,8 +53,8 @@ export const getEditEntityRelationsQuery = (
!!relations.length &&
`query ${upperFirst(action)}${model.name}Relations {
${relations
.map(({ name, typeName }) => {
const model = summonByName(models, typeName);
.map(({ name, type }) => {
const model = summonByName(models, type);
let filters = '';
if (model.displayField) {
Expand Down Expand Up @@ -209,7 +209,7 @@ export type VisibleRelationsByRole = Record<string, Record<string, string[]>>;

export const isVisibleRelation = (visibleRelationsByRole: VisibleRelationsByRole, modelName: string, role: string) => {
const whitelist = visibleRelationsByRole[role]?.[modelName];
return ({ name }: Field) => (whitelist ? whitelist.includes(name) : true);
return ({ name }: ModelField) => (whitelist ? whitelist.includes(name) : true);
};

export const getEntityQuery = (
Expand All @@ -224,7 +224,7 @@ export const getEntityQuery = (
${model.fields.filter(and(isSimpleField, isQueriableBy(role))).map(({ name }) => name)}
${queryRelations(
models,
model.fields.filter(and(isRelation, isVisibleRelation(visibleRelationsByRole, model.name, role))),
model.fields.filter(isRelation).filter(isVisibleRelation(visibleRelationsByRole, model.name, role)),
role,
typesWithSubRelations
)}
Expand Down
3 changes: 2 additions & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { DocumentNode, GraphQLResolveInfo } from 'graphql';
import { IncomingMessage } from 'http';
import { Knex } from 'knex';
import { DateTime } from 'luxon';
import { Entity, Models, MutationHook, RawModels } from './models/models';
import { Models, RawModels } from './models/models';
import { Entity, MutationHook } from './models/mutation-hook';
import { Permissions } from './permissions/generate';
import { AliasGenerator } from './resolvers/utils';

Expand Down
52 changes: 25 additions & 27 deletions src/db/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export const generateDBModels = (rawModels: RawModels) => {

for (const model of models) {
// TODO: deprecate allowing to define foreignKey
const fields = model.fields.some((field) => field.type === 'relation' && field.foreignKey === 'id')
const fields = model.fields.some((field) => field.kind === 'relation' && field.foreignKey === 'id')
? model.fields.filter((field) => field.name !== 'id')
: model.fields;

writer
.write(`export type ${model.name} = `)
.inlineBlock(() => {
for (const field of fields.filter(not(isRaw))) {
writer.write(`'${getFieldName(field)}': ${getFieldOutputType(field)}${field.nonNull ? '' : ' | null'},`).newLine();
writer.write(`'${getFieldName(field)}': ${getFieldType(field)}${field.nonNull ? '' : ' | null'},`).newLine();
}
})
.blankLine();
Expand All @@ -48,9 +48,9 @@ export const generateDBModels = (rawModels: RawModels) => {
for (const field of fields.filter(not(isRaw))) {
writer
.write(
`'${getFieldName(field)}'${field.nonNull && field.default === undefined ? '' : '?'}: ${getFieldInputType(
`'${getFieldName(field)}'${field.nonNull && field.defaultValue === undefined ? '' : '?'}: ${getFieldType(
field
)}${field.nonNull ? '' : ' | null'},`
)}${field.list ? ' | string' : ''}${field.nonNull ? '' : ' | null'},`
)
.newLine();
}
Expand All @@ -61,7 +61,13 @@ export const generateDBModels = (rawModels: RawModels) => {
.write(`export type ${model.name}Mutator = `)
.inlineBlock(() => {
for (const field of fields.filter(not(isRaw))) {
writer.write(`'${getFieldName(field)}'?: ${getFieldInputType(field)}${field.nonNull ? '' : ' | null'},`).newLine();
writer
.write(
`'${getFieldName(field)}'?: ${getFieldType(field)}${field.list ? ' | string' : ''}${
field.nonNull ? '' : ' | null'
},`
)
.newLine();
}
})
.blankLine();
Expand All @@ -74,11 +80,10 @@ export const generateDBModels = (rawModels: RawModels) => {
writer
.write(
`'${getFieldName(field)}'${
field.nonNull && field.default === undefined && !OPTIONAL_SEED_FIELDS.includes(fieldName) ? '' : '?'
}: ${getFieldInputType(
field,
rawModels.filter(isEnumModel).map(({ name }) => name)
)}${field.list ? ' | string' : ''}${field.nonNull ? '' : ' | null'},`
field.nonNull && field.defaultValue === undefined && !OPTIONAL_SEED_FIELDS.includes(fieldName) ? '' : '?'
}: ${field.kind === 'enum' ? (field.list ? 'string[]' : 'string') : getFieldType(field)}${
field.list ? ' | string' : ''
}${field.nonNull ? '' : ' | null'},`
)
.newLine();
}
Expand All @@ -95,36 +100,29 @@ export const generateDBModels = (rawModels: RawModels) => {
return writer.toString();
};

const getFieldName = (field: ModelField) => (field.type === 'relation' ? field.foreignKey || `${field.name}Id` : field.name);
const getFieldName = (field: ModelField) => (field.kind === 'relation' ? field.foreignKey || `${field.name}Id` : field.name);

const getFieldOutputType = (field: ModelField) => {
switch (field.type) {
const getFieldType = (field: ModelField) => {
const kind = field.kind;
switch (kind) {
case 'json':
// JSON data is stored as string
return 'string';
case 'relation':
// Relations are stored as ids
return 'string';
case 'enum':
return field.typeName + (field.list ? '[]' : '');
return field.type + (field.list ? '[]' : '');
case 'raw':
throw new Error(`Raw fields are not in the db.`);
default:
case 'primitive':
case undefined:
return get(PRIMITIVE_TYPES, field.type) + (field.list ? '[]' : '');
}
};

const getFieldInputType = (field: ModelField, stringTypes: string[] = []) => {
let outputType = getFieldOutputType(field);

if (field.list || stringTypes.includes(field.type)) {
outputType += ' | string';
if (field.list && stringTypes.includes(field.type)) {
outputType += ' | string[]';
default: {
const exhaustiveCheck: never = kind;
throw new Error(exhaustiveCheck);
}
}

return outputType;
};

export const generateKnexTables = (rawModels: RawModels) => {
Expand Down
Loading

0 comments on commit 1d5b728

Please sign in to comment.