Skip to content

Commit

Permalink
fix(mongodb): Support composite types (behaves like model)
Browse files Browse the repository at this point in the history
close: #99
  • Loading branch information
unlight committed May 2, 2022
1 parent 1a49fa7 commit d505ecb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cz-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
{ name: 'configuration' },
{ name: 'custom decorators' },
{ name: 'compatibility' },
{ name: 'mongodb' },
{ name: 'other' },
],

Expand Down
5 changes: 5 additions & 0 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ export async function generate(
await eventEmitter.emit('Model', model, eventArguments);
}

// Types behaves like model
for (const model of datamodel.types) {

This comment has been minimized.

Copy link
@TMInnovations

TMInnovations May 10, 2022

Contributor

#106 Can we write something like:

for (const model of datamodel.types || []) {

await eventEmitter.emit('Model', model, eventArguments);
}

await eventEmitter.emit('PostBegin', eventArguments);

for (const enumType of enumTypes.prisma.concat(enumTypes.model || [])) {
Expand Down
52 changes: 52 additions & 0 deletions src/test/mongodb.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import expect from 'expect';
import { Project, SourceFile } from 'ts-morph';

import { testSourceFile } from './helpers';
import { testGenerate } from './test-generate';

let project: Project;
let sourceFiles: SourceFile[];

describe('type has been treated as model #99', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
provider: 'mongodb',
schema: `
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
preferences Preference[]
}
type Preference {
name String
value String
}
`,
options: [
// `outputFilePattern = "{name}.{type}.ts"`
],
}));
});

it('smoke', () => {
expect(project).toBeTruthy();
});

it('files', () => {
const files = project.getSourceFiles().map(s => s.getBaseName());
expect(files.length).toBeGreaterThan(0);
});

it('user model', () => {
const s = testSourceFile({
project,
file: 'user.model.ts',
property: 'preferences',
});
expect(s.namedImports).toContainEqual({
name: 'Preference',
specifier: '../preference/preference.model',
});
expect(s.property?.type).toEqual('Array<Preference>');
expect(s.fieldDecoratorType).toEqual('() => [Preference]');
});
});

0 comments on commit d505ecb

Please sign in to comment.