# @dmcl/mssql-model-generator

> Generation of the MSSQL base model, creation of entities of the TypeORM (nodejs).

Latest version **1.0.5** (published 2020-09-21) · ISC license · 0 weekly downloads

## Install

```sh
npm install @dmcl/mssql-model-generator
pnpm add @dmcl/mssql-model-generator
yarn add @dmcl/mssql-model-generator
bun add @dmcl/mssql-model-generator
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2020-09-21 |
| First published | 2020-05-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 31.2 KB |
| Known vulnerabilities | 0 (+4 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | dmcl |
| Keywords | typeorm, mssql, generator, model |

## Links

- npm: https://www.npmjs.com/package/@dmcl/mssql-model-generator
- npm.io page: https://npm.io/package/@dmcl/mssql-model-generator

## Dependencies (8)

- [path](https://npm.io/package/path.md) ^0.12.7
- [mssql](https://npm.io/package/mssql.md) ^5.1.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [mkdirp](https://npm.io/package/mkdirp.md) ^1.0.4
- [tedious](https://npm.io/package/tedious.md) ^6.4.0
- [typeorm](https://npm.io/package/typeorm.md) ^0.2.25
- [mypluralize](https://npm.io/package/mypluralize.md) ^1.0.3
- [graceful-fs-extra](https://npm.io/package/graceful-fs-extra.md) ^2.0.0

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 1.0.5 (latest) — 2020-09-21
- 1.0.4 — 2020-06-07
- 1.0.3 — 2020-05-27
- 1.0.2 — 2020-05-27
- 1.0.1 — 2020-05-27
- 1.0.0 — 2020-05-27

## README

## Module functions

1. Getting the MSSQL database model (getModel);
2. Generation of TypeOrm model files (createTypeOrmEntities);


## Examples of using

```javascript
// import module
const {MsSqlModelGenerator} = require('./src/msSqlModelGenerator');

const run = async () => {

    // generator creation
    const generator = new ModelGenerator();

    // getting the database model
    const model = await generator.getModel({
        host: 'server',
        database: 'test',
        user: 'sa',
        password: '123',
        port: 1433, // optional
    });

    // configuration for generating TypeOrm entities
    const config = {
        tUsers: {
            name: 'User',
        },
        tRoleUsers: {
            name: 'RoleUser',
        },
        tRoles: {
            name: 'Role'
        }
    };

    // generating TypeOrm entities in the 'entity' directory
    await generator.createTypeOrmEntities({
        dir: 'entity',
        model: model,
        config: config,
        swagger: false, // optional
    });
};

run().then(() => {
    console.log('ok.');
});
```
    

Let's say that there are two tables [tRoles] and [tUsers] in the database,
related among themselves using the third table [tRoleUsers]:

    ...
        tRoles:
            id: int
            name: nvarchar(100)
    
        tRoleUsers:
            id: int
            roleId: int
            userId: int
    
        tUsers:
            id: int
            name: nvarchar(100)
    ...
    
In order for the files with the description of the tables to be generated,
it is necessary to describe them in config:
```javascript
const config = {
    tUsers: {
        name: 'User',
    },
    tRoleUsers: {
        name: 'RoleUser',
    },
    tRoles: {
        name: 'Role'
    }
};
````

Result:
```javascript
// Role.ts
import ...

@Entity('tRoles', {schema: 'dbo'})
export class Role {

    @PrimaryGeneratedColumn('int')
    id: number;

    @OneToMany(type => RoleUser, RoleUser => RoleUser.Role)
    RoleUsers: RoleUser[];

    @Column('nvarchar', {length: 100})
    name: string;
}

// User.ts
import ...

@Entity('tUsers', {schema: 'dbo'})
export class User {

    @PrimaryGeneratedColumn('int')
    id: number;

    @OneToMany(type => RoleUser, RoleUser => RoleUser.User)
    RoleUsers: RoleUser[];

    @Column('nvarchar', {length: 100})
    name: string;
}

// RoleUser.ts
import ...

@Entity('tRoleUsers', {schema: 'dbo'})
export class RoleUser {

    @PrimaryGeneratedColumn('int')
    id: number;

    @ManyToOne(type => User, User => User.RoleUsers)
    @JoinColumn({name: 'userId'})
    User: User;

    @ManyToOne(type => Role, Role => Role.RoleUsers)
    @JoinColumn({name: 'roleId'})
    Role: Role;
}
```


