New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

dui-active-record

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dui-active-record

Implementation of the Active Record pattern for typescript

0.2.4
latest
Version published
Weekly downloads
19
850%
Maintainers
1
Weekly downloads
 
Created

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');
    }
}

FAQs

Package last updated on 26 Aug 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts