Rename schema to property (#7761)
This commit is contained in:
@@ -0,0 +1,69 @@
|
|||||||
|
import { expectTypeOf } from "expect-type"
|
||||||
|
import { BaseProperty } from "../properties/base"
|
||||||
|
import { PropertyMetadata } from "../types"
|
||||||
|
|
||||||
|
describe("Base property", () => {
|
||||||
|
test("create a property type from base property", () => {
|
||||||
|
class StringProperty extends BaseProperty<string> {
|
||||||
|
protected dataType: PropertyMetadata["dataType"] = {
|
||||||
|
name: "text",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const property = new StringProperty()
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<string>()
|
||||||
|
expect(property.parse("username")).toEqual({
|
||||||
|
fieldName: "username",
|
||||||
|
dataType: {
|
||||||
|
name: "text",
|
||||||
|
},
|
||||||
|
nullable: false,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test("apply nullable modifier", () => {
|
||||||
|
class StringProperty extends BaseProperty<string> {
|
||||||
|
protected dataType: PropertyMetadata["dataType"] = {
|
||||||
|
name: "text",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const property = new StringProperty().nullable()
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<string | null>()
|
||||||
|
expect(property.parse("username")).toEqual({
|
||||||
|
fieldName: "username",
|
||||||
|
dataType: {
|
||||||
|
name: "text",
|
||||||
|
},
|
||||||
|
nullable: true,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test("define default value", () => {
|
||||||
|
class StringProperty extends BaseProperty<string> {
|
||||||
|
protected dataType: PropertyMetadata["dataType"] = {
|
||||||
|
name: "text",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const property = new StringProperty().default("foo")
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<string>()
|
||||||
|
expect(property.parse("username")).toEqual({
|
||||||
|
fieldName: "username",
|
||||||
|
dataType: {
|
||||||
|
name: "text",
|
||||||
|
},
|
||||||
|
defaultValue: "foo",
|
||||||
|
nullable: false,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
import { expectTypeOf } from "expect-type"
|
||||||
import { TextSchema } from "../schema/text"
|
import { TextProperty } from "../properties/text"
|
||||||
import { BaseRelationship } from "../relations/base"
|
import { BaseRelationship } from "../relations/base"
|
||||||
|
|
||||||
describe("Base relationship", () => {
|
describe("Base relationship", () => {
|
||||||
@@ -9,7 +9,7 @@ describe("Base relationship", () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const user = {
|
const user = {
|
||||||
username: new TextSchema(),
|
username: new TextProperty(),
|
||||||
}
|
}
|
||||||
|
|
||||||
const entityRef = () => user
|
const entityRef = () => user
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
|
||||||
import { BaseSchema } from "../schema/base"
|
|
||||||
import { SchemaMetadata } from "../types"
|
|
||||||
|
|
||||||
describe("Base schema", () => {
|
|
||||||
test("create a schema type from base schema", () => {
|
|
||||||
class StringSchema extends BaseSchema<string> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "text",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const schema = new StringSchema()
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<string>()
|
|
||||||
expect(schema.parse("username")).toEqual({
|
|
||||||
fieldName: "username",
|
|
||||||
dataType: {
|
|
||||||
name: "text",
|
|
||||||
},
|
|
||||||
nullable: false,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test("apply nullable modifier", () => {
|
|
||||||
class StringSchema extends BaseSchema<string> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "text",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const schema = new StringSchema().nullable()
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<string | null>()
|
|
||||||
expect(schema.parse("username")).toEqual({
|
|
||||||
fieldName: "username",
|
|
||||||
dataType: {
|
|
||||||
name: "text",
|
|
||||||
},
|
|
||||||
nullable: true,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test("define default value", () => {
|
|
||||||
class StringSchema extends BaseSchema<string> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "text",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const schema = new StringSchema().default("foo")
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<string>()
|
|
||||||
expect(schema.parse("username")).toEqual({
|
|
||||||
fieldName: "username",
|
|
||||||
dataType: {
|
|
||||||
name: "text",
|
|
||||||
},
|
|
||||||
defaultValue: "foo",
|
|
||||||
nullable: false,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { expectTypeOf } from "expect-type"
|
||||||
|
import { BooleanProperty } from "../properties/boolean"
|
||||||
|
|
||||||
|
describe("Boolean property", () => {
|
||||||
|
test("create boolean property type", () => {
|
||||||
|
const property = new BooleanProperty()
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<boolean>()
|
||||||
|
expect(property.parse("isAdmin")).toEqual({
|
||||||
|
fieldName: "isAdmin",
|
||||||
|
dataType: {
|
||||||
|
name: "boolean",
|
||||||
|
},
|
||||||
|
nullable: false,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
|
||||||
import { BooleanSchema } from "../schema/boolean"
|
|
||||||
|
|
||||||
describe("Boolean schema", () => {
|
|
||||||
test("create boolean schema type", () => {
|
|
||||||
const schema = new BooleanSchema()
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<boolean>()
|
|
||||||
expect(schema.parse("isAdmin")).toEqual({
|
|
||||||
fieldName: "isAdmin",
|
|
||||||
dataType: {
|
|
||||||
name: "boolean",
|
|
||||||
},
|
|
||||||
nullable: false,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { expectTypeOf } from "expect-type"
|
||||||
|
import { DateTimeProperty } from "../properties/date-time"
|
||||||
|
|
||||||
|
describe("DateTime property", () => {
|
||||||
|
test("create datetime property type", () => {
|
||||||
|
const property = new DateTimeProperty()
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<Date>()
|
||||||
|
expect(property.parse("created_at")).toEqual({
|
||||||
|
fieldName: "created_at",
|
||||||
|
dataType: {
|
||||||
|
name: "dateTime",
|
||||||
|
},
|
||||||
|
nullable: false,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
|
||||||
import { DateTimeSchema } from "../schema/date-time"
|
|
||||||
|
|
||||||
describe("DateTime schema", () => {
|
|
||||||
test("create datetime schema type", () => {
|
|
||||||
const schema = new DateTimeSchema()
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<Date>()
|
|
||||||
expect(schema.parse("created_at")).toEqual({
|
|
||||||
fieldName: "created_at",
|
|
||||||
dataType: {
|
|
||||||
name: "dateTime",
|
|
||||||
},
|
|
||||||
nullable: false,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
import { expectTypeOf } from "expect-type"
|
||||||
import { EnumSchema } from "../schema/enum"
|
import { EnumProperty } from "../properties/enum"
|
||||||
|
|
||||||
describe("Enum schema", () => {
|
describe("Enum property", () => {
|
||||||
test("create enum schema type", () => {
|
test("create enum property type", () => {
|
||||||
const schema = new EnumSchema(["admin", "moderator", "editor"])
|
const property = new EnumProperty(["admin", "moderator", "editor"])
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<
|
||||||
"admin" | "moderator" | "editor"
|
"admin" | "moderator" | "editor"
|
||||||
>()
|
>()
|
||||||
|
|
||||||
expect(schema.parse("role")).toEqual({
|
expect(property.parse("role")).toEqual({
|
||||||
fieldName: "role",
|
fieldName: "role",
|
||||||
dataType: {
|
dataType: {
|
||||||
name: "enum",
|
name: "enum",
|
||||||
@@ -23,13 +23,17 @@ describe("Enum schema", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test("apply nullable modifier", () => {
|
test("apply nullable modifier", () => {
|
||||||
const schema = new EnumSchema(["admin", "moderator", "editor"]).nullable()
|
const property = new EnumProperty([
|
||||||
|
"admin",
|
||||||
|
"moderator",
|
||||||
|
"editor",
|
||||||
|
]).nullable()
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<
|
||||||
"admin" | "moderator" | "editor" | null
|
"admin" | "moderator" | "editor" | null
|
||||||
>()
|
>()
|
||||||
|
|
||||||
expect(schema.parse("role")).toEqual({
|
expect(property.parse("role")).toEqual({
|
||||||
fieldName: "role",
|
fieldName: "role",
|
||||||
dataType: {
|
dataType: {
|
||||||
name: "enum",
|
name: "enum",
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
import { expectTypeOf } from "expect-type"
|
||||||
import { TextSchema } from "../schema/text"
|
import { TextProperty } from "../properties/text"
|
||||||
import { HasMany } from "../relations/has-many"
|
import { HasMany } from "../relations/has-many"
|
||||||
|
|
||||||
describe("HasMany relationship", () => {
|
describe("HasMany relationship", () => {
|
||||||
test("define hasMany relationship", () => {
|
test("define hasMany relationship", () => {
|
||||||
const user = {
|
const user = {
|
||||||
username: new TextSchema(),
|
username: new TextProperty(),
|
||||||
}
|
}
|
||||||
|
|
||||||
const entityRef = () => user
|
const entityRef = () => user
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
import { expectTypeOf } from "expect-type"
|
||||||
import { TextSchema } from "../schema/text"
|
import { TextProperty } from "../properties/text"
|
||||||
import { HasOne } from "../relations/has-one"
|
import { HasOne } from "../relations/has-one"
|
||||||
|
|
||||||
describe("HasOne relationship", () => {
|
describe("HasOne relationship", () => {
|
||||||
test("define hasOne relationship", () => {
|
test("define hasOne relationship", () => {
|
||||||
const user = {
|
const user = {
|
||||||
username: new TextSchema(),
|
username: new TextProperty(),
|
||||||
}
|
}
|
||||||
|
|
||||||
const entityRef = () => user
|
const entityRef = () => user
|
||||||
@@ -22,7 +22,7 @@ describe("HasOne relationship", () => {
|
|||||||
|
|
||||||
test("mark relationship as nullable", () => {
|
test("mark relationship as nullable", () => {
|
||||||
const user = {
|
const user = {
|
||||||
username: new TextSchema(),
|
username: new TextProperty(),
|
||||||
}
|
}
|
||||||
|
|
||||||
const entityRef = () => user
|
const entityRef = () => user
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { expectTypeOf } from "expect-type"
|
||||||
|
import { JSONProperty } from "../properties/json"
|
||||||
|
|
||||||
|
describe("JSON property", () => {
|
||||||
|
test("create json property type", () => {
|
||||||
|
const property = new JSONProperty()
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<string>()
|
||||||
|
expect(property.parse("coordinates")).toEqual({
|
||||||
|
fieldName: "coordinates",
|
||||||
|
dataType: {
|
||||||
|
name: "json",
|
||||||
|
},
|
||||||
|
nullable: false,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
|
||||||
import { JSONSchema } from "../schema/json"
|
|
||||||
|
|
||||||
describe("JSON schema", () => {
|
|
||||||
test("create json schema type", () => {
|
|
||||||
const schema = new JSONSchema()
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<string>()
|
|
||||||
expect(schema.parse("coordinates")).toEqual({
|
|
||||||
fieldName: "coordinates",
|
|
||||||
dataType: {
|
|
||||||
name: "json",
|
|
||||||
},
|
|
||||||
nullable: false,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
import { expectTypeOf } from "expect-type"
|
||||||
import { TextSchema } from "../schema/text"
|
import { TextProperty } from "../properties/text"
|
||||||
import { ManyToMany } from "../relations/many-to-many"
|
import { ManyToMany } from "../relations/many-to-many"
|
||||||
|
|
||||||
describe("ManyToMany relationship", () => {
|
describe("ManyToMany relationship", () => {
|
||||||
test("define manyToMany relationship", () => {
|
test("define manyToMany relationship", () => {
|
||||||
const user = {
|
const user = {
|
||||||
username: new TextSchema(),
|
username: new TextProperty(),
|
||||||
}
|
}
|
||||||
|
|
||||||
const entityRef = () => user
|
const entityRef = () => user
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { expectTypeOf } from "expect-type"
|
||||||
|
import { NumberProperty } from "../properties/number"
|
||||||
|
|
||||||
|
describe("Number property", () => {
|
||||||
|
test("create number property type", () => {
|
||||||
|
const property = new NumberProperty()
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<number>()
|
||||||
|
expect(property.parse("age")).toEqual({
|
||||||
|
fieldName: "age",
|
||||||
|
dataType: {
|
||||||
|
name: "number",
|
||||||
|
},
|
||||||
|
nullable: false,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
|
||||||
import { NumberSchema } from "../schema/number"
|
|
||||||
|
|
||||||
describe("Number schema", () => {
|
|
||||||
test("create number schema type", () => {
|
|
||||||
const schema = new NumberSchema()
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<number>()
|
|
||||||
expect(schema.parse("age")).toEqual({
|
|
||||||
fieldName: "age",
|
|
||||||
dataType: {
|
|
||||||
name: "number",
|
|
||||||
},
|
|
||||||
nullable: false,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { expectTypeOf } from "expect-type"
|
||||||
|
import { TextProperty } from "../properties/text"
|
||||||
|
|
||||||
|
describe("Text property", () => {
|
||||||
|
test("create text property type", () => {
|
||||||
|
const property = new TextProperty()
|
||||||
|
|
||||||
|
expectTypeOf(property["$dataType"]).toEqualTypeOf<string>()
|
||||||
|
expect(property.parse("username")).toEqual({
|
||||||
|
fieldName: "username",
|
||||||
|
dataType: {
|
||||||
|
name: "text",
|
||||||
|
},
|
||||||
|
nullable: false,
|
||||||
|
indexes: [],
|
||||||
|
relationships: [],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { expectTypeOf } from "expect-type"
|
|
||||||
import { TextSchema } from "../schema/text"
|
|
||||||
|
|
||||||
describe("String schema", () => {
|
|
||||||
test("create string schema type", () => {
|
|
||||||
const schema = new TextSchema()
|
|
||||||
|
|
||||||
expectTypeOf(schema["$dataType"]).toEqualTypeOf<string>()
|
|
||||||
expect(schema.parse("username")).toEqual({
|
|
||||||
fieldName: "username",
|
|
||||||
dataType: {
|
|
||||||
name: "text",
|
|
||||||
},
|
|
||||||
nullable: false,
|
|
||||||
indexes: [],
|
|
||||||
relationships: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -1,16 +1,20 @@
|
|||||||
import { DmlEntity } from "./entity"
|
import { DmlEntity } from "./entity"
|
||||||
import { TextSchema } from "./schema/text"
|
import { TextProperty } from "./properties/text"
|
||||||
import { EnumSchema } from "./schema/enum"
|
import { EnumProperty } from "./properties/enum"
|
||||||
import { JSONSchema } from "./schema/json"
|
import { JSONProperty } from "./properties/json"
|
||||||
import { HasOne } from "./relations/has-one"
|
import { HasOne } from "./relations/has-one"
|
||||||
import { HasMany } from "./relations/has-many"
|
import { HasMany } from "./relations/has-many"
|
||||||
import { NumberSchema } from "./schema/number"
|
import { NumberProperty } from "./properties/number"
|
||||||
import { BooleanSchema } from "./schema/boolean"
|
import { BooleanProperty } from "./properties/boolean"
|
||||||
import { BelongsTo } from "./relations/belongs-to"
|
import { BelongsTo } from "./relations/belongs-to"
|
||||||
import { DateTimeSchema } from "./schema/date-time"
|
import { DateTimeProperty } from "./properties/date-time"
|
||||||
import { ManyToMany } from "./relations/many-to-many"
|
import { ManyToMany } from "./relations/many-to-many"
|
||||||
import type { RelationshipOptions, RelationshipType, SchemaType } from "./types"
|
import type {
|
||||||
import { NullableModifier } from "./schema/nullable"
|
RelationshipOptions,
|
||||||
|
RelationshipType,
|
||||||
|
PropertyType,
|
||||||
|
} from "./types"
|
||||||
|
import { NullableModifier } from "./properties/nullable"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Entity builder exposes the API to create an entity and define its
|
* Entity builder exposes the API to create an entity and define its
|
||||||
@@ -22,15 +26,15 @@ export class EntityBuilder {
|
|||||||
* all the entities.
|
* all the entities.
|
||||||
*/
|
*/
|
||||||
define<
|
define<
|
||||||
Schema extends Record<string, SchemaType<any> | RelationshipType<any>>
|
Schema extends Record<string, PropertyType<any> | RelationshipType<any>>
|
||||||
>(name: string, schema: Schema) {
|
>(name: string, schema: Schema) {
|
||||||
return new DmlEntity<
|
return new DmlEntity<
|
||||||
Schema & {
|
Schema & {
|
||||||
deleted_at: NullableModifier<Date, DateTimeSchema>
|
deleted_at: NullableModifier<Date, DateTimeProperty>
|
||||||
}
|
}
|
||||||
>(name, {
|
>(name, {
|
||||||
...schema,
|
...schema,
|
||||||
deleted_at: new DateTimeSchema().nullable(),
|
deleted_at: new DateTimeProperty().nullable(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,28 +42,28 @@ export class EntityBuilder {
|
|||||||
* Define a text/string based column
|
* Define a text/string based column
|
||||||
*/
|
*/
|
||||||
text() {
|
text() {
|
||||||
return new TextSchema()
|
return new TextProperty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a boolean column
|
* Define a boolean column
|
||||||
*/
|
*/
|
||||||
boolean() {
|
boolean() {
|
||||||
return new BooleanSchema()
|
return new BooleanProperty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a numeric/integer column
|
* Define a numeric/integer column
|
||||||
*/
|
*/
|
||||||
number() {
|
number() {
|
||||||
return new NumberSchema()
|
return new NumberProperty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a timestampz column
|
* Define a timestampz column
|
||||||
*/
|
*/
|
||||||
dateTime() {
|
dateTime() {
|
||||||
return new DateTimeSchema()
|
return new DateTimeProperty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -67,7 +71,7 @@ export class EntityBuilder {
|
|||||||
* JSON string
|
* JSON string
|
||||||
*/
|
*/
|
||||||
json() {
|
json() {
|
||||||
return new JSONSchema()
|
return new JSONProperty()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,7 +79,7 @@ export class EntityBuilder {
|
|||||||
* of values are allowed.
|
* of values are allowed.
|
||||||
*/
|
*/
|
||||||
enum<const Values extends unknown>(values: Values[]) {
|
enum<const Values extends unknown>(values: Values[]) {
|
||||||
return new EnumSchema<Values>(values)
|
return new EnumProperty<Values>(values)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { BelongsTo } from "./relations/belongs-to"
|
import { BelongsTo } from "./relations/belongs-to"
|
||||||
import {
|
import {
|
||||||
SchemaType,
|
PropertyType,
|
||||||
EntityCascades,
|
EntityCascades,
|
||||||
RelationshipType,
|
RelationshipType,
|
||||||
ExtractEntityRelations,
|
ExtractEntityRelations,
|
||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
* name, its schema and relationships.
|
* name, its schema and relationships.
|
||||||
*/
|
*/
|
||||||
export class DmlEntity<
|
export class DmlEntity<
|
||||||
Schema extends Record<string, SchemaType<any> | RelationshipType<any>>
|
Schema extends Record<string, PropertyType<any> | RelationshipType<any>>
|
||||||
> {
|
> {
|
||||||
#cascades: EntityCascades<string[]> = {}
|
#cascades: EntityCascades<string[]> = {}
|
||||||
constructor(public name: string, public schema: Schema) {}
|
constructor(public name: string, public schema: Schema) {}
|
||||||
@@ -21,12 +21,14 @@ export class DmlEntity<
|
|||||||
*/
|
*/
|
||||||
parse(): {
|
parse(): {
|
||||||
name: string
|
name: string
|
||||||
schema: SchemaType<any> | RelationshipType<any>
|
schema: PropertyType<any> | RelationshipType<any>
|
||||||
cascades: EntityCascades<string[]>
|
cascades: EntityCascades<string[]>
|
||||||
} {
|
} {
|
||||||
return {
|
return {
|
||||||
name: this.name,
|
name: this.name,
|
||||||
schema: this.schema as unknown as SchemaType<any> | RelationshipType<any>,
|
schema: this.schema as unknown as
|
||||||
|
| PropertyType<any>
|
||||||
|
| RelationshipType<any>,
|
||||||
cascades: this.#cascades,
|
cascades: this.#cascades,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,10 @@ import {
|
|||||||
import { upperCaseFirst } from "../../common/upper-case-first"
|
import { upperCaseFirst } from "../../common/upper-case-first"
|
||||||
import type {
|
import type {
|
||||||
Infer,
|
Infer,
|
||||||
SchemaType,
|
PropertyType,
|
||||||
EntityCascades,
|
EntityCascades,
|
||||||
KnownDataTypes,
|
KnownDataTypes,
|
||||||
SchemaMetadata,
|
PropertyMetadata,
|
||||||
RelationshipType,
|
RelationshipType,
|
||||||
EntityConstructor,
|
EntityConstructor,
|
||||||
RelationshipMetadata,
|
RelationshipMetadata,
|
||||||
@@ -90,7 +90,7 @@ export function createMikrORMEntity() {
|
|||||||
*/
|
*/
|
||||||
function defineProperty(
|
function defineProperty(
|
||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
field: SchemaMetadata
|
field: PropertyMetadata
|
||||||
) {
|
) {
|
||||||
/**
|
/**
|
||||||
* Defining an enum property
|
* Defining an enum property
|
||||||
@@ -124,7 +124,7 @@ export function createMikrORMEntity() {
|
|||||||
function applyIndexes(
|
function applyIndexes(
|
||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
tableName: string,
|
tableName: string,
|
||||||
field: SchemaMetadata
|
field: PropertyMetadata
|
||||||
) {
|
) {
|
||||||
field.indexes.forEach((index) => {
|
field.indexes.forEach((index) => {
|
||||||
const name =
|
const name =
|
||||||
@@ -149,7 +149,7 @@ export function createMikrORMEntity() {
|
|||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
relationship: RelationshipMetadata,
|
relationship: RelationshipMetadata,
|
||||||
relatedEntity: DmlEntity<
|
relatedEntity: DmlEntity<
|
||||||
Record<string, SchemaType<any> | RelationshipType<any>>
|
Record<string, PropertyType<any> | RelationshipType<any>>
|
||||||
>,
|
>,
|
||||||
cascades: EntityCascades<string[]>
|
cascades: EntityCascades<string[]>
|
||||||
) {
|
) {
|
||||||
@@ -173,7 +173,7 @@ export function createMikrORMEntity() {
|
|||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
relationship: RelationshipMetadata,
|
relationship: RelationshipMetadata,
|
||||||
relatedEntity: DmlEntity<
|
relatedEntity: DmlEntity<
|
||||||
Record<string, SchemaType<any> | RelationshipType<any>>
|
Record<string, PropertyType<any> | RelationshipType<any>>
|
||||||
>,
|
>,
|
||||||
cascades: EntityCascades<string[]>
|
cascades: EntityCascades<string[]>
|
||||||
) {
|
) {
|
||||||
@@ -203,7 +203,7 @@ export function createMikrORMEntity() {
|
|||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
relationship: RelationshipMetadata,
|
relationship: RelationshipMetadata,
|
||||||
relatedEntity: DmlEntity<
|
relatedEntity: DmlEntity<
|
||||||
Record<string, SchemaType<any> | RelationshipType<any>>
|
Record<string, PropertyType<any> | RelationshipType<any>>
|
||||||
>
|
>
|
||||||
) {
|
) {
|
||||||
const mappedBy =
|
const mappedBy =
|
||||||
@@ -280,7 +280,7 @@ export function createMikrORMEntity() {
|
|||||||
MikroORMEntity: EntityConstructor<any>,
|
MikroORMEntity: EntityConstructor<any>,
|
||||||
relationship: RelationshipMetadata,
|
relationship: RelationshipMetadata,
|
||||||
relatedEntity: DmlEntity<
|
relatedEntity: DmlEntity<
|
||||||
Record<string, SchemaType<any> | RelationshipType<any>>
|
Record<string, PropertyType<any> | RelationshipType<any>>
|
||||||
>,
|
>,
|
||||||
cascades: EntityCascades<string[]>
|
cascades: EntityCascades<string[]>
|
||||||
) {
|
) {
|
||||||
|
|||||||
+15
-8
@@ -1,20 +1,27 @@
|
|||||||
import { SchemaMetadata, SchemaType } from "../types"
|
import { PropertyMetadata, PropertyType } from "../types"
|
||||||
import { NullableModifier } from "./nullable"
|
import { NullableModifier } from "./nullable"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base schema class offers shared affordances to define
|
* The BaseProperty class offers shared affordances to define
|
||||||
* schema classes
|
* property classes
|
||||||
*/
|
*/
|
||||||
export abstract class BaseSchema<T> implements SchemaType<T> {
|
export abstract class BaseProperty<T> implements PropertyType<T> {
|
||||||
#indexes: SchemaMetadata["indexes"] = []
|
/**
|
||||||
#relationships: SchemaMetadata["relationships"] = []
|
* Defined indexes and relationships
|
||||||
|
*/
|
||||||
|
#indexes: PropertyMetadata["indexes"] = []
|
||||||
|
#relationships: PropertyMetadata["relationships"] = []
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default value for the property
|
||||||
|
*/
|
||||||
#defaultValue?: T
|
#defaultValue?: T
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The runtime dataType for the schema. It is not the same as
|
* The runtime dataType for the schema. It is not the same as
|
||||||
* the "$dataType".
|
* the "$dataType".
|
||||||
*/
|
*/
|
||||||
protected abstract dataType: SchemaMetadata["dataType"]
|
protected abstract dataType: PropertyMetadata["dataType"]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A type-only property to infer the JavScript data-type
|
* A type-only property to infer the JavScript data-type
|
||||||
@@ -56,7 +63,7 @@ export abstract class BaseSchema<T> implements SchemaType<T> {
|
|||||||
/**
|
/**
|
||||||
* Returns the serialized metadata
|
* Returns the serialized metadata
|
||||||
*/
|
*/
|
||||||
parse(fieldName: string): SchemaMetadata {
|
parse(fieldName: string): PropertyMetadata {
|
||||||
return {
|
return {
|
||||||
fieldName,
|
fieldName,
|
||||||
dataType: this.dataType,
|
dataType: this.dataType,
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { BaseProperty } from "./base"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The BooleanProperty class is used to define a boolean
|
||||||
|
* property
|
||||||
|
*/
|
||||||
|
export class BooleanProperty extends BaseProperty<boolean> {
|
||||||
|
protected dataType = {
|
||||||
|
name: "boolean",
|
||||||
|
} as const
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { BaseProperty } from "./base"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The DateTimeProperty class is used to define a timestampz
|
||||||
|
* property
|
||||||
|
*/
|
||||||
|
export class DateTimeProperty extends BaseProperty<Date> {
|
||||||
|
protected dataType = {
|
||||||
|
name: "dateTime",
|
||||||
|
} as const
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { BaseProperty } from "./base"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The EnumProperty is used to define a property with pre-defined
|
||||||
|
* list of choices.
|
||||||
|
*/
|
||||||
|
export class EnumProperty<
|
||||||
|
const Values extends unknown
|
||||||
|
> extends BaseProperty<Values> {
|
||||||
|
protected dataType: {
|
||||||
|
name: "enum"
|
||||||
|
options: { choices: Values[] }
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(values: Values[]) {
|
||||||
|
super()
|
||||||
|
this.dataType = { name: "enum", options: { choices: values } }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { BaseProperty } from "./base"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The JSONProperty is used to define a property that stores
|
||||||
|
* data as a JSON string
|
||||||
|
*/
|
||||||
|
export class JSONProperty extends BaseProperty<string> {
|
||||||
|
protected dataType = {
|
||||||
|
name: "json",
|
||||||
|
} as const
|
||||||
|
}
|
||||||
+6
-7
@@ -1,12 +1,11 @@
|
|||||||
import { RelationshipType, SchemaType } from "../types"
|
import { PropertyType } from "../types"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nullable modifier marks a schema node as nullable
|
* Nullable modifier marks a schema node as nullable
|
||||||
*/
|
*/
|
||||||
export class NullableModifier<
|
export class NullableModifier<T, Schema extends PropertyType<T>>
|
||||||
T,
|
implements PropertyType<T | null>
|
||||||
Schema extends SchemaType<T> | RelationshipType<T>
|
{
|
||||||
> {
|
|
||||||
/**
|
/**
|
||||||
* A type-only property to infer the JavScript data-type
|
* A type-only property to infer the JavScript data-type
|
||||||
* of the schema property
|
* of the schema property
|
||||||
@@ -26,9 +25,9 @@ export class NullableModifier<
|
|||||||
/**
|
/**
|
||||||
* Returns the serialized metadata
|
* Returns the serialized metadata
|
||||||
*/
|
*/
|
||||||
parse(fieldName: string): ReturnType<Schema["parse"]> {
|
parse(fieldName: string) {
|
||||||
const schema = this.#schema.parse(fieldName)
|
const schema = this.#schema.parse(fieldName)
|
||||||
schema.nullable = true
|
schema.nullable = true
|
||||||
return schema as ReturnType<Schema["parse"]>
|
return schema
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { BaseProperty } from "./base"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The NumberProperty is used to define a numeric/integer
|
||||||
|
* property
|
||||||
|
*/
|
||||||
|
export class NumberProperty extends BaseProperty<number> {
|
||||||
|
protected dataType = {
|
||||||
|
name: "number",
|
||||||
|
} as const
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { BaseProperty } from "./base"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The TextProperty is used to define a textual property
|
||||||
|
*/
|
||||||
|
export class TextProperty extends BaseProperty<string> {
|
||||||
|
protected dataType = {
|
||||||
|
name: "text",
|
||||||
|
} as const
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { RelationshipType, SchemaType } from "../types"
|
import { RelationshipType, PropertyType } from "../types"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Nullable modifier marks a schema node as nullable
|
* Nullable modifier marks a schema node as nullable
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
import { SchemaMetadata } from "../types"
|
|
||||||
import { BaseSchema } from "./base"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The BooleanSchema class is used to define a boolean
|
|
||||||
* property
|
|
||||||
*/
|
|
||||||
export class BooleanSchema extends BaseSchema<boolean> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "boolean",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { SchemaMetadata } from "../types"
|
|
||||||
import { BaseSchema } from "./base"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The DateTimeSchema class is used to define a timestampz
|
|
||||||
* property
|
|
||||||
*/
|
|
||||||
export class DateTimeSchema extends BaseSchema<Date> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "dateTime",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
import { SchemaMetadata } from "../types"
|
|
||||||
import { BaseSchema } from "./base"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The EnumSchema is used to define a property with pre-defined
|
|
||||||
* list of choices.
|
|
||||||
*/
|
|
||||||
export class EnumSchema<
|
|
||||||
const Values extends unknown
|
|
||||||
> extends BaseSchema<Values> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "enum",
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(values: Values[]) {
|
|
||||||
super()
|
|
||||||
this.dataType.options = { choices: values }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { SchemaMetadata } from "../types"
|
|
||||||
import { BaseSchema } from "./base"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The JSONSchema is used to define a property that stores
|
|
||||||
* data as a JSON string
|
|
||||||
*/
|
|
||||||
export class JSONSchema extends BaseSchema<string> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "json",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
import { SchemaMetadata } from "../types"
|
|
||||||
import { BaseSchema } from "./base"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The NumberSchema is used to define a numeric/integer
|
|
||||||
* property
|
|
||||||
*/
|
|
||||||
export class NumberSchema extends BaseSchema<number> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "number",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
import { SchemaMetadata } from "../types"
|
|
||||||
import { BaseSchema } from "./base"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The NumberSchema is used to define a textual property
|
|
||||||
*/
|
|
||||||
export class TextSchema extends BaseSchema<string> {
|
|
||||||
protected dataType: SchemaMetadata["dataType"] = {
|
|
||||||
name: "text",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -21,22 +21,12 @@ export type RelationshipTypes =
|
|||||||
| "manyToMany"
|
| "manyToMany"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Any field that contains "nullable" and "optional" properties
|
* The meta-data returned by the property parse method
|
||||||
* in their metadata are qualified as maybe fields.
|
|
||||||
*
|
|
||||||
* This allows us to wrap them inside "NullableModifier" and
|
|
||||||
* "OptionalModifier" classes.
|
|
||||||
*/
|
*/
|
||||||
export type MaybeFieldMetadata = {
|
export type PropertyMetadata = {
|
||||||
nullable: boolean
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The meta-data returned by the schema parse method
|
|
||||||
*/
|
|
||||||
export type SchemaMetadata = MaybeFieldMetadata & {
|
|
||||||
fieldName: string
|
fieldName: string
|
||||||
defaultValue?: any
|
defaultValue?: any
|
||||||
|
nullable: boolean
|
||||||
dataType: {
|
dataType: {
|
||||||
name: KnownDataTypes
|
name: KnownDataTypes
|
||||||
options?: Record<string, any>
|
options?: Record<string, any>
|
||||||
@@ -49,13 +39,13 @@ export type SchemaMetadata = MaybeFieldMetadata & {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Definition of a schema type. It should have a parse
|
* Definition of a property type. It should have a parse
|
||||||
* method to get the metadata and a type-only property
|
* method to get the metadata and a type-only property
|
||||||
* to get its static type
|
* to get its static type
|
||||||
*/
|
*/
|
||||||
export type SchemaType<T> = {
|
export type PropertyType<T> = {
|
||||||
$dataType: T
|
$dataType: T
|
||||||
parse(fieldName: string): SchemaMetadata
|
parse(fieldName: string): PropertyMetadata
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,10 +59,11 @@ export type RelationshipOptions = {
|
|||||||
* The meta-data returned by the relationship parse
|
* The meta-data returned by the relationship parse
|
||||||
* method
|
* method
|
||||||
*/
|
*/
|
||||||
export type RelationshipMetadata = MaybeFieldMetadata & {
|
export type RelationshipMetadata = {
|
||||||
name: string
|
name: string
|
||||||
type: RelationshipTypes
|
type: RelationshipTypes
|
||||||
entity: unknown
|
entity: unknown
|
||||||
|
nullable: boolean
|
||||||
mappedBy?: string
|
mappedBy?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user