@contember/schema-definition
Advanced tools
Comparing version 0.10.0-alpha.7 to 0.10.0-alpha.8
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const schema_1 = require("@contember/schema"); | ||
const getColumnType_1 = require("../../utils/getColumnType"); | ||
class ColumnProcessor { | ||
@@ -18,26 +19,7 @@ constructor(conventions) { | ||
}; | ||
switch (type) { | ||
case schema_1.Model.ColumnType.Int: | ||
return { ...common, type: type, columnType: 'integer' }; | ||
case schema_1.Model.ColumnType.Double: | ||
return { ...common, type: type, columnType: 'double precision' }; | ||
case schema_1.Model.ColumnType.String: | ||
return { ...common, type: type, columnType: 'text' }; | ||
case schema_1.Model.ColumnType.Uuid: | ||
return { ...common, type: type, columnType: 'uuid' }; | ||
case schema_1.Model.ColumnType.Bool: | ||
return { ...common, type: type, columnType: 'boolean' }; | ||
case schema_1.Model.ColumnType.DateTime: | ||
return { ...common, type: type, columnType: 'timestamptz' }; | ||
case schema_1.Model.ColumnType.Date: | ||
return { ...common, type: type, columnType: 'date' }; | ||
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 }; | ||
default: | ||
; | ||
(({}) => { | ||
throw new Error(); | ||
})(type); | ||
if (type === schema_1.Model.ColumnType.Enum) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return { ...common, type: type, columnType: options.enumName }; | ||
} | ||
return { ...common, type, columnType: getColumnType_1.getColumnType(type) }; | ||
} | ||
@@ -44,0 +26,0 @@ } |
@@ -9,2 +9,3 @@ import { Model } from '@contember/schema'; | ||
columnName(columnName: string): Interface<ColumnDefinition<Type>>; | ||
columnType(columnType: string): Interface<ColumnDefinition<Type>>; | ||
nullable(): Interface<ColumnDefinition<Type>>; | ||
@@ -22,6 +23,7 @@ notNull(): Interface<ColumnDefinition<Type>>; | ||
type: Model.ColumnType; | ||
columnType?: string; | ||
columnName?: string; | ||
unique?: boolean; | ||
nullable?: boolean; | ||
default?: Model.ColumnByType<Type>['default']; | ||
default?: Model.ColumnTypeDefinition['default']; | ||
} & TypeOptions; | ||
@@ -28,0 +30,0 @@ } |
@@ -5,2 +5,3 @@ "use strict"; | ||
const FieldDefinition_1 = require("./FieldDefinition"); | ||
const getColumnType_1 = require("../utils/getColumnType"); | ||
class ColumnDefinition extends FieldDefinition_1.default { | ||
@@ -20,2 +21,5 @@ constructor() { | ||
} | ||
columnType(columnType) { | ||
return this.withOption('columnType', columnType); | ||
} | ||
nullable() { | ||
@@ -34,3 +38,3 @@ return this.withOption('nullable', true); | ||
createField({ name, conventions, enumRegistry, entityName }) { | ||
const { type, nullable, columnName, enumDefinition, default: defaultValue } = this.options; | ||
const { type, nullable, columnName, enumDefinition, default: defaultValue, columnType } = this.options; | ||
const common = { | ||
@@ -42,36 +46,17 @@ name: name, | ||
}; | ||
switch (type) { | ||
case schema_1.Model.ColumnType.Int: | ||
return { ...common, type: type, columnType: 'integer' }; | ||
case schema_1.Model.ColumnType.Double: | ||
return { ...common, type: type, columnType: 'double precision' }; | ||
case schema_1.Model.ColumnType.String: | ||
return { ...common, type: type, columnType: 'text' }; | ||
case schema_1.Model.ColumnType.Uuid: | ||
return { ...common, type: type, columnType: 'uuid' }; | ||
case schema_1.Model.ColumnType.Bool: | ||
return { ...common, type: type, columnType: 'boolean' }; | ||
case schema_1.Model.ColumnType.DateTime: | ||
return { ...common, type: type, columnType: 'timestamptz' }; | ||
case schema_1.Model.ColumnType.Date: | ||
return { ...common, type: type, columnType: 'date' }; | ||
case schema_1.Model.ColumnType.Enum: | ||
let enumName; | ||
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); | ||
} | ||
return { ...common, type: type, columnType: enumName, enumName: enumName }; | ||
default: | ||
; | ||
(({}) => { | ||
throw new Error(); | ||
})(type); | ||
if (type === schema_1.Model.ColumnType.Enum) { | ||
let enumName; | ||
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); | ||
} | ||
return { ...common, type: type, columnType: enumName }; | ||
} | ||
return { ...common, type: type, columnType: columnType || getColumnType_1.getColumnType(type) }; | ||
} | ||
@@ -78,0 +63,0 @@ } |
@@ -22,2 +22,3 @@ import { Interface } from './types'; | ||
export declare function dateTimeColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function jsonColumn(): ColumnDefinition<Model.ColumnType>; | ||
export declare function enumColumn(enumDefinition: EnumDefinition): ColumnDefinition<Model.ColumnType>; | ||
@@ -24,0 +25,0 @@ export declare function manyHasOne(target: EntityConstructor, inversedBy?: string): ManyHasOneDefinition; |
@@ -13,3 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.EnumDefinition = exports.OrderDirection = exports.OnDelete = exports.createModel = exports.Entity = exports.Unique = exports.createEnum = exports.oneHasOneInversed = exports.oneHasOneInverse = exports.oneHasOne = exports.manyHasManyInversed = exports.manyHasManyInverse = exports.manyHasMany = exports.oneHasMany = exports.manyHasOne = exports.enumColumn = exports.dateTimeColumn = exports.dateColumn = exports.doubleColumn = exports.boolColumn = exports.intColumn = exports.stringColumn = exports.column = void 0; | ||
exports.EnumDefinition = exports.OrderDirection = exports.OnDelete = exports.createModel = exports.Entity = exports.Unique = exports.createEnum = exports.oneHasOneInversed = exports.oneHasOneInverse = exports.oneHasOne = exports.manyHasManyInversed = exports.manyHasManyInverse = exports.manyHasMany = exports.oneHasMany = exports.manyHasOne = exports.enumColumn = exports.jsonColumn = exports.dateTimeColumn = exports.dateColumn = exports.doubleColumn = exports.boolColumn = exports.intColumn = exports.stringColumn = exports.column = void 0; | ||
const schema_1 = require("@contember/schema"); | ||
@@ -57,2 +57,6 @@ const ColumnDefinition_1 = require("./ColumnDefinition"); | ||
exports.dateTimeColumn = dateTimeColumn; | ||
function jsonColumn() { | ||
return column(schema_1.Model.ColumnType.Json); | ||
} | ||
exports.jsonColumn = jsonColumn; | ||
function enumColumn(enumDefinition) { | ||
@@ -59,0 +63,0 @@ return column(schema_1.Model.ColumnType.Enum, { enumDefinition }); |
@@ -0,2 +1,3 @@ | ||
export * from './assertNever'; | ||
export * from './tuple'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -13,3 +13,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./assertNever"), exports); | ||
__exportStar(require("./tuple"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@contember/schema-definition", | ||
"version": "0.10.0-alpha.7", | ||
"version": "0.10.0-alpha.8", | ||
"license": "Apache-2.0", | ||
@@ -11,4 +11,4 @@ "main": "dist/src/index.js", | ||
"dependencies": { | ||
"@contember/schema": "^0.10.0-alpha.7", | ||
"@contember/schema-utils": "^0.10.0-alpha.7", | ||
"@contember/schema": "^0.10.0-alpha.8", | ||
"@contember/schema-utils": "^0.10.0-alpha.8", | ||
"reflect-metadata": "^0.1.13" | ||
@@ -15,0 +15,0 @@ }, |
@@ -5,2 +5,3 @@ import FieldProcessor from './FieldProcessor' | ||
import { Model } from '@contember/schema' | ||
import { getColumnType } from '../../utils/getColumnType' | ||
@@ -30,27 +31,8 @@ export default class ColumnProcessor implements FieldProcessor<ColumnBuilder.Options> { | ||
} | ||
switch (type) { | ||
case Model.ColumnType.Int: | ||
return { ...common, type: type, columnType: 'integer' } | ||
case Model.ColumnType.Double: | ||
return { ...common, type: type, columnType: 'double precision' } | ||
case Model.ColumnType.String: | ||
return { ...common, type: type, columnType: 'text' } | ||
case Model.ColumnType.Uuid: | ||
return { ...common, type: type, columnType: 'uuid' } | ||
case Model.ColumnType.Bool: | ||
return { ...common, type: type, columnType: 'boolean' } | ||
case Model.ColumnType.DateTime: | ||
return { ...common, type: type, columnType: 'timestamptz' } | ||
case Model.ColumnType.Date: | ||
return { ...common, type: type, columnType: 'date' } | ||
case Model.ColumnType.Enum: | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return { ...common, type: type, columnType: options.enumName!, enumName: options.enumName! } | ||
default: | ||
;(({}: never): never => { | ||
throw new Error() | ||
})(type) | ||
if (type === Model.ColumnType.Enum) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
return { ...common, type: type, columnType: options.enumName! } | ||
} | ||
return { ...common, type, columnType: getColumnType(type) } | ||
} | ||
} |
@@ -5,2 +5,3 @@ import { Model } from '@contember/schema' | ||
import EnumDefinition from './EnumDefinition' | ||
import { getColumnType } from '../utils/getColumnType' | ||
@@ -24,2 +25,6 @@ class ColumnDefinition<Type extends Model.ColumnType> extends FieldDefinition<ColumnDefinition.Options<Type>> { | ||
public columnType(columnType: string): Interface<ColumnDefinition<Type>> { | ||
return this.withOption('columnType', columnType) | ||
} | ||
public nullable(): Interface<ColumnDefinition<Type>> { | ||
@@ -42,3 +47,3 @@ return this.withOption('nullable', true) | ||
createField({ name, conventions, enumRegistry, entityName }: FieldDefinition.CreateFieldContext): Model.AnyField { | ||
const { type, nullable, columnName, enumDefinition, default: defaultValue } = this.options | ||
const { type, nullable, columnName, enumDefinition, default: defaultValue, columnType } = this.options | ||
const common = { | ||
@@ -48,38 +53,19 @@ name: name, | ||
nullable: nullable === undefined ? true : nullable, | ||
...(defaultValue !== undefined ? { default: defaultValue as any } : {}), | ||
...(defaultValue !== undefined ? { default: defaultValue } : {}), | ||
} | ||
if (type === Model.ColumnType.Enum) { | ||
let enumName: string | ||
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) | ||
} | ||
switch (type) { | ||
case Model.ColumnType.Int: | ||
return { ...common, type: type, columnType: 'integer' } | ||
case Model.ColumnType.Double: | ||
return { ...common, type: type, columnType: 'double precision' } | ||
case Model.ColumnType.String: | ||
return { ...common, type: type, columnType: 'text' } | ||
case Model.ColumnType.Uuid: | ||
return { ...common, type: type, columnType: 'uuid' } | ||
case Model.ColumnType.Bool: | ||
return { ...common, type: type, columnType: 'boolean' } | ||
case Model.ColumnType.DateTime: | ||
return { ...common, type: type, columnType: 'timestamptz' } | ||
case Model.ColumnType.Date: | ||
return { ...common, type: type, columnType: 'date' } | ||
case Model.ColumnType.Enum: | ||
let enumName: string | ||
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) | ||
} | ||
return { ...common, type: type, columnType: enumName, enumName: enumName } | ||
default: | ||
;(({}: never): never => { | ||
throw new Error() | ||
})(type) | ||
return { ...common, type: type, columnType: enumName } | ||
} | ||
return { ...common, type: type, columnType: columnType || getColumnType(type) } | ||
} | ||
@@ -95,6 +81,7 @@ } | ||
type: Model.ColumnType | ||
columnType?: string | ||
columnName?: string | ||
unique?: boolean | ||
nullable?: boolean | ||
default?: Model.ColumnByType<Type>['default'] | ||
default?: Model.ColumnTypeDefinition['default'] | ||
} & TypeOptions | ||
@@ -101,0 +88,0 @@ } |
@@ -46,2 +46,6 @@ import { Interface } from './types' | ||
export function jsonColumn() { | ||
return column(Model.ColumnType.Json) | ||
} | ||
export function enumColumn(enumDefinition: EnumDefinition) { | ||
@@ -48,0 +52,0 @@ return column(Model.ColumnType.Enum, { enumDefinition }) |
@@ -0,1 +1,2 @@ | ||
export * from './assertNever' | ||
export * from './tuple' |
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
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
562396
246
4746