@casekit/orm2-schema 0.0.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{src/Config.ts → build/Config.d.ts} +1 -7
- package/build/Config.js +1 -0
- package/build/Logger.js +1 -0
- package/{src/definition/FieldDefinition.ts → build/definition/FieldDefinition.d.ts} +5 -18
- package/build/definition/FieldDefinition.js +1 -0
- package/build/definition/ForeignKeyDefinition.js +1 -0
- package/{src/definition/ModelDefinition.ts → build/definition/ModelDefinition.d.ts} +0 -7
- package/build/definition/ModelDefinition.js +1 -0
- package/{src/definition/ModelDefinitions.ts → build/definition/ModelDefinitions.d.ts} +0 -1
- package/build/definition/ModelDefinitions.js +1 -0
- package/{src/definition/OperatorDefinitions.ts → build/definition/OperatorDefinitions.d.ts} +0 -1
- package/build/definition/OperatorDefinitions.js +1 -0
- package/{src/definition/PostgresDataTypes.ts → build/definition/PostgresDataTypes.d.ts} +0 -3
- package/build/definition/PostgresDataTypes.js +1 -0
- package/{src/definition/RelationDefinition.ts → build/definition/RelationDefinition.d.ts} +1 -7
- package/build/definition/RelationDefinition.js +1 -0
- package/{src/definition/RelationDefinitions.ts → build/definition/RelationDefinitions.d.ts} +0 -1
- package/build/definition/RelationDefinitions.js +1 -0
- package/{src/definition/UniqueConstraintDefinition.ts → build/definition/UniqueConstraintDefinition.d.ts} +0 -1
- package/build/definition/UniqueConstraintDefinition.js +1 -0
- package/build/definition/WhereOperator.d.ts +5 -0
- package/build/definition/WhereOperator.js +1 -0
- package/build/helper/DefaultFieldType.d.ts +7 -0
- package/build/helper/DefaultFieldType.js +1 -0
- package/build/helper/DefaultFieldType.test-d.d.ts +1 -0
- package/build/helper/DefaultFieldType.test-d.js +57 -0
- package/{src/helper/FieldName.ts → build/helper/FieldName.d.ts} +0 -1
- package/build/helper/FieldName.js +1 -0
- package/build/helper/FieldName.test-d.d.ts +1 -0
- package/{src/helper/FieldName.test-d.ts → build/helper/FieldName.test-d.js} +2 -9
- package/build/helper/FieldType.d.ts +5 -0
- package/build/helper/FieldType.js +1 -0
- package/build/helper/FieldType.test-d.d.ts +1 -0
- package/build/helper/FieldType.test-d.js +59 -0
- package/{src/helper/FieldWithDefault.ts → build/helper/FieldWithDefault.d.ts} +1 -6
- package/build/helper/FieldWithDefault.js +1 -0
- package/build/helper/FieldWithDefault.test-d.d.ts +1 -0
- package/{src/helper/FieldWithDefault.test-d.ts → build/helper/FieldWithDefault.test-d.js} +6 -17
- package/{src/helper/ModelName.ts → build/helper/ModelName.d.ts} +1 -5
- package/build/helper/ModelName.js +1 -0
- package/build/helper/ModelName.test-d.d.ts +1 -0
- package/{src/helper/ModelName.test-d.ts → build/helper/ModelName.test-d.js} +2 -9
- package/{src/helper/ModelType.ts → build/helper/ModelType.d.ts} +0 -2
- package/build/helper/ModelType.js +1 -0
- package/build/helper/ModelType.test-d.d.ts +1 -0
- package/{src/helper/ModelType.test-d.ts → build/helper/ModelType.test-d.js} +2 -17
- package/{src/helper/NullableField.ts → build/helper/NullableField.d.ts} +1 -4
- package/build/helper/NullableField.js +1 -0
- package/build/helper/NullableField.test-d.d.ts +1 -0
- package/{src/helper/NullableField.test-d.ts → build/helper/NullableField.test-d.js} +6 -17
- package/{src/helper/OptionalField.ts → build/helper/OptionalField.d.ts} +1 -5
- package/build/helper/OptionalField.js +1 -0
- package/build/helper/OptionalField.test-d.d.ts +1 -0
- package/{src/helper/OptionalField.test-d.ts → build/helper/OptionalField.test-d.js} +6 -21
- package/{src/helper/ProvidedField.ts → build/helper/ProvidedField.d.ts} +1 -4
- package/build/helper/ProvidedField.js +1 -0
- package/build/helper/ProvidedField.test-d.d.ts +1 -0
- package/{src/helper/ProvidedField.test-d.ts → build/helper/ProvidedField.test-d.js} +6 -17
- package/build/helper/RelationModel.d.ts +4 -0
- package/build/helper/RelationModel.js +1 -0
- package/build/helper/RelationModel.test-d.d.ts +1 -0
- package/{src/helper/RelationModel.test-d.ts → build/helper/RelationModel.test-d.js} +6 -27
- package/build/helper/RelationName.d.ts +2 -0
- package/build/helper/RelationName.js +1 -0
- package/build/helper/RelationName.test-d.d.ts +1 -0
- package/{src/helper/RelationName.test-d.ts → build/helper/RelationName.test-d.js} +4 -14
- package/{src/helper/RequiredField.ts → build/helper/RequiredField.d.ts} +1 -8
- package/build/helper/RequiredField.js +1 -0
- package/build/helper/RequiredField.test-d.d.ts +1 -0
- package/{src/helper/RequiredField.test-d.ts → build/helper/RequiredField.test-d.js} +6 -19
- package/{src/helper/SerialField.ts → build/helper/SerialField.d.ts} +1 -7
- package/build/helper/SerialField.js +1 -0
- package/build/helper/SerialField.test-d.d.ts +1 -0
- package/{src/helper/SerialField.test-d.ts → build/helper/SerialField.test-d.js} +8 -23
- package/{src/index.ts → build/index.d.ts} +1 -8
- package/build/index.js +1 -0
- package/package.json +8 -8
- package/src/definition/WhereOperator.ts +0 -6
- package/src/helper/DefaultFieldType.test-d.ts +0 -60
- package/src/helper/DefaultFieldType.ts +0 -99
- package/src/helper/FieldType.test-d.ts +0 -81
- package/src/helper/FieldType.ts +0 -16
- package/src/helper/RelationModel.ts +0 -9
- package/src/helper/RelationName.ts +0 -4
- /package/{src/Logger.ts → build/Logger.d.ts} +0 -0
- /package/{src/definition/ForeignKeyDefinition.ts → build/definition/ForeignKeyDefinition.d.ts} +0 -0
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import pg from "pg";
|
|
2
|
-
|
|
3
2
|
import { Logger } from "./Logger.js";
|
|
4
3
|
import { ModelDefinitions } from "./definition/ModelDefinitions.js";
|
|
5
4
|
import { OperatorDefinitions } from "./definition/OperatorDefinitions.js";
|
|
6
|
-
|
|
7
5
|
export interface Config {
|
|
8
6
|
readonly schema?: string;
|
|
9
7
|
readonly models: ModelDefinitions;
|
|
10
8
|
readonly operators?: OperatorDefinitions;
|
|
11
9
|
readonly extensions?: readonly string[];
|
|
12
|
-
readonly connection?:
|
|
13
|
-
| pg.ConnectionConfig
|
|
14
|
-
| pg.PoolConfig
|
|
15
|
-
| pg.PoolOptions
|
|
16
|
-
| null;
|
|
10
|
+
readonly connection?: pg.ConnectionConfig | pg.PoolConfig | pg.PoolOptions | null;
|
|
17
11
|
readonly pool?: boolean;
|
|
18
12
|
readonly logger?: Logger;
|
|
19
13
|
readonly naming?: {
|
package/build/Config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/build/Logger.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
|
|
3
2
|
import { SQLStatement } from "@casekit/sql";
|
|
4
|
-
|
|
5
3
|
import { PostgresDataTypes } from "./PostgresDataTypes.js";
|
|
6
|
-
|
|
7
4
|
export interface FieldDefinition {
|
|
8
5
|
/**
|
|
9
6
|
* The name of the column in the database. If not specified,
|
|
@@ -12,14 +9,10 @@ export interface FieldDefinition {
|
|
|
12
9
|
* in the config applied.
|
|
13
10
|
*/
|
|
14
11
|
column?: string;
|
|
15
|
-
|
|
16
12
|
/**
|
|
17
13
|
* The postgresql datatype of the column.
|
|
18
14
|
*/
|
|
19
|
-
type:
|
|
20
|
-
| `${PostgresDataTypes[keyof PostgresDataTypes]}${string}` // the string at the end allows for arrays
|
|
21
|
-
| `${Uppercase<PostgresDataTypes[keyof PostgresDataTypes]>}${string}`;
|
|
22
|
-
|
|
15
|
+
type: `${PostgresDataTypes[keyof PostgresDataTypes]}${string}` | `${Uppercase<PostgresDataTypes[keyof PostgresDataTypes]>}${string}`;
|
|
23
16
|
/**
|
|
24
17
|
* A Zod schema used to infer the type of the column and to
|
|
25
18
|
* validate/transform data coming from the database.
|
|
@@ -29,12 +22,10 @@ export interface FieldDefinition {
|
|
|
29
22
|
* and transformations.
|
|
30
23
|
*/
|
|
31
24
|
zodSchema?: z.ZodType;
|
|
32
|
-
|
|
33
25
|
/**
|
|
34
26
|
* Are null values allowed in this column?
|
|
35
27
|
*/
|
|
36
28
|
nullable?: boolean;
|
|
37
|
-
|
|
38
29
|
/**
|
|
39
30
|
* The default value for this column, if it has one. This can be either a value such
|
|
40
31
|
* as a number or string, or if you want to specify a SQL function for the default,
|
|
@@ -51,21 +42,19 @@ export interface FieldDefinition {
|
|
|
51
42
|
* default: sql`uuid_generate_v4()`,
|
|
52
43
|
*/
|
|
53
44
|
default?: unknown;
|
|
54
|
-
|
|
55
45
|
/**
|
|
56
46
|
* Is this column unique? If so, you can specify a where clause to
|
|
57
47
|
* specify a partial unique index, and whether nulls should be considered
|
|
58
48
|
* distinct.
|
|
59
49
|
*/
|
|
60
|
-
unique?:
|
|
61
|
-
|
|
|
62
|
-
|
|
63
|
-
|
|
50
|
+
unique?: boolean | {
|
|
51
|
+
where?: SQLStatement | null;
|
|
52
|
+
nullsNotDistinct?: boolean;
|
|
53
|
+
};
|
|
64
54
|
/**
|
|
65
55
|
* Is this column a single-column primary key?
|
|
66
56
|
*/
|
|
67
57
|
primaryKey?: boolean;
|
|
68
|
-
|
|
69
58
|
/**
|
|
70
59
|
* Is this column a foreign key? If so, you can specify the table and column it references,
|
|
71
60
|
* and what should happen on update or delete.
|
|
@@ -77,11 +66,9 @@ export interface FieldDefinition {
|
|
|
77
66
|
onUpdate?: "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT" | null;
|
|
78
67
|
onDelete?: "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT" | null;
|
|
79
68
|
} | null;
|
|
80
|
-
|
|
81
69
|
/**
|
|
82
70
|
* Is this column provided by middleware? If so, it will not be included in
|
|
83
71
|
* the set of required columns for inserts and updates.
|
|
84
72
|
*/
|
|
85
|
-
|
|
86
73
|
provided?: boolean;
|
|
87
74
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -2,7 +2,6 @@ import { FieldDefinition } from "./FieldDefinition.js";
|
|
|
2
2
|
import { ForeignKeyDefinition } from "./ForeignKeyDefinition.js";
|
|
3
3
|
import { RelationDefinitions } from "./RelationDefinitions.js";
|
|
4
4
|
import { UniqueConstraintDefinition } from "./UniqueConstraintDefinition.js";
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* Configuration object for a database model.
|
|
8
7
|
*/
|
|
@@ -13,38 +12,32 @@ export interface ModelDefinition {
|
|
|
13
12
|
* config applied.
|
|
14
13
|
*/
|
|
15
14
|
table?: string;
|
|
16
|
-
|
|
17
15
|
/**
|
|
18
16
|
* The schema in which the model sits. If not specified, will use the
|
|
19
17
|
* schema specified in the global config; if no schema is specified in
|
|
20
18
|
* the global config, will be set to `public`.
|
|
21
19
|
*/
|
|
22
20
|
schema?: string;
|
|
23
|
-
|
|
24
21
|
/**
|
|
25
22
|
* The model's fields - a map of field name to field
|
|
26
23
|
* definitions. The keys of this map will be used in generated functions
|
|
27
24
|
* and object fields, so must be valid Javascript identifiers.
|
|
28
25
|
*/
|
|
29
26
|
fields: Record<string, FieldDefinition>;
|
|
30
|
-
|
|
31
27
|
/**
|
|
32
28
|
* If the table's primary key contains multiple columns, specify them here.
|
|
33
29
|
* If the primary key is on a single column, you can specify it here or in the column definition.
|
|
34
30
|
*/
|
|
35
31
|
primaryKey?: string[] | null;
|
|
36
|
-
|
|
37
32
|
/**
|
|
38
33
|
* If the table has unique constraints that span multiple columns, you must specify them here.
|
|
39
34
|
* If the unique constraint is on a single column, you can specify it here or in the column definition.
|
|
40
35
|
*/
|
|
41
36
|
uniqueConstraints?: UniqueConstraintDefinition[];
|
|
42
|
-
|
|
43
37
|
/**
|
|
44
38
|
* If the table has foreign keys, you must specify them here. If the foreign key is on a single column,
|
|
45
39
|
* you can specify it here or in the column definition.
|
|
46
40
|
*/
|
|
47
41
|
foreignKeys?: ForeignKeyDefinition[];
|
|
48
|
-
|
|
49
42
|
relations?: RelationDefinitions;
|
|
50
43
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -14,7 +14,6 @@ export interface PostgresDataTypes {
|
|
|
14
14
|
character_varying: `character varying`;
|
|
15
15
|
character_varying_n: `character varying (${number})`;
|
|
16
16
|
cidr: "cidr";
|
|
17
|
-
// circle: "circle";
|
|
18
17
|
date: "date";
|
|
19
18
|
daterange: "daterange";
|
|
20
19
|
double_precision: "double precision";
|
|
@@ -23,7 +22,6 @@ export interface PostgresDataTypes {
|
|
|
23
22
|
int4range: "int4range";
|
|
24
23
|
int8range: "int8range";
|
|
25
24
|
integer: "integer";
|
|
26
|
-
// interval: `interval`;
|
|
27
25
|
json: "json";
|
|
28
26
|
jsonb: "jsonb";
|
|
29
27
|
line: "line";
|
|
@@ -37,7 +35,6 @@ export interface PostgresDataTypes {
|
|
|
37
35
|
oid: "oid";
|
|
38
36
|
path: "path";
|
|
39
37
|
pg_lsn: "pg_lsn";
|
|
40
|
-
// point: "point";
|
|
41
38
|
polygon: "polygon";
|
|
42
39
|
real: "real";
|
|
43
40
|
regclass: "regclass";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -4,7 +4,6 @@ export interface OneToManyRelationDefinition {
|
|
|
4
4
|
fromField: string | string[];
|
|
5
5
|
toField: string | string[];
|
|
6
6
|
}
|
|
7
|
-
|
|
8
7
|
export interface ManyToOneRelationDefinition {
|
|
9
8
|
type: "N:1";
|
|
10
9
|
model: string;
|
|
@@ -12,7 +11,6 @@ export interface ManyToOneRelationDefinition {
|
|
|
12
11
|
toField: string | string[];
|
|
13
12
|
optional?: boolean | null;
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
export interface ManyToManyRelationDefinition {
|
|
17
15
|
type: "N:N";
|
|
18
16
|
model: string;
|
|
@@ -22,8 +20,4 @@ export interface ManyToManyRelationDefinition {
|
|
|
22
20
|
toRelation: string;
|
|
23
21
|
};
|
|
24
22
|
}
|
|
25
|
-
|
|
26
|
-
export type RelationDefinition =
|
|
27
|
-
| OneToManyRelationDefinition
|
|
28
|
-
| ManyToOneRelationDefinition
|
|
29
|
-
| ManyToManyRelationDefinition;
|
|
23
|
+
export type RelationDefinition = OneToManyRelationDefinition | ManyToOneRelationDefinition | ManyToManyRelationDefinition;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WARNING!!! The types in this file must be kept in sync
|
|
3
|
+
* with the zod schemas in packages/orm/src/config/defaultZodSchema.ts.
|
|
4
|
+
* If you make a change here, make sure to update the
|
|
5
|
+
* corresponding zod schema.
|
|
6
|
+
*/
|
|
7
|
+
export type DefaultFieldType<DataType extends string> = DataType extends `${infer T extends string}[]` ? DefaultFieldType<T>[] : Uppercase<DataType> extends "BIGINT" | "BIGSERIAL" ? bigint : Uppercase<DataType> extends "DOUBLE PRECISION" | "INTEGER" | "OID" | "REAL" | "SMALLINT" | "SMALLSERIAL" | "SERIAL" ? number : Uppercase<DataType> extends "BPCHAR" | "BIT" | "BOX" | "CIDR" | "CHAR" | "DATERANGE" | "DECIMAL" | "INET" | "INT4RANGE" | "INT8RANGE" | "INT2VECTOR" | "PG_LSN" | "REGCLASS" | "REGCONFIG" | "REGDICTIONARY" | "REGNAMESPACE" | "REGOPER" | "REGOPERATOR" | "REGPROC" | "REGPROCEDURE" | "REGROLE" | "REGTYPE" | "TID" | "XID" | "NUMRANGE" | "TSRANGE" | "TSTZRANGE" | "LINE" | "LSEG" | "MACADDR" | "MACADDR8" | "MONEY" | "PATH" | "POLYGON" | "TEXT" | "TIME" | "TIMETZ" | "TSQUERY" | "TSVECTOR" | "TXID_SNAPSHOT" | "XML" | `BIT${string}` | `CHARACTER${string}` | `NUMERIC${string}` | `TIME ${string}` | `TIME(${string})` | `VARCHAR` | `VARCHAR ${string}` ? string : Uppercase<DataType> extends "BYTEA" ? Buffer : Uppercase<DataType> extends "JSON" | "JSONB" ? unknown : Uppercase<DataType> extends "UUID" ? string : Uppercase<DataType> extends "BOOLEAN" ? boolean : Uppercase<DataType> extends "DATE" | "TIMESTAMP" | "TIMESTAMPTZ" | `TIMESTAMP${string}` ? Date : unknown;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { expectTypeOf, test } from "vitest";
|
|
2
|
+
test("DefaultFieldType", () => {
|
|
3
|
+
expectTypeOf().toBeBigInt();
|
|
4
|
+
expectTypeOf().toBeNumber();
|
|
5
|
+
expectTypeOf().toBeNumber();
|
|
6
|
+
expectTypeOf().toBeNumber();
|
|
7
|
+
expectTypeOf().toBeNumber();
|
|
8
|
+
expectTypeOf().toBeNumber();
|
|
9
|
+
expectTypeOf().toBeNumber();
|
|
10
|
+
expectTypeOf().toBeNumber();
|
|
11
|
+
expectTypeOf().toBeString();
|
|
12
|
+
expectTypeOf().toBeString();
|
|
13
|
+
expectTypeOf().toEqualTypeOf();
|
|
14
|
+
// expectTypeOf<DefaultFieldType<"circle">>().toEqualTypeOf<{
|
|
15
|
+
// x: number;
|
|
16
|
+
// y: number;
|
|
17
|
+
// radius: number;
|
|
18
|
+
// }>();
|
|
19
|
+
// expectTypeOf<DefaultFieldType<"point">>().toEqualTypeOf<{
|
|
20
|
+
// x: number;
|
|
21
|
+
// y: number;
|
|
22
|
+
// }>();
|
|
23
|
+
expectTypeOf().toBeUnknown();
|
|
24
|
+
expectTypeOf().toBeUnknown();
|
|
25
|
+
expectTypeOf().toBeBoolean();
|
|
26
|
+
expectTypeOf().toEqualTypeOf();
|
|
27
|
+
expectTypeOf().toEqualTypeOf();
|
|
28
|
+
// expectTypeOf<DefaultFieldType<"interval">>().toEqualTypeOf<{
|
|
29
|
+
// years?: number;
|
|
30
|
+
// months?: number;
|
|
31
|
+
// days?: number;
|
|
32
|
+
// hours?: number;
|
|
33
|
+
// minutes?: number;
|
|
34
|
+
// seconds?: number;
|
|
35
|
+
// milliseconds?: number;
|
|
36
|
+
// }>();
|
|
37
|
+
expectTypeOf().toEqualTypeOf();
|
|
38
|
+
expectTypeOf().toEqualTypeOf();
|
|
39
|
+
expectTypeOf().toEqualTypeOf();
|
|
40
|
+
expectTypeOf().toBeString();
|
|
41
|
+
expectTypeOf().toBeString();
|
|
42
|
+
expectTypeOf().toBeString();
|
|
43
|
+
expectTypeOf().toBeString();
|
|
44
|
+
expectTypeOf().toBeString();
|
|
45
|
+
expectTypeOf().toBeString();
|
|
46
|
+
expectTypeOf().toBeString();
|
|
47
|
+
expectTypeOf().toBeString();
|
|
48
|
+
expectTypeOf().toBeString();
|
|
49
|
+
expectTypeOf().toBeString();
|
|
50
|
+
expectTypeOf().toBeString();
|
|
51
|
+
expectTypeOf().toBeString();
|
|
52
|
+
expectTypeOf().toBeString();
|
|
53
|
+
expectTypeOf().toBeString();
|
|
54
|
+
expectTypeOf().toBeString();
|
|
55
|
+
expectTypeOf().toBeString();
|
|
56
|
+
expectTypeOf().toBeString();
|
|
57
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { describe, expectTypeOf, test } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { ModelDefinition } from "#definition/ModelDefinition.js";
|
|
4
|
-
import { FieldName } from "./FieldName.js";
|
|
5
|
-
|
|
6
2
|
describe("FieldName", () => {
|
|
7
3
|
test("it evaluates to a string union of the model's column names", () => {
|
|
8
4
|
const model = {
|
|
@@ -14,10 +10,7 @@ describe("FieldName", () => {
|
|
|
14
10
|
email: { type: "text" },
|
|
15
11
|
type: { type: "text" },
|
|
16
12
|
},
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
expectTypeOf<FieldName<typeof model>>().toEqualTypeOf<
|
|
20
|
-
"id" | "name" | "createdAt" | "dateOfBirth" | "email" | "type"
|
|
21
|
-
>();
|
|
13
|
+
};
|
|
14
|
+
expectTypeOf().toEqualTypeOf();
|
|
22
15
|
});
|
|
23
16
|
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ModelDefinition } from "#definition/ModelDefinition.js";
|
|
3
|
+
import { DefaultFieldType } from "./DefaultFieldType.js";
|
|
4
|
+
import { FieldName } from "./FieldName.js";
|
|
5
|
+
export type FieldType<Model extends ModelDefinition, C extends FieldName<Model>> = Model["fields"][C]["zodSchema"] extends z.ZodType ? z.infer<Model["fields"][C]["zodSchema"]> | (Model["fields"][C]["nullable"] extends true ? null : never) : DefaultFieldType<Model["fields"][C]["type"]> | (Model["fields"][C]["nullable"] extends true ? null : never);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { describe, expectTypeOf } from "vitest";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
const user = {
|
|
4
|
+
fields: {
|
|
5
|
+
email: {
|
|
6
|
+
type: "text",
|
|
7
|
+
zodSchema: z.email(),
|
|
8
|
+
},
|
|
9
|
+
id: {
|
|
10
|
+
type: "serial",
|
|
11
|
+
},
|
|
12
|
+
age: {
|
|
13
|
+
type: "integer",
|
|
14
|
+
zodSchema: z.number().min(0).max(150),
|
|
15
|
+
},
|
|
16
|
+
tags: {
|
|
17
|
+
type: "text[]",
|
|
18
|
+
zodSchema: z.array(z.string()),
|
|
19
|
+
},
|
|
20
|
+
status: {
|
|
21
|
+
type: "text",
|
|
22
|
+
zodSchema: z.enum(["active", "inactive", "pending"]),
|
|
23
|
+
},
|
|
24
|
+
createdAt: {
|
|
25
|
+
type: "timestamp with time zone",
|
|
26
|
+
},
|
|
27
|
+
deletedAt: {
|
|
28
|
+
type: "timestamp with time zone",
|
|
29
|
+
zodSchema: z.date().nullable(),
|
|
30
|
+
},
|
|
31
|
+
metadata: {
|
|
32
|
+
type: "jsonb",
|
|
33
|
+
default: "{}",
|
|
34
|
+
zodSchema: z.object({
|
|
35
|
+
foo: z.enum(["a", "b", "c"]),
|
|
36
|
+
bar: z.array(z.object({
|
|
37
|
+
baz: z.enum(["good", "bad", "indifferent"]),
|
|
38
|
+
quux: z.boolean(),
|
|
39
|
+
})),
|
|
40
|
+
}),
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
describe("FieldType", () => {
|
|
45
|
+
describe("json field", () => {
|
|
46
|
+
expectTypeOf().toEqualTypeOf();
|
|
47
|
+
});
|
|
48
|
+
describe("fields with Zod schemas", () => {
|
|
49
|
+
expectTypeOf().toBeString();
|
|
50
|
+
expectTypeOf().toBeNumber();
|
|
51
|
+
expectTypeOf().toEqualTypeOf();
|
|
52
|
+
expectTypeOf().toEqualTypeOf();
|
|
53
|
+
expectTypeOf().toEqualTypeOf();
|
|
54
|
+
});
|
|
55
|
+
describe("fields without Zod schemas (using DefaultFieldType)", () => {
|
|
56
|
+
expectTypeOf().toBeNumber();
|
|
57
|
+
expectTypeOf().toEqualTypeOf();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -1,9 +1,4 @@
|
|
|
1
1
|
import { ModelDefinition } from "../definition/ModelDefinition.js";
|
|
2
|
-
|
|
3
2
|
export type FieldWithDefault<Model extends ModelDefinition> = {
|
|
4
|
-
[K in keyof Model["fields"]]: Model["fields"][K]["default"] extends NonNullable<
|
|
5
|
-
Model["fields"][K]["default"]
|
|
6
|
-
>
|
|
7
|
-
? K
|
|
8
|
-
: never;
|
|
3
|
+
[K in keyof Model["fields"]]: Model["fields"][K]["default"] extends NonNullable<Model["fields"][K]["default"]> ? K : never;
|
|
9
4
|
}[keyof Model["fields"]];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { describe, expectTypeOf, test } from "vitest";
|
|
2
|
-
|
|
3
|
-
import type { ModelDefinition } from "../definition/ModelDefinition.js";
|
|
4
|
-
import type { FieldWithDefault } from "./FieldWithDefault.js";
|
|
5
|
-
|
|
6
2
|
describe("FieldWithDefault", () => {
|
|
7
3
|
test("extracts just the fields with a default value from the model", () => {
|
|
8
4
|
const user = {
|
|
@@ -12,13 +8,9 @@ describe("FieldWithDefault", () => {
|
|
|
12
8
|
age: { type: "integer", default: 18 },
|
|
13
9
|
email: { type: "text", default: null },
|
|
14
10
|
},
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
expectTypeOf<FieldWithDefault<typeof user>>().toEqualTypeOf<
|
|
18
|
-
"name" | "age"
|
|
19
|
-
>();
|
|
11
|
+
};
|
|
12
|
+
expectTypeOf().toEqualTypeOf();
|
|
20
13
|
});
|
|
21
|
-
|
|
22
14
|
test("returns never if there are no fields with defaults", () => {
|
|
23
15
|
const user = {
|
|
24
16
|
fields: {
|
|
@@ -26,11 +18,9 @@ describe("FieldWithDefault", () => {
|
|
|
26
18
|
name: { type: "text" },
|
|
27
19
|
age: { type: "integer" },
|
|
28
20
|
},
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
expectTypeOf<FieldWithDefault<typeof user>>().toEqualTypeOf<never>();
|
|
21
|
+
};
|
|
22
|
+
expectTypeOf().toEqualTypeOf();
|
|
32
23
|
});
|
|
33
|
-
|
|
34
24
|
test("excludes fields with null or undefined defaults", () => {
|
|
35
25
|
const user = {
|
|
36
26
|
fields: {
|
|
@@ -39,8 +29,7 @@ describe("FieldWithDefault", () => {
|
|
|
39
29
|
email: { type: "text", default: null },
|
|
40
30
|
phone: { type: "text", default: undefined },
|
|
41
31
|
},
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
expectTypeOf<FieldWithDefault<typeof user>>().toEqualTypeOf<"name">();
|
|
32
|
+
};
|
|
33
|
+
expectTypeOf().toEqualTypeOf();
|
|
45
34
|
});
|
|
46
35
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { describe, expectTypeOf, test } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { ModelDefinitions } from "../definition/ModelDefinitions.js";
|
|
4
|
-
import { ModelName } from "./ModelName.js";
|
|
5
|
-
|
|
6
2
|
describe("ModelName", () => {
|
|
7
3
|
test("it evaluates to a string union of the names of the models", () => {
|
|
8
4
|
const models = {
|
|
@@ -12,10 +8,7 @@ describe("ModelName", () => {
|
|
|
12
8
|
comment: { fields: {} },
|
|
13
9
|
mention: { fields: {} },
|
|
14
10
|
color: { fields: {} },
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
expectTypeOf<ModelName<typeof models>>().toEqualTypeOf<
|
|
18
|
-
"user" | "post" | "like" | "comment" | "mention" | "color"
|
|
19
|
-
>();
|
|
11
|
+
};
|
|
12
|
+
expectTypeOf().toEqualTypeOf();
|
|
20
13
|
});
|
|
21
14
|
});
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { Simplify } from "@casekit/toolbox";
|
|
2
|
-
|
|
3
2
|
import { ModelDefinition } from "#definition/ModelDefinition.js";
|
|
4
3
|
import { FieldType } from "./FieldType.js";
|
|
5
|
-
|
|
6
4
|
export type ModelType<Model extends ModelDefinition> = Simplify<{
|
|
7
5
|
[C in keyof Model["fields"]]: FieldType<Model, C>;
|
|
8
6
|
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { describe, expectTypeOf } from "vitest";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
import type { ModelDefinition } from "#definition/ModelDefinition.js";
|
|
5
|
-
import { ModelType } from "./ModelType.js";
|
|
6
|
-
|
|
7
3
|
const user = {
|
|
8
4
|
fields: {
|
|
9
5
|
email: {
|
|
@@ -33,20 +29,9 @@ const user = {
|
|
|
33
29
|
zodSchema: z.date().nullable(),
|
|
34
30
|
},
|
|
35
31
|
},
|
|
36
|
-
}
|
|
37
|
-
|
|
32
|
+
};
|
|
38
33
|
describe("ModelType", () => {
|
|
39
34
|
describe("Evaluate to a type containing all the fields of the", () => {
|
|
40
|
-
expectTypeOf
|
|
41
|
-
Readonly<{
|
|
42
|
-
email: string;
|
|
43
|
-
id: number;
|
|
44
|
-
age: number;
|
|
45
|
-
tags: string[];
|
|
46
|
-
status: "active" | "inactive" | "pending";
|
|
47
|
-
createdAt: Date;
|
|
48
|
-
deletedAt: Date | null;
|
|
49
|
-
}>
|
|
50
|
-
>();
|
|
35
|
+
expectTypeOf().toEqualTypeOf();
|
|
51
36
|
});
|
|
52
37
|
});
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import { ModelDefinition } from "../definition/ModelDefinition.js";
|
|
2
|
-
|
|
3
2
|
export type NullableField<Model extends ModelDefinition> = {
|
|
4
|
-
[K in keyof Model["fields"]]: Model["fields"][K]["nullable"] extends true
|
|
5
|
-
? K
|
|
6
|
-
: never;
|
|
3
|
+
[K in keyof Model["fields"]]: Model["fields"][K]["nullable"] extends true ? K : never;
|
|
7
4
|
}[keyof Model["fields"]];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|