@casekit/orm 0.0.1-alpha.12 → 0.0.1-alpha.13

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.
Files changed (227) hide show
  1. package/package.json +13 -3
  2. package/.env +0 -2
  3. package/.eslintrc.cjs +0 -69
  4. package/.github/actions/ci-setup/action.yml +0 -19
  5. package/.github/dependabot.yml +0 -12
  6. package/.github/workflows/ci.yml +0 -119
  7. package/.github/workflows/dependabot.yml +0 -26
  8. package/.github/workflows/semgrep.yml +0 -24
  9. package/.prettierrc.cjs +0 -11
  10. package/.vscode/tasks.json +0 -28
  11. package/codecov.yml +0 -7
  12. package/lib/orm.query.test.d.ts +0 -2
  13. package/lib/orm.query.test.d.ts.map +0 -1
  14. package/lib/orm.query.test.js +0 -22
  15. package/lib/orm.query.test.js.map +0 -1
  16. package/src/Connection.ts +0 -65
  17. package/src/errors.ts +0 -18
  18. package/src/index.ts +0 -16
  19. package/src/logger.ts +0 -3
  20. package/src/migrate/commands/implode.ts +0 -46
  21. package/src/migrate/index.ts +0 -1
  22. package/src/migrate/migrator.ts +0 -24
  23. package/src/migrate/sql/createExtensionsSql.test.ts +0 -26
  24. package/src/migrate/sql/createExtensionsSql.ts +0 -16
  25. package/src/migrate/sql/createForeignKeyConstraintSql.test.ts +0 -50
  26. package/src/migrate/sql/createForeignKeyConstraintSql.ts +0 -44
  27. package/src/migrate/sql/createSchemasSql.test.ts +0 -81
  28. package/src/migrate/sql/createSchemasSql.ts +0 -15
  29. package/src/migrate/sql/createTableSql.properties.ts +0 -38
  30. package/src/migrate/sql/createTableSql.test.ts +0 -74
  31. package/src/migrate/sql/createTableSql.ts +0 -53
  32. package/src/migrate/sql/createUniqueConstraintSql.ts +0 -27
  33. package/src/migrate/sql/dropSchemasSql.ts +0 -15
  34. package/src/migrate/sql/dropTableSql.ts +0 -13
  35. package/src/orm.query.test.ts +0 -28
  36. package/src/orm.ts +0 -370
  37. package/src/pull/index.ts +0 -1
  38. package/src/pull/introspect/getForeignKeys.ts +0 -64
  39. package/src/pull/introspect/getPrimaryKeys.ts +0 -26
  40. package/src/pull/introspect/getTables.ts +0 -51
  41. package/src/pull/introspect/getUniqueConstraints.ts +0 -39
  42. package/src/pull/parse/parseCreateUniqueIndexStatement.test.ts +0 -14
  43. package/src/pull/parse/parseCreateUniqueIndexStatement.ts +0 -19
  44. package/src/pull/pull.ts +0 -78
  45. package/src/pull/render/renderModel.test.ts +0 -144
  46. package/src/pull/render/renderModel.ts +0 -141
  47. package/src/pull/render/renderModelsIndex.ts +0 -24
  48. package/src/pull/render/renderRelations.ts +0 -77
  49. package/src/pull/render/renderRelationsIndex.ts +0 -24
  50. package/src/pull/types/ColumnMeta.ts +0 -10
  51. package/src/pull/types/ForeignKey.ts +0 -6
  52. package/src/pull/types/PrimaryKey.ts +0 -6
  53. package/src/pull/types/UniqueConstraint.ts +0 -8
  54. package/src/pull/util/format.ts +0 -17
  55. package/src/pull/util/quote.ts +0 -7
  56. package/src/pull/util/unquote.ts +0 -9
  57. package/src/queries/clauses/IncludeClause.ts +0 -39
  58. package/src/queries/clauses/LateralByClause.ts +0 -4
  59. package/src/queries/clauses/ReturningClause.ts +0 -7
  60. package/src/queries/clauses/SelectClause.ts +0 -7
  61. package/src/queries/clauses/WhereClause.ts +0 -16
  62. package/src/queries/clauses/helpers/OptionalColumn.ts +0 -18
  63. package/src/queries/clauses/helpers/OptionalParams.ts +0 -14
  64. package/src/queries/clauses/helpers/RequiredColumn.ts +0 -8
  65. package/src/queries/clauses/helpers/RequiredParams.ts +0 -14
  66. package/src/queries/clauses/include/IncludedRelationModel.ts +0 -13
  67. package/src/queries/clauses/include/IncludedRelationName.ts +0 -11
  68. package/src/queries/clauses/include/IncludedRelationQuery.ts +0 -20
  69. package/src/queries/clauses/where/buildWhereClause.ts +0 -121
  70. package/src/queries/clauses/where/buildWhereClauses.test.ts +0 -145
  71. package/src/queries/clauses/where/buildWhereClauses.ts +0 -45
  72. package/src/queries/clauses/where/operators.ts +0 -13
  73. package/src/queries/clauses/where/types/WhereClauseValue.ts +0 -39
  74. package/src/queries/count/buildCount.ts +0 -77
  75. package/src/queries/count/countToSql.ts +0 -66
  76. package/src/queries/count/tests/count.test.ts +0 -35
  77. package/src/queries/count/types/BaseCountParams.ts +0 -11
  78. package/src/queries/count/types/CountBuilder.ts +0 -45
  79. package/src/queries/count/types/CountParams.ts +0 -27
  80. package/src/queries/count.ts +0 -33
  81. package/src/queries/create/buildCreate.ts +0 -67
  82. package/src/queries/create/createResultSchema.ts +0 -24
  83. package/src/queries/create/createToSql.ts +0 -44
  84. package/src/queries/create/tests/createMany.varied-keys.test.ts +0 -28
  85. package/src/queries/create/tests/createOne.test-d.ts +0 -116
  86. package/src/queries/create/tests/createOne.test.ts +0 -197
  87. package/src/queries/create/types/BaseCreateManyParams.ts +0 -7
  88. package/src/queries/create/types/BaseCreateOneParams.ts +0 -7
  89. package/src/queries/create/types/CreateManyParams.ts +0 -15
  90. package/src/queries/create/types/CreateManyResult.ts +0 -17
  91. package/src/queries/create/types/CreateOneParams.ts +0 -22
  92. package/src/queries/create/types/CreateOneResult.ts +0 -17
  93. package/src/queries/createMany.ts +0 -38
  94. package/src/queries/createOne.ts +0 -27
  95. package/src/queries/delete/buildDelete.ts +0 -56
  96. package/src/queries/delete/deleteResultSchema.ts +0 -23
  97. package/src/queries/delete/deleteToSql.ts +0 -48
  98. package/src/queries/delete/tests/deleteOne.test.ts +0 -108
  99. package/src/queries/delete/types/BaseDeleteParams.ts +0 -9
  100. package/src/queries/delete/types/DeleteManyResult.ts +0 -16
  101. package/src/queries/delete/types/DeleteOneResult.ts +0 -16
  102. package/src/queries/delete/types/DeleteParams.ts +0 -12
  103. package/src/queries/deleteMany.ts +0 -33
  104. package/src/queries/deleteOne.ts +0 -32
  105. package/src/queries/find/buildFind.ts +0 -138
  106. package/src/queries/find/findResultSchema.ts +0 -32
  107. package/src/queries/find/findToSql.test.ts +0 -123
  108. package/src/queries/find/findToSql.ts +0 -141
  109. package/src/queries/find/getIncludedManyToManyRelations.ts +0 -49
  110. package/src/queries/find/getIncludedOneToManyRelations.ts +0 -44
  111. package/src/queries/find/tests/findMany.include.test.ts +0 -107
  112. package/src/queries/find/tests/findMany.limit.test-d.ts +0 -75
  113. package/src/queries/find/tests/findMany.limit.test.ts +0 -176
  114. package/src/queries/find/tests/findMany.nullable-relations.test.ts +0 -127
  115. package/src/queries/find/tests/findMany.orderBy.test-d.ts +0 -84
  116. package/src/queries/find/tests/findMany.orderBy.test.ts +0 -184
  117. package/src/queries/find/tests/findMany.select.test-d.ts +0 -117
  118. package/src/queries/find/tests/findMany.select.test.ts +0 -188
  119. package/src/queries/find/tests/findMany.too-deep.test-d.ts +0 -154
  120. package/src/queries/find/tests/findMany.where.test-d.ts +0 -85
  121. package/src/queries/find/tests/findMany.where.test.ts +0 -76
  122. package/src/queries/find/tests/middleware.find.where.test.ts +0 -467
  123. package/src/queries/find/types/BaseFindParams.ts +0 -18
  124. package/src/queries/find/types/FindBuilder.ts +0 -73
  125. package/src/queries/find/types/FindManyParams.ts +0 -24
  126. package/src/queries/find/types/FindManyResult.ts +0 -13
  127. package/src/queries/find/types/FindOneParams.ts +0 -17
  128. package/src/queries/find/types/FindOneResult.ts +0 -50
  129. package/src/queries/findMany.ts +0 -134
  130. package/src/queries/findOne.ts +0 -30
  131. package/src/queries/middleware/Middleware.ts +0 -53
  132. package/src/queries/middleware/ValuesMiddleware.ts +0 -9
  133. package/src/queries/middleware/WhereMiddleware.ts +0 -16
  134. package/src/queries/update/buildUpdate.ts +0 -76
  135. package/src/queries/update/tests/updateOne.test.ts +0 -118
  136. package/src/queries/update/types/BaseUpdateParams.ts +0 -10
  137. package/src/queries/update/types/UpdateManyResult.ts +0 -16
  138. package/src/queries/update/types/UpdateOneResult.ts +0 -16
  139. package/src/queries/update/types/UpdateParams.ts +0 -14
  140. package/src/queries/update/types/UpdateValues.ts +0 -12
  141. package/src/queries/update/updateResultSchema.ts +0 -23
  142. package/src/queries/update/updateToSql.ts +0 -43
  143. package/src/queries/updateMany.ts +0 -33
  144. package/src/queries/updateOne.ts +0 -33
  145. package/src/queries/util/hasConditions.test.ts +0 -36
  146. package/src/queries/util/hasConditions.ts +0 -14
  147. package/src/queries/util/rowToObject.test.ts +0 -76
  148. package/src/queries/util/rowToObject.ts +0 -13
  149. package/src/queries/util/tableAlias.test.ts +0 -20
  150. package/src/queries/util/tableAlias.ts +0 -10
  151. package/src/schema/populate/composeMiddleware.ts +0 -51
  152. package/src/schema/populate/populateConfiguration.ts +0 -48
  153. package/src/schema/populate/populateModel.ts +0 -98
  154. package/src/schema/populate/suggestedColumnSchema.ts +0 -62
  155. package/src/schema/types/base/BaseColumn.ts +0 -10
  156. package/src/schema/types/base/BaseConfiguration.ts +0 -14
  157. package/src/schema/types/base/BaseModel.ts +0 -12
  158. package/src/schema/types/base/BaseModels.ts +0 -3
  159. package/src/schema/types/base/BaseOrm.ts +0 -8
  160. package/src/schema/types/base/BaseRelation.ts +0 -19
  161. package/src/schema/types/base/BaseRelations.ts +0 -3
  162. package/src/schema/types/constraints/ForeignKey.ts +0 -13
  163. package/src/schema/types/constraints/UniqueConstraint.ts +0 -9
  164. package/src/schema/types/helpers/ColumnName.ts +0 -6
  165. package/src/schema/types/helpers/ColumnType.ts +0 -23
  166. package/src/schema/types/helpers/Columns.ts +0 -3
  167. package/src/schema/types/helpers/HasDefault.ts +0 -11
  168. package/src/schema/types/helpers/IsNullable.ts +0 -7
  169. package/src/schema/types/helpers/IsProvided.ts +0 -8
  170. package/src/schema/types/helpers/IsSerial.ts +0 -10
  171. package/src/schema/types/helpers/ModelName.ts +0 -6
  172. package/src/schema/types/loose/LooseColumnDefinition.ts +0 -80
  173. package/src/schema/types/loose/LooseModelDefinition.ts +0 -47
  174. package/src/schema/types/loose/LooseModelDefinitions.ts +0 -3
  175. package/src/schema/types/loose/LooseRelationDefinition.ts +0 -10
  176. package/src/schema/types/loose/LooseRelationsDefinition.ts +0 -8
  177. package/src/schema/types/loose/LooseRelationsDefinitions.ts +0 -7
  178. package/src/schema/types/postgres/DataType.ts +0 -65
  179. package/src/schema/types/relations/ManyToManyRelation.ts +0 -15
  180. package/src/schema/types/relations/ManyToOneRelation.ts +0 -14
  181. package/src/schema/types/relations/OneToManyRelation.ts +0 -12
  182. package/src/schema/types/strict/ColumnDefinition.ts +0 -81
  183. package/src/schema/types/strict/ModelDefinition.ts +0 -47
  184. package/src/schema/types/strict/ModelDefinitions.ts +0 -3
  185. package/src/schema/types/strict/RelationDefinition.ts +0 -13
  186. package/src/schema/types/strict/RelationsDefinition.ts +0 -8
  187. package/src/schema/types/strict/RelationsDefinitions.ts +0 -7
  188. package/src/schema/validate/validateConfiguration.ts +0 -9
  189. package/src/schema/validate/validateModel.ts +0 -32
  190. package/src/sql/SQLStatement.test.ts +0 -112
  191. package/src/sql/SQLStatement.ts +0 -86
  192. package/src/sql/index.ts +0 -2
  193. package/src/sql/sql.ts +0 -47
  194. package/src/test/db/index.ts +0 -27
  195. package/src/test/db/models/foo.model.ts +0 -24
  196. package/src/test/db/models/foo.relations.ts +0 -4
  197. package/src/test/db/models/post.model.ts +0 -34
  198. package/src/test/db/models/post.relations.ts +0 -21
  199. package/src/test/db/models/tenant.model.ts +0 -14
  200. package/src/test/db/models/tenant.relations.ts +0 -22
  201. package/src/test/db/models/tenantUser.model.ts +0 -23
  202. package/src/test/db/models/tenantUser.relations.ts +0 -15
  203. package/src/test/db/models/user.model.ts +0 -29
  204. package/src/test/db/models/user.relations.ts +0 -38
  205. package/src/test/db/models.ts +0 -15
  206. package/src/test/db/relations.ts +0 -13
  207. package/src/test/gen/column.ts +0 -39
  208. package/src/test/gen/index.ts +0 -2
  209. package/src/test/gen/model.ts +0 -58
  210. package/src/test/gen/sqldate.ts +0 -8
  211. package/src/test/globalSetup.ts +0 -6
  212. package/src/test/seed/index.ts +0 -75
  213. package/src/test/util/withRollback.ts +0 -18
  214. package/src/test/util/withTransaction.ts +0 -19
  215. package/src/types/ColumnName.ts +0 -6
  216. package/src/types/ColumnType.ts +0 -20
  217. package/src/types/Configuration.ts +0 -21
  218. package/src/types/ModelType.ts +0 -20
  219. package/src/types/util/DeepRequired.ts +0 -7
  220. package/src/types/util/DisallowExtraKeys.ts +0 -5
  221. package/src/types/util/NonEmptyArray.ts +0 -1
  222. package/src/types/util/Simplify.ts +0 -8
  223. package/src/util/ensureArray.ts +0 -4
  224. package/src/util/interleave.test.ts +0 -35
  225. package/src/util/interleave.ts +0 -8
  226. package/tsconfig.json +0 -18
  227. package/vitest.config.ts +0 -15
