mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
initialized postgres support
This commit is contained in:
@@ -32,9 +32,11 @@ export class BooleanField<Required extends true | false = false> extends Field<
|
||||
}
|
||||
}
|
||||
|
||||
schema() {
|
||||
// @todo: potentially use "integer" instead
|
||||
return this.useSchemaHelper("boolean");
|
||||
override schema() {
|
||||
return Object.freeze({
|
||||
...super.schema()!,
|
||||
type: "boolean",
|
||||
});
|
||||
}
|
||||
|
||||
override getHtmlConfig() {
|
||||
|
||||
@@ -32,8 +32,10 @@ export class DateField<Required extends true | false = false> extends Field<
|
||||
}
|
||||
|
||||
override schema() {
|
||||
const type = this.config.type === "datetime" ? "datetime" : "date";
|
||||
return this.useSchemaHelper(type);
|
||||
return Object.freeze({
|
||||
...super.schema()!,
|
||||
type: this.config.type === "datetime" ? "datetime" : "date",
|
||||
});
|
||||
}
|
||||
|
||||
override getHtmlConfig() {
|
||||
|
||||
@@ -66,10 +66,6 @@ export class EnumField<Required extends true | false = false, TypeOverride = str
|
||||
return enumFieldConfigSchema;
|
||||
}
|
||||
|
||||
override schema() {
|
||||
return this.useSchemaHelper("text");
|
||||
}
|
||||
|
||||
getOptions(): { label: string; value: string }[] {
|
||||
const options = this.config?.options ?? { type: "strings", values: [] };
|
||||
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import {
|
||||
parse,
|
||||
snakeToPascalWithSpaces,
|
||||
type Static,
|
||||
StringEnum,
|
||||
type TSchema,
|
||||
Type,
|
||||
TypeInvalidError,
|
||||
parse,
|
||||
snakeToPascalWithSpaces,
|
||||
} from "core/utils";
|
||||
import type { ColumnBuilderCallback, ColumnDataType, ColumnDefinitionBuilder } from "kysely";
|
||||
import type { HTMLInputTypeAttribute, InputHTMLAttributes } from "react";
|
||||
import type { EntityManager } from "../entities";
|
||||
import { InvalidFieldConfigException, TransformPersistFailedException } from "../errors";
|
||||
import type { FieldSpec } from "data/connection/Connection";
|
||||
|
||||
// @todo: contexts need to be reworked
|
||||
// e.g. "table" is irrelevant, because if read is not given, it fails
|
||||
@@ -67,8 +67,6 @@ export const baseFieldConfigSchema = Type.Object(
|
||||
);
|
||||
export type BaseFieldConfig = Static<typeof baseFieldConfigSchema>;
|
||||
|
||||
export type SchemaResponse = [string, ColumnDataType, ColumnBuilderCallback] | undefined;
|
||||
|
||||
export abstract class Field<
|
||||
Config extends BaseFieldConfig = BaseFieldConfig,
|
||||
Type = any,
|
||||
@@ -106,25 +104,18 @@ export abstract class Field<
|
||||
|
||||
protected abstract getSchema(): TSchema;
|
||||
|
||||
protected useSchemaHelper(
|
||||
type: ColumnDataType,
|
||||
builder?: (col: ColumnDefinitionBuilder) => ColumnDefinitionBuilder,
|
||||
): SchemaResponse {
|
||||
return [
|
||||
this.name,
|
||||
type,
|
||||
(col: ColumnDefinitionBuilder) => {
|
||||
if (builder) return builder(col);
|
||||
return col;
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in SchemaManager.ts
|
||||
* @param em
|
||||
*/
|
||||
abstract schema(em: EntityManager<any>): SchemaResponse;
|
||||
schema(): FieldSpec | undefined {
|
||||
return Object.freeze({
|
||||
name: this.name,
|
||||
type: "text",
|
||||
nullable: true,
|
||||
dflt: this.getDefault(),
|
||||
});
|
||||
}
|
||||
|
||||
hasDefault() {
|
||||
return this.config.default_value !== undefined;
|
||||
|
||||
@@ -18,10 +18,6 @@ export class JsonField<Required extends true | false = false, TypeOverride = obj
|
||||
return jsonFieldConfigSchema;
|
||||
}
|
||||
|
||||
override schema() {
|
||||
return this.useSchemaHelper("text");
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform value after retrieving from database
|
||||
* @param value
|
||||
|
||||
@@ -36,10 +36,6 @@ export class JsonSchemaField<
|
||||
return jsonSchemaFieldConfigSchema;
|
||||
}
|
||||
|
||||
override schema() {
|
||||
return this.useSchemaHelper("text");
|
||||
}
|
||||
|
||||
getJsonSchema(): JsonSchema {
|
||||
return this.config?.schema as JsonSchema;
|
||||
}
|
||||
|
||||
@@ -44,8 +44,11 @@ export class NumberField<Required extends true | false = false> extends Field<
|
||||
};
|
||||
}
|
||||
|
||||
schema() {
|
||||
return this.useSchemaHelper("integer");
|
||||
override schema() {
|
||||
return Object.freeze({
|
||||
...super.schema()!,
|
||||
type: "integer",
|
||||
});
|
||||
}
|
||||
|
||||
override getValue(value: any, context?: TRenderContext): any {
|
||||
|
||||
@@ -30,9 +30,12 @@ export class PrimaryField<Required extends true | false = false> extends Field<
|
||||
return baseFieldConfigSchema;
|
||||
}
|
||||
|
||||
schema() {
|
||||
return this.useSchemaHelper("integer", (col) => {
|
||||
return col.primaryKey().notNull().autoIncrement();
|
||||
override schema() {
|
||||
return Object.freeze({
|
||||
type: "integer",
|
||||
name: this.name,
|
||||
primary: true,
|
||||
nullable: false,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -47,10 +47,6 @@ export class TextField<Required extends true | false = false> extends Field<
|
||||
return textFieldConfigSchema;
|
||||
}
|
||||
|
||||
override schema() {
|
||||
return this.useSchemaHelper("text");
|
||||
}
|
||||
|
||||
override getHtmlConfig() {
|
||||
if (this.config.html_config) {
|
||||
return this.config.html_config as any;
|
||||
|
||||
@@ -17,7 +17,7 @@ export class VirtualField extends Field<VirtualFieldConfig> {
|
||||
return virtualFieldConfigSchema;
|
||||
}
|
||||
|
||||
schema() {
|
||||
override schema() {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user