Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/services/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'reflect-metadata';
import * as fs from 'fs';
import * as glob from 'glob';
import * as path from 'path';
import {DataTypeAbstract, DefineOptions} from 'sequelize';
import {Model} from "../models/Model";
Expand Down Expand Up @@ -182,14 +182,16 @@ export function getModels(arg: Array<typeof Model | string>): Array<typeof Model

return arg.reduce((models: any[], dir) => {

const _models = fs
.readdirSync(dir as string)
if (!glob.hasMagic(dir)) dir = path.join(dir, '/*');
const _models = glob
.sync(dir as string)
.filter(isImportable)
.map(getFilenameWithoutExtension)
.map(getFullfilepathWithoutExtension)
.filter(uniqueFilter)
.map(fileName => {
const fullPath = path.join(dir, fileName);
.map(fullPath => {

const module = require(fullPath);
const fileName = getFilenameWithoutExtension(fullPath);

if (!module[fileName] && !module.default) {
throw new Error(`No default export defined for file "${fileName}" or ` +
Expand Down Expand Up @@ -313,3 +315,11 @@ function isImportable(file: string): boolean {
function getFilenameWithoutExtension(file: string): string {
return path.parse(file).name;
}

/**
* Return the value of the full path with filename, without extension
*/
function getFullfilepathWithoutExtension(file: string): string {
const parsedFile = path.parse(file);
return path.join(parsedFile.dir, parsedFile.name);
}
7 changes: 7 additions & 0 deletions test/models/globs/match-dir-only/PlayerDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Table, Model, Column,} from "../../../../index";

@Table
export default class PlayerDir extends Model<PlayerDir> {
@Column
name: string;
}
9 changes: 9 additions & 0 deletions test/models/globs/match-dir-only/ShoeDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Table, Model, Column} from "../../../../index";

@Table
export default class ShoeDir extends Model<ShoeDir> {

@Column
brand: string;

}
8 changes: 8 additions & 0 deletions test/models/globs/match-dir-only/TeamDir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {Table, Model, Column} from "../../../../index";

@Table
export default class TeamDir extends Model<TeamDir> {

@Column
name: string;
}
7 changes: 7 additions & 0 deletions test/models/globs/match-sub-dir-files/players/player.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {Table, Model, Column,} from "../../../../../index";

@Table
export default class PlayerGlob extends Model<PlayerGlob> {
@Column
name: string;
}
9 changes: 9 additions & 0 deletions test/models/globs/match-sub-dir-files/shoes/shoe.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Table, Model, Column} from "../../../../../index";

@Table
export default class ShoeGlob extends Model<ShoeGlob> {

@Column
brand: string;

}
8 changes: 8 additions & 0 deletions test/models/globs/match-sub-dir-files/teams/team.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {Table, Model, Column} from "../../../../../index";

@Table
export default class TeamGlob extends Model<TeamGlob> {

@Column
name: string;
}
67 changes: 66 additions & 1 deletion test/specs/models/sequelize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import Gamer from "../../models/exports/gamer.model";
import {Sequelize} from "../../../lib/models/Sequelize";
import {Model} from '../../../lib/models/Model';
import {Table} from '../../../lib/annotations/Table';
import PlayerGlob from "../../models/globs/match-sub-dir-files/players/player.model";
import ShoeGlob from "../../models/globs/match-sub-dir-files/shoes/shoe.model";
import TeamGlob from "../../models/globs/match-sub-dir-files/teams/team.model";
import PlayerDir from "../../models/globs/match-dir-only/PlayerDir";
import TeamDir from "../../models/globs/match-dir-only/TeamDir";
import ShoeDir from "../../models/globs/match-dir-only/ShoeDir";

describe('sequelize', () => {

Expand Down Expand Up @@ -104,7 +110,7 @@ describe('sequelize', () => {
.to.have.property('options')
.that.has.property('define')
.that.eqls(DEFINE_OPTIONS)
;
;
});

it('should set define options for models', () => {
Expand Down Expand Up @@ -188,4 +194,63 @@ describe('sequelize', () => {
});

});

describe('Add models as glob and dir', () => {
it('should load classes from subfolders matching glob criteria', () => {
const db = '__';
const sequelizeGlob = new Sequelize({
name: db,
dialect: 'sqlite',
username: 'root',
password: '',
storage: ':memory:',
logging: !('SEQ_SILENT' in process.env),
modelPaths: [__dirname + '/../../models/globs/match-sub-dir-files/**/*.model.ts']
});

expect(sequelizeGlob._).to.have.property('PlayerGlob', PlayerGlob);
expect(sequelizeGlob._).to.have.property('TeamGlob', TeamGlob);
expect(sequelizeGlob._).to.have.property('ShoeGlob', ShoeGlob);

});

it('should load classes from folders', () => {
const db = '__';
const sequelizeFolder = new Sequelize({
name: db,
dialect: 'sqlite',
username: 'root',
password: '',
storage: ':memory:',
logging: !('SEQ_SILENT' in process.env),
modelPaths: [__dirname + '/../../models/globs/match-dir-only']
});

expect(sequelizeFolder._).to.have.property('PlayerDir', PlayerDir);
expect(sequelizeFolder._).to.have.property('TeamDir', TeamDir);
expect(sequelizeFolder._).to.have.property('ShoeDir', ShoeDir);

});

it('should load classes from folders and from glob', () => {
const db = '__';
const sequelizeGlobFolder = new Sequelize({
name: db,
dialect: 'sqlite',
username: 'root',
password: '',
storage: ':memory:',
logging: !('SEQ_SILENT' in process.env),
modelPaths: [__dirname + '/../../models/globs/match-dir-only', __dirname + '/../../models/globs/match-sub-dir-files/**/*.model.ts']
});

expect(sequelizeGlobFolder._).to.have.property('PlayerDir', PlayerDir);
expect(sequelizeGlobFolder._).to.have.property('TeamDir', TeamDir);
expect(sequelizeGlobFolder._).to.have.property('ShoeDir', ShoeDir);
expect(sequelizeGlobFolder._).to.have.property('PlayerGlob', PlayerGlob);
expect(sequelizeGlobFolder._).to.have.property('TeamGlob', TeamGlob);
expect(sequelizeGlobFolder._).to.have.property('ShoeGlob', ShoeGlob);

});
});
});