@@ -1,80 +0,0 @@
1
- import { z } from "zod";
2
-
3
- import { SQLStatement } from "../../../sql";
4
-
5
- export type LooseColumnDefinition<ColumnType = unknown> = {
6
- /**
7
- * The postgresql datatype of the column.
8
- */
9
- type: string;
10
-
11
- /**
12
- * The name of the column in the database. If not specified,
13
- * it will be inferred based on the model's field name,
14
- * with any column naming function (e.g. `snakeCase`) specified
15
- * in the config applied.
16
- */
17
- name?: string;
18
-
19
- /**
20
- * A Zod schema used to infer the type of the column and to
21
- * validate/transform data coming from the database.
22
- * If not specified, a basic schema will be inferred based on the
23
- * postgresql data type of the column. Specifying it
24
- * allows to you configure more sophisticated parsing, validation,
25
- * and transformations.
26
- */
27
- zodSchema?: z.ZodType<ColumnType>;
28
-
29
- /**
30
- * Are null values allowed in this column?
31
- */
32
- nullable?: boolean;
33
-
34
- /**
35
- * Does this column have a default value? This can be either a value such as a number or string,
36
- * or if you want to specify a SQL function for the default, you can use the `sql` tagged
37
- * template literal. So examples would include:
38
- *
39
- * default: 3,
40
- *
41
- * or
42
- *
43
- * default: "foo",
44
- *
45
- * or
46
- *
47
- * default: sql`uuid_generate_v4()`,
48
- */
49
- default?: ColumnType | SQLStatement | null;
50
-
51
- /**
52
- * Is this column unique? If so, you can specify a where clause to
53
- * specify a partial unique index, and whether nulls should be considered
54
- * distinct.
55
- */
56
- unique?: boolean | { where?: SQLStatement; nullsNotDistinct?: boolean };
57
-
58
- /**
59
- * Is this column a single-column primary key?
60
- */
61
- primaryKey?: boolean;
62
-
63
- /**
64
- * Is this column a foreign key? If so, you can specify the table and column it references,
65
- * and what should happen on update or delete.
66
- */
67
- references?: {
68
- schema?: string;
69
- table: string;
70
- column: string;
71
- onUpdate?: SQLStatement;
72
- onDelete?: SQLStatement;
73
- };
74
-
75
- /**
76
- * Is this column provided by middleware? If so, it will not be included in
77
- * the set of required columns for inserts and updates.
78
- */
79
- provided?: boolean;
80
- };
@@ -1,47 +0,0 @@
1
- import { ForeignKey } from "../constraints/ForeignKey";
2
- import { UniqueConstraint } from "../constraints/UniqueConstraint";
3
- import { LooseColumnDefinition } from "./LooseColumnDefinition";
4
-
5
- /**
6
- * Configuration object for a database model.
7
- */
8
- export type LooseModelDefinition = {
9
- /**
10
- * The name of the model's table. If not specified, the table name will be
11
- * derived from the model name, with any transform function specified in
12
- * config applied.
13
- */
14
- table?: string;
15
-
16
- /**
17
- * The schema in which the model sits. If not specified, will use the
18
- * schema specified in the global config; if no schema is specified in
19
- * the global config, will be set to `public`.
20
- */
21
- schema?: string;
22
-
23
- /**
24
- * The model's column configuration - a map of field name to column
25
- * definitions. The keys of this map will be used in generated functions
26
- * and object fields, so must be valid Javascript identifiers.
27
- */
28
- columns: Record<string, LooseColumnDefinition>;
29
-
30
- /**
31
- * If the table's primary key contains multiple columns, specify them here.
32
- * If the primary key is on a single column, you can specify it here or in the column definition.
33
- */
34
- primaryKey?: string[];
35
-
36
- /**
37
- * If the table has unique constraints that span multiple columns, you must specify them here.
38
- * If the unique constraint is on a single column, you can specify it here or in the column definition.
39
- */
40
- uniqueConstraints?: UniqueConstraint[];
41
-
42
- /**
43
- * If the table has foreign keys, you must specify them here. If the foreign key is on a single column,
44
- * you can specify it here or in the column definition.
45
- */
46
- foreignKeys?: ForeignKey[];
47
- };
@@ -1,3 +0,0 @@
1
- import { LooseModelDefinition } from "./LooseModelDefinition";
2
-
3
- export type LooseModelDefinitions = Record<string, LooseModelDefinition>;
@@ -1,10 +0,0 @@
1
- export type LooseRelationDefinition =
2
- | { model: string; type: "1:N"; foreignKey: string }
3
- | { model: string; type: "N:1"; foreignKey: string; optional?: true }
4
- | {
5
- model: string;
6
- type: "N:N";
7
- through: string;
8
- foreignKey: string;
9
- otherKey: string;
10
- };
@@ -1,8 +0,0 @@
1
- import { ModelName } from "../helpers/ModelName";
2
- import { LooseModelDefinitions } from "./LooseModelDefinitions";
3
- import { LooseRelationDefinition } from "./LooseRelationDefinition";
4
-
5
- export type LooseRelationsDefinition<
6
- Models extends LooseModelDefinitions,
7
- _M extends ModelName<Models>,
8
- > = Record<string, LooseRelationDefinition>;
@@ -1,7 +0,0 @@
1
- import { ModelName } from "../helpers/ModelName";
2
- import { LooseModelDefinitions } from "./LooseModelDefinitions";
3
- import { LooseRelationsDefinition } from "./LooseRelationsDefinition";
4
-
5
- export type LooseRelationsDefinitions<Models extends LooseModelDefinitions> = {
6
- [M in ModelName<Models>]?: LooseRelationsDefinition<Models, M>;
7
- };
@@ -1,65 +0,0 @@
1
- export type ScalarDataType =
2
- | "bigint"
3
- | "bigserial"
4
- | `bit(${number})`
5
- | `bit varying(${number})`
6
- | "boolean"
7
- | "box"
8
- | "bytea"
9
- | `character(${number})`
10
- | `character varying(${number})`
11
- | "cidr"
12
- | "circle"
13
- | "date"
14
- | "double precision"
15
- | "inet"
16
- | "integer"
17
- | "interval"
18
- | "json"
19
- | "jsonb"
20
- | "line"
21
- | "lseg"
22
- | "macaddr"
23
- | "macaddr8"
24
- | "money"
25
- | `numeric(${number}, ${number})`
26
- | "path"
27
- | "pg_lsn"
28
- | "pg_snapshot"
29
- | "point"
30
- | "polygon"
31
- | "real"
32
- | "smallint"
33
- | "smallserial"
34
- | "serial"
35
- | "text"
36
- | "time"
37
- | `time (${number})`
38
- | `time (${number}) without time zone`
39
- | `time with time zone`
40
- | `time without time zone`
41
- | `time (${number}) with time zone`
42
- | "timestamp"
43
- | `timestamp (${number})`
44
- | `timestamp (${number}) without time zone`
45
- | `timestamp with time zone`
46
- | `timestamp without time zone`
47
- | `timestamp (${number}) with time zone`
48
- | "tsquery"
49
- | "tsvector"
50
- | "txid_snapshot"
51
- | "uuid"
52
- | "xml";
53
-
54
- export type ArrayDataType =
55
- | `${ScalarDataType}[${number | ""}]`
56
- | `${ScalarDataType}[${number | ""}][${number | ""}]`
57
- | `${ScalarDataType}[${number | ""}][${number | ""}][${number | ""}]`
58
- | `${ScalarDataType}[${number | ""}][${number | ""}][${number | ""}][${
59
- | number
60
- | ""}]`
61
- | `${ScalarDataType}[${number | ""}][${number | ""}][${number | ""}][${
62
- | number
63
- | ""}][${number | ""}]`;
64
-
65
- export type DataType = ScalarDataType | ArrayDataType;
@@ -1,15 +0,0 @@
1
- import { ColumnName } from "../helpers/ColumnName";
2
- import { ModelName } from "../helpers/ModelName";
3
- import { LooseModelDefinitions } from "../loose/LooseModelDefinitions";
4
-
5
- export type ManyToManyRelation<Models extends LooseModelDefinitions> = {
6
- [M2 in ModelName<Models>]: {
7
- [J in ModelName<Models>]: {
8
- model: M2;
9
- through: J;
10
- type: "N:N";
11
- foreignKey: ColumnName<Models[J]> | ColumnName<Models[J]>[];
12
- otherKey: ColumnName<Models[J]> | ColumnName<Models[J]>[];
13
- };
14
- };
15
- }[ModelName<Models>][ModelName<Models>];
@@ -1,14 +0,0 @@
1
- import { ColumnName } from "../helpers/ColumnName";
2
- import { ModelName } from "../helpers/ModelName";
3
- import { LooseModelDefinitions } from "../loose/LooseModelDefinitions";
4
-
5
- export type ManyToOneRelation<
6
- Models extends LooseModelDefinitions,
7
- M extends ModelName<Models>,
8
- > = {
9
- [M2 in ModelName<Models>]: {
10
- model: M2;
11
- type: "N:1";
12
- foreignKey: ColumnName<Models[M]> | ColumnName<Models[M]>[];
13
- };
14
- }[ModelName<Models>];
@@ -1,12 +0,0 @@
1
- import { ColumnName } from "../helpers/ColumnName";
2
- import { ModelName } from "../helpers/ModelName";
3
- import { LooseModelDefinitions } from "../loose/LooseModelDefinitions";
4
-
5
- export type OneToManyRelation<Models extends LooseModelDefinitions> = {
6
- [M2 in ModelName<Models>]: {
7
- model: M2;
8
- type: "1:N";
9
- foreignKey: ColumnName<Models[M2]> | ColumnName<Models[M2]>[];
10
- optional?: boolean;
11
- };
12
- }[ModelName<Models>];
@@ -1,81 +0,0 @@
1
- import { z } from "zod";
2
-
3
- import { SQLStatement } from "../../../sql";
4
- import { DataType } from "../postgres/DataType";
5
-
6
- export type ColumnDefinition<ColumnType = unknown> = {
7
- /**
8
- * The postgresql datatype of the column.
9
- */
10
- type: DataType;
11
-
12
- /**
13
- * The name of the column in the database. If not specified,
14
- * it will be inferred based on the model's field name,
15
- * with any column naming function (e.g. `snakeCase`) specified
16
- * in the config applied.
17
- */
18
- name?: string;
19
-
20
- /**
21
- * A Zod schema used to infer the type of the column and to
22
- * validate/transform data coming from the database.
23
- * If not specified, a basic schema will be inferred based on the
24
- * postgresql data type of the column. Specifying it
25
- * allows to you configure more sophisticated parsing, validation,
26
- * and transformations.
27
- */
28
- zodSchema?: z.ZodType<ColumnType>;
29
-
30
- /**
31
- * Are null values allowed in this column?
32
- */
33
- nullable?: boolean;
34
-
35
- /**
36
- * Does this column have a default value? This can be either a value such as a number or string,
37
- * or if you want to specify a SQL function for the default, you can use the `sql` tagged
38
- * template literal. So examples would include:
39
- *
40
- * default: 3,
41
- *
42
- * or
43
- *
44
- * default: "foo",
45
- *
46
- * or
47
- *
48
- * default: sql`uuid_generate_v4()`,
49
- */
50
- default?: ColumnType | SQLStatement | null;
51
-
52
- /**
53
- * Is this column unique? If so, you can specify a where clause to
54
- * specify a partial unique index, and whether nulls should be considered
55
- * distinct.
56
- */
57
- unique?: boolean | { where?: SQLStatement; nullsNotDistinct?: boolean };
58
-
59
- /**
60
- * Is this column a single-column primary key?
61
- */
62
- primaryKey?: boolean;
63
-
64
- /**
65
- * Is this column a foreign key? If so, you can specify the table and column it references,
66
- * and what should happen on update or delete.
67
- */
68
- references?: {
69
- schema?: string;
70
- table: string;
71
- column: string;
72
- onUpdate?: SQLStatement;
73
- onDelete?: SQLStatement;
74
- };
75
-
76
- /**
77
- * Is this column provided by middleware? If so, it will not be included in
78
- * the set of required columns for inserts and updates.
79
- */
80
- provided?: boolean;
81
- };
@@ -1,47 +0,0 @@
1
- import { ForeignKey } from "../constraints/ForeignKey";
2
- import { UniqueConstraint } from "../constraints/UniqueConstraint";
3
- import { ColumnDefinition } from "./ColumnDefinition";
4
-
5
- /**
6
- * Configuration object for a database model.
7
- */
8
- export type ModelDefinition = {
9
- /**
10
- * The name of the model's table. If not specified, the table name will be
11
- * derived from the model name, with any transform function specified in
12
- * config applied.
13
- */
14
- table?: string;
15
-
16
- /**
17
- * The schema in which the model sits. If not specified, will use the
18
- * schema specified in the global config; if no schema is specified in
19
- * the global config, will be set to `public`.
20
- */
21
- schema?: string;
22
-
23
- /**
24
- * The model's column configuration - a map of field name to column
25
- * definitions. The keys of this map will be used in generated functions
26
- * and object fields, so must be valid Javascript identifiers.
27
- */
28
- columns: Record<string, ColumnDefinition>;
29
-
30
- /**
31
- * If the table's primary key contains multiple columns, specify them here.
32
- * If the primary key is on a single column, you can specify it here or in the column definition.
33
- */
34
- primaryKey?: string[];
35
-
36
- /**
37
- * If the table has unique constraints that span multiple columns, you must specify them here.
38
- * If the unique constraint is on a single column, you can specify it here or in the column definition.
39
- */
40
- uniqueConstraints?: UniqueConstraint[];
41
-
42
- /**
43
- * If the table has foreign keys, you must specify them here. If the foreign key is on a single column,
44
- * you can specify it here or in the column definition.
45
- */
46
- foreignKeys?: ForeignKey[];
47
- };
@@ -1,3 +0,0 @@
1
- import { ModelDefinition } from "./ModelDefinition";
2
-
3
- export type ModelDefinitions = Record<string, ModelDefinition>;
@@ -1,13 +0,0 @@
1
- import { ModelName } from "../helpers/ModelName";
2
- import { ManyToManyRelation } from "../relations/ManyToManyRelation";
3
- import { ManyToOneRelation } from "../relations/ManyToOneRelation";
4
- import { OneToManyRelation } from "../relations/OneToManyRelation";
5
- import { ModelDefinitions } from "./ModelDefinitions";
6
-
7
- export type RelationDefinition<
8
- Models extends ModelDefinitions,
9
- M extends ModelName<Models>,
10
- > =
11
- | OneToManyRelation<Models>
12
- | ManyToOneRelation<Models, M>
13
- | ManyToManyRelation<Models>;
@@ -1,8 +0,0 @@
1
- import { ModelName } from "../helpers/ModelName";
2
- import { ModelDefinitions } from "./ModelDefinitions";
3
- import { RelationDefinition } from "./RelationDefinition";
4
-
5
- export type RelationsDefinition<
6
- Models extends ModelDefinitions,
7
- M extends ModelName<Models>,
8
- > = Record<string, RelationDefinition<Models, M>>;
@@ -1,7 +0,0 @@
1
- import { ModelName } from "../helpers/ModelName";
2
- import { ModelDefinitions } from "./ModelDefinitions";
3
- import { RelationsDefinition } from "./RelationsDefinition";
4
-
5
- export type RelationsDefinitions<Models extends ModelDefinitions> = {
6
- [M in ModelName<Models>]?: RelationsDefinition<Models, M>;
7
- };
@@ -1,9 +0,0 @@
1
- import { BaseConfiguration } from "src/schema/types/base/BaseConfiguration";
2
-
3
- import { validateModel } from "./validateModel";
4
-
5
- export const validateConfiguration = (config: BaseConfiguration) => {
6
- for (const [name, model] of Object.entries(config.models)) {
7
- validateModel(config, name, model);
8
- }
9
- };
@@ -1,32 +0,0 @@
1
- import { uniq } from "lodash-es";
2
- import { BaseConfiguration } from "src/schema/types/base/BaseConfiguration";
3
- import { BaseModel } from "src/schema/types/base/BaseModel";
4
-
5
- import { InvalidModelDefinitionError } from "../../errors";
6
-
7
- export const validateModel = (
8
- _config: BaseConfiguration,
9
- name: string,
10
- model: BaseModel,
11
- ) => {
12
- const columns = Object.values(model.columns);
13
-
14
- if (model.table === "") {
15
- throw new InvalidModelDefinitionError("Model has empty table name", {
16
- model: [name, model],
17
- });
18
- }
19
-
20
- if (columns.length === 0) {
21
- throw new InvalidModelDefinitionError("Model has no columns", {
22
- model: [name, model],
23
- });
24
- }
25
-
26
- if (uniq(columns.map((c) => c.name)).length < columns.length) {
27
- throw new InvalidModelDefinitionError(
28
- "Model has two columns with the same name",
29
- { model: [name, model] },
30
- );
31
- }
32
- };
@@ -1,112 +0,0 @@
1
- import { unindent } from "@casekit/unindent";
2
-
3
- import { describe, expect, test } from "vitest";
4
-
5
- import { SQLStatement } from "./SQLStatement";
6
- import { sql } from "./sql";
7
-
8
- describe("SQLStatement", () => {
9
- test("It can be created using the `sql` tagged template literal", () => {
10
- const title = "my first post";
11
- const statement = sql`select * from casekit.post where title = ${title}`;
12
- expect(statement).toBeInstanceOf(SQLStatement);
13
- expect(statement.text).toEqual(unindent`
14
- select * from casekit.post where title = $1
15
- `);
16
- expect(statement.values).toEqual(["my first post"]);
17
- });
18
-
19
- test("Other statement and SQL strings can be appended to it", () => {
20
- const title = "my first post";
21
- const content = "%cats%";
22
- const statement = sql`select *\n`;
23
- statement.push(
24
- "from casekit.post\n",
25
- sql`where title = ${title}\n`,
26
- "and 1 = 1\n",
27
- sql`and content ilike ${content}\n`,
28
- `limit 1`,
29
- );
30
- expect(statement).toBeInstanceOf(SQLStatement);
31
- expect(statement.text).toEqual(unindent`
32
- select *
33
- from casekit.post
34
- where title = $1
35
- and 1 = 1
36
- and content ilike $2
37
- limit 1
38
- `);
39
- expect(statement.values).toEqual(["my first post", "%cats%"]);
40
- });
41
-
42
- test("Interpolated fragments always end with a string even if a variable comes last", () => {
43
- const title = "my first post";
44
- const content = "%cats%";
45
- const statement = sql`select *\n`;
46
- statement.push(
47
- "from casekit.post\n",
48
- sql`where title = ${title}\n`,
49
- "and 1 = 1\n",
50
- sql`and content ilike ${content}`,
51
- );
52
- expect(statement).toBeInstanceOf(SQLStatement);
53
- expect(statement.text).toEqual(unindent`
54
- select *
55
- from casekit.post
56
- where title = $1
57
- and 1 = 1
58
- and content ilike $2
59
- `);
60
- expect(statement.fragments[statement.fragments.length - 1]).toEqual("");
61
- });
62
-
63
- test("Identifiers can be interpolated into the SQL string with the .withIdentifiers method", () => {
64
- const statement =
65
- sql`select * from %I.%I as %I where %I.%I = ${3}`.withIdentifiers(
66
- "casekit",
67
- "foo",
68
- "f",
69
- "f",
70
- "bar",
71
- );
72
- expect(statement.text).toEqual(
73
- `select * from casekit.foo as f where f.bar = $1`,
74
- );
75
- });
76
-
77
- test("Identifiers can be interpolated before and after parameters are included", () => {
78
- const statement =
79
- sql`select * from %I.%I as %I where %I.%I = ${3} and %I.%I = ${"baz"} and %I.%I in (${sql.splat(["quux", "zyzzy"])})`.withIdentifiers(
80
- "casekit",
81
- "foo",
82
- "f",
83
- "f",
84
- "bar",
85
- "f",
86
- "baz",
87
- "f",
88
- "quux",
89
- );
90
- expect(statement.text).toEqual(
91
- `select * from casekit.foo as f where f.bar = $1 and f.baz = $2 and f.quux in ($3, $4)`,
92
- );
93
- expect(statement.values).toEqual([3, "baz", "quux", "zyzzy"]);
94
- });
95
-
96
- test("Empty SQL statements can be interpolated inside SQLStatements", () => {
97
- const statement = sql`select * from (${sql`select * from casekit.foo where bar = ${3} and baz = ${"baz"}`}) zyzzy where quux in (${sql``})`;
98
- expect(statement.text).toEqual(
99
- `select * from (select * from casekit.foo where bar = $1 and baz = $2) zyzzy where quux in ()`,
100
- );
101
- expect(statement.values).toEqual([3, "baz"]);
102
- });
103
-
104
- test("sql.splat allows specifying a separator", () => {
105
- const clauses = [sql`foo = ${3}`, sql`bar = ${"baz"}`];
106
- const statement = sql`select * from casekit.foo where ${sql.splat(clauses, " and ")}`;
107
- expect(statement.text).toEqual(
108
- `select * from casekit.foo where foo = $1 and bar = $2`,
109
- );
110
- expect(statement.values).toEqual([3, "baz"]);
111
- });
112
- });