blow-data
Version:
Data access layer for Blow.
76 lines (65 loc) • 1.44 kB
text/typescript
;
import {Query} from 'blow-query';
import {PersistedModel} from '../../src/PersistedModel';
import * as decorators from '../../src/decorators';
import {IBelongsToRelation} from '../../src/interfaces';
import {Author, Edition} from './interfaces';
.model({
name: 'Book',
connection: 'default'
})
export class Book extends PersistedModel {
.property({
type: 'String',
validations: {
required: true
}
})
title: string;
.property({
type: 'String',
validations: {
required: true,
custom: uniqueValidator
}
})
no: string;
.property({
type: 'Number',
validations: {
required: true,
max: 5
}
})
price: number;
.property({
type: 'Boolean',
hidden: true
})
promo: boolean;
.property({
type: 'Boolean',
default: true
})
available: boolean;
.property({
type: 'Edition'
})
edition: Edition;
.relation({
type: 'belongsTo',
model: 'Author',
foreignKey: 'authorId'
})
author: IBelongsToRelation<Author>;
}
function uniqueValidator(property) {
const query = new Query();
query.equal(property.name, this[property.name]);
return Book.count(query.toJSON().where).map(c => {
if(c === 0) {
return;
}
return `"${property.name}" does not have unique value`;
});
}