Skip to content

Commit

Permalink
improve entity type gen
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniopresto committed Dec 2, 2024
1 parent 3e7812b commit 5cf631d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
24 changes: 15 additions & 9 deletions packages/entity/src/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '@powership/transporter';
import {
AnyFunction,
BJSON,
capitalize,
createAsyncPlugin,
createProxy,
Expand Down Expand Up @@ -743,33 +744,38 @@ export function createEntity(
PowershipWatchTypesPubSub.emit('created', {
custom: () => ({
name: entityOptions.name,
imports: [
`import { DocumentIndexesConfig, EntityOptions } from 'powership';`,
],
imports: [`import { Entity } from 'powership';`],
head: ['export type ID = number | string;'],
body: (() => {
const conf = (
let { type, name, indexes } = (
typeof configOptions === 'function'
? configOptions()
: configOptions
) as EntityOptions;

const configJSON = JSON.stringify(conf, null, 2);
const indexesJSON = JSON.stringify(conf.indexes, null, 2);
const typeId = type.optionalId || entityResult.type.id;
const indexesJSON = JSON.stringify(indexes);

const configJSON = [
'{',
`name: "${name}",`,
`indexes: ${indexesJSON},`,
`type: GraphTypeRuntime<RuntimeDefinitions['${typeId}'], RuntimeTypes['${typeId}'], '${typeId}'>,`,
'[K: string]: any,',
'}',
].join('\n');

const config_str =
typeof configOptions === 'function'
? `() => (${configJSON})`
: `${configJSON}`;

const typeId = conf.type.optionalId || entityResult.type.id;

return [
'export function createEntity(',
`configOptions:`,
config_str,
'',
`): Entity<RuntimeDefinitions['${typeId}'], ${indexesJSON}>;`,
`): Entity<RuntimeDefinitions['${typeId}']['def'], ${indexesJSON}>;`,
];
})(),
}),
Expand Down
21 changes: 18 additions & 3 deletions packages/entity/src/__tests__/writeTypes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { createType, resetTypesCache, generateTypes } from '@powership/schema';
import { MongoTransporter } from '@powership/mongo';
import { createType, generateTypes } from '@powership/schema';
import { delay } from '@powership/utils';
import { nodePath } from '@powership/utils/server-utils';
import { createEntity } from '../Entity';
import { AppMock, createAppMock } from './createAppMock';

xdescribe('writeTypes', () => {
beforeEach(async () => {
await resetTypesCache();
let mockApp: AppMock;
let transporter: MongoTransporter;

beforeEach(async function () {
mockApp = createAppMock();
await mockApp.start();
transporter = new MongoTransporter({
collection: 'temp1',
client: mockApp.client!,
});
});

afterEach(async function () {
await mockApp.reset();
});

const dest = nodePath.resolve(__dirname, 'writeTypesResult.txt');
Expand All @@ -23,6 +37,7 @@ xdescribe('writeTypes', () => {
createEntity({
name: 'SUTEntity',
type: SUTType,
transporter,
indexes: [
{
PK: ['.accountId'],
Expand Down

0 comments on commit 5cf631d

Please sign in to comment.