@contember/schema-definition
Advanced tools
Comparing version 0.8.0-alpha.1 to 0.8.0-alpha.2
@@ -34,2 +34,3 @@ "use strict"; | ||
case schema_1.Model.ColumnType.Enum: | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return { ...common, type: type, columnType: options.enumName, enumName: options.enumName }; | ||
@@ -36,0 +37,0 @@ default: |
@@ -5,9 +5,10 @@ import { Model } from '@contember/schema'; | ||
import EnumDefinition from './EnumDefinition'; | ||
declare class ColumnDefinition extends FieldDefinition<ColumnDefinition.Options> { | ||
declare class ColumnDefinition<Type extends Model.ColumnType> extends FieldDefinition<ColumnDefinition.Options<Type>> { | ||
type: "ColumnDefinition"; | ||
static create(type: Model.ColumnType, typeOptions?: ColumnDefinition.TypeOptions): ColumnDefinition; | ||
columnName(columnName: string): Interface<ColumnDefinition>; | ||
nullable(): Interface<ColumnDefinition>; | ||
notNull(): Interface<ColumnDefinition>; | ||
unique(): Interface<ColumnDefinition>; | ||
static create<Type extends Model.ColumnType>(type: Type, typeOptions?: ColumnDefinition.TypeOptions): ColumnDefinition<Type>; | ||
columnName(columnName: string): Interface<ColumnDefinition<Type>>; | ||
nullable(): Interface<ColumnDefinition<Type>>; | ||
notNull(): Interface<ColumnDefinition<Type>>; | ||
unique(): Interface<ColumnDefinition<Type>>; | ||
default(value: ColumnDefinition<Type>['options']['default']): Interface<ColumnDefinition<Type>>; | ||
createField({ name, conventions, enumRegistry, entityName }: FieldDefinition.CreateFieldContext): Model.AnyField; | ||
@@ -19,3 +20,3 @@ } | ||
}; | ||
type Options = { | ||
type Options<Type extends Model.ColumnType> = { | ||
type: Model.ColumnType; | ||
@@ -25,2 +26,3 @@ columnName?: string; | ||
nullable?: boolean; | ||
default?: Model.ColumnByType<Type>['default']; | ||
} & TypeOptions; | ||
@@ -27,0 +29,0 @@ } |
@@ -28,4 +28,7 @@ "use strict"; | ||
} | ||
default(value) { | ||
return this.withOption('default', value); | ||
} | ||
createField({ name, conventions, enumRegistry, entityName }) { | ||
const { type, nullable, columnName, enumDefinition } = this.options; | ||
const { type, nullable, columnName, enumDefinition, default: defaultValue } = this.options; | ||
const common = { | ||
@@ -35,2 +38,3 @@ name: name, | ||
nullable: nullable === undefined ? true : nullable, | ||
default: defaultValue, | ||
}; | ||
@@ -54,2 +58,5 @@ switch (type) { | ||
let enumName; | ||
if (!enumDefinition) { | ||
throw new Error(); | ||
} | ||
if (enumRegistry.has(enumDefinition)) { | ||
@@ -56,0 +63,0 @@ enumName = enumRegistry.getName(enumDefinition); |
@@ -14,10 +14,10 @@ import { Interface } from './types'; | ||
import 'reflect-metadata'; | ||
export declare function column(type: Model.ColumnType, typeOptions?: ColumnDefinition.TypeOptions): ColumnDefinition; | ||
export declare function stringColumn(): ColumnDefinition; | ||
export declare function intColumn(): ColumnDefinition; | ||
export declare function boolColumn(): ColumnDefinition; | ||
export declare function doubleColumn(): ColumnDefinition; | ||
export declare function dateColumn(): ColumnDefinition; | ||
export declare function dateTimeColumn(): ColumnDefinition; | ||
export declare function enumColumn(enumDefinition: EnumDefinition): ColumnDefinition; | ||
export declare function column(type: Model.ColumnType, typeOptions?: ColumnDefinition.TypeOptions): ColumnDefinition<Model.ColumnType>; | ||
export declare function stringColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function intColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function boolColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function doubleColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function dateColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function dateTimeColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function enumColumn(enumDefinition: EnumDefinition): ColumnDefinition<Model.ColumnType>; | ||
declare type KeysOfType<T, TProp> = { | ||
@@ -24,0 +24,0 @@ [P in keyof T]: T[P] extends TProp ? P : never; |
{ | ||
"name": "@contember/schema-definition", | ||
"version": "0.8.0-alpha.1", | ||
"version": "0.8.0-alpha.2", | ||
"license": "Apache-2.0", | ||
@@ -9,4 +9,4 @@ "main": "dist/src/index.js", | ||
"dependencies": { | ||
"@contember/schema": "^0.8.0-alpha.1", | ||
"@contember/schema-utils": "^0.8.0-alpha.1", | ||
"@contember/schema": "^0.8.0-alpha.2", | ||
"@contember/schema-utils": "^0.8.0-alpha.2", | ||
"reflect-metadata": "^0.1.13" | ||
@@ -17,3 +17,3 @@ }, | ||
}, | ||
"gitHead": "c20d28db4163768b7e479fabf8ffb2d15a7fc153" | ||
"gitHead": "dfc666f11f4d24a67b260bf57f62b420657d51c2" | ||
} |
@@ -46,2 +46,3 @@ import FieldProcessor from './FieldProcessor' | ||
case Model.ColumnType.Enum: | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return { ...common, type: type, columnType: options.enumName!, enumName: options.enumName! } | ||
@@ -48,0 +49,0 @@ default: |
@@ -6,6 +6,9 @@ import { Model } from '@contember/schema' | ||
class ColumnDefinition extends FieldDefinition<ColumnDefinition.Options> { | ||
class ColumnDefinition<Type extends Model.ColumnType> extends FieldDefinition<ColumnDefinition.Options<Type>> { | ||
type = 'ColumnDefinition' as const | ||
public static create(type: Model.ColumnType, typeOptions: ColumnDefinition.TypeOptions = {}): ColumnDefinition { | ||
public static create<Type extends Model.ColumnType>( | ||
type: Type, | ||
typeOptions: ColumnDefinition.TypeOptions = {}, | ||
): ColumnDefinition<Type> { | ||
return new ColumnDefinition({ | ||
@@ -17,20 +20,24 @@ type: type, | ||
public columnName(columnName: string): Interface<ColumnDefinition> { | ||
public columnName(columnName: string): Interface<ColumnDefinition<Type>> { | ||
return this.withOption('columnName', columnName) | ||
} | ||
public nullable(): Interface<ColumnDefinition> { | ||
public nullable(): Interface<ColumnDefinition<Type>> { | ||
return this.withOption('nullable', true) | ||
} | ||
public notNull(): Interface<ColumnDefinition> { | ||
public notNull(): Interface<ColumnDefinition<Type>> { | ||
return this.withOption('nullable', false) | ||
} | ||
public unique(): Interface<ColumnDefinition> { | ||
public unique(): Interface<ColumnDefinition<Type>> { | ||
return this.withOption('unique', true) | ||
} | ||
public default(value: ColumnDefinition<Type>['options']['default']): Interface<ColumnDefinition<Type>> { | ||
return this.withOption('default', value) | ||
} | ||
createField({ name, conventions, enumRegistry, entityName }: FieldDefinition.CreateFieldContext): Model.AnyField { | ||
const { type, nullable, columnName, enumDefinition } = this.options | ||
const { type, nullable, columnName, enumDefinition, default: defaultValue } = this.options | ||
const common = { | ||
@@ -40,2 +47,3 @@ name: name, | ||
nullable: nullable === undefined ? true : nullable, | ||
default: defaultValue as any, | ||
} | ||
@@ -60,10 +68,13 @@ | ||
let enumName: string | ||
if (enumRegistry.has(enumDefinition!)) { | ||
enumName = enumRegistry.getName(enumDefinition!) | ||
if (!enumDefinition) { | ||
throw new Error() | ||
} | ||
if (enumRegistry.has(enumDefinition)) { | ||
enumName = enumRegistry.getName(enumDefinition) | ||
} else { | ||
enumName = entityName + name.substring(0, 1).toUpperCase() + name.substring(1) | ||
enumRegistry.register(enumName, enumDefinition!) | ||
enumRegistry.register(enumName, enumDefinition) | ||
} | ||
return { ...common, type: type, columnType: enumName!, enumName: enumName! } | ||
return { ...common, type: type, columnType: enumName, enumName: enumName } | ||
default: | ||
@@ -82,3 +93,3 @@ ;(({}: never): never => { | ||
export type Options = { | ||
export type Options<Type extends Model.ColumnType> = { | ||
type: Model.ColumnType | ||
@@ -88,2 +99,3 @@ columnName?: string | ||
nullable?: boolean | ||
default?: Model.ColumnByType<Type>['default'] | ||
} & TypeOptions | ||
@@ -90,0 +102,0 @@ } |
@@ -72,3 +72,3 @@ import { Model } from '@contember/schema' | ||
private createPrimaryColumn(): ColumnDefinition { | ||
private createPrimaryColumn(): ColumnDefinition<Model.ColumnType.Uuid> { | ||
return new ColumnDefinition({ | ||
@@ -75,0 +75,0 @@ nullable: false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
493549
4782
7