Skip to content

Commit

Permalink
Entity type creator fixes (#236)
Browse files Browse the repository at this point in the history
* Properly serialize SQL for in-editor testing

* error handling
  • Loading branch information
ncovercash authored Apr 15, 2024
1 parent 2198e81 commit 0acdeed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 8 additions & 1 deletion entity-type-creator/pages/api/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ export default function SocketHandler(req: NextApiRequest, res: NextApiResponse<
const files = await Promise.all(
(await readdir(ENTITY_TYPE_FILE_PATH, { recursive: true }))
.filter((f) => f.endsWith('.json5'))
.map(async (f) => ({ file: f, data: json5.parse((await readFile(ENTITY_TYPE_FILE_PATH + f)).toString()) })),
.map(async (f) => {
try {
return { file: f, data: json5.parse((await readFile(ENTITY_TYPE_FILE_PATH + f)).toString()) };
} catch (e) {
console.error('Error reading entity type file', `${ENTITY_TYPE_FILE_PATH}${f}`, e);
return { file: `⚠️⚠️⚠️${f}⚠️⚠️⚠️`, data: {} };
}
}),
);

console.log('Found', files.length, 'entity types');
Expand Down
10 changes: 6 additions & 4 deletions entity-type-creator/socket/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,26 @@ export async function aggregateSchemaForAutocompletion(pg: postgres.Sql, tenant:
export async function persistEntityType(pg: postgres.Sql, tenant: string, entityType: EntityType) {
console.log('Persisting entity type', entityType);

const entityTypeReplaced = JSON.parse(JSON.stringify(entityType).replaceAll('TENANT_', `${tenant}_`)) as EntityType;

// check if table entity_type_definition has matching ID for entityType.id
const existing = await pg`
SELECT id
FROM ${pg.unsafe(tenant + '_mod_fqm_manager.entity_type_definition')}
WHERE id = ${entityType.id};`;

if (existing.length === 0) {
console.log('Inserting new entity type', entityType.id);
console.log('Inserting new entity type', entityTypeReplaced.id);

await pg`
INSERT INTO ${pg.unsafe(tenant + '_mod_fqm_manager.entity_type_definition')}
(id, definition)
VALUES (${entityType.id}, ${pg.json(entityType as any)});`;
VALUES (${entityTypeReplaced.id}, ${pg.json(entityTypeReplaced as any)});`;
} else {
await pg`
UPDATE ${pg.unsafe(tenant + '_mod_fqm_manager.entity_type_definition')}
SET definition = ${pg.json(entityType as any)}
WHERE id = ${entityType.id};`;
SET definition = ${pg.json(entityTypeReplaced as any)}
WHERE id = ${entityTypeReplaced.id};`;
}
}

Expand Down

0 comments on commit 0acdeed

Please sign in to comment.