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

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 (229) hide show
  1. package/lib/queries/find/buildFind.js +1 -1
  2. package/lib/queries/find/buildFind.js.map +1 -1
  3. package/package.json +13 -3
  4. package/.env +0 -2
  5. package/.eslintrc.cjs +0 -69
  6. package/.github/actions/ci-setup/action.yml +0 -19
  7. package/.github/dependabot.yml +0 -12
  8. package/.github/workflows/ci.yml +0 -119
  9. package/.github/workflows/dependabot.yml +0 -26
  10. package/.github/workflows/semgrep.yml +0 -24
  11. package/.prettierrc.cjs +0 -11
  12. package/.vscode/tasks.json +0 -28
  13. package/codecov.yml +0 -7
  14. package/lib/orm.query.test.d.ts +0 -2
  15. package/lib/orm.query.test.d.ts.map +0 -1
  16. package/lib/orm.query.test.js +0 -22
  17. package/lib/orm.query.test.js.map +0 -1
  18. package/src/Connection.ts +0 -65
  19. package/src/errors.ts +0 -18
  20. package/src/index.ts +0 -16
  21. package/src/logger.ts +0 -3
  22. package/src/migrate/commands/implode.ts +0 -46
  23. package/src/migrate/index.ts +0 -1
  24. package/src/migrate/migrator.ts +0 -24
  25. package/src/migrate/sql/createExtensionsSql.test.ts +0 -26
  26. package/src/migrate/sql/createExtensionsSql.ts +0 -16
  27. package/src/migrate/sql/createForeignKeyConstraintSql.test.ts +0 -50
  28. package/src/migrate/sql/createForeignKeyConstraintSql.ts +0 -44
  29. package/src/migrate/sql/createSchemasSql.test.ts +0 -81
  30. package/src/migrate/sql/createSchemasSql.ts +0 -15
  31. package/src/migrate/sql/createTableSql.properties.ts +0 -38
  32. package/src/migrate/sql/createTableSql.test.ts +0 -74
  33. package/src/migrate/sql/createTableSql.ts +0 -53
  34. package/src/migrate/sql/createUniqueConstraintSql.ts +0 -27
  35. package/src/migrate/sql/dropSchemasSql.ts +0 -15
  36. package/src/migrate/sql/dropTableSql.ts +0 -13
  37. package/src/orm.query.test.ts +0 -28
  38. package/src/orm.ts +0 -370
  39. package/src/pull/index.ts +0 -1
  40. package/src/pull/introspect/getForeignKeys.ts +0 -64
  41. package/src/pull/introspect/getPrimaryKeys.ts +0 -26
  42. package/src/pull/introspect/getTables.ts +0 -51
  43. package/src/pull/introspect/getUniqueConstraints.ts +0 -39
  44. package/src/pull/parse/parseCreateUniqueIndexStatement.test.ts +0 -14
  45. package/src/pull/parse/parseCreateUniqueIndexStatement.ts +0 -19
  46. package/src/pull/pull.ts +0 -78
  47. package/src/pull/render/renderModel.test.ts +0 -144
  48. package/src/pull/render/renderModel.ts +0 -141
  49. package/src/pull/render/renderModelsIndex.ts +0 -24
  50. package/src/pull/render/renderRelations.ts +0 -77
  51. package/src/pull/render/renderRelationsIndex.ts +0 -24
  52. package/src/pull/types/ColumnMeta.ts +0 -10
  53. package/src/pull/types/ForeignKey.ts +0 -6
  54. package/src/pull/types/PrimaryKey.ts +0 -6
  55. package/src/pull/types/UniqueConstraint.ts +0 -8
  56. package/src/pull/util/format.ts +0 -17
  57. package/src/pull/util/quote.ts +0 -7
  58. package/src/pull/util/unquote.ts +0 -9
  59. package/src/queries/clauses/IncludeClause.ts +0 -39
  60. package/src/queries/clauses/LateralByClause.ts +0 -4
  61. package/src/queries/clauses/ReturningClause.ts +0 -7
  62. package/src/queries/clauses/SelectClause.ts +0 -7
  63. package/src/queries/clauses/WhereClause.ts +0 -16
  64. package/src/queries/clauses/helpers/OptionalColumn.ts +0 -18
  65. package/src/queries/clauses/helpers/OptionalParams.ts +0 -14
  66. package/src/queries/clauses/helpers/RequiredColumn.ts +0 -8
  67. package/src/queries/clauses/helpers/RequiredParams.ts +0 -14
  68. package/src/queries/clauses/include/IncludedRelationModel.ts +0 -13
  69. package/src/queries/clauses/include/IncludedRelationName.ts +0 -11
  70. package/src/queries/clauses/include/IncludedRelationQuery.ts +0 -20
  71. package/src/queries/clauses/where/buildWhereClause.ts +0 -121
  72. package/src/queries/clauses/where/buildWhereClauses.test.ts +0 -145
  73. package/src/queries/clauses/where/buildWhereClauses.ts +0 -45
  74. package/src/queries/clauses/where/operators.ts +0 -13
  75. package/src/queries/clauses/where/types/WhereClauseValue.ts +0 -39
  76. package/src/queries/count/buildCount.ts +0 -77
  77. package/src/queries/count/countToSql.ts +0 -66
  78. package/src/queries/count/tests/count.test.ts +0 -35
  79. package/src/queries/count/types/BaseCountParams.ts +0 -11
  80. package/src/queries/count/types/CountBuilder.ts +0 -45
  81. package/src/queries/count/types/CountParams.ts +0 -27
  82. package/src/queries/count.ts +0 -33
  83. package/src/queries/create/buildCreate.ts +0 -67
  84. package/src/queries/create/createResultSchema.ts +0 -24
  85. package/src/queries/create/createToSql.ts +0 -44
  86. package/src/queries/create/tests/createMany.varied-keys.test.ts +0 -28
  87. package/src/queries/create/tests/createOne.test-d.ts +0 -116
  88. package/src/queries/create/tests/createOne.test.ts +0 -197
  89. package/src/queries/create/types/BaseCreateManyParams.ts +0 -7
  90. package/src/queries/create/types/BaseCreateOneParams.ts +0 -7
  91. package/src/queries/create/types/CreateManyParams.ts +0 -15
  92. package/src/queries/create/types/CreateManyResult.ts +0 -17
  93. package/src/queries/create/types/CreateOneParams.ts +0 -22
  94. package/src/queries/create/types/CreateOneResult.ts +0 -17
  95. package/src/queries/createMany.ts +0 -38
  96. package/src/queries/createOne.ts +0 -27
  97. package/src/queries/delete/buildDelete.ts +0 -56
  98. package/src/queries/delete/deleteResultSchema.ts +0 -23
  99. package/src/queries/delete/deleteToSql.ts +0 -48
  100. package/src/queries/delete/tests/deleteOne.test.ts +0 -108
  101. package/src/queries/delete/types/BaseDeleteParams.ts +0 -9
  102. package/src/queries/delete/types/DeleteManyResult.ts +0 -16
  103. package/src/queries/delete/types/DeleteOneResult.ts +0 -16
  104. package/src/queries/delete/types/DeleteParams.ts +0 -12
  105. package/src/queries/deleteMany.ts +0 -33
  106. package/src/queries/deleteOne.ts +0 -32
  107. package/src/queries/find/buildFind.ts +0 -138
  108. package/src/queries/find/findResultSchema.ts +0 -32
  109. package/src/queries/find/findToSql.test.ts +0 -123
  110. package/src/queries/find/findToSql.ts +0 -141
  111. package/src/queries/find/getIncludedManyToManyRelations.ts +0 -49
  112. package/src/queries/find/getIncludedOneToManyRelations.ts +0 -44
  113. package/src/queries/find/tests/findMany.include.test.ts +0 -107
  114. package/src/queries/find/tests/findMany.limit.test-d.ts +0 -75
  115. package/src/queries/find/tests/findMany.limit.test.ts +0 -176
  116. package/src/queries/find/tests/findMany.nullable-relations.test.ts +0 -127
  117. package/src/queries/find/tests/findMany.orderBy.test-d.ts +0 -84
  118. package/src/queries/find/tests/findMany.orderBy.test.ts +0 -184
  119. package/src/queries/find/tests/findMany.select.test-d.ts +0 -117
  120. package/src/queries/find/tests/findMany.select.test.ts +0 -188
  121. package/src/queries/find/tests/findMany.too-deep.test-d.ts +0 -154
  122. package/src/queries/find/tests/findMany.where.test-d.ts +0 -85
  123. package/src/queries/find/tests/findMany.where.test.ts +0 -76
  124. package/src/queries/find/tests/middleware.find.where.test.ts +0 -467
  125. package/src/queries/find/types/BaseFindParams.ts +0 -18
  126. package/src/queries/find/types/FindBuilder.ts +0 -73
  127. package/src/queries/find/types/FindManyParams.ts +0 -24
  128. package/src/queries/find/types/FindManyResult.ts +0 -13
  129. package/src/queries/find/types/FindOneParams.ts +0 -17
  130. package/src/queries/find/types/FindOneResult.ts +0 -50
  131. package/src/queries/findMany.ts +0 -134
  132. package/src/queries/findOne.ts +0 -30
  133. package/src/queries/middleware/Middleware.ts +0 -53
  134. package/src/queries/middleware/ValuesMiddleware.ts +0 -9
  135. package/src/queries/middleware/WhereMiddleware.ts +0 -16
  136. package/src/queries/update/buildUpdate.ts +0 -76
  137. package/src/queries/update/tests/updateOne.test.ts +0 -118
  138. package/src/queries/update/types/BaseUpdateParams.ts +0 -10
  139. package/src/queries/update/types/UpdateManyResult.ts +0 -16
  140. package/src/queries/update/types/UpdateOneResult.ts +0 -16
  141. package/src/queries/update/types/UpdateParams.ts +0 -14
  142. package/src/queries/update/types/UpdateValues.ts +0 -12
  143. package/src/queries/update/updateResultSchema.ts +0 -23
  144. package/src/queries/update/updateToSql.ts +0 -43
  145. package/src/queries/updateMany.ts +0 -33
  146. package/src/queries/updateOne.ts +0 -33
  147. package/src/queries/util/hasConditions.test.ts +0 -36
  148. package/src/queries/util/hasConditions.ts +0 -14
  149. package/src/queries/util/rowToObject.test.ts +0 -76
  150. package/src/queries/util/rowToObject.ts +0 -13
  151. package/src/queries/util/tableAlias.test.ts +0 -20
  152. package/src/queries/util/tableAlias.ts +0 -10
  153. package/src/schema/populate/composeMiddleware.ts +0 -51
  154. package/src/schema/populate/populateConfiguration.ts +0 -48
  155. package/src/schema/populate/populateModel.ts +0 -98
  156. package/src/schema/populate/suggestedColumnSchema.ts +0 -62
  157. package/src/schema/types/base/BaseColumn.ts +0 -10
  158. package/src/schema/types/base/BaseConfiguration.ts +0 -14
  159. package/src/schema/types/base/BaseModel.ts +0 -12
  160. package/src/schema/types/base/BaseModels.ts +0 -3
  161. package/src/schema/types/base/BaseOrm.ts +0 -8
  162. package/src/schema/types/base/BaseRelation.ts +0 -19
  163. package/src/schema/types/base/BaseRelations.ts +0 -3
  164. package/src/schema/types/constraints/ForeignKey.ts +0 -13
  165. package/src/schema/types/constraints/UniqueConstraint.ts +0 -9
  166. package/src/schema/types/helpers/ColumnName.ts +0 -6
  167. package/src/schema/types/helpers/ColumnType.ts +0 -23
  168. package/src/schema/types/helpers/Columns.ts +0 -3
  169. package/src/schema/types/helpers/HasDefault.ts +0 -11
  170. package/src/schema/types/helpers/IsNullable.ts +0 -7
  171. package/src/schema/types/helpers/IsProvided.ts +0 -8
  172. package/src/schema/types/helpers/IsSerial.ts +0 -10
  173. package/src/schema/types/helpers/ModelName.ts +0 -6
  174. package/src/schema/types/loose/LooseColumnDefinition.ts +0 -80
  175. package/src/schema/types/loose/LooseModelDefinition.ts +0 -47
  176. package/src/schema/types/loose/LooseModelDefinitions.ts +0 -3
  177. package/src/schema/types/loose/LooseRelationDefinition.ts +0 -10
  178. package/src/schema/types/loose/LooseRelationsDefinition.ts +0 -8
  179. package/src/schema/types/loose/LooseRelationsDefinitions.ts +0 -7
  180. package/src/schema/types/postgres/DataType.ts +0 -65
  181. package/src/schema/types/relations/ManyToManyRelation.ts +0 -15
  182. package/src/schema/types/relations/ManyToOneRelation.ts +0 -14
  183. package/src/schema/types/relations/OneToManyRelation.ts +0 -12
  184. package/src/schema/types/strict/ColumnDefinition.ts +0 -81
  185. package/src/schema/types/strict/ModelDefinition.ts +0 -47
  186. package/src/schema/types/strict/ModelDefinitions.ts +0 -3
  187. package/src/schema/types/strict/RelationDefinition.ts +0 -13
  188. package/src/schema/types/strict/RelationsDefinition.ts +0 -8
  189. package/src/schema/types/strict/RelationsDefinitions.ts +0 -7
  190. package/src/schema/validate/validateConfiguration.ts +0 -9
  191. package/src/schema/validate/validateModel.ts +0 -32
  192. package/src/sql/SQLStatement.test.ts +0 -112
  193. package/src/sql/SQLStatement.ts +0 -86
  194. package/src/sql/index.ts +0 -2
  195. package/src/sql/sql.ts +0 -47
  196. package/src/test/db/index.ts +0 -27
  197. package/src/test/db/models/foo.model.ts +0 -24
  198. package/src/test/db/models/foo.relations.ts +0 -4
  199. package/src/test/db/models/post.model.ts +0 -34
  200. package/src/test/db/models/post.relations.ts +0 -21
  201. package/src/test/db/models/tenant.model.ts +0 -14
  202. package/src/test/db/models/tenant.relations.ts +0 -22
  203. package/src/test/db/models/tenantUser.model.ts +0 -23
  204. package/src/test/db/models/tenantUser.relations.ts +0 -15
  205. package/src/test/db/models/user.model.ts +0 -29
  206. package/src/test/db/models/user.relations.ts +0 -38
  207. package/src/test/db/models.ts +0 -15
  208. package/src/test/db/relations.ts +0 -13
  209. package/src/test/gen/column.ts +0 -39
  210. package/src/test/gen/index.ts +0 -2
  211. package/src/test/gen/model.ts +0 -58
  212. package/src/test/gen/sqldate.ts +0 -8
  213. package/src/test/globalSetup.ts +0 -6
  214. package/src/test/seed/index.ts +0 -75
  215. package/src/test/util/withRollback.ts +0 -18
  216. package/src/test/util/withTransaction.ts +0 -19
  217. package/src/types/ColumnName.ts +0 -6
  218. package/src/types/ColumnType.ts +0 -20
  219. package/src/types/Configuration.ts +0 -21
  220. package/src/types/ModelType.ts +0 -20
  221. package/src/types/util/DeepRequired.ts +0 -7
  222. package/src/types/util/DisallowExtraKeys.ts +0 -5
  223. package/src/types/util/NonEmptyArray.ts +0 -1
  224. package/src/types/util/Simplify.ts +0 -8
  225. package/src/util/ensureArray.ts +0 -4
  226. package/src/util/interleave.test.ts +0 -35
  227. package/src/util/interleave.ts +0 -8
  228. package/tsconfig.json +0 -18
  229. package/vitest.config.ts +0 -15
@@ -1,50 +0,0 @@
1
- import { unindent } from "@casekit/unindent";
2
-
3
- import pg from "pg";
4
- import { describe, expect, test } from "vitest";
5
-
6
- import { ModelDefinition, orm, sql } from "../../";
7
- import { createForeignKeyConstraintSql } from "./createForeignKeyConstraintSql";
8
-
9
- describe("createForeignKeyConstraintSql", () => {
10
- test("it generates an ALTER TABLE command for the specified foreign key constraint", () => {
11
- const foo = {
12
- schema: "casekit",
13
- columns: {
14
- id: { type: "uuid" },
15
- barId: {
16
- name: "bar_id",
17
- type: "uuid",
18
- references: { table: "bar", column: "id" },
19
- },
20
- bazId: {
21
- name: "baz_id",
22
- type: "uuid",
23
- references: { table: "baz", column: "id" },
24
- },
25
- },
26
- foreignKeys: [
27
- {
28
- columns: ["bar_id", "baz_id"],
29
- references: {
30
- table: "quux",
31
- columns: ["bar_id", "baz_id"],
32
- },
33
- onUpdate: sql`CASCADE`,
34
- },
35
- ],
36
- } as const satisfies ModelDefinition;
37
-
38
- const db = orm({ models: { foo }, pool: new pg.Pool() });
39
-
40
- const result = db.models.foo.foreignKeys
41
- .map((fk) => createForeignKeyConstraintSql(db.models.foo, fk).text)
42
- .join("\n");
43
-
44
- expect(result).toEqual(unindent`
45
- ALTER TABLE casekit.foo ADD CONSTRAINT foo_bar_id_baz_id_fkey FOREIGN KEY (bar_id, baz_id) REFERENCES casekit.quux (bar_id, baz_id) ON UPDATE CASCADE;
46
- ALTER TABLE casekit.foo ADD CONSTRAINT foo_bar_id_fkey FOREIGN KEY (bar_id) REFERENCES casekit.bar (id);
47
- ALTER TABLE casekit.foo ADD CONSTRAINT foo_baz_id_fkey FOREIGN KEY (baz_id) REFERENCES casekit.baz (id);
48
- `);
49
- });
50
- });
@@ -1,44 +0,0 @@
1
- import pgfmt from "pg-format";
2
-
3
- import { SQLStatement } from "../..";
4
- import { BaseModel } from "../../schema/types/base/BaseModel";
5
- import { ForeignKey } from "../../schema/types/constraints/ForeignKey";
6
-
7
- export const createForeignKeyConstraintSql = (
8
- model: BaseModel,
9
- constraint: ForeignKey,
10
- ) => {
11
- const statement = new SQLStatement();
12
- statement.push(
13
- pgfmt(`ALTER TABLE %I.%I ADD CONSTRAINT `, model.schema, model.table),
14
- );
15
-
16
- statement.push(
17
- pgfmt("%I", [model.table, ...constraint.columns, "fkey"].join("_")),
18
- );
19
-
20
- statement.push(pgfmt(` FOREIGN KEY (`, model.schema, model.table));
21
- statement.push(constraint.columns.map((c) => pgfmt("%I", c)).join(", "));
22
- statement.push(")");
23
- statement.push(
24
- pgfmt(
25
- ` REFERENCES %I.%I (`,
26
- constraint.references.schema,
27
- constraint.references.table,
28
- ),
29
- );
30
- statement.push(
31
- constraint.references.columns.map((c) => pgfmt("%I", c)).join(", "),
32
- );
33
- statement.push(")");
34
- if (constraint.onDelete) {
35
- statement.push(` ON DELETE `);
36
- statement.push(constraint.onDelete);
37
- }
38
- if (constraint.onUpdate) {
39
- statement.push(` ON UPDATE `);
40
- statement.push(constraint.onUpdate);
41
- }
42
- statement.push(";");
43
- return statement;
44
- };
@@ -1,81 +0,0 @@
1
- import { unindent } from "@casekit/unindent";
2
-
3
- import pg from "pg";
4
- import { describe, expect, test } from "vitest";
5
- import { z } from "zod";
6
-
7
- import { ModelDefinition, orm } from "../../";
8
- import { createSchemasSql } from "./createSchemasSql";
9
-
10
- describe("createSchemaSql", () => {
11
- test("it generates a CREATE SCHEMA command for each unique schema used", () => {
12
- const a = {
13
- schema: "foo",
14
- columns: {
15
- id: { type: "uuid", zodSchema: z.string() },
16
- },
17
- } as const satisfies ModelDefinition;
18
-
19
- const b = {
20
- schema: "bar",
21
- columns: {
22
- id: { type: "uuid", zodSchema: z.string() },
23
- },
24
- } as const satisfies ModelDefinition;
25
-
26
- const db = orm({ models: { a, b }, pool: new pg.Pool() });
27
- expect(createSchemasSql(db).text).toEqual(unindent`
28
- CREATE SCHEMA IF NOT EXISTS foo;
29
- CREATE SCHEMA IF NOT EXISTS bar;
30
- `);
31
- });
32
-
33
- test("it pulls schema from the config if not specified on the model", () => {
34
- const a = {
35
- columns: {
36
- id: { type: "uuid", zodSchema: z.string() },
37
- },
38
- } as const satisfies ModelDefinition;
39
-
40
- const b = {
41
- schema: "bar",
42
- columns: {
43
- id: { type: "uuid", zodSchema: z.string() },
44
- },
45
- } as const satisfies ModelDefinition;
46
-
47
- const db = orm({
48
- schema: "foo",
49
- models: { a, b },
50
- pool: new pg.Pool(),
51
- });
52
- expect(createSchemasSql(db).text).toEqual(unindent`
53
- CREATE SCHEMA IF NOT EXISTS foo;
54
- CREATE SCHEMA IF NOT EXISTS bar;
55
- `);
56
- });
57
-
58
- test("if no schema is specified at all, it tries to create the public schema", () => {
59
- const a = {
60
- columns: {
61
- id: { type: "uuid", zodSchema: z.string() },
62
- },
63
- } as const satisfies ModelDefinition;
64
-
65
- const b = {
66
- schema: "bar",
67
- columns: {
68
- id: { type: "uuid", zodSchema: z.string() },
69
- },
70
- } as const satisfies ModelDefinition;
71
-
72
- const db = orm({
73
- models: { a, b },
74
- pool: new pg.Pool(),
75
- });
76
- expect(createSchemasSql(db).text).toEqual(unindent`
77
- CREATE SCHEMA IF NOT EXISTS public;
78
- CREATE SCHEMA IF NOT EXISTS bar;
79
- `);
80
- });
81
- });
@@ -1,15 +0,0 @@
1
- import { uniq } from "lodash-es";
2
- import pgfmt from "pg-format";
3
-
4
- import { BaseOrm } from "../../schema/types/base/BaseOrm";
5
- import { SQLStatement } from "../../sql";
6
-
7
- export const createSchemasSql = (db: BaseOrm): SQLStatement => {
8
- const schemas = uniq(Object.values(db.models).map((m) => m.schema));
9
-
10
- return new SQLStatement(
11
- schemas
12
- .map((s) => pgfmt("CREATE SCHEMA IF NOT EXISTS %I;", s))
13
- .join("\n"),
14
- );
15
- };
@@ -1,38 +0,0 @@
1
- import { test } from "@fast-check/vitest";
2
- import { isEqual } from "lodash-es";
3
- import pg from "pg";
4
- import pgfmt from "pg-format";
5
-
6
- import { orm } from "../..";
7
- import * as gen from "../../test/gen";
8
- import { withRollback } from "../../test/util/withRollback";
9
- import { createSchemasSql } from "./createSchemasSql";
10
- import { createTableSql } from "./createTableSql";
11
-
12
- test.prop([gen.model()])("should generate valid DDL", async (model) => {
13
- return await withRollback(async (client) => {
14
- const db = orm({
15
- models: { model },
16
- relations: { model: {} },
17
-
18
- pool: new pg.Pool(),
19
- });
20
-
21
- // create the schema
22
- await client.query(createSchemasSql(db));
23
-
24
- // create the table
25
- await client.query(createTableSql(db.models.model));
26
-
27
- // select from the newly created table so we can check it's there
28
- const result = await client.query(
29
- pgfmt("select * from %I.%I", model.schema, model.table),
30
- );
31
-
32
- // the selected fields should match the ones defined in the model
33
- return isEqual(
34
- result.fields.map((f) => f.name),
35
- Object.values(model.columns).map((c) => c.name),
36
- );
37
- });
38
- });
@@ -1,74 +0,0 @@
1
- import { unindent } from "@casekit/unindent";
2
-
3
- import { snakeCase, uniqueId } from "lodash-es";
4
- import pg from "pg";
5
- import { describe, expect, test } from "vitest";
6
- import { z } from "zod";
7
-
8
- import { ModelDefinition, orm } from "../..";
9
- import { sql } from "../../sql";
10
- import { db } from "../../test/db";
11
- import { createTableSql } from "./createTableSql";
12
-
13
- describe("createTableSql", () => {
14
- test("it generates a CREATE TABLE command", () => {
15
- expect(createTableSql(db.models.user).text).toEqual(unindent`
16
- CREATE TABLE casekit."user" (
17
- id uuid NOT NULL DEFAULT uuid_generate_v4(),
18
- username text NOT NULL,
19
- invited_by_id uuid,
20
- created_at timestamp,
21
- updated_at timestamp,
22
- deleted_at timestamp,
23
- PRIMARY KEY (id)
24
- );
25
- `);
26
- });
27
-
28
- test("the generated DDL successfully creates a table", async () => {
29
- const table = uniqueId("table-");
30
- const post = {
31
- table,
32
- columns: {
33
- id: {
34
- zodSchema: z.string().uuid(),
35
- type: "uuid",
36
- default: sql`uuid_generate_v4()`,
37
- },
38
- title: {
39
- zodSchema: z.string(),
40
- type: "text",
41
- default: "My first post",
42
- },
43
- content: { zodSchema: z.string(), type: "text" },
44
- publishedAt: {
45
- zodSchema: z.date(),
46
- type: "timestamp",
47
- nullable: true,
48
- },
49
- },
50
- } as const satisfies ModelDefinition;
51
- await orm({
52
- models: { post },
53
- relations: { post: {} },
54
- naming: { column: snakeCase },
55
- pool: new pg.Pool(),
56
- }).transact(
57
- async (db) => {
58
- await db.connection.query(createTableSql(db.models.post));
59
-
60
- const result = await db.connection.query(
61
- sql`select * from public.%I`.withIdentifiers(table),
62
- );
63
-
64
- expect(result.fields.map((f) => f.name)).toEqual([
65
- "id",
66
- "title",
67
- "content",
68
- "published_at",
69
- ]);
70
- },
71
- { rollback: true },
72
- );
73
- });
74
- });
@@ -1,53 +0,0 @@
1
- import pgfmt from "pg-format";
2
-
3
- import { BaseModel } from "../../schema/types/base/BaseModel";
4
- import { SQLStatement } from "../../sql";
5
- import { interleave } from "../../util/interleave";
6
-
7
- export const createTableSql = (model: BaseModel): SQLStatement => {
8
- const columns = Object.values(model.columns);
9
- const primaryKey = model.primaryKey;
10
-
11
- const statement = new SQLStatement();
12
-
13
- statement.push(
14
- pgfmt("CREATE TABLE %I.%I (\n", model.schema ?? "public", model.table),
15
- );
16
-
17
- statement.push(
18
- ...interleave(
19
- columns.map((column) => {
20
- const definition = new SQLStatement(
21
- pgfmt(` %I %s`, column.name, column.type),
22
- );
23
-
24
- if (!column.nullable) definition.push(pgfmt(" NOT NULL"));
25
-
26
- if (column.default instanceof SQLStatement) {
27
- definition.push(" DEFAULT ");
28
- definition.push(column.default);
29
- } else if (
30
- column.default !== null &&
31
- column.default !== undefined
32
- ) {
33
- definition.push(pgfmt(" DEFAULT %L", column.default));
34
- }
35
- return definition;
36
- }),
37
- ",\n",
38
- ),
39
- );
40
-
41
- if (primaryKey.length > 0) {
42
- statement.push(
43
- pgfmt(
44
- `,\n PRIMARY KEY (${primaryKey.map(() => "%I").join(", ")})`,
45
- ...primaryKey,
46
- ),
47
- );
48
- }
49
-
50
- statement.push(pgfmt("\n);"));
51
-
52
- return statement;
53
- };
@@ -1,27 +0,0 @@
1
- import pgfmt from "pg-format";
2
-
3
- import { SQLStatement } from "../..";
4
- import { BaseModel } from "../../schema/types/base/BaseModel";
5
- import { UniqueConstraint } from "../../schema/types/constraints/UniqueConstraint";
6
-
7
- export const createUniqueConstraintSql = (
8
- model: BaseModel,
9
- constraint: UniqueConstraint,
10
- ) => {
11
- const statement = new SQLStatement();
12
- statement.push(
13
- pgfmt(`CREATE UNIQUE INDEX ON %I.%I (`, model.schema, model.table),
14
- );
15
- statement.push(constraint.columns.map((c) => pgfmt("%I", c)).join(", "));
16
- statement.push(")");
17
- if (constraint.nullsNotDistinct) {
18
- statement.push(" NULLS NOT DISTINCT");
19
- }
20
- if (constraint.where) {
21
- statement.push(" WHERE (");
22
- statement.push(constraint.where);
23
- statement.push(")");
24
- }
25
- statement.push(";");
26
- return statement;
27
- };
@@ -1,15 +0,0 @@
1
- import { uniq } from "lodash-es";
2
- import pgfmt from "pg-format";
3
-
4
- import { BaseOrm } from "../../schema/types/base/BaseOrm";
5
- import { SQLStatement } from "../../sql";
6
-
7
- export const dropSchemasSql = (db: BaseOrm): SQLStatement => {
8
- const schemas = uniq(Object.values(db.models).map((m) => m.schema));
9
-
10
- return new SQLStatement(
11
- schemas
12
- .map((s) => pgfmt("DROP SCHEMA IF EXISTS %I CASCADE;", s))
13
- .join("\n"),
14
- );
15
- };
@@ -1,13 +0,0 @@
1
- import pgfmt from "pg-format";
2
-
3
- import { BaseModel } from "../../schema/types/base/BaseModel";
4
- import { SQLStatement } from "../../sql";
5
-
6
- export const dropTableSql = (model: BaseModel): SQLStatement => {
7
- const statement = pgfmt(
8
- "DROP TABLE IF EXISTS %I.%I CASCADE;",
9
- model.schema ?? "public",
10
- model.table,
11
- );
12
- return new SQLStatement(statement);
13
- };
@@ -1,28 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
-
3
- import { db } from "./test/db";
4
- import { seed } from "./test/seed";
5
-
6
- describe("orm.query", () => {
7
- test("allows arbitrary sql queries to be run against the database", async () => {
8
- await db.transact(
9
- async (db) => {
10
- await seed(db, {
11
- users: [
12
- {
13
- username: "Russell",
14
- tenants: [{ name: "Popova Park", posts: 6 }],
15
- },
16
- ],
17
- });
18
-
19
- const result = await db.query<{ total: number }>`
20
- select count(1)::int as total from casekit.post;
21
- `;
22
-
23
- expect(result[0]).toEqual({ total: 6 });
24
- },
25
- { rollback: true },
26
- );
27
- });
28
- });