Skip to content

Utilities

Jeongho Nam edited this page Feb 6, 2023 · 3 revisions

EncryptedColumn

EncryptedColumn is a property decorator that can define a VARCHAR typed column with two way encryption through AES (Advanced Encryption Standard) algorithm. It is useful for storing sensitive private information like phone number or real name.

Detailed encryption algorithm of EncryptedColumn is like below:

  • AES-128/256
  • CBC mode
  • PKCS #5 Padding
  • Base64 Encoding

Also, do not worry about how to encrypt or decrypt value. EncryptedColumn does it automatically. When you store value to the EncryptedColumn, it will be stored with encryption and when you read value from it, it will also be decrypted automatically.

When you want to specify EncryptedColumn to construct composite index (or unique key), you have to use EncryptedColumn.getIndexField() method. Do not write its property name on composite index constructor directly.

@orm.Unique([
    "shopping_channel_id",
    safe.EncryptedColumn.getIndexField("mobile"), 
])
@orm.Entity()
export class ShoppingCitizen {
    safe.Belongs.ManyToOne(
        () => ShoppingChannel,
        "uuid",
        "shopping_channel_id",
    )
    public readonly channel!: safe.Belongs.ManyToOne<
        ShoppingChannel,
        "uuid"
    >;

    @safe.EncryptedColumn("varchar", {
        index: true,
        password: () => ShoppingCitizen.ENCRYPTION_PASSWORD,
    })
    public readonly mobile!: string;

    @safe.EncryptedColumn("varchar", {
        index: true,
        password: () => ShoppingCitizen.ENCRYPTION_PASSWORD,
    })
    public readonly name!: string;
}
export namespace ShoppingCitizen {
    export const ENCRYPTION_PASSWORD = {
        key: "abcdefghijklmnopqrstuvwxyz123456",
        iv: "abcdefg123456789",
    };
}

Paginator

Paginator is an utility class for helping pagination.

const stmt = BbsArticle.createQueryBuilder();
const page: Paginator.IPage<IBbsArticle> = await Paginator.regular(stmt)
    (data => BbsArticleProvider.json(data).getMany())
    ({
        page: 1,
        limit: 100,
    });

Password

Password is a class storing data with bcrypt algorithm.

Define and use like below:

import * as orm from "typeorm";
import safe from "safe-typeorm";

export class BbsArticle
{
    @orm.Column(() => safe.Password, { prefix: "" })
    public readonly password: safe.Password = new safe.Password();
}

const article: BbsArticle;
await article.password.set("1234");
if (await article.password.equals("1234") === false)
    throw new Error("Wrong password.");

SnakeCaseStrategy

Default naming convension of typeorm is camelCase, but regular RDB recommends to use snake_case_strategy. The SnakeCaseStrategy class has been designed to support such standard sanae_case_strategy.

await orm.createConnection({
    type: "sqlite",
    name,
    database: `:memory:`,
    entities: [
        `${__dirname}/models/**/*.${__filename.substr(-2)}`,
    ],
    dropSchema: true,
    synchronize: true,
    namingStrategy: new SnakeCaseStrategy(), // adapt snake_case
});