Skip to content

Commit

Permalink
fix(typescript): correctly dump typescript files when collections nam…
Browse files Browse the repository at this point in the history
…e are complexe (#653)
  • Loading branch information
SteveBunlon authored Dec 14, 2023
1 parent e9e4445 commit 269fc41
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{{#each models as |model|}}
import type { {{capitalise model.collectionName}}Interface } from './{{model.modelFileName}}';
import type { {{capitalise model.modelName}}Interface } from './{{model.modelFileName}}';
{{/each}}

import Mongoose from 'mongoose';

{{#each models as |model|}}
import { {{model.collectionName}}Schema } from './{{model.modelFileName}}';
import { {{model.modelName}}Schema } from './{{model.modelFileName}}';
{{/each}}

const connection = Mongoose.createConnection(process.env.DATABASE_URL);

{{#each models as |model|}}
export const {{model.modelName}} = connection.model<{{capitalise model.collectionName}}Interface>('{{model.modelName}}', {{model.collectionName}}Schema, '{{model.collectionName}}');
export const {{model.modelName}} = connection.model<{{capitalise model.modelName}}Interface>('{{model.modelName}}', {{model.modelName}}Schema, '{{model.collectionName}}');
{{/each}}

export default connection;
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Mongoose from 'mongoose';

interface {{capitalise collectionName}}Interface {
interface {{capitalise modelName}}Interface {
{{#each fields as |field|}}
{{wsc field.name}}: {{#if (isObject field.type)}}{{>renderNestedInterface type=field.type level=1}}{{else}}{{toInterfaceType field.type}}{{/if}};
{{/each}}
}

const {{collectionName}}Schema = new Mongoose.Schema({
const {{modelName}}Schema = new Mongoose.Schema({
{{#each fields as |field|}}
{{wsc field.name}}: {{#if field.ref}}{ type: {{field.type}}, ref: '{{field.ref}}' }{{else if (isObject field.type)}}{{>renderNested type=field.type level=1}}{{else}}{{field.type}}{{/if}},
{{/each}}
}, {
timestamps: {{timestamps}},
});

export { {{capitalise collectionName}}Interface, {{collectionName}}Schema };
export { {{capitalise modelName}}Interface, {{modelName}}Schema };
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"_special:character": {
"fields": [{
"name": "name",
"type": "String"
}],
"options": {
"timestamps": false
},
"primaryKeys": [
"_id"
],
"references": []
},
":otherSpecial*character": {
"fields": [{
"name": "age",
"type": "Number"
}],
"options": {
"timestamps": false
},
"primaryKeys": [
"_id"
],
"references": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import rimraf from 'rimraf';
import defaultPlan from '../../../../src/context/plan';
import AgentNodeJsDumper from '../../../../src/services/dumpers/agent-nodejs';
import languages from '../../../../src/utils/languages';
import collectionsWithSpecialCharacters from '../../analyzer/expected/mongo/db-analysis-output/collections-with-special-characters.expected.json';
import deepNestedColumn from '../../analyzer/expected/mongo/db-analysis-output/deep-nested-fields-column.expected.json';
import deepNested from '../../analyzer/expected/mongo/db-analysis-output/deep-nested-fields.expected.json';
import hasMany from '../../analyzer/expected/mongo/db-analysis-output/hasmany.expected.json';
Expand Down Expand Up @@ -150,6 +151,43 @@ describe('services > dumpers > agentNodejsDumper > mongoose models', () => {
'Ignoring field so:column from collection persons as it contains column and is not valid.',
);
});

it('should correctly dump interfaces and classes when collection has special characters', async () => {
expect.assertions(3);

rimraf.sync(`${appRoot}/test-output/${language.name}/mongodb/`);

await dump(language, collectionsWithSpecialCharacters);

const expectedIndex = fs.readFileSync(
`${__dirname}/expected/${language.name}/mongo-models/collection-special-characters/index.${language.fileExtension}`,
'utf-8',
);
const generatedIndex = fs.readFileSync(
`${appRoot}/test-output/${language.name}/mongodb/models/index.${language.fileExtension}`,
'utf8',
);
const expectedOtherSpecialCharacter = fs.readFileSync(
`${__dirname}/expected/${language.name}/mongo-models/collection-special-characters/other-special-character.expected.${language.fileExtension}`,
'utf-8',
);
const generatedOtherSpecialCharacter = fs.readFileSync(
`${appRoot}/test-output/${language.name}/mongodb/models/other-special-character.${language.fileExtension}`,
'utf8',
);
const expectedSpecialCharacter = fs.readFileSync(
`${__dirname}/expected/${language.name}/mongo-models/collection-special-characters/special-character.expected.${language.fileExtension}`,
'utf-8',
);
const generatedSpecialCharacter = fs.readFileSync(
`${appRoot}/test-output/${language.name}/mongodb/models/special-character.${language.fileExtension}`,
'utf8',
);

expect(generatedIndex).toStrictEqual(expectedIndex);
expect(generatedSpecialCharacter).toStrictEqual(expectedSpecialCharacter);
expect(generatedOtherSpecialCharacter).toStrictEqual(expectedOtherSpecialCharacter);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs');
const path = require('path');
const Mongoose = require('mongoose');

const connection = Mongoose.createConnection(process.env.DATABASE_URL);

fs
.readdirSync(__dirname)
.filter((file) => file !== 'index.js')
.forEach((file) => {
try {
const { schema, modelName, collectionName } = require(path.join(__dirname, file));
connection.model(modelName, schema, collectionName);
} catch (error) {
console.error(`Model creation error: ${error}`);
}
});

module.exports = connection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Mongoose = require('mongoose');

const schema = new Mongoose.Schema({
age: Number,
}, {
timestamps: false,
});

module.exports = {
collectionName: ':otherSpecial*character',
modelName: 'otherSpecialCharacter',
schema,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Mongoose = require('mongoose');

const schema = new Mongoose.Schema({
name: String,
}, {
timestamps: false,
});

module.exports = {
collectionName: '_special:character',
modelName: 'specialCharacter',
schema,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { OtherSpecialCharacterInterface } from './other-special-character';
import type { SpecialCharacterInterface } from './special-character';

import Mongoose from 'mongoose';

import { otherSpecialCharacterSchema } from './other-special-character';
import { specialCharacterSchema } from './special-character';

const connection = Mongoose.createConnection(process.env.DATABASE_URL);

export const otherSpecialCharacter = connection.model<OtherSpecialCharacterInterface>('otherSpecialCharacter', otherSpecialCharacterSchema, ':otherSpecial*character');
export const specialCharacter = connection.model<SpecialCharacterInterface>('specialCharacter', specialCharacterSchema, '_special:character');

export default connection;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Mongoose from 'mongoose';

interface OtherSpecialCharacterInterface {
age: number;
}

const otherSpecialCharacterSchema = new Mongoose.Schema({
age: Number,
}, {
timestamps: false,
});

export { OtherSpecialCharacterInterface, otherSpecialCharacterSchema };
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Mongoose from 'mongoose';

interface SpecialCharacterInterface {
name: string;
}

const specialCharacterSchema = new Mongoose.Schema({
name: String,
}, {
timestamps: false,
});

export { SpecialCharacterInterface, specialCharacterSchema };

0 comments on commit 269fc41

Please sign in to comment.