@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.
- package/package.json +13 -3
- package/.env +0 -2
- package/.eslintrc.cjs +0 -69
- package/.github/actions/ci-setup/action.yml +0 -19
- package/.github/dependabot.yml +0 -12
- package/.github/workflows/ci.yml +0 -119
- package/.github/workflows/dependabot.yml +0 -26
- package/.github/workflows/semgrep.yml +0 -24
- package/.prettierrc.cjs +0 -11
- package/.vscode/tasks.json +0 -28
- package/codecov.yml +0 -7
- package/lib/orm.query.test.d.ts +0 -2
- package/lib/orm.query.test.d.ts.map +0 -1
- package/lib/orm.query.test.js +0 -22
- package/lib/orm.query.test.js.map +0 -1
- package/src/Connection.ts +0 -65
- package/src/errors.ts +0 -18
- package/src/index.ts +0 -16
- package/src/logger.ts +0 -3
- package/src/migrate/commands/implode.ts +0 -46
- package/src/migrate/index.ts +0 -1
- package/src/migrate/migrator.ts +0 -24
- package/src/migrate/sql/createExtensionsSql.test.ts +0 -26
- package/src/migrate/sql/createExtensionsSql.ts +0 -16
- package/src/migrate/sql/createForeignKeyConstraintSql.test.ts +0 -50
- package/src/migrate/sql/createForeignKeyConstraintSql.ts +0 -44
- package/src/migrate/sql/createSchemasSql.test.ts +0 -81
- package/src/migrate/sql/createSchemasSql.ts +0 -15
- package/src/migrate/sql/createTableSql.properties.ts +0 -38
- package/src/migrate/sql/createTableSql.test.ts +0 -74
- package/src/migrate/sql/createTableSql.ts +0 -53
- package/src/migrate/sql/createUniqueConstraintSql.ts +0 -27
- package/src/migrate/sql/dropSchemasSql.ts +0 -15
- package/src/migrate/sql/dropTableSql.ts +0 -13
- package/src/orm.query.test.ts +0 -28
- package/src/orm.ts +0 -370
- package/src/pull/index.ts +0 -1
- package/src/pull/introspect/getForeignKeys.ts +0 -64
- package/src/pull/introspect/getPrimaryKeys.ts +0 -26
- package/src/pull/introspect/getTables.ts +0 -51
- package/src/pull/introspect/getUniqueConstraints.ts +0 -39
- package/src/pull/parse/parseCreateUniqueIndexStatement.test.ts +0 -14
- package/src/pull/parse/parseCreateUniqueIndexStatement.ts +0 -19
- package/src/pull/pull.ts +0 -78
- package/src/pull/render/renderModel.test.ts +0 -144
- package/src/pull/render/renderModel.ts +0 -141
- package/src/pull/render/renderModelsIndex.ts +0 -24
- package/src/pull/render/renderRelations.ts +0 -77
- package/src/pull/render/renderRelationsIndex.ts +0 -24
- package/src/pull/types/ColumnMeta.ts +0 -10
- package/src/pull/types/ForeignKey.ts +0 -6
- package/src/pull/types/PrimaryKey.ts +0 -6
- package/src/pull/types/UniqueConstraint.ts +0 -8
- package/src/pull/util/format.ts +0 -17
- package/src/pull/util/quote.ts +0 -7
- package/src/pull/util/unquote.ts +0 -9
- package/src/queries/clauses/IncludeClause.ts +0 -39
- package/src/queries/clauses/LateralByClause.ts +0 -4
- package/src/queries/clauses/ReturningClause.ts +0 -7
- package/src/queries/clauses/SelectClause.ts +0 -7
- package/src/queries/clauses/WhereClause.ts +0 -16
- package/src/queries/clauses/helpers/OptionalColumn.ts +0 -18
- package/src/queries/clauses/helpers/OptionalParams.ts +0 -14
- package/src/queries/clauses/helpers/RequiredColumn.ts +0 -8
- package/src/queries/clauses/helpers/RequiredParams.ts +0 -14
- package/src/queries/clauses/include/IncludedRelationModel.ts +0 -13
- package/src/queries/clauses/include/IncludedRelationName.ts +0 -11
- package/src/queries/clauses/include/IncludedRelationQuery.ts +0 -20
- package/src/queries/clauses/where/buildWhereClause.ts +0 -121
- package/src/queries/clauses/where/buildWhereClauses.test.ts +0 -145
- package/src/queries/clauses/where/buildWhereClauses.ts +0 -45
- package/src/queries/clauses/where/operators.ts +0 -13
- package/src/queries/clauses/where/types/WhereClauseValue.ts +0 -39
- package/src/queries/count/buildCount.ts +0 -77
- package/src/queries/count/countToSql.ts +0 -66
- package/src/queries/count/tests/count.test.ts +0 -35
- package/src/queries/count/types/BaseCountParams.ts +0 -11
- package/src/queries/count/types/CountBuilder.ts +0 -45
- package/src/queries/count/types/CountParams.ts +0 -27
- package/src/queries/count.ts +0 -33
- package/src/queries/create/buildCreate.ts +0 -67
- package/src/queries/create/createResultSchema.ts +0 -24
- package/src/queries/create/createToSql.ts +0 -44
- package/src/queries/create/tests/createMany.varied-keys.test.ts +0 -28
- package/src/queries/create/tests/createOne.test-d.ts +0 -116
- package/src/queries/create/tests/createOne.test.ts +0 -197
- package/src/queries/create/types/BaseCreateManyParams.ts +0 -7
- package/src/queries/create/types/BaseCreateOneParams.ts +0 -7
- package/src/queries/create/types/CreateManyParams.ts +0 -15
- package/src/queries/create/types/CreateManyResult.ts +0 -17
- package/src/queries/create/types/CreateOneParams.ts +0 -22
- package/src/queries/create/types/CreateOneResult.ts +0 -17
- package/src/queries/createMany.ts +0 -38
- package/src/queries/createOne.ts +0 -27
- package/src/queries/delete/buildDelete.ts +0 -56
- package/src/queries/delete/deleteResultSchema.ts +0 -23
- package/src/queries/delete/deleteToSql.ts +0 -48
- package/src/queries/delete/tests/deleteOne.test.ts +0 -108
- package/src/queries/delete/types/BaseDeleteParams.ts +0 -9
- package/src/queries/delete/types/DeleteManyResult.ts +0 -16
- package/src/queries/delete/types/DeleteOneResult.ts +0 -16
- package/src/queries/delete/types/DeleteParams.ts +0 -12
- package/src/queries/deleteMany.ts +0 -33
- package/src/queries/deleteOne.ts +0 -32
- package/src/queries/find/buildFind.ts +0 -138
- package/src/queries/find/findResultSchema.ts +0 -32
- package/src/queries/find/findToSql.test.ts +0 -123
- package/src/queries/find/findToSql.ts +0 -141
- package/src/queries/find/getIncludedManyToManyRelations.ts +0 -49
- package/src/queries/find/getIncludedOneToManyRelations.ts +0 -44
- package/src/queries/find/tests/findMany.include.test.ts +0 -107
- package/src/queries/find/tests/findMany.limit.test-d.ts +0 -75
- package/src/queries/find/tests/findMany.limit.test.ts +0 -176
- package/src/queries/find/tests/findMany.nullable-relations.test.ts +0 -127
- package/src/queries/find/tests/findMany.orderBy.test-d.ts +0 -84
- package/src/queries/find/tests/findMany.orderBy.test.ts +0 -184
- package/src/queries/find/tests/findMany.select.test-d.ts +0 -117
- package/src/queries/find/tests/findMany.select.test.ts +0 -188
- package/src/queries/find/tests/findMany.too-deep.test-d.ts +0 -154
- package/src/queries/find/tests/findMany.where.test-d.ts +0 -85
- package/src/queries/find/tests/findMany.where.test.ts +0 -76
- package/src/queries/find/tests/middleware.find.where.test.ts +0 -467
- package/src/queries/find/types/BaseFindParams.ts +0 -18
- package/src/queries/find/types/FindBuilder.ts +0 -73
- package/src/queries/find/types/FindManyParams.ts +0 -24
- package/src/queries/find/types/FindManyResult.ts +0 -13
- package/src/queries/find/types/FindOneParams.ts +0 -17
- package/src/queries/find/types/FindOneResult.ts +0 -50
- package/src/queries/findMany.ts +0 -134
- package/src/queries/findOne.ts +0 -30
- package/src/queries/middleware/Middleware.ts +0 -53
- package/src/queries/middleware/ValuesMiddleware.ts +0 -9
- package/src/queries/middleware/WhereMiddleware.ts +0 -16
- package/src/queries/update/buildUpdate.ts +0 -76
- package/src/queries/update/tests/updateOne.test.ts +0 -118
- package/src/queries/update/types/BaseUpdateParams.ts +0 -10
- package/src/queries/update/types/UpdateManyResult.ts +0 -16
- package/src/queries/update/types/UpdateOneResult.ts +0 -16
- package/src/queries/update/types/UpdateParams.ts +0 -14
- package/src/queries/update/types/UpdateValues.ts +0 -12
- package/src/queries/update/updateResultSchema.ts +0 -23
- package/src/queries/update/updateToSql.ts +0 -43
- package/src/queries/updateMany.ts +0 -33
- package/src/queries/updateOne.ts +0 -33
- package/src/queries/util/hasConditions.test.ts +0 -36
- package/src/queries/util/hasConditions.ts +0 -14
- package/src/queries/util/rowToObject.test.ts +0 -76
- package/src/queries/util/rowToObject.ts +0 -13
- package/src/queries/util/tableAlias.test.ts +0 -20
- package/src/queries/util/tableAlias.ts +0 -10
- package/src/schema/populate/composeMiddleware.ts +0 -51
- package/src/schema/populate/populateConfiguration.ts +0 -48
- package/src/schema/populate/populateModel.ts +0 -98
- package/src/schema/populate/suggestedColumnSchema.ts +0 -62
- package/src/schema/types/base/BaseColumn.ts +0 -10
- package/src/schema/types/base/BaseConfiguration.ts +0 -14
- package/src/schema/types/base/BaseModel.ts +0 -12
- package/src/schema/types/base/BaseModels.ts +0 -3
- package/src/schema/types/base/BaseOrm.ts +0 -8
- package/src/schema/types/base/BaseRelation.ts +0 -19
- package/src/schema/types/base/BaseRelations.ts +0 -3
- package/src/schema/types/constraints/ForeignKey.ts +0 -13
- package/src/schema/types/constraints/UniqueConstraint.ts +0 -9
- package/src/schema/types/helpers/ColumnName.ts +0 -6
- package/src/schema/types/helpers/ColumnType.ts +0 -23
- package/src/schema/types/helpers/Columns.ts +0 -3
- package/src/schema/types/helpers/HasDefault.ts +0 -11
- package/src/schema/types/helpers/IsNullable.ts +0 -7
- package/src/schema/types/helpers/IsProvided.ts +0 -8
- package/src/schema/types/helpers/IsSerial.ts +0 -10
- package/src/schema/types/helpers/ModelName.ts +0 -6
- package/src/schema/types/loose/LooseColumnDefinition.ts +0 -80
- package/src/schema/types/loose/LooseModelDefinition.ts +0 -47
- package/src/schema/types/loose/LooseModelDefinitions.ts +0 -3
- package/src/schema/types/loose/LooseRelationDefinition.ts +0 -10
- package/src/schema/types/loose/LooseRelationsDefinition.ts +0 -8
- package/src/schema/types/loose/LooseRelationsDefinitions.ts +0 -7
- package/src/schema/types/postgres/DataType.ts +0 -65
- package/src/schema/types/relations/ManyToManyRelation.ts +0 -15
- package/src/schema/types/relations/ManyToOneRelation.ts +0 -14
- package/src/schema/types/relations/OneToManyRelation.ts +0 -12
- package/src/schema/types/strict/ColumnDefinition.ts +0 -81
- package/src/schema/types/strict/ModelDefinition.ts +0 -47
- package/src/schema/types/strict/ModelDefinitions.ts +0 -3
- package/src/schema/types/strict/RelationDefinition.ts +0 -13
- package/src/schema/types/strict/RelationsDefinition.ts +0 -8
- package/src/schema/types/strict/RelationsDefinitions.ts +0 -7
- package/src/schema/validate/validateConfiguration.ts +0 -9
- package/src/schema/validate/validateModel.ts +0 -32
- package/src/sql/SQLStatement.test.ts +0 -112
- package/src/sql/SQLStatement.ts +0 -86
- package/src/sql/index.ts +0 -2
- package/src/sql/sql.ts +0 -47
- package/src/test/db/index.ts +0 -27
- package/src/test/db/models/foo.model.ts +0 -24
- package/src/test/db/models/foo.relations.ts +0 -4
- package/src/test/db/models/post.model.ts +0 -34
- package/src/test/db/models/post.relations.ts +0 -21
- package/src/test/db/models/tenant.model.ts +0 -14
- package/src/test/db/models/tenant.relations.ts +0 -22
- package/src/test/db/models/tenantUser.model.ts +0 -23
- package/src/test/db/models/tenantUser.relations.ts +0 -15
- package/src/test/db/models/user.model.ts +0 -29
- package/src/test/db/models/user.relations.ts +0 -38
- package/src/test/db/models.ts +0 -15
- package/src/test/db/relations.ts +0 -13
- package/src/test/gen/column.ts +0 -39
- package/src/test/gen/index.ts +0 -2
- package/src/test/gen/model.ts +0 -58
- package/src/test/gen/sqldate.ts +0 -8
- package/src/test/globalSetup.ts +0 -6
- package/src/test/seed/index.ts +0 -75
- package/src/test/util/withRollback.ts +0 -18
- package/src/test/util/withTransaction.ts +0 -19
- package/src/types/ColumnName.ts +0 -6
- package/src/types/ColumnType.ts +0 -20
- package/src/types/Configuration.ts +0 -21
- package/src/types/ModelType.ts +0 -20
- package/src/types/util/DeepRequired.ts +0 -7
- package/src/types/util/DisallowExtraKeys.ts +0 -5
- package/src/types/util/NonEmptyArray.ts +0 -1
- package/src/types/util/Simplify.ts +0 -8
- package/src/util/ensureArray.ts +0 -4
- package/src/util/interleave.test.ts +0 -35
- package/src/util/interleave.ts +0 -8
- package/tsconfig.json +0 -18
- package/vitest.config.ts +0 -15
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { unindent } from "@casekit/unindent";
|
|
2
|
-
|
|
3
|
-
import { describe, expect, test } from "vitest";
|
|
4
|
-
|
|
5
|
-
import { renderModel } from "./renderModel";
|
|
6
|
-
|
|
7
|
-
describe("renderModel", () => {
|
|
8
|
-
test("it renders a valid model definition given introspection results", async () => {
|
|
9
|
-
expect(
|
|
10
|
-
await renderModel({
|
|
11
|
-
table: "my_table",
|
|
12
|
-
columns: [
|
|
13
|
-
{
|
|
14
|
-
name: "id",
|
|
15
|
-
type: "uuid",
|
|
16
|
-
default: "uuid_generate_v4()",
|
|
17
|
-
cardinality: 0,
|
|
18
|
-
elementtype: null,
|
|
19
|
-
table: "my_table",
|
|
20
|
-
ordinal: 0,
|
|
21
|
-
nullable: false,
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
name: "name",
|
|
25
|
-
type: "text",
|
|
26
|
-
default: null,
|
|
27
|
-
cardinality: 0,
|
|
28
|
-
elementtype: null,
|
|
29
|
-
table: "my_table",
|
|
30
|
-
ordinal: 1,
|
|
31
|
-
nullable: true,
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
uniqueConstraints: [
|
|
35
|
-
{
|
|
36
|
-
name: "my_table_name_unique",
|
|
37
|
-
table: "my_table",
|
|
38
|
-
definition:
|
|
39
|
-
"CREATE UNIQUE INDEX ON my_table USING btree (name)",
|
|
40
|
-
columns: ["name"],
|
|
41
|
-
where: undefined,
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
foreignKeys: [],
|
|
45
|
-
primaryKey: ["id"],
|
|
46
|
-
}),
|
|
47
|
-
).toEqual(
|
|
48
|
-
unindent`
|
|
49
|
-
import { type ModelDefinition, type ModelType, sql } from "@casekit/orm";
|
|
50
|
-
|
|
51
|
-
export const myTable = {
|
|
52
|
-
table: "my_table",
|
|
53
|
-
columns: {
|
|
54
|
-
id: {
|
|
55
|
-
name: "id",
|
|
56
|
-
type: "uuid",
|
|
57
|
-
primaryKey: true,
|
|
58
|
-
default: sql\`uuid_generate_v4()\`,
|
|
59
|
-
},
|
|
60
|
-
name: { name: "name", type: "text", unique: true, nullable: true },
|
|
61
|
-
},
|
|
62
|
-
} as const satisfies ModelDefinition;
|
|
63
|
-
|
|
64
|
-
export type MyTable = ModelType<typeof myTable>;
|
|
65
|
-
` + "\n",
|
|
66
|
-
);
|
|
67
|
-
});
|
|
68
|
-
test("it renders a valid model definition given introspection results with a column-local unique constraint", async () => {
|
|
69
|
-
expect(
|
|
70
|
-
await renderModel({
|
|
71
|
-
table: "my_table",
|
|
72
|
-
columns: [
|
|
73
|
-
{
|
|
74
|
-
name: "id",
|
|
75
|
-
type: "uuid",
|
|
76
|
-
default: "uuid_generate_v4()",
|
|
77
|
-
cardinality: 0,
|
|
78
|
-
elementtype: null,
|
|
79
|
-
table: "my_table",
|
|
80
|
-
ordinal: 0,
|
|
81
|
-
nullable: false,
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
name: "name",
|
|
85
|
-
type: "text",
|
|
86
|
-
default: null,
|
|
87
|
-
cardinality: 0,
|
|
88
|
-
elementtype: null,
|
|
89
|
-
table: "my_table",
|
|
90
|
-
ordinal: 1,
|
|
91
|
-
nullable: true,
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
name: "deleted_at",
|
|
95
|
-
type: "timestamp",
|
|
96
|
-
default: null,
|
|
97
|
-
cardinality: 0,
|
|
98
|
-
elementtype: null,
|
|
99
|
-
table: "my_table",
|
|
100
|
-
ordinal: 2,
|
|
101
|
-
nullable: true,
|
|
102
|
-
},
|
|
103
|
-
],
|
|
104
|
-
uniqueConstraints: [
|
|
105
|
-
{
|
|
106
|
-
name: "my_table_name_unique",
|
|
107
|
-
table: "my_table",
|
|
108
|
-
definition:
|
|
109
|
-
"CREATE UNIQUE INDEX ON my_table USING btree (name)",
|
|
110
|
-
columns: ["name"],
|
|
111
|
-
where: "deleted_at IS NULL",
|
|
112
|
-
},
|
|
113
|
-
],
|
|
114
|
-
primaryKey: ["id"],
|
|
115
|
-
foreignKeys: [],
|
|
116
|
-
}),
|
|
117
|
-
).toEqual(
|
|
118
|
-
unindent`
|
|
119
|
-
import { type ModelDefinition, type ModelType, sql } from "@casekit/orm";
|
|
120
|
-
|
|
121
|
-
export const myTable = {
|
|
122
|
-
table: "my_table",
|
|
123
|
-
columns: {
|
|
124
|
-
id: {
|
|
125
|
-
name: "id",
|
|
126
|
-
type: "uuid",
|
|
127
|
-
primaryKey: true,
|
|
128
|
-
default: sql\`uuid_generate_v4()\`,
|
|
129
|
-
},
|
|
130
|
-
name: {
|
|
131
|
-
name: "name",
|
|
132
|
-
type: "text",
|
|
133
|
-
unique: { where: sql\`deleted_at IS NULL\` },
|
|
134
|
-
nullable: true,
|
|
135
|
-
},
|
|
136
|
-
deletedAt: { name: "deleted_at", type: "timestamp", nullable: true },
|
|
137
|
-
},
|
|
138
|
-
} as const satisfies ModelDefinition;
|
|
139
|
-
|
|
140
|
-
export type MyTable = ModelType<typeof myTable>;
|
|
141
|
-
` + "\n",
|
|
142
|
-
);
|
|
143
|
-
});
|
|
144
|
-
});
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { camelCase, identity, times, upperFirst } from "lodash-es";
|
|
2
|
-
|
|
3
|
-
import { ColumnMeta } from "../types/ColumnMeta";
|
|
4
|
-
import { ForeignKey } from "../types/ForeignKey";
|
|
5
|
-
import { UniqueConstraint } from "../types/UniqueConstraint";
|
|
6
|
-
import { format } from "../util/format";
|
|
7
|
-
import { quote } from "../util/quote";
|
|
8
|
-
|
|
9
|
-
type Definition = {
|
|
10
|
-
table: string;
|
|
11
|
-
columns: ColumnMeta[];
|
|
12
|
-
primaryKey: string[];
|
|
13
|
-
uniqueConstraints: UniqueConstraint[];
|
|
14
|
-
foreignKeys: ForeignKey[];
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const renderDefault = (d: string) => {
|
|
18
|
-
return d.match(/^\d+$/)
|
|
19
|
-
? `${d}`
|
|
20
|
-
: d.match(/::text$/)
|
|
21
|
-
? d.replace(/::text$/, "")
|
|
22
|
-
: ["true", "false"].includes(d)
|
|
23
|
-
? d
|
|
24
|
-
: `sql\`${d}\``;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const renderType = (column: ColumnMeta) => {
|
|
28
|
-
if (column.type === "ARRAY") {
|
|
29
|
-
return (
|
|
30
|
-
column.elementtype +
|
|
31
|
-
times(column.cardinality)
|
|
32
|
-
.map(() => "[]")
|
|
33
|
-
.join("")
|
|
34
|
-
);
|
|
35
|
-
} else {
|
|
36
|
-
return column.type;
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export const renderColumn = (def: Definition) => (column: ColumnMeta) => {
|
|
41
|
-
const primaryKey =
|
|
42
|
-
def.primaryKey.length === 1 && def.primaryKey[0] === column.name;
|
|
43
|
-
|
|
44
|
-
const unique = def.uniqueConstraints.find(
|
|
45
|
-
(uc) => uc.columns.length === 1 && uc.columns[0] === column.name,
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
const references = def.foreignKeys.find(
|
|
49
|
-
(fk) =>
|
|
50
|
-
fk.columnsFrom.length === 1 && fk.columnsFrom[0] === column.name,
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
const definition = [
|
|
54
|
-
`name: "${column.name}"`,
|
|
55
|
-
`type: "${renderType(column)}"`,
|
|
56
|
-
primaryKey ? "primaryKey: true" : null,
|
|
57
|
-
unique ? `unique: ${renderColumnUniqueConstraint(unique)}` : null,
|
|
58
|
-
column.nullable ? "nullable: true" : null,
|
|
59
|
-
column.default ? `default: ${renderDefault(column.default)}` : null,
|
|
60
|
-
references
|
|
61
|
-
? `references: { table: ${quote(references.tableTo)}, column: ${quote(references.columnsTo[0])} }`
|
|
62
|
-
: null,
|
|
63
|
-
]
|
|
64
|
-
.filter(identity)
|
|
65
|
-
.join(", ");
|
|
66
|
-
|
|
67
|
-
return `"${camelCase(column.name)}": { ${definition} }`;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export const renderColumnUniqueConstraint = (constraint: UniqueConstraint) => {
|
|
71
|
-
if (constraint.nullsNotDistinct || constraint.where) {
|
|
72
|
-
return `{${constraint.nullsNotDistinct ? " nullsNotDistinct: true," : ""}${constraint.where ? ` where: sql\`${constraint.where}\`}` : ""}`;
|
|
73
|
-
} else {
|
|
74
|
-
return "true";
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export const renderUniqueConstraint = (constraint: UniqueConstraint) => {
|
|
79
|
-
return `{ columns: [${constraint.columns.map(quote).join(", ")}], ${constraint.where ? `where: sql\`${constraint.where}\`,` : ""} }`;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const renderUniqueConstraints = (constraints: UniqueConstraint[]) => {
|
|
83
|
-
return `uniqueConstraints: [ ${constraints.map(renderUniqueConstraint).join(", ")} ]`;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export const renderForeignKey = (constraint: ForeignKey) => {
|
|
87
|
-
return `{
|
|
88
|
-
columns: [${constraint.columnsFrom.map(quote).join(", ")}],
|
|
89
|
-
references: {
|
|
90
|
-
table: ${quote(constraint.tableTo)},
|
|
91
|
-
columns: [${constraint.columnsTo.map(quote).join(", ")}]
|
|
92
|
-
}
|
|
93
|
-
}`;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export const renderForeignKeys = (constraints: ForeignKey[]) => {
|
|
97
|
-
return `foreignKeys: [ ${constraints.map(renderForeignKey).join(", ")} ]`;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export const renderModel = async (def: Definition) => {
|
|
101
|
-
const imports = ["type ModelDefinition", "type ModelType"];
|
|
102
|
-
if (def.columns.find((c) => c.default !== null)) {
|
|
103
|
-
imports.push("sql");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const lines: string[] = [];
|
|
107
|
-
|
|
108
|
-
lines.push(
|
|
109
|
-
`columns: { ${def.columns.map(renderColumn(def)).join(",\n")} }`,
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
if (def.primaryKey.length > 1) {
|
|
113
|
-
lines.push(`primaryKey: [${def.primaryKey.map(quote).join(", ")}]`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const multiColumnUniqueConstraints = def.uniqueConstraints.filter(
|
|
117
|
-
(uc) => uc.columns.length > 1,
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
if (multiColumnUniqueConstraints.length > 0) {
|
|
121
|
-
lines.push(renderUniqueConstraints(multiColumnUniqueConstraints));
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const multiColumnForeignKeys = def.foreignKeys.filter(
|
|
125
|
-
(fk) => fk.columnsFrom.length > 1,
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
if (multiColumnForeignKeys.length > 0) {
|
|
129
|
-
lines.push(renderForeignKeys(multiColumnForeignKeys));
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return await format(`
|
|
133
|
-
import { ${imports.join(", ")} } from "@casekit/orm";
|
|
134
|
-
|
|
135
|
-
export const ${camelCase(def.table)} = {
|
|
136
|
-
table: "${def.table}",
|
|
137
|
-
${lines.join(",\n")}
|
|
138
|
-
} as const satisfies ModelDefinition;
|
|
139
|
-
|
|
140
|
-
export type ${upperFirst(camelCase(def.table))} = ModelType<typeof ${camelCase(def.table)}>;`);
|
|
141
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { camelCase } from "lodash-es";
|
|
2
|
-
|
|
3
|
-
import { format } from "../util/format";
|
|
4
|
-
|
|
5
|
-
export const renderModelsIndex = async (tableNames: string[]) => {
|
|
6
|
-
const names = tableNames.map((t) => camelCase(t));
|
|
7
|
-
|
|
8
|
-
const imports = names.map(
|
|
9
|
-
(name) => `import { ${name} } from "./models/${name}.model";`,
|
|
10
|
-
);
|
|
11
|
-
const exports = names.map((name) => `${name},`);
|
|
12
|
-
|
|
13
|
-
return await format(
|
|
14
|
-
`
|
|
15
|
-
${imports.join("\n")}
|
|
16
|
-
|
|
17
|
-
export const models = {
|
|
18
|
-
${exports.join("\n")}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type Models = typeof models;
|
|
22
|
-
`,
|
|
23
|
-
);
|
|
24
|
-
};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { camelCase, upperFirst } from "lodash-es";
|
|
2
|
-
import pluralize from "pluralize";
|
|
3
|
-
|
|
4
|
-
import { ForeignKey } from "../types/ForeignKey";
|
|
5
|
-
import { format } from "../util/format";
|
|
6
|
-
import { unquote } from "../util/unquote";
|
|
7
|
-
|
|
8
|
-
type Definition = {
|
|
9
|
-
table: string;
|
|
10
|
-
foreignKeys: Record<string, ForeignKey[]>;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const guessManyToOneRelationName = (fk: ForeignKey) => {
|
|
14
|
-
if (fk.columnsFrom.length > 1) {
|
|
15
|
-
return camelCase(fk.columnsFrom.join("_"));
|
|
16
|
-
} else {
|
|
17
|
-
return camelCase(fk.columnsFrom[0]).replace(/Id$/, "");
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const guessOneToManyRelationName = (fk: ForeignKey) => {
|
|
22
|
-
if (fk.columnsFrom.length > 1) {
|
|
23
|
-
return camelCase(fk.columnsFrom.join("_") + "_" + fk.tableFrom);
|
|
24
|
-
} else {
|
|
25
|
-
const foreignName = camelCase(fk.columnsFrom[0]).replace(/Id$/, "");
|
|
26
|
-
if (foreignName === camelCase(fk.tableTo)) {
|
|
27
|
-
return camelCase(pluralize(fk.tableFrom));
|
|
28
|
-
} else {
|
|
29
|
-
return camelCase(
|
|
30
|
-
upperFirst(camelCase(fk.columnsFrom[0]).replace(/Id$/, "")) +
|
|
31
|
-
" " +
|
|
32
|
-
pluralize(fk.tableFrom),
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export const renderRelations = async (def: Definition) => {
|
|
39
|
-
const manyToOne = (def.foreignKeys[def.table] ?? []).map(
|
|
40
|
-
(fk) =>
|
|
41
|
-
`${guessManyToOneRelationName(fk)}:` +
|
|
42
|
-
JSON.stringify({
|
|
43
|
-
model: camelCase(fk.tableTo),
|
|
44
|
-
type: "N:1",
|
|
45
|
-
foreignKey:
|
|
46
|
-
fk.columnsFrom.length === 1
|
|
47
|
-
? camelCase(fk.columnsFrom[0])
|
|
48
|
-
: fk.columnsFrom.map(camelCase),
|
|
49
|
-
}),
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
const oneToMany = Object.entries(def.foreignKeys).flatMap(([table, fks]) =>
|
|
53
|
-
fks
|
|
54
|
-
.filter((fk) => unquote(fk.tableTo) === unquote(def.table))
|
|
55
|
-
.map(
|
|
56
|
-
(fk) =>
|
|
57
|
-
`${guessOneToManyRelationName(fk)}:` +
|
|
58
|
-
JSON.stringify({
|
|
59
|
-
model: camelCase(table),
|
|
60
|
-
type: "1:N",
|
|
61
|
-
foreignKey:
|
|
62
|
-
fk.columnsFrom.length === 1
|
|
63
|
-
? camelCase(fk.columnsFrom[0])
|
|
64
|
-
: fk.columnsFrom.map(camelCase),
|
|
65
|
-
}),
|
|
66
|
-
),
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
return await format(`
|
|
70
|
-
import { RelationsDefinition } from "@casekit/orm";
|
|
71
|
-
import { type Models } from "../models";
|
|
72
|
-
|
|
73
|
-
export const ${camelCase(def.table)} = {
|
|
74
|
-
${[...manyToOne, ...oneToMany].map((rel) => rel).join(",\n")}
|
|
75
|
-
} as const satisfies RelationsDefinition<Models, "${camelCase(def.table)}">;
|
|
76
|
-
`);
|
|
77
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { camelCase } from "lodash-es";
|
|
2
|
-
|
|
3
|
-
import { format } from "../util/format";
|
|
4
|
-
|
|
5
|
-
export const renderRelationsIndex = async (tableNames: string[]) => {
|
|
6
|
-
const names = tableNames.map((t) => camelCase(t));
|
|
7
|
-
|
|
8
|
-
const imports = names.map(
|
|
9
|
-
(name) => `import { ${name} } from "./models/${name}.relations";`,
|
|
10
|
-
);
|
|
11
|
-
const exports = names.map((name) => `${name},`);
|
|
12
|
-
|
|
13
|
-
return await format(
|
|
14
|
-
`
|
|
15
|
-
${imports.join("\n")}
|
|
16
|
-
|
|
17
|
-
export const relations = {
|
|
18
|
-
${exports.join("\n")}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type Relations = typeof relations;
|
|
22
|
-
`,
|
|
23
|
-
);
|
|
24
|
-
};
|
package/src/pull/util/format.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import prettier from "prettier";
|
|
2
|
-
|
|
3
|
-
export const format = async (source: string) => {
|
|
4
|
-
try {
|
|
5
|
-
return await prettier.format(source, {
|
|
6
|
-
parser: "typescript",
|
|
7
|
-
printWidth: 80,
|
|
8
|
-
tabWidth: 4,
|
|
9
|
-
trailingComma: "all",
|
|
10
|
-
singleQuote: false,
|
|
11
|
-
semi: true,
|
|
12
|
-
});
|
|
13
|
-
} catch (e) {
|
|
14
|
-
console.error(e);
|
|
15
|
-
return source;
|
|
16
|
-
}
|
|
17
|
-
};
|
package/src/pull/util/quote.ts
DELETED
package/src/pull/util/unquote.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { ModelName } from "../../schema/types/helpers/ModelName";
|
|
2
|
-
import { LooseModelDefinitions } from "../../schema/types/loose/LooseModelDefinitions";
|
|
3
|
-
import { LooseRelationsDefinitions } from "../../schema/types/loose/LooseRelationsDefinitions";
|
|
4
|
-
import { FindManyParams } from "../find/types/FindManyParams";
|
|
5
|
-
import { FindOneParams } from "../find/types/FindOneParams";
|
|
6
|
-
|
|
7
|
-
export type IncludeClause<
|
|
8
|
-
Models extends LooseModelDefinitions,
|
|
9
|
-
Relations extends LooseRelationsDefinitions<Models>,
|
|
10
|
-
M extends ModelName<Models>,
|
|
11
|
-
> = {
|
|
12
|
-
[R in Extract<keyof Relations[M], string>]?: Relations[M][R] extends {
|
|
13
|
-
model: ModelName<Models>;
|
|
14
|
-
}
|
|
15
|
-
? Relations[M][R] extends { type: "N:1" }
|
|
16
|
-
? Omit<
|
|
17
|
-
FindOneParams<Models, Relations, Relations[M][R]["model"]>,
|
|
18
|
-
// for can only be applied to the top level query
|
|
19
|
-
"for"
|
|
20
|
-
>
|
|
21
|
-
: Relations[M][R] extends { type: "1:N" }
|
|
22
|
-
? Omit<
|
|
23
|
-
FindManyParams<Models, Relations, Relations[M][R]["model"]>,
|
|
24
|
-
// for can only be applied to the top level query
|
|
25
|
-
"for"
|
|
26
|
-
>
|
|
27
|
-
: Relations[M][R] extends { type: "N:N" }
|
|
28
|
-
? Omit<
|
|
29
|
-
FindManyParams<
|
|
30
|
-
Models,
|
|
31
|
-
Relations,
|
|
32
|
-
Relations[M][R]["model"]
|
|
33
|
-
>,
|
|
34
|
-
// for can only be applied to the top level query
|
|
35
|
-
"for"
|
|
36
|
-
>
|
|
37
|
-
: never
|
|
38
|
-
: never;
|
|
39
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ColumnName } from "../../schema/types/helpers/ColumnName";
|
|
2
|
-
import { LooseModelDefinition } from "../../schema/types/loose/LooseModelDefinition";
|
|
3
|
-
import { NonEmptyArray } from "../../types/util/NonEmptyArray";
|
|
4
|
-
|
|
5
|
-
export type ReturningClause<Model extends LooseModelDefinition> = NonEmptyArray<
|
|
6
|
-
ColumnName<Model>
|
|
7
|
-
>;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ColumnName } from "../../schema/types/helpers/ColumnName";
|
|
2
|
-
import { LooseModelDefinition } from "../../schema/types/loose/LooseModelDefinition";
|
|
3
|
-
import { NonEmptyArray } from "../../types/util/NonEmptyArray";
|
|
4
|
-
|
|
5
|
-
export type SelectClause<Model extends LooseModelDefinition> = NonEmptyArray<
|
|
6
|
-
ColumnName<Model>
|
|
7
|
-
>;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ColumnName } from "../../schema/types/helpers/ColumnName";
|
|
2
|
-
import { ModelName } from "../../schema/types/helpers/ModelName";
|
|
3
|
-
import { LooseModelDefinitions } from "../../schema/types/loose/LooseModelDefinitions";
|
|
4
|
-
import { $and, $not, $or } from "../clauses/where/operators";
|
|
5
|
-
import { WhereClauseValue } from "./where/types/WhereClauseValue";
|
|
6
|
-
|
|
7
|
-
export type WhereClause<
|
|
8
|
-
Models extends LooseModelDefinitions,
|
|
9
|
-
M extends ModelName<Models>,
|
|
10
|
-
> = {
|
|
11
|
-
[C in ColumnName<Models[M]>]?: WhereClauseValue<Models, M, C>;
|
|
12
|
-
} & {
|
|
13
|
-
[$and]?: WhereClause<Models, M>[];
|
|
14
|
-
[$or]?: WhereClause<Models, M>[];
|
|
15
|
-
[$not]?: WhereClause<Models, M>;
|
|
16
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { ColumnName } from "../../../schema/types/helpers/ColumnName";
|
|
2
|
-
import { HasDefault } from "../../../schema/types/helpers/HasDefault";
|
|
3
|
-
import { IsNullable } from "../../../schema/types/helpers/IsNullable";
|
|
4
|
-
import { IsProvided } from "../../../schema/types/helpers/IsProvided";
|
|
5
|
-
import { IsSerial } from "../../../schema/types/helpers/IsSerial";
|
|
6
|
-
import { LooseModelDefinition } from "../../../schema/types/loose/LooseModelDefinition";
|
|
7
|
-
|
|
8
|
-
export type OptionalColumn<Model extends LooseModelDefinition> = {
|
|
9
|
-
[C in ColumnName<Model>]: IsNullable<Model, C> extends true
|
|
10
|
-
? C
|
|
11
|
-
: IsSerial<Model, C> extends true
|
|
12
|
-
? C
|
|
13
|
-
: HasDefault<Model, C> extends true
|
|
14
|
-
? C
|
|
15
|
-
: IsProvided<Model, C> extends true
|
|
16
|
-
? C
|
|
17
|
-
: never;
|
|
18
|
-
}[ColumnName<Model>];
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ColumnType } from "../../../schema/types/helpers/ColumnType";
|
|
2
|
-
import { ModelName } from "../../../schema/types/helpers/ModelName";
|
|
3
|
-
import { LooseModelDefinitions } from "../../../schema/types/loose/LooseModelDefinitions";
|
|
4
|
-
import { OptionalColumn } from "./OptionalColumn";
|
|
5
|
-
|
|
6
|
-
export type OptionalParams<
|
|
7
|
-
Models extends LooseModelDefinitions,
|
|
8
|
-
M extends ModelName<Models>,
|
|
9
|
-
> =
|
|
10
|
-
OptionalColumn<Models[M]> extends never
|
|
11
|
-
? unknown
|
|
12
|
-
: {
|
|
13
|
-
[C in OptionalColumn<Models[M]>]?: ColumnType<Models, M, C>;
|
|
14
|
-
};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ColumnName } from "../../../schema/types/helpers/ColumnName";
|
|
2
|
-
import { LooseModelDefinition } from "../../../schema/types/loose/LooseModelDefinition";
|
|
3
|
-
import { OptionalColumn } from "./OptionalColumn";
|
|
4
|
-
|
|
5
|
-
export type RequiredColumn<Model extends LooseModelDefinition> = Exclude<
|
|
6
|
-
ColumnName<Model>,
|
|
7
|
-
OptionalColumn<Model>
|
|
8
|
-
>;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { ColumnType } from "../../../schema/types/helpers/ColumnType";
|
|
2
|
-
import { ModelName } from "../../../schema/types/helpers/ModelName";
|
|
3
|
-
import { LooseModelDefinitions } from "../../../schema/types/loose/LooseModelDefinitions";
|
|
4
|
-
import { RequiredColumn } from "./RequiredColumn";
|
|
5
|
-
|
|
6
|
-
export type RequiredParams<
|
|
7
|
-
Models extends LooseModelDefinitions,
|
|
8
|
-
M extends ModelName<Models>,
|
|
9
|
-
> =
|
|
10
|
-
RequiredColumn<Models[M]> extends never
|
|
11
|
-
? unknown
|
|
12
|
-
: {
|
|
13
|
-
[C in RequiredColumn<Models[M]>]: ColumnType<Models, M, C>;
|
|
14
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { ModelName } from "../../../schema/types/helpers/ModelName";
|
|
2
|
-
import { LooseModelDefinitions } from "../../../schema/types/loose/LooseModelDefinitions";
|
|
3
|
-
import { LooseRelationDefinition } from "../../../schema/types/loose/LooseRelationDefinition";
|
|
4
|
-
import { LooseRelationsDefinitions } from "../../../schema/types/loose/LooseRelationsDefinitions";
|
|
5
|
-
|
|
6
|
-
export type IncludedRelationModel<
|
|
7
|
-
Models extends LooseModelDefinitions,
|
|
8
|
-
Relations extends LooseRelationsDefinitions<Models>,
|
|
9
|
-
M extends ModelName<Models>,
|
|
10
|
-
R extends keyof Relations[M],
|
|
11
|
-
> = Relations[M][R] extends LooseRelationDefinition
|
|
12
|
-
? Extract<Relations[M][R]["model"], ModelName<Models>>
|
|
13
|
-
: never;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ModelName } from "../../../schema/types/helpers/ModelName";
|
|
2
|
-
import { LooseModelDefinitions } from "../../../schema/types/loose/LooseModelDefinitions";
|
|
3
|
-
import { LooseRelationsDefinitions } from "../../../schema/types/loose/LooseRelationsDefinitions";
|
|
4
|
-
import { FindManyParams } from "../../find/types/FindManyParams";
|
|
5
|
-
|
|
6
|
-
export type IncludedRelationName<
|
|
7
|
-
Models extends LooseModelDefinitions,
|
|
8
|
-
Relations extends LooseRelationsDefinitions<Models>,
|
|
9
|
-
M extends ModelName<Models>,
|
|
10
|
-
Q extends FindManyParams<Models, Relations, M>,
|
|
11
|
-
> = keyof Relations[M] & keyof Q["include"];
|