2.0.0 • Published 3 years ago

@ionaru/typeorm-utils v2.0.0

Weekly downloads
12
License
MIT
Repository
github
Last release
3 years ago

@ionaru/typeorm-utils

npm version npm version Build Status codecov

Description

This package contains common TypeORM utilities I use in my projects.

Usage

npm install @ionaru/typeorm-utils typeorm uuid

BaseModel

This extendable class extends the TypeORM BaseEntity and adds useful properties to the model.

import { BaseModel } from '@ionaru/typeorm-utils'; 

@Entity()
class UserModel extends BaseModel {

    public static alias = 'User';

    @Column({nullable: true})
    public name?: string;

}
const user = new UserModel();
await user.save()
console.log(UserModel.alias) // Alias given to the model in queries.
console.log(user.id) // A uuid4 string.
console.log(user.name) // Value from UserModel.
console.log(user.createdOn) // date of creation.
console.log(user.updatedOn) // date of last update.
console.log(user.deletedOn) // date when (soft) deleted.

await UserModel.doQuery() // Query example.
    .where(`${UserModel.alias}.name == :name`, {name: 'thomas'})
    .withDeleted() // Include (soft) deleted entries.
    .getOne()

buildMySQLConnectionOptions(options: IOptions)

A helper for building the TypeORM MysqlConnectionOptions dict.

interface IOptions {
    database: string; // Database name.
    host: string; // Database host.
    port: number; // Database port.
    username: string; // Database username.
    password: string; // Database password.
    sslCA?: string; // (Optional) Path to CA certificate, relative to ormconfig.js.
    sslCert?: string; // (Optional) Path to Client certificate, relative to ormconfig.js.
    sslKey?: string; // (Optional) Path to Client Key, relative to ormconfig.js.
    sslReject?: boolean; // (Optional) Reject unsecure SSL, 'true' or 'false'.
    timezone?: string; // (Optional) Timezone for TypeORM.
    models?: string[]; // (Optional) Filenames (without extension) of the models to include.
}
import { buildMySQLConnectionOptions } from '@ionaru/typeorm-utils'; 

const connectionOptions = buildMySQLConnectionOptions({ // Example
    database: 'userDB',
    host: 'some.external.host',
    port: 3306,
    username: 'DBAdminUser',
    password: 'SuperSecurePa$$word',
    sslCA: 'crt/ca.crt',
    sslCert: 'crt/cert.crt',
    sslKey: 'crt/key.crt',
    sslReject: true,
    timezone: 'Z',
    models: ['user.model'],
})
2.0.1-2.0

3 years ago

2.0.1-3.0

3 years ago

2.0.1-29.0

3 years ago

2.0.0

3 years ago

1.2.1-26.0

3 years ago

1.2.1

3 years ago

1.2.2-28.0

3 years ago

1.2.1-25.0

3 years ago

1.2.0

3 years ago

1.2.1-24.0

3 years ago

1.1.1-22.0

3 years ago

1.1.1-21.0

3 years ago

1.1.1-20.0

3 years ago

1.1.1-18.0

4 years ago

1.1.0

4 years ago

1.0.1-16.0

4 years ago

1.0.1-12.0

4 years ago

0.0.1-9.0

4 years ago

1.0.0

4 years ago

0.0.1-1.0

4 years ago

0.0.0

4 years ago