@ionaru/typeorm-utils
Description
This package contains common TypeORM utilities I use in my projects.
Usage
npm install @ionaru/typeorm-utils chalk debug supports-color 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'],
})
QueryLogger
QueryLogger is an implementation of the TypeORM Logger interface.
import { QueryLogger } from '@ionaru/typeorm-utils';
const connectionOptions = await getConnectionOptions();
// Inject the QueryLogger into TypeORM.
Object.assign(connectionOptions, {
logger: new QueryLogger(),
logging: ['query', 'error'],
});
const connection = await createConnection(connectionOptions);
The class can also log queries using the debug
package. Useful for development.
QueryLogger optionally takes a Debugger
instance in its constructor, this will enable query logging.
import Debug from 'debug';
new QueryLogger(Debug('my-app'));
The above code will log queries to my-app:QueryLogger
.