You can change the names of the columns:
```javascript
const config = {
    tUsers: {
        columns: {
            name: 'ExtName'
        },
    },
};
```

Result:
```javascript
// User.ts
import ...

@Entity('tUsers', {schema: 'dbo'})
export class User {

    @PrimaryGeneratedColumn('int')
    id: number;

    @OneToMany(type => RoleUser, RoleUser => RoleUser.User)
    RoleUsers: RoleUser[];

    @Column('nvarchar', {length: 100})
    ExtName: string; // <---
}
```


Suppose there is a table in which the column names begin with capital
letters and you need to convert the names so that they begin with a
lowercase letter. You can use the [columns] option described above.
But if this conversion needs to be performed for all columns of the table,
then it is more convenient to use the option [lowercase].

```javascript
const config = {
    tUsers: {
        lowercase: true,
    },
};
```    


Column names in ManyToOne and OneToMany relationships will be
generated automatically, but sometimes you need to change them:

```javascript
const config = {
    tUsers: {
        name: 'User',
    },
    tRoleUsers: {
        name: 'RoleUser',
        manyToOne: {
            userId: ['AAA', 'BBB']  // <---
        },
    },
    tRoles: {
        name: 'Role'
    }
};
```
    
Result:
```javascript
// Role.ts
import ...

@Entity('tRoles', {schema: 'dbo'})
export class Role {

    @PrimaryGeneratedColumn('int')
    id: number;

    @OneToMany(type => RoleUser, RoleUser => RoleUser.Role)
    RoleUsers: RoleUser[];

    @Column('nvarchar', {length: 100})
    name: null;
}

// User.ts
import ...

@Entity('tUsers', {schema: 'dbo'})
export class User {

    @PrimaryGeneratedColumn('int')
    id: number;

    @OneToMany(type => RoleUser, RoleUser => RoleUser.AAA)
    BBB: RoleUser[]; // <---

    @Column('nvarchar', {length: 100})
    name: string;
}

// RoleUser.ts
import ...

@Entity('tRoleUsers', {schema: 'dbo'})
export class RoleUser {

    @PrimaryGeneratedColumn('int')
    id: number;

    @ManyToOne(type => User, User => User.BBB)
    @JoinColumn({name: 'userId'})
    AAA: User; // <---

    @ManyToOne(type => Role, Role => Role.RoleUsers)
    @JoinColumn({name: 'roleId'})
    Role: Role;
}
```

    
If you want to make a bunch of ManyToMany, then config should be modified as follows:
```javascript
const config = {
    tUsers: {
        name: 'User'
    },
    tRoleUsers: {
        name: 'RoleUser',
        manyToMany: [
            ['roleId', 'AAA'], // <---
            ['userId', 'BBB'], // <---
        ]
    },
    tRoles: {
        name: 'Role'
    }
};
```

Result:
```javascript
// Role.ts
import ...

@Entity('tRoles', {schema: 'dbo'})
export class Role {

    @PrimaryGeneratedColumn('int')
    id: number;

    @Column('nvarchar', {length: 100})
    name: null;

    @ManyToMany(type => User)
    @JoinTable({
        name: 'RoleUser',
        joinColumns: [{name: 'roleId', referencedColumnName: 'id'}],
        inverseJoinColumns: [{name: 'userId', referencedColumnName: 'id'}],
    })
    BBB: User[]; // <---
}

// User.ts
import ...

@Entity('tUsers', {schema: 'dbo'})
export class User {

    @PrimaryGeneratedColumn('int')
    id: number;

    @Column('nvarchar', {length: 100})
    name: string;

    @ManyToMany(type => Role)
    @JoinTable({
        name: 'RoleUser',
        joinColumns: [{name: 'userId', referencedColumnName: 'id'}],
        inverseJoinColumns: [{name: 'roleId', referencedColumnName: 'id'}],
    })
    AAA: Role[]; // <---
}

// RoleUser.ts
import ...

@Entity('tRoleUsers', {schema: 'dbo'})
export class RoleUser {

    @PrimaryGeneratedColumn('int')
    id: number;

    @Column('int')
    userId: number;

    @Column('int')
    roleId: number;
}
```

---
_Source: https://npm.io/package/@dmcl/mssql-model-generator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
