@casekit/orm 0.0.1-alpha.6 → 0.0.1-alpha.8
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/lib/migrate/commands/implode.js +1 -1
- package/lib/migrate/commands/implode.js.map +1 -1
- package/lib/migrate/sql/createTableSql.test.js +1 -0
- package/lib/migrate/sql/createTableSql.test.js.map +1 -1
- package/lib/orm.d.ts.map +1 -1
- package/lib/orm.js +4 -2
- package/lib/orm.js.map +1 -1
- package/lib/queries/clauses/where/buildWhereClauses.d.ts +1 -1
- package/lib/queries/clauses/where/buildWhereClauses.d.ts.map +1 -1
- package/lib/queries/clauses/where/buildWhereClauses.js.map +1 -1
- package/lib/queries/clauses/where/buildWhereClauses.test.js +1 -1
- package/lib/queries/clauses/where/buildWhereClauses.test.js.map +1 -1
- package/lib/queries/count/buildCount.d.ts.map +1 -1
- package/lib/queries/count/buildCount.js +33 -29
- package/lib/queries/count/buildCount.js.map +1 -1
- package/lib/queries/count/countToSql.d.ts.map +1 -1
- package/lib/queries/count/countToSql.js +12 -17
- package/lib/queries/count/countToSql.js.map +1 -1
- package/lib/queries/count/types/CountBuilder.d.ts +12 -4
- package/lib/queries/count/types/CountBuilder.d.ts.map +1 -1
- package/lib/queries/count.d.ts.map +1 -1
- package/lib/queries/count.js +3 -1
- package/lib/queries/count.js.map +1 -1
- package/lib/queries/create/buildCreate.d.ts +1 -1
- package/lib/queries/create/buildCreate.d.ts.map +1 -1
- package/lib/queries/create/buildCreate.js +1 -1
- package/lib/queries/create/buildCreate.js.map +1 -1
- package/lib/queries/create/createResultSchema.d.ts.map +1 -1
- package/lib/queries/create/createResultSchema.js +3 -1
- package/lib/queries/create/createResultSchema.js.map +1 -1
- package/lib/queries/create/createToSql.d.ts.map +1 -1
- package/lib/queries/create/createToSql.js +2 -2
- package/lib/queries/create/createToSql.js.map +1 -1
- package/lib/queries/createMany.d.ts.map +1 -1
- package/lib/queries/createMany.js +3 -1
- package/lib/queries/createMany.js.map +1 -1
- package/lib/queries/createOne.d.ts.map +1 -1
- package/lib/queries/createOne.js +6 -0
- package/lib/queries/createOne.js.map +1 -1
- package/lib/queries/delete/buildDelete.d.ts +1 -1
- package/lib/queries/delete/buildDelete.d.ts.map +1 -1
- package/lib/queries/delete/buildDelete.js +1 -1
- package/lib/queries/delete/buildDelete.js.map +1 -1
- package/lib/queries/delete/deleteResultSchema.d.ts.map +1 -1
- package/lib/queries/delete/deleteResultSchema.js +3 -1
- package/lib/queries/delete/deleteResultSchema.js.map +1 -1
- package/lib/queries/delete/deleteToSql.js +1 -1
- package/lib/queries/delete/deleteToSql.js.map +1 -1
- package/lib/queries/deleteMany.d.ts.map +1 -1
- package/lib/queries/deleteMany.js +3 -1
- package/lib/queries/deleteMany.js.map +1 -1
- package/lib/queries/find/buildFind.d.ts.map +1 -1
- package/lib/queries/find/buildFind.js +34 -30
- package/lib/queries/find/buildFind.js.map +1 -1
- package/lib/queries/find/findResultSchema.d.ts.map +1 -1
- package/lib/queries/find/findResultSchema.js +9 -2
- package/lib/queries/find/findResultSchema.js.map +1 -1
- package/lib/queries/find/findToSql.d.ts.map +1 -1
- package/lib/queries/find/findToSql.js +12 -17
- package/lib/queries/find/findToSql.js.map +1 -1
- package/lib/queries/find/tests/findMany.nullable-relations.test.d.ts +2 -0
- package/lib/queries/find/tests/findMany.nullable-relations.test.d.ts.map +1 -0
- package/lib/queries/find/tests/findMany.nullable-relations.test.js +118 -0
- package/lib/queries/find/tests/findMany.nullable-relations.test.js.map +1 -0
- package/lib/queries/find/types/FindBuilder.d.ts +12 -4
- package/lib/queries/find/types/FindBuilder.d.ts.map +1 -1
- package/lib/queries/findMany.d.ts.map +1 -1
- package/lib/queries/findMany.js +17 -9
- package/lib/queries/findMany.js.map +1 -1
- package/lib/queries/update/buildUpdate.d.ts +1 -1
- package/lib/queries/update/buildUpdate.d.ts.map +1 -1
- package/lib/queries/update/buildUpdate.js +1 -1
- package/lib/queries/update/buildUpdate.js.map +1 -1
- package/lib/queries/update/updateResultSchema.d.ts.map +1 -1
- package/lib/queries/update/updateResultSchema.js +3 -1
- package/lib/queries/update/updateResultSchema.js.map +1 -1
- package/lib/queries/update/updateToSql.js +1 -1
- package/lib/queries/update/updateToSql.js.map +1 -1
- package/lib/queries/updateMany.d.ts.map +1 -1
- package/lib/queries/updateMany.js +3 -1
- package/lib/queries/updateMany.js.map +1 -1
- package/lib/queries/util/rowToObject.d.ts.map +1 -1
- package/lib/queries/util/rowToObject.js +1 -1
- package/lib/queries/util/rowToObject.js.map +1 -1
- package/lib/schema/types/base/BaseRelation.d.ts +1 -0
- package/lib/schema/types/base/BaseRelation.d.ts.map +1 -1
- package/lib/schema/types/loose/LooseRelationDefinition.d.ts +1 -0
- package/lib/schema/types/loose/LooseRelationDefinition.d.ts.map +1 -1
- package/lib/schema/types/relations/OneToManyRelation.d.ts +1 -0
- package/lib/schema/types/relations/OneToManyRelation.d.ts.map +1 -1
- package/lib/test/db/index.d.ts +40 -0
- package/lib/test/db/index.d.ts.map +1 -1
- package/lib/test/db/models/post.model.d.ts +9 -0
- package/lib/test/db/models/post.model.d.ts.map +1 -1
- package/lib/test/db/models/post.model.js +6 -0
- package/lib/test/db/models/post.model.js.map +1 -1
- package/lib/test/db/models/post.relations.d.ts +6 -0
- package/lib/test/db/models/post.relations.d.ts.map +1 -1
- package/lib/test/db/models/post.relations.js +6 -0
- package/lib/test/db/models/post.relations.js.map +1 -1
- package/lib/test/db/models/user.model.d.ts +9 -0
- package/lib/test/db/models/user.model.d.ts.map +1 -1
- package/lib/test/db/models/user.model.js +6 -0
- package/lib/test/db/models/user.model.js.map +1 -1
- package/lib/test/db/models/user.relations.d.ts +16 -0
- package/lib/test/db/models/user.relations.d.ts.map +1 -1
- package/lib/test/db/models/user.relations.js +16 -0
- package/lib/test/db/models/user.relations.js.map +1 -1
- package/lib/test/db/models.d.ts +18 -0
- package/lib/test/db/models.d.ts.map +1 -1
- package/lib/test/db/relations.d.ts +22 -0
- package/lib/test/db/relations.d.ts.map +1 -1
- package/lib/test/seed/index.d.ts +40 -0
- package/lib/test/seed/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/migrate/commands/implode.ts +1 -1
- package/src/migrate/sql/createTableSql.test.ts +1 -0
- package/src/orm.ts +5 -2
- package/src/queries/clauses/where/buildWhereClauses.test.ts +1 -1
- package/src/queries/clauses/where/buildWhereClauses.ts +1 -1
- package/src/queries/count/buildCount.ts +39 -33
- package/src/queries/count/countToSql.ts +27 -32
- package/src/queries/count/types/CountBuilder.ts +23 -6
- package/src/queries/count.ts +4 -1
- package/src/queries/create/buildCreate.ts +2 -2
- package/src/queries/create/createResultSchema.ts +3 -1
- package/src/queries/create/createToSql.ts +6 -2
- package/src/queries/createMany.ts +4 -1
- package/src/queries/createOne.ts +9 -0
- package/src/queries/delete/buildDelete.ts +2 -2
- package/src/queries/delete/deleteResultSchema.ts +3 -1
- package/src/queries/delete/deleteToSql.ts +1 -1
- package/src/queries/deleteMany.ts +4 -1
- package/src/queries/find/buildFind.ts +40 -34
- package/src/queries/find/findResultSchema.ts +9 -2
- package/src/queries/find/findToSql.ts +27 -32
- package/src/queries/find/tests/findMany.nullable-relations.test.ts +127 -0
- package/src/queries/find/types/FindBuilder.ts +23 -6
- package/src/queries/findMany.ts +25 -17
- package/src/queries/update/buildUpdate.ts +2 -2
- package/src/queries/update/updateResultSchema.ts +3 -1
- package/src/queries/update/updateToSql.ts +1 -1
- package/src/queries/updateMany.ts +4 -1
- package/src/queries/util/rowToObject.ts +5 -1
- package/src/schema/types/base/BaseRelation.ts +1 -0
- package/src/schema/types/loose/LooseRelationDefinition.ts +1 -1
- package/src/schema/types/relations/OneToManyRelation.ts +1 -0
- package/src/test/db/models/post.model.ts +6 -0
- package/src/test/db/models/post.relations.ts +6 -0
- package/src/test/db/models/user.model.ts +6 -0
- package/src/test/db/models/user.relations.ts +16 -0
package/src/orm.ts
CHANGED
|
@@ -330,7 +330,7 @@ export class Orm<
|
|
|
330
330
|
...variables: readonly unknown[]
|
|
331
331
|
): Promise<T[]> {
|
|
332
332
|
const query = sql(fragments, ...variables);
|
|
333
|
-
if (
|
|
333
|
+
if (process.env.ORM_VERBOSE_LOGGING) {
|
|
334
334
|
console.log(query.text);
|
|
335
335
|
console.log(query.values);
|
|
336
336
|
}
|
|
@@ -343,7 +343,10 @@ export class Orm<
|
|
|
343
343
|
...variables: readonly unknown[]
|
|
344
344
|
): Promise<T> {
|
|
345
345
|
const query = sql(fragments, ...variables);
|
|
346
|
-
if (
|
|
346
|
+
if (process.env.ORM_VERBOSE_LOGGING) {
|
|
347
|
+
console.log(query.text);
|
|
348
|
+
console.log(query.values);
|
|
349
|
+
}
|
|
347
350
|
const result = await this.connection.query<T>(query);
|
|
348
351
|
if (result.rowCount === 0 || result.rowCount === null)
|
|
349
352
|
throw new OrmError("No rows returned from query");
|
|
@@ -135,7 +135,7 @@ describe("buildWhereClauses", () => {
|
|
|
135
135
|
) => {
|
|
136
136
|
const clause = buildWhereClauses(
|
|
137
137
|
db.config,
|
|
138
|
-
{
|
|
138
|
+
{ table: "foo", schema: "casekit", model: "foo", alias: "a" },
|
|
139
139
|
where,
|
|
140
140
|
);
|
|
141
141
|
expect(clause.text).toEqual(sql);
|
|
@@ -8,7 +8,7 @@ import { $and, $not, $or } from "./operators";
|
|
|
8
8
|
|
|
9
9
|
export const buildWhereClauses = (
|
|
10
10
|
config: BaseConfiguration,
|
|
11
|
-
table: {
|
|
11
|
+
table: { table: string; schema: string; alias: string; model: string },
|
|
12
12
|
where: WhereClause<LooseModelDefinitions, ModelName<LooseModelDefinitions>>,
|
|
13
13
|
): SQLStatement => {
|
|
14
14
|
const clauses: SQLStatement[] = [];
|
|
@@ -13,23 +13,25 @@ export const buildCount = (
|
|
|
13
13
|
_tableIndex = 0,
|
|
14
14
|
): CountBuilder => {
|
|
15
15
|
const builder: CountBuilder = {
|
|
16
|
+
table: {
|
|
17
|
+
table: config.models[m]["table"],
|
|
18
|
+
model: m,
|
|
19
|
+
schema: config.models[m]["schema"],
|
|
20
|
+
alias: tableAlias(_tableIndex++),
|
|
21
|
+
joins: [],
|
|
22
|
+
where: config.middleware.count?.where
|
|
23
|
+
? config.middleware.count.where(query.where, {
|
|
24
|
+
config,
|
|
25
|
+
model: m,
|
|
26
|
+
})
|
|
27
|
+
: query.where,
|
|
28
|
+
},
|
|
16
29
|
tableIndex: _tableIndex,
|
|
17
|
-
tables: [],
|
|
18
30
|
};
|
|
19
31
|
|
|
20
32
|
const model = config.models[m];
|
|
21
33
|
|
|
22
|
-
const alias =
|
|
23
|
-
|
|
24
|
-
builder.tables.push({
|
|
25
|
-
name: config.models[m]["table"],
|
|
26
|
-
model: m,
|
|
27
|
-
schema: config.models[m]["schema"],
|
|
28
|
-
alias: alias,
|
|
29
|
-
where: config.middleware.count?.where
|
|
30
|
-
? config.middleware.count.where(query.where, { config, model: m })
|
|
31
|
-
: query.where,
|
|
32
|
-
});
|
|
34
|
+
const alias = builder.table.alias;
|
|
33
35
|
|
|
34
36
|
for (const [r, subquery] of Object.entries(query.include ?? {})) {
|
|
35
37
|
const relation = config.relations[m][r];
|
|
@@ -42,28 +44,32 @@ export const buildCount = (
|
|
|
42
44
|
[...path, r],
|
|
43
45
|
builder.tableIndex++,
|
|
44
46
|
);
|
|
45
|
-
const
|
|
46
|
-
builder.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
47
|
+
const joinedTable = joinBuilder.table;
|
|
48
|
+
builder.table.joins.push(
|
|
49
|
+
{
|
|
50
|
+
from: {
|
|
51
|
+
schema: config.models[m].schema,
|
|
52
|
+
table: config.models[m].table,
|
|
53
|
+
alias,
|
|
54
|
+
model: m,
|
|
55
|
+
columns: ensureArray(relation.foreignKey).map(
|
|
56
|
+
(c) => model.columns[c].name,
|
|
57
|
+
),
|
|
58
|
+
},
|
|
59
|
+
to: {
|
|
60
|
+
schema: joinedModel.schema,
|
|
61
|
+
table: joinedTable.table,
|
|
62
|
+
alias: joinedTable.alias,
|
|
63
|
+
model: relation.model,
|
|
64
|
+
columns: joinedModel.primaryKey.map(
|
|
65
|
+
(c) => joinedModel.columns[c].name,
|
|
66
|
+
),
|
|
63
67
|
},
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
where: joinedTable.where,
|
|
69
|
+
type: relation.optional ? "left" : "inner",
|
|
70
|
+
},
|
|
71
|
+
...joinedTable.joins,
|
|
72
|
+
);
|
|
67
73
|
}
|
|
68
74
|
}
|
|
69
75
|
|
|
@@ -11,50 +11,45 @@ export const countToSql = (
|
|
|
11
11
|
config: BaseConfiguration,
|
|
12
12
|
builder: CountBuilder,
|
|
13
13
|
): SQLStatement => {
|
|
14
|
-
const
|
|
14
|
+
const table = builder.table;
|
|
15
15
|
|
|
16
16
|
const frag = sql`SELECT count(1) as "count"`;
|
|
17
17
|
|
|
18
|
-
frag.push(pgfmt(`\nFROM %I.%I %I`, table.schema, table.
|
|
18
|
+
frag.push(pgfmt(`\nFROM %I.%I %I`, table.schema, table.table, table.alias));
|
|
19
19
|
|
|
20
|
-
for (const
|
|
21
|
-
if (
|
|
22
|
-
throw new OrmError(
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
for (const join of table.joins ?? []) {
|
|
21
|
+
if (join.from.columns.length !== join.to.columns.length) {
|
|
22
|
+
throw new OrmError(
|
|
23
|
+
"Number of foreign keys doesn't match number of primary keys in join",
|
|
24
|
+
{ data: builder },
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
frag.push(join.type === "left" ? "\nLEFT JOIN" : "\nJOIN");
|
|
25
29
|
frag.push(
|
|
26
30
|
pgfmt(
|
|
27
|
-
"\
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
"\n %I.%I %I\n ON ",
|
|
32
|
+
join.to.schema,
|
|
33
|
+
join.to.table,
|
|
34
|
+
join.to.alias,
|
|
31
35
|
),
|
|
32
36
|
);
|
|
33
37
|
frag.push(
|
|
34
|
-
|
|
35
|
-
.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
pgfmt(
|
|
45
|
-
"%I.%I = %I.%I",
|
|
46
|
-
table.alias,
|
|
47
|
-
from,
|
|
48
|
-
joinedTable.alias,
|
|
49
|
-
join.to.columns[i],
|
|
50
|
-
),
|
|
51
|
-
);
|
|
52
|
-
})
|
|
38
|
+
join.from.columns
|
|
39
|
+
.map((from, i) =>
|
|
40
|
+
pgfmt(
|
|
41
|
+
"%I.%I = %I.%I",
|
|
42
|
+
join.from.alias,
|
|
43
|
+
from,
|
|
44
|
+
join.to.alias,
|
|
45
|
+
join.to.columns[i],
|
|
46
|
+
),
|
|
47
|
+
)
|
|
53
48
|
.join(" AND "),
|
|
54
49
|
);
|
|
55
|
-
if (hasConditions(
|
|
50
|
+
if (hasConditions(join.where)) {
|
|
56
51
|
frag.push(
|
|
57
|
-
sql`\n AND ${buildWhereClauses(config,
|
|
52
|
+
sql`\n AND ${buildWhereClauses(config, join.to, join.where)}`,
|
|
58
53
|
);
|
|
59
54
|
}
|
|
60
55
|
}
|
|
@@ -5,12 +5,12 @@ import { WhereClause } from "../../clauses/WhereClause";
|
|
|
5
5
|
export type CountBuilder = {
|
|
6
6
|
tableIndex: number;
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
table: {
|
|
9
|
+
table: string;
|
|
10
10
|
model: string;
|
|
11
11
|
schema: string;
|
|
12
12
|
alias: string;
|
|
13
|
-
joins
|
|
13
|
+
joins: Join[];
|
|
14
14
|
conditions?: WhereClause<
|
|
15
15
|
LooseModelDefinitions,
|
|
16
16
|
ModelName<LooseModelDefinitions>
|
|
@@ -19,10 +19,27 @@ export type CountBuilder = {
|
|
|
19
19
|
LooseModelDefinitions,
|
|
20
20
|
ModelName<LooseModelDefinitions>
|
|
21
21
|
>;
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
export type Join = {
|
|
26
|
-
from: {
|
|
27
|
-
|
|
26
|
+
from: {
|
|
27
|
+
schema: string;
|
|
28
|
+
table: string;
|
|
29
|
+
alias: string;
|
|
30
|
+
model: string;
|
|
31
|
+
columns: string[];
|
|
32
|
+
};
|
|
33
|
+
to: {
|
|
34
|
+
schema: string;
|
|
35
|
+
table: string;
|
|
36
|
+
alias: string;
|
|
37
|
+
model: string;
|
|
38
|
+
columns: string[];
|
|
39
|
+
};
|
|
40
|
+
where?: WhereClause<
|
|
41
|
+
LooseModelDefinitions,
|
|
42
|
+
ModelName<LooseModelDefinitions>
|
|
43
|
+
>;
|
|
44
|
+
type?: "inner" | "left";
|
|
28
45
|
};
|
package/src/queries/count.ts
CHANGED
|
@@ -20,7 +20,10 @@ export const count = async (
|
|
|
20
20
|
values: statement.values,
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
if (
|
|
23
|
+
if (process.env.ORM_VERBOSE_LOGGING) {
|
|
24
|
+
console.log(statement.text);
|
|
25
|
+
console.log(statement.values);
|
|
26
|
+
}
|
|
24
27
|
|
|
25
28
|
const result = await conn
|
|
26
29
|
.query(statement)
|
|
@@ -6,7 +6,7 @@ import { BaseCreateManyParams } from "./types/BaseCreateManyParams";
|
|
|
6
6
|
|
|
7
7
|
export type CreateBuilder = {
|
|
8
8
|
tableIndex: number;
|
|
9
|
-
table: {
|
|
9
|
+
table: { table: string; schema: string };
|
|
10
10
|
params: { name: string; path: string; values: unknown[] }[];
|
|
11
11
|
onConflict?: { do: "nothing" };
|
|
12
12
|
returning: { name: string; path: string; alias: string }[];
|
|
@@ -21,7 +21,7 @@ export const buildCreate = (
|
|
|
21
21
|
const builder: CreateBuilder = {
|
|
22
22
|
tableIndex: _tableIndex,
|
|
23
23
|
table: {
|
|
24
|
-
|
|
24
|
+
table: config.models[m].table,
|
|
25
25
|
schema: config.models[m].schema,
|
|
26
26
|
},
|
|
27
27
|
params: [],
|
|
@@ -15,7 +15,9 @@ export const createResultSchema = (
|
|
|
15
15
|
|
|
16
16
|
params.returning?.forEach((s) => {
|
|
17
17
|
const col = config.models[m].columns[s];
|
|
18
|
-
obj[s] = col.nullable
|
|
18
|
+
obj[s] = col.nullable
|
|
19
|
+
? col.zodSchema.nullish().transform((x) => x ?? null)
|
|
20
|
+
: col.zodSchema;
|
|
19
21
|
});
|
|
20
22
|
|
|
21
23
|
return z.object(obj);
|
|
@@ -10,10 +10,14 @@ export const createToSql = (builder: CreateBuilder): SQLStatement => {
|
|
|
10
10
|
|
|
11
11
|
if (params.length === 0) {
|
|
12
12
|
frag.push(
|
|
13
|
-
pgfmt(
|
|
13
|
+
pgfmt(
|
|
14
|
+
"INSERT INTO %I.%I DEFAULT VALUES",
|
|
15
|
+
table.schema,
|
|
16
|
+
table.table,
|
|
17
|
+
),
|
|
14
18
|
);
|
|
15
19
|
} else {
|
|
16
|
-
frag.push(pgfmt("INSERT INTO %I.%I (\n", table.schema, table.
|
|
20
|
+
frag.push(pgfmt("INSERT INTO %I.%I (\n", table.schema, table.table));
|
|
17
21
|
frag.push(params.map((p) => pgfmt(" %I", p.name)).join(",\n"));
|
|
18
22
|
frag.push(") VALUES ");
|
|
19
23
|
const values = params[0].values.map((_, index) => {
|
|
@@ -26,7 +26,10 @@ export const createMany = async (
|
|
|
26
26
|
values: statement.values,
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
if (
|
|
29
|
+
if (process.env.ORM_VERBOSE_LOGGING) {
|
|
30
|
+
console.log(statement.text);
|
|
31
|
+
console.log(statement.values);
|
|
32
|
+
}
|
|
30
33
|
|
|
31
34
|
const result = await conn.query(statement);
|
|
32
35
|
return params.returning
|
package/src/queries/createOne.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BaseConfiguration } from "src/schema/types/base/BaseConfiguration";
|
|
2
2
|
|
|
3
|
+
import { OrmError } from "..";
|
|
3
4
|
import { Connection } from "../Connection";
|
|
4
5
|
import { BaseCreateOneParams } from "./create/types/BaseCreateOneParams";
|
|
5
6
|
import { createMany } from "./createMany";
|
|
@@ -10,6 +11,14 @@ export const createOne = async (
|
|
|
10
11
|
m: string,
|
|
11
12
|
params: BaseCreateOneParams,
|
|
12
13
|
) => {
|
|
14
|
+
if (params.onConflict?.do === "nothing" && params.returning) {
|
|
15
|
+
throw new OrmError(
|
|
16
|
+
"Cannot use 'returning' with 'onConflict' in createOne",
|
|
17
|
+
{
|
|
18
|
+
data: { m, params },
|
|
19
|
+
},
|
|
20
|
+
);
|
|
21
|
+
}
|
|
13
22
|
const result = await createMany(conn, config, m, {
|
|
14
23
|
...params,
|
|
15
24
|
values: [params.values],
|
|
@@ -9,7 +9,7 @@ import { BaseDeleteParams } from "./types/BaseDeleteParams";
|
|
|
9
9
|
|
|
10
10
|
export type DeleteBuilder = {
|
|
11
11
|
tableIndex: number;
|
|
12
|
-
table: {
|
|
12
|
+
table: { table: string; model: string; alias: string; schema: string };
|
|
13
13
|
where: WhereClause<LooseModelDefinitions, ModelName<LooseModelDefinitions>>;
|
|
14
14
|
returning: { name: string; path: string; alias: string }[];
|
|
15
15
|
};
|
|
@@ -23,7 +23,7 @@ export const buildDelete = (
|
|
|
23
23
|
const builder: DeleteBuilder = {
|
|
24
24
|
tableIndex: _tableIndex,
|
|
25
25
|
table: {
|
|
26
|
-
|
|
26
|
+
table: config.models[m].table,
|
|
27
27
|
schema: config.models[m].schema,
|
|
28
28
|
model: m,
|
|
29
29
|
alias: tableAlias(_tableIndex++),
|
|
@@ -14,7 +14,9 @@ export const deleteResultSchema = (
|
|
|
14
14
|
|
|
15
15
|
params.returning?.forEach((s) => {
|
|
16
16
|
const col = config.models[m].columns[s];
|
|
17
|
-
obj[s] = col.nullable
|
|
17
|
+
obj[s] = col.nullable
|
|
18
|
+
? col.zodSchema.nullish().transform((x) => x ?? null)
|
|
19
|
+
: col.zodSchema;
|
|
18
20
|
});
|
|
19
21
|
|
|
20
22
|
return z.object(obj);
|
|
@@ -21,7 +21,10 @@ export const deleteMany = async (
|
|
|
21
21
|
values: statement.values,
|
|
22
22
|
});
|
|
23
23
|
|
|
24
|
-
if (
|
|
24
|
+
if (process.env.ORM_VERBOSE_LOGGING) {
|
|
25
|
+
console.log(statement.text);
|
|
26
|
+
console.log(statement.values);
|
|
27
|
+
}
|
|
25
28
|
|
|
26
29
|
const result = await conn.query(statement);
|
|
27
30
|
return params.returning
|
|
@@ -14,27 +14,29 @@ export const buildFind = (
|
|
|
14
14
|
_tableIndex = 0,
|
|
15
15
|
): FindBuilder => {
|
|
16
16
|
const builder: FindBuilder = {
|
|
17
|
-
tableIndex: _tableIndex,
|
|
18
17
|
columns: [],
|
|
19
|
-
|
|
18
|
+
table: {
|
|
19
|
+
table: config.models[m]["table"],
|
|
20
|
+
model: m,
|
|
21
|
+
schema: config.models[m]["schema"],
|
|
22
|
+
alias: tableAlias(_tableIndex++),
|
|
23
|
+
where: config.middleware.find?.where
|
|
24
|
+
? config.middleware.find.where(query.where, {
|
|
25
|
+
config,
|
|
26
|
+
model: m,
|
|
27
|
+
})
|
|
28
|
+
: query.where,
|
|
29
|
+
joins: [],
|
|
30
|
+
},
|
|
20
31
|
orderBy: [],
|
|
32
|
+
tableIndex: _tableIndex,
|
|
21
33
|
};
|
|
22
34
|
|
|
23
35
|
const model = config.models[m];
|
|
24
36
|
|
|
25
|
-
const alias =
|
|
37
|
+
const alias = builder.table.alias;
|
|
26
38
|
let colIndex = 0;
|
|
27
39
|
|
|
28
|
-
builder.tables.push({
|
|
29
|
-
name: config.models[m]["table"],
|
|
30
|
-
model: m,
|
|
31
|
-
schema: config.models[m]["schema"],
|
|
32
|
-
alias: alias,
|
|
33
|
-
where: config.middleware.find?.where
|
|
34
|
-
? config.middleware.find.where(query.where, { config, model: m })
|
|
35
|
-
: query.where,
|
|
36
|
-
});
|
|
37
|
-
|
|
38
40
|
// make sure we always select the model's primary key,
|
|
39
41
|
// and if necessary the foreign key for a lateral join
|
|
40
42
|
// - we'll strip them out later
|
|
@@ -64,34 +66,38 @@ export const buildFind = (
|
|
|
64
66
|
[...path, r],
|
|
65
67
|
builder.tableIndex++,
|
|
66
68
|
);
|
|
67
|
-
const
|
|
68
|
-
builder.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
},
|
|
79
|
-
to: {
|
|
80
|
-
table: joinedTable.alias,
|
|
81
|
-
columns: joinedModel.primaryKey.map(
|
|
82
|
-
(c) => joinedModel.columns[c].name,
|
|
83
|
-
),
|
|
84
|
-
},
|
|
69
|
+
const joinedTable = joinBuilder.table;
|
|
70
|
+
builder.table.joins.push(
|
|
71
|
+
{
|
|
72
|
+
from: {
|
|
73
|
+
schema: config.models[m].schema,
|
|
74
|
+
table: config.models[m].table,
|
|
75
|
+
alias,
|
|
76
|
+
model: m,
|
|
77
|
+
columns: ensureArray(relation.foreignKey).map(
|
|
78
|
+
(c) => model.columns[c].name,
|
|
79
|
+
),
|
|
85
80
|
},
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
to: {
|
|
82
|
+
schema: joinedModel.schema,
|
|
83
|
+
table: joinedTable.table,
|
|
84
|
+
alias: joinedTable.alias,
|
|
85
|
+
model: relation.model,
|
|
86
|
+
columns: joinedModel.primaryKey.map(
|
|
87
|
+
(c) => joinedModel.columns[c].name,
|
|
88
|
+
),
|
|
89
|
+
},
|
|
90
|
+
type: relation.optional ? "left" : "inner",
|
|
91
|
+
where: joinedTable.where,
|
|
92
|
+
},
|
|
93
|
+
...joinedTable.joins,
|
|
94
|
+
);
|
|
88
95
|
// this is admittedly a bit weird,
|
|
89
96
|
// we wouldn't expect N:1 relations to specify
|
|
90
97
|
// ordering, skipping, and limiting, and typescript
|
|
91
98
|
// prevents users from doing this, but
|
|
92
99
|
// we rely on them being present as part of the N:N
|
|
93
100
|
// implementation
|
|
94
|
-
builder.tables.push(...otherTables);
|
|
95
101
|
builder.columns.push(...joinBuilder.columns);
|
|
96
102
|
builder.orderBy.push(...joinBuilder.orderBy);
|
|
97
103
|
builder.limit = min([builder.limit, joinBuilder.limit]);
|
|
@@ -12,13 +12,20 @@ export const findResultSchema = (
|
|
|
12
12
|
|
|
13
13
|
query.select.forEach((field) => {
|
|
14
14
|
const col = config.models[m].columns[field];
|
|
15
|
-
obj[field] = col.nullable
|
|
15
|
+
obj[field] = col.nullable
|
|
16
|
+
? col.zodSchema.nullish().transform((x) => x ?? null)
|
|
17
|
+
: col.zodSchema;
|
|
16
18
|
});
|
|
17
19
|
|
|
18
20
|
for (const [field, subquery] of Object.entries(query.include || {})) {
|
|
19
21
|
const relation = config.relations[m][field];
|
|
20
22
|
const schema = findResultSchema(config, relation.model, subquery!);
|
|
21
|
-
obj[field] =
|
|
23
|
+
obj[field] =
|
|
24
|
+
relation.type === "N:1"
|
|
25
|
+
? relation.optional
|
|
26
|
+
? schema.nullish().transform((x) => x ?? null)
|
|
27
|
+
: schema
|
|
28
|
+
: z.array(schema);
|
|
22
29
|
}
|
|
23
30
|
|
|
24
31
|
return z.object(obj);
|
|
@@ -13,7 +13,7 @@ export const findToSql = (
|
|
|
13
13
|
builder: FindBuilder,
|
|
14
14
|
): SQLStatement => {
|
|
15
15
|
const frag = new SQLStatement();
|
|
16
|
-
const
|
|
16
|
+
const table = builder.table;
|
|
17
17
|
|
|
18
18
|
if (builder.lateralBy) {
|
|
19
19
|
const { columns, groupTable, itemTable } = builder.lateralBy;
|
|
@@ -43,46 +43,41 @@ export const findToSql = (
|
|
|
43
43
|
),
|
|
44
44
|
);
|
|
45
45
|
|
|
46
|
-
frag.push(pgfmt(`\nFROM %I.%I %I`, table.schema, table.
|
|
46
|
+
frag.push(pgfmt(`\nFROM %I.%I %I`, table.schema, table.table, table.alias));
|
|
47
47
|
|
|
48
|
-
for (const
|
|
49
|
-
if (
|
|
50
|
-
throw new OrmError(
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
for (const join of table.joins ?? []) {
|
|
49
|
+
if (join.from.columns.length !== join.to.columns.length) {
|
|
50
|
+
throw new OrmError(
|
|
51
|
+
"Number of foreign keys doesn't match number of primary keys in join",
|
|
52
|
+
{ data: builder },
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
frag.push(join.type === "left" ? "\nLEFT JOIN" : "\nJOIN");
|
|
53
57
|
frag.push(
|
|
54
58
|
pgfmt(
|
|
55
|
-
"
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
" %I.%I %I\n ON ",
|
|
60
|
+
join.to.schema,
|
|
61
|
+
join.to.table,
|
|
62
|
+
join.to.alias,
|
|
59
63
|
),
|
|
60
64
|
);
|
|
61
65
|
frag.push(
|
|
62
|
-
|
|
63
|
-
.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
pgfmt(
|
|
73
|
-
"%I.%I = %I.%I",
|
|
74
|
-
table.alias,
|
|
75
|
-
from,
|
|
76
|
-
joinedTable.alias,
|
|
77
|
-
join.to.columns[i],
|
|
78
|
-
),
|
|
79
|
-
);
|
|
80
|
-
})
|
|
66
|
+
join.from.columns
|
|
67
|
+
.map((from, i) =>
|
|
68
|
+
pgfmt(
|
|
69
|
+
"%I.%I = %I.%I",
|
|
70
|
+
join.from.alias,
|
|
71
|
+
from,
|
|
72
|
+
join.to.alias,
|
|
73
|
+
join.to.columns[i],
|
|
74
|
+
),
|
|
75
|
+
)
|
|
81
76
|
.join(" AND "),
|
|
82
77
|
);
|
|
83
|
-
if (hasConditions(
|
|
78
|
+
if (hasConditions(join.where)) {
|
|
84
79
|
frag.push(
|
|
85
|
-
sql`\n AND ${buildWhereClauses(config,
|
|
80
|
+
sql`\n AND ${buildWhereClauses(config, join.to, join.where)}`,
|
|
86
81
|
);
|
|
87
82
|
}
|
|
88
83
|
}
|