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);
FooValidator.allErrors(foo);
};
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');
let foo = {
bar: 123,
};
FooStorage.save(foo);
FooStorage.save('someId', foo);
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');
}
}