@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
package/src/orm.ts
DELETED
|
@@ -1,370 +0,0 @@
|
|
|
1
|
-
import { QueryResultRow } from "pg";
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
import { Connection } from "./Connection";
|
|
5
|
-
import { OrmError } from "./errors";
|
|
6
|
-
import { count } from "./queries/count";
|
|
7
|
-
import { BaseCountParams } from "./queries/count/types/BaseCountParams";
|
|
8
|
-
import { CountParams } from "./queries/count/types/CountParams";
|
|
9
|
-
import { createResultSchema } from "./queries/create/createResultSchema";
|
|
10
|
-
import { BaseCreateManyParams } from "./queries/create/types/BaseCreateManyParams";
|
|
11
|
-
import { BaseCreateOneParams } from "./queries/create/types/BaseCreateOneParams";
|
|
12
|
-
import { CreateManyParams } from "./queries/create/types/CreateManyParams";
|
|
13
|
-
import { CreateManyResult } from "./queries/create/types/CreateManyResult";
|
|
14
|
-
import { CreateOneParams } from "./queries/create/types/CreateOneParams";
|
|
15
|
-
import { CreateOneResult } from "./queries/create/types/CreateOneResult";
|
|
16
|
-
import { createMany } from "./queries/createMany";
|
|
17
|
-
import { createOne } from "./queries/createOne";
|
|
18
|
-
import { deleteResultSchema } from "./queries/delete/deleteResultSchema";
|
|
19
|
-
import { BaseDeleteParams } from "./queries/delete/types/BaseDeleteParams";
|
|
20
|
-
import { DeleteManyResult } from "./queries/delete/types/DeleteManyResult";
|
|
21
|
-
import { DeleteOneResult } from "./queries/delete/types/DeleteOneResult";
|
|
22
|
-
import { DeleteParams } from "./queries/delete/types/DeleteParams";
|
|
23
|
-
import { deleteMany } from "./queries/deleteMany";
|
|
24
|
-
import { deleteOne } from "./queries/deleteOne";
|
|
25
|
-
import { findResultSchema } from "./queries/find/findResultSchema";
|
|
26
|
-
import { FindManyParams } from "./queries/find/types/FindManyParams";
|
|
27
|
-
import { FindManyResult } from "./queries/find/types/FindManyResult";
|
|
28
|
-
import { FindOneResult } from "./queries/find/types/FindOneResult";
|
|
29
|
-
import { findMany } from "./queries/findMany";
|
|
30
|
-
import { findOne } from "./queries/findOne";
|
|
31
|
-
import { BaseUpdateParams } from "./queries/update/types/BaseUpdateParams";
|
|
32
|
-
import { UpdateManyResult } from "./queries/update/types/UpdateManyResult";
|
|
33
|
-
import { UpdateOneResult } from "./queries/update/types/UpdateOneResult";
|
|
34
|
-
import { UpdateParams } from "./queries/update/types/UpdateParams";
|
|
35
|
-
import { updateResultSchema } from "./queries/update/updateResultSchema";
|
|
36
|
-
import { updateMany } from "./queries/updateMany";
|
|
37
|
-
import { updateOne } from "./queries/updateOne";
|
|
38
|
-
import { populateConfiguration } from "./schema/populate/populateConfiguration";
|
|
39
|
-
import { BaseConfiguration } from "./schema/types/base/BaseConfiguration";
|
|
40
|
-
import { BaseModel } from "./schema/types/base/BaseModel";
|
|
41
|
-
import { ModelName } from "./schema/types/helpers/ModelName";
|
|
42
|
-
import { LooseModelDefinitions } from "./schema/types/loose/LooseModelDefinitions";
|
|
43
|
-
import { LooseRelationsDefinitions } from "./schema/types/loose/LooseRelationsDefinitions";
|
|
44
|
-
import { validateConfiguration } from "./schema/validate/validateConfiguration";
|
|
45
|
-
import { sql } from "./sql";
|
|
46
|
-
import { Configuration } from "./types/Configuration";
|
|
47
|
-
import { DisallowExtraKeys } from "./types/util/DisallowExtraKeys";
|
|
48
|
-
|
|
49
|
-
export class Orm<
|
|
50
|
-
Models extends LooseModelDefinitions,
|
|
51
|
-
Relations extends LooseRelationsDefinitions<Models>,
|
|
52
|
-
> {
|
|
53
|
-
public config: BaseConfiguration;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* As a nicety, we expose the models directly on the Orm instance with literal keys
|
|
57
|
-
* for the models. The deeper values are not literally typed however.
|
|
58
|
-
*/
|
|
59
|
-
public get models(): {
|
|
60
|
-
[M in ModelName<Models>]: BaseModel;
|
|
61
|
-
} {
|
|
62
|
-
return this.config.models as {
|
|
63
|
-
[M in ModelName<Models>]: BaseModel;
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public get connection(): Connection {
|
|
68
|
-
return this.config.connection;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
constructor(config: BaseConfiguration) {
|
|
72
|
-
this.config = config;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public async transact<T>(
|
|
76
|
-
cb: (db: Orm<Models, Relations>) => Promise<T>,
|
|
77
|
-
opts = { rollback: false },
|
|
78
|
-
): Promise<T> {
|
|
79
|
-
return await this.connection.transact(async (conn) => {
|
|
80
|
-
return await cb(
|
|
81
|
-
new Orm<Models, Relations>({
|
|
82
|
-
...this.config,
|
|
83
|
-
connection: conn,
|
|
84
|
-
}),
|
|
85
|
-
);
|
|
86
|
-
}, opts);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public async findMany<
|
|
90
|
-
M extends ModelName<Models>,
|
|
91
|
-
Q extends FindManyParams<Models, Relations, M>,
|
|
92
|
-
>(
|
|
93
|
-
m: M,
|
|
94
|
-
query: DisallowExtraKeys<FindManyParams<Models, Relations, M>, Q>,
|
|
95
|
-
): Promise<FindManyResult<Models, Relations, M, Q>> {
|
|
96
|
-
const results = await findMany(this.connection, this.config, m, query);
|
|
97
|
-
const parser = z.array(findResultSchema(this.config, m, query));
|
|
98
|
-
return parser.parse(results) as FindManyResult<Models, Relations, M, Q>;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public async findOne<
|
|
102
|
-
M extends ModelName<Models>,
|
|
103
|
-
Q extends FindManyParams<Models, Relations, M>,
|
|
104
|
-
>(
|
|
105
|
-
m: M,
|
|
106
|
-
query: DisallowExtraKeys<FindManyParams<Models, Relations, M>, Q>,
|
|
107
|
-
): Promise<FindOneResult<Models, Relations, M, Q>> {
|
|
108
|
-
const result = await findOne(this.connection, this.config, m, query);
|
|
109
|
-
const parser = findResultSchema(this.config, m, query);
|
|
110
|
-
return parser.parse(result) as FindOneResult<Models, Relations, M, Q>;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public async createOne<
|
|
114
|
-
M extends ModelName<Models>,
|
|
115
|
-
P extends CreateOneParams<Models, M>,
|
|
116
|
-
>(
|
|
117
|
-
m: M,
|
|
118
|
-
params: DisallowExtraKeys<CreateOneParams<Models, M>, P>,
|
|
119
|
-
): Promise<CreateOneResult<Models, M, P>> {
|
|
120
|
-
const result = await createOne(
|
|
121
|
-
this.connection,
|
|
122
|
-
this.config,
|
|
123
|
-
m,
|
|
124
|
-
params as BaseCreateOneParams,
|
|
125
|
-
);
|
|
126
|
-
const parser = createResultSchema(
|
|
127
|
-
this.config,
|
|
128
|
-
m,
|
|
129
|
-
params as BaseCreateOneParams,
|
|
130
|
-
);
|
|
131
|
-
return parser.parse(result) as CreateOneResult<Models, M, P>;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
public async createMany<
|
|
135
|
-
M extends ModelName<Models>,
|
|
136
|
-
P extends CreateManyParams<Models, M>,
|
|
137
|
-
>(
|
|
138
|
-
m: M,
|
|
139
|
-
params: DisallowExtraKeys<CreateManyParams<Models, M>, P>,
|
|
140
|
-
): Promise<CreateManyResult<Models, M, P>> {
|
|
141
|
-
const result = await createMany(
|
|
142
|
-
this.connection,
|
|
143
|
-
this.config,
|
|
144
|
-
m,
|
|
145
|
-
params as BaseCreateManyParams,
|
|
146
|
-
);
|
|
147
|
-
if (typeof result === "number")
|
|
148
|
-
return result as CreateManyResult<Models, M, P>;
|
|
149
|
-
|
|
150
|
-
const parser = z.array(
|
|
151
|
-
createResultSchema(this.config, m, params as BaseCreateManyParams),
|
|
152
|
-
);
|
|
153
|
-
return parser.parse(result) as CreateManyResult<Models, M, P>;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
public async updateOne<
|
|
157
|
-
M extends ModelName<Models>,
|
|
158
|
-
P extends UpdateParams<Models, M>,
|
|
159
|
-
>(
|
|
160
|
-
m: M,
|
|
161
|
-
params: DisallowExtraKeys<UpdateParams<Models, M>, P>,
|
|
162
|
-
): Promise<UpdateOneResult<Models, M, P>> {
|
|
163
|
-
const result = await updateOne(
|
|
164
|
-
this.connection,
|
|
165
|
-
this.config,
|
|
166
|
-
m,
|
|
167
|
-
params as BaseUpdateParams,
|
|
168
|
-
);
|
|
169
|
-
const parser = updateResultSchema(
|
|
170
|
-
this.config,
|
|
171
|
-
m,
|
|
172
|
-
params as BaseUpdateParams,
|
|
173
|
-
);
|
|
174
|
-
return parser.parse(result) as UpdateOneResult<Models, M, P>;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
public async updateMany<
|
|
178
|
-
M extends ModelName<Models>,
|
|
179
|
-
P extends UpdateParams<Models, M>,
|
|
180
|
-
>(
|
|
181
|
-
m: M,
|
|
182
|
-
params: DisallowExtraKeys<UpdateParams<Models, M>, P>,
|
|
183
|
-
): Promise<UpdateManyResult<Models, M, P>> {
|
|
184
|
-
const result = await updateMany(
|
|
185
|
-
this.connection,
|
|
186
|
-
this.config,
|
|
187
|
-
m,
|
|
188
|
-
params as BaseUpdateParams,
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
if (typeof result === "number")
|
|
192
|
-
return result as UpdateManyResult<Models, M, P>;
|
|
193
|
-
|
|
194
|
-
const parser = z.array(
|
|
195
|
-
updateResultSchema(this.config, m, params as BaseUpdateParams),
|
|
196
|
-
);
|
|
197
|
-
return parser.parse(result) as UpdateManyResult<Models, M, P>;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
public async deleteOne<
|
|
201
|
-
M extends ModelName<Models>,
|
|
202
|
-
P extends DeleteParams<Models, M>,
|
|
203
|
-
>(
|
|
204
|
-
m: M,
|
|
205
|
-
params: DisallowExtraKeys<DeleteParams<Models, M>, P>,
|
|
206
|
-
): Promise<DeleteOneResult<Models, M, P>> {
|
|
207
|
-
const parser = deleteResultSchema(
|
|
208
|
-
this.config,
|
|
209
|
-
m,
|
|
210
|
-
params as BaseDeleteParams,
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
if (this.config.middleware.delete?.deleteOne) {
|
|
214
|
-
const result = await this.config.middleware.delete.deleteOne(
|
|
215
|
-
params,
|
|
216
|
-
{
|
|
217
|
-
model: m,
|
|
218
|
-
config: this.config,
|
|
219
|
-
deleteOne: (params) => {
|
|
220
|
-
return deleteOne(
|
|
221
|
-
this.connection,
|
|
222
|
-
this.config,
|
|
223
|
-
m,
|
|
224
|
-
params,
|
|
225
|
-
// TODO figure out a better way to type this
|
|
226
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
227
|
-
) as any;
|
|
228
|
-
},
|
|
229
|
-
updateOne: (params) => {
|
|
230
|
-
return updateOne(
|
|
231
|
-
this.connection,
|
|
232
|
-
this.config,
|
|
233
|
-
m,
|
|
234
|
-
params,
|
|
235
|
-
// TODO figure out a better way to type this
|
|
236
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
237
|
-
) as any;
|
|
238
|
-
},
|
|
239
|
-
},
|
|
240
|
-
);
|
|
241
|
-
|
|
242
|
-
return parser.parse(result) as DeleteOneResult<Models, M, P>;
|
|
243
|
-
} else {
|
|
244
|
-
const result = await deleteOne(
|
|
245
|
-
this.connection,
|
|
246
|
-
this.config,
|
|
247
|
-
m,
|
|
248
|
-
params as BaseDeleteParams,
|
|
249
|
-
);
|
|
250
|
-
|
|
251
|
-
return parser.parse(result) as DeleteOneResult<Models, M, P>;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
public async deleteMany<
|
|
256
|
-
M extends ModelName<Models>,
|
|
257
|
-
P extends DeleteParams<Models, M>,
|
|
258
|
-
>(
|
|
259
|
-
m: M,
|
|
260
|
-
params: DisallowExtraKeys<DeleteParams<Models, M>, P>,
|
|
261
|
-
): Promise<DeleteManyResult<Models, M, P>> {
|
|
262
|
-
const parser = z.array(
|
|
263
|
-
deleteResultSchema(this.config, m, params as BaseDeleteParams),
|
|
264
|
-
);
|
|
265
|
-
|
|
266
|
-
if (this.config.middleware.delete?.deleteMany) {
|
|
267
|
-
const result = await this.config.middleware.delete.deleteMany(
|
|
268
|
-
params,
|
|
269
|
-
{
|
|
270
|
-
model: m,
|
|
271
|
-
config: this.config,
|
|
272
|
-
deleteMany: (params) => {
|
|
273
|
-
return deleteMany(
|
|
274
|
-
this.connection,
|
|
275
|
-
this.config,
|
|
276
|
-
m,
|
|
277
|
-
params,
|
|
278
|
-
// TODO figure out a better way to type this
|
|
279
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
280
|
-
) as any;
|
|
281
|
-
},
|
|
282
|
-
updateMany: (params) => {
|
|
283
|
-
return updateMany(
|
|
284
|
-
this.connection,
|
|
285
|
-
this.config,
|
|
286
|
-
m,
|
|
287
|
-
params,
|
|
288
|
-
// TODO figure out a better way to type this
|
|
289
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
290
|
-
) as any;
|
|
291
|
-
},
|
|
292
|
-
},
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
return (
|
|
296
|
-
typeof result === "number" ? result : parser.parse(result)
|
|
297
|
-
) as DeleteManyResult<Models, M, P>;
|
|
298
|
-
} else {
|
|
299
|
-
const result = await deleteMany(
|
|
300
|
-
this.connection,
|
|
301
|
-
this.config,
|
|
302
|
-
m,
|
|
303
|
-
params as BaseDeleteParams,
|
|
304
|
-
);
|
|
305
|
-
|
|
306
|
-
return (
|
|
307
|
-
typeof result === "number" ? result : parser.parse(result)
|
|
308
|
-
) as DeleteManyResult<Models, M, P>;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
public async count<
|
|
313
|
-
M extends ModelName<Models>,
|
|
314
|
-
P extends CountParams<Models, Relations, M>,
|
|
315
|
-
>(
|
|
316
|
-
m: M,
|
|
317
|
-
params: DisallowExtraKeys<CountParams<Models, Relations, M>, P>,
|
|
318
|
-
): Promise<number> {
|
|
319
|
-
const result = await count(
|
|
320
|
-
this.connection,
|
|
321
|
-
this.config,
|
|
322
|
-
m,
|
|
323
|
-
params as BaseCountParams,
|
|
324
|
-
);
|
|
325
|
-
return z.coerce.number().parse(result);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
public async query<T extends QueryResultRow>(
|
|
329
|
-
fragments: TemplateStringsArray,
|
|
330
|
-
...variables: readonly unknown[]
|
|
331
|
-
): Promise<T[]> {
|
|
332
|
-
const query = sql(fragments, ...variables);
|
|
333
|
-
if (process.env.ORM_VERBOSE_LOGGING) {
|
|
334
|
-
console.log(query.text);
|
|
335
|
-
console.log(query.values);
|
|
336
|
-
}
|
|
337
|
-
const result = await this.connection.query<T>(query);
|
|
338
|
-
return result.rows;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
public async queryOne<T extends QueryResultRow>(
|
|
342
|
-
fragments: TemplateStringsArray,
|
|
343
|
-
...variables: readonly unknown[]
|
|
344
|
-
): Promise<T> {
|
|
345
|
-
const query = sql(fragments, ...variables);
|
|
346
|
-
if (process.env.ORM_VERBOSE_LOGGING) {
|
|
347
|
-
console.log(query.text);
|
|
348
|
-
console.log(query.values);
|
|
349
|
-
}
|
|
350
|
-
const result = await this.connection.query<T>(query);
|
|
351
|
-
if (result.rowCount === 0 || result.rowCount === null)
|
|
352
|
-
throw new OrmError("No rows returned from query");
|
|
353
|
-
|
|
354
|
-
if (result.rowCount > 1)
|
|
355
|
-
throw new OrmError("More than one row returned from query");
|
|
356
|
-
|
|
357
|
-
return result.rows[0];
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
export const orm = <
|
|
362
|
-
Models extends LooseModelDefinitions,
|
|
363
|
-
Relations extends LooseRelationsDefinitions<Models>,
|
|
364
|
-
>(
|
|
365
|
-
config: Configuration<Models, Relations>,
|
|
366
|
-
): Orm<Models, Relations> => {
|
|
367
|
-
const populatedSchema = populateConfiguration(config);
|
|
368
|
-
validateConfiguration(populatedSchema);
|
|
369
|
-
return new Orm<Models, Relations>(populatedSchema);
|
|
370
|
-
};
|
package/src/pull/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { pull } from "./pull";
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { groupBy } from "lodash-es";
|
|
2
|
-
import pg from "pg";
|
|
3
|
-
|
|
4
|
-
import { ForeignKey } from "../types/ForeignKey";
|
|
5
|
-
|
|
6
|
-
export const getForeignKeys = async (
|
|
7
|
-
client: pg.Client,
|
|
8
|
-
schema: string,
|
|
9
|
-
): Promise<Record<string, ForeignKey[]>> => {
|
|
10
|
-
const result = await client.query<ForeignKey>(
|
|
11
|
-
`
|
|
12
|
-
SELECT
|
|
13
|
-
conname "constraintName",
|
|
14
|
-
table_from as "tableFrom",
|
|
15
|
-
array_agg(columns_from::text) AS "columnsFrom",
|
|
16
|
-
table_to AS "tableTo",
|
|
17
|
-
array_agg(columns_to::text) AS "columnsTo"
|
|
18
|
-
FROM (
|
|
19
|
-
SELECT
|
|
20
|
-
conname,
|
|
21
|
-
conrelid::regclass AS table_from,
|
|
22
|
-
a.attname AS columns_from,
|
|
23
|
-
confrelid::regclass AS table_to,
|
|
24
|
-
af.attname AS columns_to
|
|
25
|
-
FROM
|
|
26
|
-
pg_attribute AS af,
|
|
27
|
-
pg_attribute AS a,
|
|
28
|
-
pg_class c,
|
|
29
|
-
pg_namespace n,
|
|
30
|
-
(
|
|
31
|
-
SELECT
|
|
32
|
-
conname,
|
|
33
|
-
conrelid,
|
|
34
|
-
confrelid,
|
|
35
|
-
conkey[i] AS conkey,
|
|
36
|
-
confkey[i] AS confkey
|
|
37
|
-
FROM (
|
|
38
|
-
SELECT
|
|
39
|
-
conname,
|
|
40
|
-
conrelid,
|
|
41
|
-
confrelid,
|
|
42
|
-
conkey,
|
|
43
|
-
confkey,
|
|
44
|
-
generate_series(1, array_upper(conkey, 1)) AS i
|
|
45
|
-
FROM
|
|
46
|
-
pg_constraint
|
|
47
|
-
WHERE
|
|
48
|
-
contype = 'f') AS ss) AS ss2
|
|
49
|
-
WHERE
|
|
50
|
-
af.attnum = confkey
|
|
51
|
-
AND af.attrelid = confrelid
|
|
52
|
-
AND a.attnum = conkey
|
|
53
|
-
AND a.attrelid = conrelid
|
|
54
|
-
AND af.attrelid = c.oid
|
|
55
|
-
AND c.relnamespace = n.oid
|
|
56
|
-
AND n.nspname = $1) AS ss3
|
|
57
|
-
GROUP BY
|
|
58
|
-
conname,
|
|
59
|
-
table_to,
|
|
60
|
-
table_from;`,
|
|
61
|
-
[schema],
|
|
62
|
-
);
|
|
63
|
-
return groupBy(result.rows, "tableFrom");
|
|
64
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { groupBy, mapValues, sortBy } from "lodash-es";
|
|
2
|
-
import pg from "pg";
|
|
3
|
-
|
|
4
|
-
import { sql } from "../../sql";
|
|
5
|
-
import { PrimaryKey } from "../types/PrimaryKey";
|
|
6
|
-
|
|
7
|
-
export const getPrimaryKeys = async (
|
|
8
|
-
client: pg.Client,
|
|
9
|
-
schema: string,
|
|
10
|
-
): Promise<Record<string, string[]>> => {
|
|
11
|
-
const results = await client.query<PrimaryKey>(
|
|
12
|
-
sql`select
|
|
13
|
-
cc.table_name as table,
|
|
14
|
-
cc.constraint_name as name,
|
|
15
|
-
cc.column_name as column,
|
|
16
|
-
cc.ordinal_position as ordinal
|
|
17
|
-
from information_schema.table_constraints c
|
|
18
|
-
join information_schema.key_column_usage cc on c.constraint_name = cc.constraint_name and c.constraint_schema = cc.constraint_schema and c.table_name = cc.table_name
|
|
19
|
-
where c.constraint_type = 'PRIMARY KEY'
|
|
20
|
-
and c.constraint_schema = ${schema}`,
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
return mapValues(groupBy(results.rows, "table"), (columns) =>
|
|
24
|
-
sortBy(columns, "ordinal").map((c) => c.column),
|
|
25
|
-
);
|
|
26
|
-
};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { groupBy, mapValues, sortBy } from "lodash-es";
|
|
2
|
-
import pg from "pg";
|
|
3
|
-
|
|
4
|
-
import { sql } from "../../sql";
|
|
5
|
-
import { ColumnMeta } from "../types/ColumnMeta";
|
|
6
|
-
|
|
7
|
-
export const getTables = async (client: pg.Client, schema: string) => {
|
|
8
|
-
const results = await client.query<ColumnMeta>(
|
|
9
|
-
sql`WITH array_columns AS (
|
|
10
|
-
SELECT
|
|
11
|
-
ns.nspname AS schema,
|
|
12
|
-
(c.oid::regclass)::text AS table,
|
|
13
|
-
attname AS column,
|
|
14
|
-
attndims AS cardinality
|
|
15
|
-
FROM
|
|
16
|
-
pg_class c
|
|
17
|
-
JOIN pg_attribute a ON c.oid = attrelid
|
|
18
|
-
JOIN pg_type t ON t.oid = atttypid
|
|
19
|
-
JOIN pg_catalog.pg_namespace AS ns ON c.relnamespace = ns.oid
|
|
20
|
-
WHERE
|
|
21
|
-
a.attnum > 0
|
|
22
|
-
)
|
|
23
|
-
SELECT
|
|
24
|
-
c.table_name AS table,
|
|
25
|
-
c.column_name AS name,
|
|
26
|
-
c.ordinal_position AS ordinal,
|
|
27
|
-
c.column_default AS default,
|
|
28
|
-
CASE WHEN c.is_nullable = 'YES' THEN
|
|
29
|
-
TRUE
|
|
30
|
-
ELSE
|
|
31
|
-
FALSE
|
|
32
|
-
END AS nullable,
|
|
33
|
-
c.data_type AS type,
|
|
34
|
-
e.data_type AS elementtype,
|
|
35
|
-
coalesce(arr.cardinality, 0) AS cardinality
|
|
36
|
-
FROM
|
|
37
|
-
information_schema.columns c
|
|
38
|
-
LEFT JOIN information_schema.element_types e ON c.table_catalog = e.object_catalog
|
|
39
|
-
AND c.table_schema = e.object_schema
|
|
40
|
-
AND e.collection_type_identifier = c.dtd_identifier
|
|
41
|
-
AND c.data_type = 'ARRAY'
|
|
42
|
-
LEFT JOIN array_columns arr ON arr.schema = c.table_schema
|
|
43
|
-
AND arr.table = c.table_name
|
|
44
|
-
AND arr.column = c.column_name
|
|
45
|
-
WHERE
|
|
46
|
-
c.table_schema = ${schema}`,
|
|
47
|
-
);
|
|
48
|
-
return mapValues(groupBy(results.rows, "table"), (columns) =>
|
|
49
|
-
sortBy(columns, "ordinal"),
|
|
50
|
-
);
|
|
51
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { groupBy } from "lodash-es";
|
|
2
|
-
import pg from "pg";
|
|
3
|
-
|
|
4
|
-
import { sql } from "../../sql";
|
|
5
|
-
import { parseCreateUniqueIndexStatement } from "../parse/parseCreateUniqueIndexStatement";
|
|
6
|
-
import { UniqueConstraint } from "../types/UniqueConstraint";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Doing this with string manipulation in this way feels pretty hacky,
|
|
10
|
-
* so it would be great if we can find a better way to do it.
|
|
11
|
-
* In particular, this is likely to break if column names have parentheses in them.
|
|
12
|
-
* Couldn't find a way to easily get the columns and conditions from the
|
|
13
|
-
* information_schema * or pg_catalog tables though.
|
|
14
|
-
*/
|
|
15
|
-
export const getUniqueConstraints = async (
|
|
16
|
-
client: pg.Client,
|
|
17
|
-
schema: string,
|
|
18
|
-
): Promise<Record<string, UniqueConstraint[]>> => {
|
|
19
|
-
const results = await client.query<{
|
|
20
|
-
table: string;
|
|
21
|
-
definition: string;
|
|
22
|
-
name: string;
|
|
23
|
-
}>(
|
|
24
|
-
sql`select i.tablename as table, i.indexdef as definition, i.indexname as name
|
|
25
|
-
from pg_indexes i
|
|
26
|
-
left join pg_constraint c on i.indexname = c.conname and c.contype in ('p', 'x')
|
|
27
|
-
where schemaname = ${schema}
|
|
28
|
-
and c.conname is null
|
|
29
|
-
and indexdef like 'CREATE UNIQUE INDEX%';
|
|
30
|
-
`,
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
const parsed = results.rows.map((result) => ({
|
|
34
|
-
...result,
|
|
35
|
-
...parseCreateUniqueIndexStatement(result.definition),
|
|
36
|
-
}));
|
|
37
|
-
|
|
38
|
-
return groupBy(parsed, "table");
|
|
39
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { parseCreateUniqueIndexStatement } from "./parseCreateUniqueIndexStatement";
|
|
4
|
-
|
|
5
|
-
describe("parseCreateUniqueIndexStatement", () => {
|
|
6
|
-
test("it extracts out the index's columns and a condition if present", () => {
|
|
7
|
-
const parsed = parseCreateUniqueIndexStatement(
|
|
8
|
-
"CREATE UNIQUE INDEX field_value_field_id_ordinal ON casekit.field_value USING btree (field_id, ordinal) WHERE (deleted_at IS NULL)",
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
expect(parsed.columns).toEqual(["field_id", "ordinal"]);
|
|
12
|
-
expect(parsed.where).toEqual("deleted_at IS NULL");
|
|
13
|
-
});
|
|
14
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export const parseCreateUniqueIndexStatement = (
|
|
2
|
-
command: string,
|
|
3
|
-
): { columns: string[]; where?: string; nullsNotDistinct: boolean } => {
|
|
4
|
-
const columnsMatch = command.match(/ON[^(]*\(([^)]*)\)/);
|
|
5
|
-
if (!columnsMatch || !columnsMatch[1])
|
|
6
|
-
throw new Error("Unable to parse unique index statement: " + command);
|
|
7
|
-
|
|
8
|
-
const columns = columnsMatch[1].split(", ");
|
|
9
|
-
|
|
10
|
-
const whereMatch = command.match(/WHERE[^(]*\(([^)]*)\)/);
|
|
11
|
-
|
|
12
|
-
const nullsNotDistinct = !!command.match(/NULLS NOT DISTINCT/);
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
columns,
|
|
16
|
-
where: whereMatch ? whereMatch[1] : undefined,
|
|
17
|
-
nullsNotDistinct,
|
|
18
|
-
};
|
|
19
|
-
};
|
package/src/pull/pull.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import { camelCase } from "lodash-es";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import pg from "pg";
|
|
5
|
-
|
|
6
|
-
import { getForeignKeys } from "./introspect/getForeignKeys";
|
|
7
|
-
import { getPrimaryKeys } from "./introspect/getPrimaryKeys";
|
|
8
|
-
import { getTables } from "./introspect/getTables";
|
|
9
|
-
import { getUniqueConstraints } from "./introspect/getUniqueConstraints";
|
|
10
|
-
import { renderModel } from "./render/renderModel";
|
|
11
|
-
import { renderModelsIndex } from "./render/renderModelsIndex";
|
|
12
|
-
import { renderRelations } from "./render/renderRelations";
|
|
13
|
-
import { renderRelationsIndex } from "./render/renderRelationsIndex";
|
|
14
|
-
import { format } from "./util/format";
|
|
15
|
-
|
|
16
|
-
export const pull = async (
|
|
17
|
-
client: pg.Client,
|
|
18
|
-
opts: { schema: string; outDir: string },
|
|
19
|
-
) => {
|
|
20
|
-
const tables = await getTables(client, opts.schema);
|
|
21
|
-
const primaryKeys = await getPrimaryKeys(client, opts.schema);
|
|
22
|
-
const uniqueConstraints = await getUniqueConstraints(client, opts.schema);
|
|
23
|
-
const foreignKeys = await getForeignKeys(client, opts.schema);
|
|
24
|
-
|
|
25
|
-
fs.mkdirSync(path.resolve(opts.outDir, "models"), { recursive: true });
|
|
26
|
-
|
|
27
|
-
for (const [table, columns] of Object.entries(tables)) {
|
|
28
|
-
fs.writeFileSync(
|
|
29
|
-
path.resolve(opts.outDir, "models", `${camelCase(table)}.model.ts`),
|
|
30
|
-
await renderModel({
|
|
31
|
-
table,
|
|
32
|
-
columns,
|
|
33
|
-
primaryKey: primaryKeys[table] ?? [],
|
|
34
|
-
uniqueConstraints: uniqueConstraints[table] ?? [],
|
|
35
|
-
foreignKeys: foreignKeys[table] ?? [],
|
|
36
|
-
}),
|
|
37
|
-
{ encoding: "utf-8" },
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
fs.writeFileSync(
|
|
42
|
-
path.resolve(opts.outDir, "models.ts"),
|
|
43
|
-
await renderModelsIndex(Object.keys(tables)),
|
|
44
|
-
{ encoding: "utf-8" },
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
for (const table of Object.keys(tables)) {
|
|
48
|
-
fs.writeFileSync(
|
|
49
|
-
path.resolve(
|
|
50
|
-
opts.outDir,
|
|
51
|
-
"models",
|
|
52
|
-
`${camelCase(table)}.relations.ts`,
|
|
53
|
-
),
|
|
54
|
-
await renderRelations({
|
|
55
|
-
table,
|
|
56
|
-
foreignKeys: foreignKeys,
|
|
57
|
-
}),
|
|
58
|
-
{ encoding: "utf-8" },
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
fs.writeFileSync(
|
|
63
|
-
path.resolve(opts.outDir, "relations.ts"),
|
|
64
|
-
await renderRelationsIndex(Object.keys(tables)),
|
|
65
|
-
{ encoding: "utf-8" },
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
if (!fs.existsSync(path.resolve(opts.outDir, "index.ts"))) {
|
|
69
|
-
fs.writeFileSync(
|
|
70
|
-
path.resolve(opts.outDir, "index.ts"),
|
|
71
|
-
await format(`
|
|
72
|
-
export { models, type Models } from "./models";
|
|
73
|
-
export { relations, type Relations } from "./relations";
|
|
74
|
-
`),
|
|
75
|
-
{ encoding: "utf-8" },
|
|
76
|
-
);
|
|
77
|
-
}
|
|
78
|
-
};
|