Permalink
Browse files

feat(QueryBuilder): Support .now() default for timestamps

  • Loading branch information...
RWOverdijk committed Oct 8, 2016
1 parent 27dafef commit d70a8a111f9b635f102350dfe2ccba1679f9d2e4
Showing with 56 additions and 2 deletions.
  1. +41 −1 src/Mapping.ts
  2. +15 −1 src/SchemaBuilder.ts
@@ -95,6 +95,15 @@ export class Mapping<T> {
return this.target;
}
/**
* Raw command for current timestamp.
*
* @returns {Raw}
*/
public now(): Raw {
return new Raw('CURRENT_TIMESTAMP');
}
/**
* Map a field to this property. Examples:
*
@@ -805,6 +814,37 @@ export class Field {
}
}
/**
* A raw query
*/
export class Raw {
/**
* @type {string}
*/
private query: string;
/**
* @param {string} query
*/
public constructor(query) {
this.setQuery(query);
}
/**
* @returns {string}
*/
getQuery(): string {
return this.query;
}
/**
* @param {string} value
*/
setQuery(value: string) {
this.query = value;
}
}
export interface FieldOptions {
type: string,
primary?: boolean,
@@ -814,7 +854,7 @@ export interface FieldOptions {
generatedValue?: string,
scale?: number,
nullable?: boolean,
defaultsTo?: any,
defaultTo?: any | Raw,
unsigned?: boolean,
comment?: string,
size?: number,
@@ -1,4 +1,4 @@
import {Mapping, FieldOptions} from './Mapping';
import {Mapping, FieldOptions, Raw} from './Mapping';
import {EntityInterface, EntityCtor} from './EntityInterface';
import * as Knex from 'knex';
import {Scope} from './Scope';
@@ -26,6 +26,11 @@ export class SchemaBuilder {
*/
private built: boolean = false;
/**
* @type {Knex}
*/
private client: Knex;
/**
* @type {string[]}
*/
@@ -53,6 +58,7 @@ export class SchemaBuilder {
*/
public constructor(entityManager: Scope) {
this.entityManager = entityManager;
this.client = entityManager.getStore().getConnection(Store.ROLE_MASTER);
}
/**
@@ -397,6 +403,14 @@ export class SchemaBuilder {
if (field.primary) {
column.primary();
}
if (field.defaultTo) {
if (field.defaultTo instanceof Raw) {
column.defaultTo(this.client.raw(field.defaultTo.getQuery()));
} else {
column.defaultTo(field.defaultTo);
}
}
}
/**

0 comments on commit d70a8a1

Please sign in to comment.