1.3.0 • Published 2 years ago

sequelize-typescript-exports v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

sequelize-typescript-exports

Build Status Code Coverage

Installation

This assumes you have successfully installed sequelize-typescript

npm install sequelize-typescript-exports

Models

Aside from minor changes, it works the same as sequelize-typescript models

Model Declarations

In order to utilize @Exportable you must extend from ExportableModel class it's a drop in replacement for sequelize-typescript's Model

@Table
export class User extends ExportableModel {
    @Column
    name: string;

    //...
}

ExportRule

Rules are what define whether or not a field it's assigned to is exported only determining what happens if it returns a Export enum

const OnlySelf: ExportRule = (input: any, caller: ExportableModel) => {
    if (input instanceof User) {
        if(input.name === (caller as User).name) {
            return Export.Allowed
        }
    }
}

const IsntDave: ExportRule = (input: any, caller: ExportableModel) => {
    if (input instanceof User) {
        if (input.name === "Dave") {
            return Export.Denied
        }
    }
}

@Exportable

Assigned to a field within a ExportableModel, it takes an array of Export Rules and executes them sequentially upon model export

@Table
export class User extends ExportableModel {
    // Always export this value, Export enums can be used as Rules
    @Exportable([Export.Allowed]) 
    @Column
    name: string;

    // Call the method defined in the ExportRule section
    @Exportable([OnlySelf]) 
    @Column
    session_token: string;

    // Allow any export that isn't blocked by IsntDave
    @Exportable([IsntDave, Export.Allowed])
    @Column
    unseenByDave: string;
}

Exporting

Based on the information provided earlier

const userDave = await User.findOne({ where: { name: "Dave" } })

if (userDave !== null) {
    const dataDave = userDave.Export(userDave)
    // According to the rules defined above the only exposed fields will be
    // name, session_token
}
1.3.0

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago