dui-active-record
TypeScript icon, indicating that this package has built-in type declarations

0.2.4 • Public • Published

dui-active-record

Implementation of the Active Record pattern for typescript

Works on Node.js and on browsers, with minimal dependecies

Functional style Validation

let FooValidator = new ModelValidator({
    allowUnsafe: false,
    strict     : true,
    rules      : {
        bar: ['int'],
    },
});
 
let foo = {
    bar  : '123',
    other: 'abc',
};
 
FooValidator.validate(foo);
// return false
 
FooValidator.allErrors(foo);
// return [
//     new ValidationError(
//         'int',
//         'Value must be a integer',
//         undefined,
//         'bar',
//     ),
//     new ValidationError(
//         'unsafe',
//         'Unsafe attributes is not allowed',
//         undefined,
//         'other',
//     ),
// ]
 
// FooValidator.errors(foo)
// {
//     bar  : [
//         new ValidationError(
//             'int',
//             'Value must be a integer',
//             undefined,
//             'bar',
//         ),
//     ],
//     other: [
//         new ValidationError(
//             'unsafe',
//             'Unsafe attributes is not allowed',
//             undefined,
//             'other',
//         ),
//     ],
};

Using for array

let foos = [
    {
        bar: 5,
    }, {
        bar: 10,
    }
]
 
foos.map(FooValidator.validate);
foos.map(FooValidator.allErrors);
foos.map(FooValidator.errors);

Storage

let FooStorage = new ArrayStorage<IFoo>();
or
let FooStorage = MongoDB.storage<IFoo>('collection_name');
 
// single using
let foo = {
    bar: 123,
};
 
FooStorage.save(foo);
FooStorage.save('someId', foo);
 
// array using
let foos: IFoo[] = [
    {
        bar: 5,
    }, {
        bar: 10,
    }
]
 
foos.map(FooStorage.save);

Class style initialization

@ActiveRecord({
    storage: ArrayStorage
})
export class FooModel extends Model {
    constructor(a?: string) {
        super();
    }
 
    @Field({enumerable: false})
    public a: string;
    @Field()
    public b: number = 0;
    @Field()
    public d: SubDoc;
    @Field({enumerable: true})
    get c(): string {
        return 'c';
    }
    set c(value: string) {
        console.log('set c value:', value);
    }
 
    rules() {
        return [];
    }
 
    static stat() {
        console.log('stat');
    }
}

/dui-active-record/

    Package Sidebar

    Install

    npm i dui-active-record

    Weekly Downloads

    0

    Version

    0.2.4

    License

    MIT

    Unpacked Size

    61.9 kB

    Total Files

    30

    Last publish

    Collaborators

    • arswarog