Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 4x 4x 1x 3x 1x 2x 1x 1x 3x | import 'reflect-metadata'
import { Column } from './column'
import { DBtype } from './types'
import { InvalidPropTypeError } from './exceptions'
//todo: (revisar) usando solo como tipo DBtype no compila usando el decorador @columna({dbType: DBtype.TEXT)
//Se une al tipo DBtype el tipo any como solucion temporal
export function column(colData?: {dbType?: DBtype | any, size?: number, unique?: boolean, notNullable?: boolean, index?: boolean}) {
return function(target: any, propName: string) {
let dbType: DBtype;
const propType = Reflect.getMetadata('design:type', target, propName);
target.constructor.columns = target.constructor.columns || [];
//todo: de la misma forma que se añade una propiedad "columns" al constructor del modelo
//se podria añadir una funcion "hasMany()" usando un decorator para definir relaciones entre modelos
//Por ejemplo @hasMany()Post podria generar target.constructor.hasMany([Post])
if (propType) {
dbType = getDBTypeFromPropType(propType.name);
} else {
throw new InvalidPropTypeError(propType);
}
target.constructor.columns.push(
new Column({
name: propName,
dbType: (colData && colData.dbType) || dbType,
size: colData && colData.size,
unique: colData && colData.unique,
notNullable: colData && colData.notNullable,
index: colData && colData.index
})
)
}
}
/**
* Maps javascript model prop type to SQLite column type.
*
* Type correspondence:
* ---------------------------------
* Javascript type SQLite type
* ---------------------------------
* number REAL, INTEGER
* boolean INTEGER
* string TEXT
* Array Uint8Array BLOB
* null NULL
* ----------------------------------
* Ref.: https://www.sqlite.org/datatype3.html
*
* @param jsPropType JavaScript type
* @return DBtype SQLite type
*/
export function getDBTypeFromPropType(jsPropType?: string): DBtype {
jsPropType = jsPropType && jsPropType.toLowerCase();
let result: DBtype;
if (jsPropType === 'string') {
result = DBtype.STRING;
} else if (jsPropType === 'number') {
result = DBtype.INTEGER;
} else if (jsPropType === 'null') {
result = DBtype.NULL;
} else {
throw new InvalidPropTypeError(jsPropType);
}
return result;
} |