neoorm 0.7.3 → 0.8.0
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/README.md +4 -4
- package/dist/bin/neoorm.js +36 -27
- package/dist/bin/neoorm.js.map +1 -1
- package/dist/codegen/diff-manifest.d.ts.map +1 -1
- package/dist/codegen/diff-manifest.js +55 -30
- package/dist/codegen/diff-manifest.js.map +1 -1
- package/dist/codegen/emit-includes.d.ts.map +1 -1
- package/dist/codegen/emit-includes.js +6 -6
- package/dist/codegen/emit-includes.js.map +1 -1
- package/dist/codegen/emit-models.d.ts.map +1 -1
- package/dist/codegen/emit-models.js +14 -14
- package/dist/codegen/emit-models.js.map +1 -1
- package/dist/codegen/generate.d.ts +11 -1
- package/dist/codegen/generate.d.ts.map +1 -1
- package/dist/codegen/generate.js +30 -11
- package/dist/codegen/generate.js.map +1 -1
- package/dist/codegen/manifest-relations.d.ts +2 -1
- package/dist/codegen/manifest-relations.d.ts.map +1 -1
- package/dist/codegen/manifest-relations.js +4 -27
- package/dist/codegen/manifest-relations.js.map +1 -1
- package/dist/codegen/schema-to-manifest.d.ts +3 -2
- package/dist/codegen/schema-to-manifest.d.ts.map +1 -1
- package/dist/codegen/schema-to-manifest.js +9 -38
- package/dist/codegen/schema-to-manifest.js.map +1 -1
- package/dist/config.d.ts +6 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +21 -13
- package/dist/config.js.map +1 -1
- package/dist/datasource-provider.d.ts +7 -0
- package/dist/datasource-provider.d.ts.map +1 -0
- package/dist/datasource-provider.js +11 -0
- package/dist/datasource-provider.js.map +1 -0
- package/dist/dialect/fk.d.ts.map +1 -1
- package/dist/dialect/fk.js +4 -2
- package/dist/dialect/fk.js.map +1 -1
- package/dist/dialect/postgres.d.ts.map +1 -1
- package/dist/dialect/postgres.js +11 -15
- package/dist/dialect/postgres.js.map +1 -1
- package/dist/dialect/shared.d.ts +3 -1
- package/dist/dialect/shared.d.ts.map +1 -1
- package/dist/dialect/shared.js +4 -0
- package/dist/dialect/shared.js.map +1 -1
- package/dist/dialect/sqlite.d.ts.map +1 -1
- package/dist/dialect/sqlite.js +15 -18
- package/dist/dialect/sqlite.js.map +1 -1
- package/dist/dialect/sqlite.test.d.ts +2 -0
- package/dist/dialect/sqlite.test.d.ts.map +1 -0
- package/dist/dialect/sqlite.test.js +122 -0
- package/dist/dialect/sqlite.test.js.map +1 -0
- package/dist/dialect/types.d.ts +2 -1
- package/dist/dialect/types.d.ts.map +1 -1
- package/dist/docs/render.d.ts.map +1 -1
- package/dist/docs/render.js.map +1 -1
- package/dist/docs/search.d.ts.map +1 -1
- package/dist/docs/search.js +4 -4
- package/dist/docs/search.js.map +1 -1
- package/dist/docs/server.d.ts.map +1 -1
- package/dist/docs/server.js +3 -3
- package/dist/docs/server.js.map +1 -1
- package/dist/index.d.ts +12 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/init/scaffold.d.ts.map +1 -1
- package/dist/init/scaffold.js +3 -1
- package/dist/init/scaffold.js.map +1 -1
- package/dist/introspect/pull.d.ts.map +1 -1
- package/dist/introspect/pull.js +209 -154
- package/dist/introspect/pull.js.map +1 -1
- package/dist/introspect/queries.d.ts +6 -0
- package/dist/introspect/queries.d.ts.map +1 -1
- package/dist/introspect/queries.js +19 -0
- package/dist/introspect/queries.js.map +1 -1
- package/dist/introspect/sqlite/to-manifest.d.ts.map +1 -1
- package/dist/introspect/sqlite/to-manifest.js +6 -9
- package/dist/introspect/sqlite/to-manifest.js.map +1 -1
- package/dist/introspect/to-manifest.d.ts +8 -1
- package/dist/introspect/to-manifest.d.ts.map +1 -1
- package/dist/introspect/to-manifest.js +96 -21
- package/dist/introspect/to-manifest.js.map +1 -1
- package/dist/migrate/runner.d.ts +16 -0
- package/dist/migrate/runner.d.ts.map +1 -1
- package/dist/migrate/runner.js +147 -24
- package/dist/migrate/runner.js.map +1 -1
- package/dist/plugins/builtin.d.ts.map +1 -1
- package/dist/plugins/builtin.js +15 -16
- package/dist/plugins/builtin.js.map +1 -1
- package/dist/plugins/json/operators.d.ts.map +1 -1
- package/dist/plugins/json/operators.js +100 -27
- package/dist/plugins/json/operators.js.map +1 -1
- package/dist/plugins/postgis/columns.d.ts +18 -4
- package/dist/plugins/postgis/columns.d.ts.map +1 -1
- package/dist/plugins/postgis/columns.js +69 -10
- package/dist/plugins/postgis/columns.js.map +1 -1
- package/dist/plugins/postgis/columns.test.d.ts +2 -0
- package/dist/plugins/postgis/columns.test.d.ts.map +1 -0
- package/dist/plugins/postgis/columns.test.js +67 -0
- package/dist/plugins/postgis/columns.test.js.map +1 -0
- package/dist/plugins/postgis/index.d.ts +4 -2
- package/dist/plugins/postgis/index.d.ts.map +1 -1
- package/dist/plugins/postgis/index.js +2 -1
- package/dist/plugins/postgis/index.js.map +1 -1
- package/dist/plugins/postgis/where-types.d.ts +18 -0
- package/dist/plugins/postgis/where-types.d.ts.map +1 -0
- package/dist/plugins/postgis/where-types.js +2 -0
- package/dist/plugins/postgis/where-types.js.map +1 -0
- package/dist/plugins/registry.d.ts.map +1 -1
- package/dist/plugins/registry.js +20 -5
- package/dist/plugins/registry.js.map +1 -1
- package/dist/plugins/types.d.ts +1 -1
- package/dist/plugins/types.d.ts.map +1 -1
- package/dist/runtime/client.d.ts +60 -11
- package/dist/runtime/client.d.ts.map +1 -1
- package/dist/runtime/client.js +85 -39
- package/dist/runtime/client.js.map +1 -1
- package/dist/runtime/client.test.d.ts +2 -0
- package/dist/runtime/client.test.d.ts.map +1 -0
- package/dist/runtime/client.test.js +170 -0
- package/dist/runtime/client.test.js.map +1 -0
- package/dist/runtime/compile-error.d.ts +14 -0
- package/dist/runtime/compile-error.d.ts.map +1 -0
- package/dist/runtime/compile-error.js +14 -0
- package/dist/runtime/compile-error.js.map +1 -0
- package/dist/runtime/driver-sqlite.test.d.ts +2 -0
- package/dist/runtime/driver-sqlite.test.d.ts.map +1 -0
- package/dist/runtime/driver-sqlite.test.js +195 -0
- package/dist/runtime/driver-sqlite.test.js.map +1 -0
- package/dist/runtime/driver.d.ts +16 -1
- package/dist/runtime/driver.d.ts.map +1 -1
- package/dist/runtime/driver.js +118 -96
- package/dist/runtime/driver.js.map +1 -1
- package/dist/runtime/error-builders.d.ts +7 -6
- package/dist/runtime/error-builders.d.ts.map +1 -1
- package/dist/runtime/error-builders.js +2 -2
- package/dist/runtime/error-builders.js.map +1 -1
- package/dist/runtime/error-codes.d.ts +50 -0
- package/dist/runtime/error-codes.d.ts.map +1 -0
- package/dist/runtime/error-codes.js +54 -0
- package/dist/runtime/error-codes.js.map +1 -0
- package/dist/runtime/error-hints.d.ts +1 -0
- package/dist/runtime/error-hints.d.ts.map +1 -1
- package/dist/runtime/error-hints.js +13 -1
- package/dist/runtime/error-hints.js.map +1 -1
- package/dist/runtime/errors.d.ts +55 -8
- package/dist/runtime/errors.d.ts.map +1 -1
- package/dist/runtime/errors.js +143 -12
- package/dist/runtime/errors.js.map +1 -1
- package/dist/runtime/errors.test.d.ts +2 -0
- package/dist/runtime/errors.test.d.ts.map +1 -0
- package/dist/runtime/errors.test.js +115 -0
- package/dist/runtime/errors.test.js.map +1 -0
- package/dist/runtime/executor.d.ts +22 -3
- package/dist/runtime/executor.d.ts.map +1 -1
- package/dist/runtime/executor.js +93 -39
- package/dist/runtime/executor.js.map +1 -1
- package/dist/runtime/parse-db-json.d.ts +8 -0
- package/dist/runtime/parse-db-json.d.ts.map +1 -0
- package/dist/runtime/parse-db-json.js +33 -0
- package/dist/runtime/parse-db-json.js.map +1 -0
- package/dist/runtime/pg-error.d.ts.map +1 -1
- package/dist/runtime/pg-error.js +16 -16
- package/dist/runtime/pg-error.js.map +1 -1
- package/dist/runtime/pg-rollback.test.d.ts +2 -0
- package/dist/runtime/pg-rollback.test.d.ts.map +1 -0
- package/dist/runtime/pg-rollback.test.js +39 -0
- package/dist/runtime/pg-rollback.test.js.map +1 -0
- package/dist/runtime/query/aggregate.d.ts.map +1 -1
- package/dist/runtime/query/aggregate.js +15 -4
- package/dist/runtime/query/aggregate.js.map +1 -1
- package/dist/runtime/query/compile-aggregate.d.ts +39 -0
- package/dist/runtime/query/compile-aggregate.d.ts.map +1 -0
- package/dist/runtime/query/compile-aggregate.js +456 -0
- package/dist/runtime/query/compile-aggregate.js.map +1 -0
- package/dist/runtime/query/compile-where.d.ts +32 -0
- package/dist/runtime/query/compile-where.d.ts.map +1 -0
- package/dist/runtime/query/compile-where.js +708 -0
- package/dist/runtime/query/compile-where.js.map +1 -0
- package/dist/runtime/query/compile-write.d.ts +53 -0
- package/dist/runtime/query/compile-write.d.ts.map +1 -0
- package/dist/runtime/query/compile-write.js +467 -0
- package/dist/runtime/query/compile-write.js.map +1 -0
- package/dist/runtime/query/compile.d.ts +3 -114
- package/dist/runtime/query/compile.d.ts.map +1 -1
- package/dist/runtime/query/compile.js +3 -1536
- package/dist/runtime/query/compile.js.map +1 -1
- package/dist/runtime/query/count.d.ts +1 -0
- package/dist/runtime/query/count.d.ts.map +1 -1
- package/dist/runtime/query/count.js +12 -14
- package/dist/runtime/query/count.js.map +1 -1
- package/dist/runtime/query/create.d.ts.map +1 -1
- package/dist/runtime/query/create.js +20 -49
- package/dist/runtime/query/create.js.map +1 -1
- package/dist/runtime/query/cursor-codec.d.ts.map +1 -1
- package/dist/runtime/query/cursor-codec.js +8 -2
- package/dist/runtime/query/cursor-codec.js.map +1 -1
- package/dist/runtime/query/cursor.d.ts.map +1 -1
- package/dist/runtime/query/cursor.js +31 -7
- package/dist/runtime/query/cursor.js.map +1 -1
- package/dist/runtime/query/delete.d.ts.map +1 -1
- package/dist/runtime/query/delete.js +17 -17
- package/dist/runtime/query/delete.js.map +1 -1
- package/dist/runtime/query/execute.d.ts.map +1 -1
- package/dist/runtime/query/execute.js +11 -12
- package/dist/runtime/query/execute.js.map +1 -1
- package/dist/runtime/query/find-or-create.d.ts +4 -2
- package/dist/runtime/query/find-or-create.d.ts.map +1 -1
- package/dist/runtime/query/find-or-create.js +53 -43
- package/dist/runtime/query/find-or-create.js.map +1 -1
- package/dist/runtime/query/find-or-create.test.d.ts +2 -0
- package/dist/runtime/query/find-or-create.test.d.ts.map +1 -0
- package/dist/runtime/query/find-or-create.test.js +189 -0
- package/dist/runtime/query/find-or-create.test.js.map +1 -0
- package/dist/runtime/query/find.d.ts.map +1 -1
- package/dist/runtime/query/find.js +86 -58
- package/dist/runtime/query/find.js.map +1 -1
- package/dist/runtime/query/group-by.d.ts.map +1 -1
- package/dist/runtime/query/group-by.js +3 -2
- package/dist/runtime/query/group-by.js.map +1 -1
- package/dist/runtime/query/manifest-lookup.d.ts.map +1 -1
- package/dist/runtime/query/manifest-lookup.js +1 -1
- package/dist/runtime/query/manifest-lookup.js.map +1 -1
- package/dist/runtime/query/map-row.d.ts.map +1 -1
- package/dist/runtime/query/map-row.js +1 -1
- package/dist/runtime/query/map-row.js.map +1 -1
- package/dist/runtime/query/paginate.d.ts +3 -0
- package/dist/runtime/query/paginate.d.ts.map +1 -1
- package/dist/runtime/query/paginate.js +49 -11
- package/dist/runtime/query/paginate.js.map +1 -1
- package/dist/runtime/query/primary-key.d.ts +1 -1
- package/dist/runtime/query/primary-key.d.ts.map +1 -1
- package/dist/runtime/query/primary-key.js +45 -8
- package/dist/runtime/query/primary-key.js.map +1 -1
- package/dist/runtime/query/projection.d.ts +1 -0
- package/dist/runtime/query/projection.d.ts.map +1 -1
- package/dist/runtime/query/projection.js +23 -7
- package/dist/runtime/query/projection.js.map +1 -1
- package/dist/runtime/query/relation-planner.d.ts.map +1 -1
- package/dist/runtime/query/relation-planner.js +82 -53
- package/dist/runtime/query/relation-planner.js.map +1 -1
- package/dist/runtime/query/relation-writes.d.ts +3 -1
- package/dist/runtime/query/relation-writes.d.ts.map +1 -1
- package/dist/runtime/query/relation-writes.js +145 -54
- package/dist/runtime/query/relation-writes.js.map +1 -1
- package/dist/runtime/query/serialize-dialect.test.d.ts +2 -0
- package/dist/runtime/query/serialize-dialect.test.d.ts.map +1 -0
- package/dist/runtime/query/serialize-dialect.test.js +79 -0
- package/dist/runtime/query/serialize-dialect.test.js.map +1 -0
- package/dist/runtime/query/sqlite-constraint-errors.test.d.ts +2 -0
- package/dist/runtime/query/sqlite-constraint-errors.test.d.ts.map +1 -0
- package/dist/runtime/query/sqlite-constraint-errors.test.js +60 -0
- package/dist/runtime/query/sqlite-constraint-errors.test.js.map +1 -0
- package/dist/runtime/query/strip.d.ts.map +1 -1
- package/dist/runtime/query/strip.js.map +1 -1
- package/dist/runtime/query/table-index.d.ts.map +1 -1
- package/dist/runtime/query/table-index.js +4 -3
- package/dist/runtime/query/table-index.js.map +1 -1
- package/dist/runtime/query/unique.d.ts +8 -1
- package/dist/runtime/query/unique.d.ts.map +1 -1
- package/dist/runtime/query/unique.js +89 -4
- package/dist/runtime/query/unique.js.map +1 -1
- package/dist/runtime/query/unique.test.d.ts +2 -0
- package/dist/runtime/query/unique.test.d.ts.map +1 -0
- package/dist/runtime/query/unique.test.js +231 -0
- package/dist/runtime/query/unique.test.js.map +1 -0
- package/dist/runtime/query/update.d.ts.map +1 -1
- package/dist/runtime/query/update.js +30 -35
- package/dist/runtime/query/update.js.map +1 -1
- package/dist/runtime/query/update.test.d.ts +2 -0
- package/dist/runtime/query/update.test.d.ts.map +1 -0
- package/dist/runtime/query/update.test.js +186 -0
- package/dist/runtime/query/update.test.js.map +1 -0
- package/dist/runtime/query/updated-at.d.ts.map +1 -1
- package/dist/runtime/query/updated-at.js.map +1 -1
- package/dist/runtime/query/upsert.d.ts.map +1 -1
- package/dist/runtime/query/upsert.js +5 -7
- package/dist/runtime/query/upsert.js.map +1 -1
- package/dist/runtime/schema-error.d.ts.map +1 -1
- package/dist/runtime/schema-error.js +6 -2
- package/dist/runtime/schema-error.js.map +1 -1
- package/dist/runtime/sqlite-error.d.ts.map +1 -1
- package/dist/runtime/sqlite-error.js +11 -12
- package/dist/runtime/sqlite-error.js.map +1 -1
- package/dist/runtime/sqlite-open.d.ts +2 -0
- package/dist/runtime/sqlite-open.d.ts.map +1 -1
- package/dist/runtime/sqlite-open.js +18 -1
- package/dist/runtime/sqlite-open.js.map +1 -1
- package/dist/runtime/transaction.d.ts +4 -0
- package/dist/runtime/transaction.d.ts.map +1 -1
- package/dist/runtime/transaction.js +31 -1
- package/dist/runtime/transaction.js.map +1 -1
- package/dist/runtime/transaction.test.d.ts +2 -0
- package/dist/runtime/transaction.test.d.ts.map +1 -0
- package/dist/runtime/transaction.test.js +43 -0
- package/dist/runtime/transaction.test.js.map +1 -0
- package/dist/runtime/types.d.ts +50 -17
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/schema/column-where.d.ts +11 -1
- package/dist/schema/column-where.d.ts.map +1 -1
- package/dist/schema/column.d.ts +1 -1
- package/dist/schema/column.d.ts.map +1 -1
- package/dist/schema/column.js +1 -1
- package/dist/schema/column.js.map +1 -1
- package/dist/schema/define-schema.d.ts +1 -1
- package/dist/schema/define-schema.d.ts.map +1 -1
- package/dist/schema/define-schema.js +5 -23
- package/dist/schema/define-schema.js.map +1 -1
- package/dist/schema/index.d.ts +2 -2
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +1 -1
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/relation-types.d.ts +29 -22
- package/dist/schema/relation-types.d.ts.map +1 -1
- package/dist/schema/relation.d.ts.map +1 -1
- package/dist/schema/relation.js +6 -4
- package/dist/schema/relation.js.map +1 -1
- package/dist/schema/table-registry.d.ts.map +1 -1
- package/dist/schema/table-registry.js +8 -2
- package/dist/schema/table-registry.js.map +1 -1
- package/dist/schema/table.d.ts +6 -3
- package/dist/schema/table.d.ts.map +1 -1
- package/dist/schema/table.js +30 -31
- package/dist/schema/table.js.map +1 -1
- package/dist/schema/types.d.ts +33 -25
- package/dist/schema/types.d.ts.map +1 -1
- package/dist/sql/builder.d.ts +5 -1
- package/dist/sql/builder.d.ts.map +1 -1
- package/dist/sql/builder.js +23 -3
- package/dist/sql/builder.js.map +1 -1
- package/dist/sql/index.d.ts +5 -2
- package/dist/sql/index.d.ts.map +1 -1
- package/dist/sql/index.js +5 -2
- package/dist/sql/index.js.map +1 -1
- package/dist/utils/case.d.ts +4 -0
- package/dist/utils/case.d.ts.map +1 -1
- package/dist/utils/case.js +11 -0
- package/dist/utils/case.js.map +1 -1
- package/dist/utils/dotenv.d.ts +8 -0
- package/dist/utils/dotenv.d.ts.map +1 -0
- package/dist/utils/dotenv.js +70 -0
- package/dist/utils/dotenv.js.map +1 -0
- package/dist/utils/dotenv.test.d.ts +2 -0
- package/dist/utils/dotenv.test.d.ts.map +1 -0
- package/dist/utils/dotenv.test.js +32 -0
- package/dist/utils/dotenv.test.js.map +1 -0
- package/dist/utils/inflect.d.ts +6 -0
- package/dist/utils/inflect.d.ts.map +1 -0
- package/dist/utils/inflect.js +80 -0
- package/dist/utils/inflect.js.map +1 -0
- package/docs/cli.md +6 -2
- package/docs/configuration.md +3 -1
- package/docs/errors.md +61 -6
- package/docs/examples.md +63 -13
- package/docs/getting-started.md +24 -1
- package/docs/migrations.md +15 -2
- package/docs/plugins.md +13 -1
- package/docs/queries.md +109 -10
- package/docs/relations.md +5 -1
- package/docs/schema.md +12 -5
- package/docs/sqlite.md +24 -10
- package/docs/transactions.md +13 -2
- package/package.json +4 -2
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
import { effectiveRelations } from "../../codegen/manifest-relations.js";
|
|
2
|
+
import { postgresDialect, quoteIdentifier, tableRef, } from "../../dialect/postgres.js";
|
|
3
|
+
import { getColumnType } from "../../plugins/registry.js";
|
|
4
|
+
import { compileError } from "../compile-error.js";
|
|
5
|
+
import { QueryErrorCode } from "../error-codes.js";
|
|
6
|
+
import { didYouMean, suggestWhereOperator } from "../error-hints.js";
|
|
7
|
+
import { findM2M } from "./manifest-lookup.js";
|
|
8
|
+
import { primaryKeySqlName, requireScalarPrimaryKey, targetRelationPkSql, } from "./primary-key.js";
|
|
9
|
+
import { columnByTsName, columnsByTsNames, getOrSetSqlCache, getTableIndex, requireTsColumn, } from "./table-index.js";
|
|
10
|
+
export function colByTs(table, tsName, manifestIndex) {
|
|
11
|
+
return columnByTsName(getTableIndex(manifestIndex, table.accessor), table, tsName);
|
|
12
|
+
}
|
|
13
|
+
function compiledResult(sql, params, nextParamIndex, impossible = false) {
|
|
14
|
+
return {
|
|
15
|
+
sql,
|
|
16
|
+
params,
|
|
17
|
+
nextParamIndex,
|
|
18
|
+
...(impossible ? { impossible: true } : {}),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const PARAMLESS_OPERATORS = new Set(["isNull", "isNotNull"]);
|
|
22
|
+
export function isOperatorObject(value) {
|
|
23
|
+
return (typeof value === "object" &&
|
|
24
|
+
value !== null &&
|
|
25
|
+
!Array.isArray(value) &&
|
|
26
|
+
!(value instanceof Date));
|
|
27
|
+
}
|
|
28
|
+
const operatorParamTransform = {
|
|
29
|
+
contains: (v) => `%${String(v)}%`,
|
|
30
|
+
startsWith: (v) => `${String(v)}%`,
|
|
31
|
+
endsWith: (v) => `%${String(v)}`,
|
|
32
|
+
};
|
|
33
|
+
function parseQueryMode(value) {
|
|
34
|
+
if (value === undefined || value === "default")
|
|
35
|
+
return "default";
|
|
36
|
+
if (value === "insensitive")
|
|
37
|
+
return "insensitive";
|
|
38
|
+
compileError(`unsupported query mode: ${String(value)}`);
|
|
39
|
+
}
|
|
40
|
+
function isStringPatternOp(op) {
|
|
41
|
+
return (op === "equals" ||
|
|
42
|
+
op === "contains" ||
|
|
43
|
+
op === "startsWith" ||
|
|
44
|
+
op === "endsWith" ||
|
|
45
|
+
op === "search");
|
|
46
|
+
}
|
|
47
|
+
function stringFilterSql(op, sqlCol, paramIndex, mode, dialect) {
|
|
48
|
+
switch (op) {
|
|
49
|
+
case "equals":
|
|
50
|
+
return mode === "insensitive"
|
|
51
|
+
? dialect.ilike(sqlCol, paramIndex)
|
|
52
|
+
: dialect.whereOperators.equals(sqlCol, paramIndex);
|
|
53
|
+
case "contains":
|
|
54
|
+
case "startsWith":
|
|
55
|
+
case "endsWith":
|
|
56
|
+
return mode === "insensitive"
|
|
57
|
+
? dialect.ilike(sqlCol, paramIndex)
|
|
58
|
+
: dialect.whereOperators[op](sqlCol, paramIndex);
|
|
59
|
+
case "search":
|
|
60
|
+
return dialect.regex(sqlCol, paramIndex, mode === "insensitive");
|
|
61
|
+
default: {
|
|
62
|
+
const _never = op;
|
|
63
|
+
compileError(`unsupported string filter: ${_never}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function pluginWhereOperators(col) {
|
|
68
|
+
if (col.kind === "fk")
|
|
69
|
+
return {};
|
|
70
|
+
return getColumnType(col.kind)?.whereOperators ?? {};
|
|
71
|
+
}
|
|
72
|
+
export function serializeColumnValue(col, value, dialect = postgresDialect) {
|
|
73
|
+
if (col.kind === "fk")
|
|
74
|
+
return value;
|
|
75
|
+
const plugin = getColumnType(col.kind);
|
|
76
|
+
if (plugin?.serializeValue) {
|
|
77
|
+
return plugin.serializeValue(col, value, dialect);
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
function defaultColumnRef(col) {
|
|
82
|
+
return quoteIdentifier(col.sqlName);
|
|
83
|
+
}
|
|
84
|
+
function qualifiedColumnRefForTable(table) {
|
|
85
|
+
return (col) => `${tableRef(table)}.${quoteIdentifier(col.sqlName)}`;
|
|
86
|
+
}
|
|
87
|
+
function parentPkRef(table) {
|
|
88
|
+
const pkSql = primaryKeySqlName(table);
|
|
89
|
+
return `${tableRef(table)}.${quoteIdentifier(pkSql)}`;
|
|
90
|
+
}
|
|
91
|
+
function knownWhereOperatorNames(col, dialect) {
|
|
92
|
+
return [
|
|
93
|
+
...Object.keys(dialect.whereOperators),
|
|
94
|
+
...Object.keys(pluginWhereOperators(col)),
|
|
95
|
+
];
|
|
96
|
+
}
|
|
97
|
+
function throwUnsupportedWhereOperator(op, col, operators) {
|
|
98
|
+
compileError(`unsupported where operator "${op}" on column "${col.tsName}"`, {
|
|
99
|
+
code: QueryErrorCode.invalid_args,
|
|
100
|
+
columnTsName: col.tsName,
|
|
101
|
+
suggestions: suggestWhereOperator(op, operators),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function compileColumnCondition(col, rawValue, dialect, paramIndex, columnRef) {
|
|
105
|
+
const sqlCol = columnRef(col);
|
|
106
|
+
const spatialOps = pluginWhereOperators(col);
|
|
107
|
+
const conditions = [];
|
|
108
|
+
const params = [];
|
|
109
|
+
let nextParamIndex = paramIndex;
|
|
110
|
+
let impossible = false;
|
|
111
|
+
if (rawValue === null) {
|
|
112
|
+
conditions.push(dialect.whereOperators.isNull(sqlCol, nextParamIndex));
|
|
113
|
+
return { sql: conditions.join(" AND "), params, nextParamIndex };
|
|
114
|
+
}
|
|
115
|
+
if (!isOperatorObject(rawValue) || Array.isArray(rawValue)) {
|
|
116
|
+
conditions.push(dialect.whereOperators.equals(sqlCol, nextParamIndex));
|
|
117
|
+
params.push(serializeColumnValue(col, rawValue, dialect));
|
|
118
|
+
nextParamIndex++;
|
|
119
|
+
return { sql: conditions.join(" AND "), params, nextParamIndex };
|
|
120
|
+
}
|
|
121
|
+
const queryMode = parseQueryMode(rawValue.mode);
|
|
122
|
+
const knownOps = knownWhereOperatorNames(col, dialect);
|
|
123
|
+
const knownOpSet = new Set(knownOps);
|
|
124
|
+
const operatorKeys = Object.keys(rawValue).filter((key) => key !== "mode");
|
|
125
|
+
const knownKeys = operatorKeys.filter((key) => knownOpSet.has(key));
|
|
126
|
+
const unknownKeys = operatorKeys.filter((key) => !knownOpSet.has(key));
|
|
127
|
+
if (knownKeys.length === 0) {
|
|
128
|
+
if (operatorKeys.length > 0) {
|
|
129
|
+
const jsonColumn = col.kind === "json" || col.kind === "jsonb";
|
|
130
|
+
const typo = unknownKeys.find((key) => didYouMean(key, knownOps).length > 0);
|
|
131
|
+
if (typo || !jsonColumn) {
|
|
132
|
+
const badOp = typo ?? unknownKeys[0];
|
|
133
|
+
if (badOp) {
|
|
134
|
+
throwUnsupportedWhereOperator(badOp, col, knownOps);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
conditions.push(dialect.whereOperators.equals(sqlCol, nextParamIndex));
|
|
139
|
+
params.push(rawValue);
|
|
140
|
+
nextParamIndex++;
|
|
141
|
+
return { sql: conditions.join(" AND "), params, nextParamIndex };
|
|
142
|
+
}
|
|
143
|
+
if (unknownKeys[0]) {
|
|
144
|
+
throwUnsupportedWhereOperator(unknownKeys[0], col, knownOps);
|
|
145
|
+
}
|
|
146
|
+
for (const [op, value] of Object.entries(rawValue)) {
|
|
147
|
+
if (op === "mode")
|
|
148
|
+
continue;
|
|
149
|
+
if (op in spatialOps) {
|
|
150
|
+
const operator = spatialOps[op];
|
|
151
|
+
if (!operator) {
|
|
152
|
+
throwUnsupportedWhereOperator(op, col, knownOps);
|
|
153
|
+
}
|
|
154
|
+
const compiled = operator.compile(sqlCol, value, col, nextParamIndex, dialect);
|
|
155
|
+
conditions.push(compiled.sql);
|
|
156
|
+
params.push(...compiled.params);
|
|
157
|
+
nextParamIndex += compiled.params.length;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (!(op in dialect.whereOperators)) {
|
|
161
|
+
throwUnsupportedWhereOperator(op, col, knownOps);
|
|
162
|
+
}
|
|
163
|
+
const operator = op;
|
|
164
|
+
if (PARAMLESS_OPERATORS.has(operator)) {
|
|
165
|
+
conditions.push(dialect.whereOperators[operator](sqlCol, nextParamIndex));
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (Array.isArray(value) && value.length === 0) {
|
|
169
|
+
if (operator === "in") {
|
|
170
|
+
conditions.push("1=0");
|
|
171
|
+
impossible = true;
|
|
172
|
+
}
|
|
173
|
+
else if (operator === "notIn") {
|
|
174
|
+
conditions.push("1=1");
|
|
175
|
+
}
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const transform = operatorParamTransform[operator];
|
|
179
|
+
const paramValue = operator === "in" || operator === "notIn"
|
|
180
|
+
? Array.isArray(value)
|
|
181
|
+
? value.map((item) => serializeColumnValue(col, item, dialect))
|
|
182
|
+
: value
|
|
183
|
+
: transform
|
|
184
|
+
? transform(value)
|
|
185
|
+
: serializeColumnValue(col, value, dialect);
|
|
186
|
+
conditions.push(isStringPatternOp(operator)
|
|
187
|
+
? stringFilterSql(operator, sqlCol, nextParamIndex, queryMode, dialect)
|
|
188
|
+
: dialect.whereOperators[operator](sqlCol, nextParamIndex));
|
|
189
|
+
params.push(paramValue);
|
|
190
|
+
nextParamIndex++;
|
|
191
|
+
}
|
|
192
|
+
return compiledResult(conditions.join(" AND "), params, nextParamIndex, impossible);
|
|
193
|
+
}
|
|
194
|
+
function compileExistsSubquery(existsSql, negate) {
|
|
195
|
+
return negate ? `NOT EXISTS (${existsSql})` : `EXISTS (${existsSql})`;
|
|
196
|
+
}
|
|
197
|
+
function compileRelationCondition(manifest, parentTable, relation, rawValue, dialect, paramIndex, manifestIndex) {
|
|
198
|
+
const m2m = findM2M(manifest, parentTable.accessor, relation.name);
|
|
199
|
+
const targetTable = manifest.tables[relation.targetAccessor];
|
|
200
|
+
if (!targetTable) {
|
|
201
|
+
return { sql: "", params: [], nextParamIndex: paramIndex };
|
|
202
|
+
}
|
|
203
|
+
const parentTableIndex = getTableIndex(manifestIndex, parentTable.accessor);
|
|
204
|
+
if (relation.cardinality === "one") {
|
|
205
|
+
if (!isOperatorObject(rawValue) || Array.isArray(rawValue)) {
|
|
206
|
+
compileError(`Relation filter "${relation.name}" must be a where object`, {
|
|
207
|
+
tableAccessor: parentTable.accessor,
|
|
208
|
+
tableSqlName: parentTable.sqlName,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const relAlias = "_rel";
|
|
212
|
+
const columnRef = (col) => `${quoteIdentifier(relAlias)}.${quoteIdentifier(col.sqlName)}`;
|
|
213
|
+
const nested = compileWhereNode(manifest, targetTable, rawValue, dialect, paramIndex, columnRef, manifestIndex);
|
|
214
|
+
const parentFkCol = columnByTsName(parentTableIndex, parentTable, relation.fkColumn);
|
|
215
|
+
const parentFkRef = parentFkCol
|
|
216
|
+
? `${tableRef(parentTable)}.${quoteIdentifier(parentFkCol.sqlName)}`
|
|
217
|
+
: `${tableRef(parentTable)}.${quoteIdentifier(relation.fkSqlColumn)}`;
|
|
218
|
+
const targetPkSql = targetRelationPkSql(targetTable, relation);
|
|
219
|
+
const joinCond = `${quoteIdentifier(relAlias)}.${quoteIdentifier(targetPkSql)} = ${parentFkRef}`;
|
|
220
|
+
const whereParts = [joinCond];
|
|
221
|
+
if (nested.sql)
|
|
222
|
+
whereParts.push(nested.sql);
|
|
223
|
+
const existsSql = `SELECT 1 FROM ${tableRef(targetTable)} AS ${quoteIdentifier(relAlias)} WHERE ${whereParts.join(" AND ")}`;
|
|
224
|
+
return compiledResult(compileExistsSubquery(existsSql, false), nested.params, nested.nextParamIndex, nested.impossible);
|
|
225
|
+
}
|
|
226
|
+
if (!isOperatorObject(rawValue) || Array.isArray(rawValue)) {
|
|
227
|
+
compileError(`Relation filter "${relation.name}" must be an object with some, every, or none`, {
|
|
228
|
+
tableAccessor: parentTable.accessor,
|
|
229
|
+
tableSqlName: parentTable.sqlName,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
const relationModes = ["some", "every", "none"];
|
|
233
|
+
const modes = relationModes.filter((key) => key in rawValue);
|
|
234
|
+
if (modes.length !== 1) {
|
|
235
|
+
const hintKey = Object.keys(rawValue)[0] ?? "some";
|
|
236
|
+
compileError(`Relation filter "${relation.name}" requires exactly one of some, every, or none`, {
|
|
237
|
+
tableAccessor: parentTable.accessor,
|
|
238
|
+
tableSqlName: parentTable.sqlName,
|
|
239
|
+
suggestions: suggestWhereOperator(hintKey, relationModes),
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
const mode = modes[0];
|
|
243
|
+
if (!mode) {
|
|
244
|
+
compileError(`Relation filter "${relation.name}" requires exactly one of some, every, or none`, {
|
|
245
|
+
tableAccessor: parentTable.accessor,
|
|
246
|
+
tableSqlName: parentTable.sqlName,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
const extraKeys = Object.keys(rawValue).filter((key) => key !== mode);
|
|
250
|
+
if (extraKeys[0]) {
|
|
251
|
+
compileError(`unsupported relation filter "${extraKeys[0]}" on "${relation.name}"`, {
|
|
252
|
+
tableAccessor: parentTable.accessor,
|
|
253
|
+
tableSqlName: parentTable.sqlName,
|
|
254
|
+
suggestions: suggestWhereOperator(extraKeys[0], relationModes),
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
const nestedWhere = rawValue[mode];
|
|
258
|
+
if (nestedWhere !== undefined && !isOperatorObject(nestedWhere)) {
|
|
259
|
+
compileError(`Relation filter "${relation.name}.${mode}" must be a where object`, {
|
|
260
|
+
tableAccessor: parentTable.accessor,
|
|
261
|
+
tableSqlName: parentTable.sqlName,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
const relAlias = "_rel";
|
|
265
|
+
const columnRef = (col) => `${quoteIdentifier(relAlias)}.${quoteIdentifier(col.sqlName)}`;
|
|
266
|
+
const nested = compileWhereNode(manifest, targetTable, (nestedWhere ?? {}), dialect, paramIndex, columnRef, manifestIndex);
|
|
267
|
+
let fromClause;
|
|
268
|
+
const joinParts = [];
|
|
269
|
+
if (m2m) {
|
|
270
|
+
const isLeft = m2m.leftAccessor === parentTable.accessor;
|
|
271
|
+
const throughTable = manifest.tables[m2m.throughAccessor];
|
|
272
|
+
if (!throughTable) {
|
|
273
|
+
return { sql: "", params: [], nextParamIndex: paramIndex };
|
|
274
|
+
}
|
|
275
|
+
const junctionAlias = "_jt";
|
|
276
|
+
const parentFkCol = isLeft ? m2m.leftFkColumn : m2m.rightFkColumn;
|
|
277
|
+
const targetFkCol = isLeft ? m2m.rightFkColumn : m2m.leftFkColumn;
|
|
278
|
+
const targetPkSql = targetRelationPkSql(targetTable);
|
|
279
|
+
fromClause = `${tableRef(throughTable)} AS ${quoteIdentifier(junctionAlias)} INNER JOIN ${tableRef(targetTable)} AS ${quoteIdentifier(relAlias)} ON ${quoteIdentifier(relAlias)}.${quoteIdentifier(targetPkSql)} = ${quoteIdentifier(junctionAlias)}.${quoteIdentifier(targetFkCol)}`;
|
|
280
|
+
joinParts.push(`${quoteIdentifier(junctionAlias)}.${quoteIdentifier(parentFkCol)} = ${parentPkRef(parentTable)}`);
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
fromClause = `${tableRef(targetTable)} AS ${quoteIdentifier(relAlias)}`;
|
|
284
|
+
joinParts.push(`${quoteIdentifier(relAlias)}.${quoteIdentifier(relation.fkSqlColumn)} = ${parentPkRef(parentTable)}`);
|
|
285
|
+
}
|
|
286
|
+
switch (mode) {
|
|
287
|
+
case "some":
|
|
288
|
+
case "none": {
|
|
289
|
+
const whereParts = nested.sql
|
|
290
|
+
? [...joinParts, nested.sql]
|
|
291
|
+
: joinParts;
|
|
292
|
+
const existsSql = `SELECT 1 FROM ${fromClause} WHERE ${whereParts.join(" AND ")}`;
|
|
293
|
+
return compiledResult(compileExistsSubquery(existsSql, mode === "none"), nested.params, nested.nextParamIndex, mode === "some" && nested.impossible);
|
|
294
|
+
}
|
|
295
|
+
case "every": {
|
|
296
|
+
const everyWhereParts = nested.sql
|
|
297
|
+
? [...joinParts, `NOT (${nested.sql})`]
|
|
298
|
+
: [...joinParts, "FALSE"];
|
|
299
|
+
const everySql = `SELECT 1 FROM ${fromClause} WHERE ${everyWhereParts.join(" AND ")}`;
|
|
300
|
+
return {
|
|
301
|
+
sql: compileExistsSubquery(everySql, true),
|
|
302
|
+
params: nested.params,
|
|
303
|
+
nextParamIndex: nested.nextParamIndex,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
default: {
|
|
307
|
+
const _never = mode;
|
|
308
|
+
compileError(`unsupported relation filter "${String(_never)}" on "${relation.name}"`, {
|
|
309
|
+
tableAccessor: parentTable.accessor,
|
|
310
|
+
tableSqlName: parentTable.sqlName,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function combinatorTableContext(table) {
|
|
316
|
+
return {
|
|
317
|
+
tableAccessor: table.accessor,
|
|
318
|
+
tableSqlName: table.sqlName,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function compileLogicalCombinator(combinator, value, manifest, table, dialect, startParamIndex, columnRef, manifestIndex) {
|
|
322
|
+
if (!Array.isArray(value)) {
|
|
323
|
+
compileError(`${combinator} must be an array of where objects`, {
|
|
324
|
+
...combinatorTableContext(table),
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
if (value.length === 0) {
|
|
328
|
+
return compiledResult(combinator === "OR" ? "1=0" : "1=1", [], startParamIndex, combinator === "OR");
|
|
329
|
+
}
|
|
330
|
+
const parts = [];
|
|
331
|
+
const params = [];
|
|
332
|
+
let paramIndex = startParamIndex;
|
|
333
|
+
const childImpossible = [];
|
|
334
|
+
for (const item of value) {
|
|
335
|
+
if (!isOperatorObject(item)) {
|
|
336
|
+
compileError(`${combinator} items must be where objects`, {
|
|
337
|
+
...combinatorTableContext(table),
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
const compiled = compileWhereNode(manifest, table, item, dialect, paramIndex, columnRef, manifestIndex);
|
|
341
|
+
parts.push(`(${compiled.sql || "1=1"})`);
|
|
342
|
+
params.push(...compiled.params);
|
|
343
|
+
paramIndex = compiled.nextParamIndex;
|
|
344
|
+
childImpossible.push(Boolean(compiled.impossible));
|
|
345
|
+
}
|
|
346
|
+
const joiner = combinator === "OR" ? " OR " : " AND ";
|
|
347
|
+
const impossible = combinator === "OR"
|
|
348
|
+
? childImpossible.every(Boolean)
|
|
349
|
+
: childImpossible.some(Boolean);
|
|
350
|
+
return compiledResult(`(${parts.join(joiner)})`, params, paramIndex, impossible);
|
|
351
|
+
}
|
|
352
|
+
function compileWhereNode(manifest, table, where, dialect, startParamIndex, columnRef = defaultColumnRef, manifestIndex) {
|
|
353
|
+
const conditions = [];
|
|
354
|
+
const params = [];
|
|
355
|
+
let paramIndex = startParamIndex;
|
|
356
|
+
let impossible = false;
|
|
357
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
358
|
+
const relations = tableIndex?.effectiveRelationsByName ??
|
|
359
|
+
new Map(effectiveRelations(manifest, table).map((rel) => [rel.name, rel]));
|
|
360
|
+
for (const [key, value] of Object.entries(where)) {
|
|
361
|
+
if (value === undefined)
|
|
362
|
+
continue;
|
|
363
|
+
if (key === "AND" || key === "OR") {
|
|
364
|
+
const compiled = compileLogicalCombinator(key, value, manifest, table, dialect, paramIndex, columnRef, manifestIndex);
|
|
365
|
+
conditions.push(compiled.sql);
|
|
366
|
+
params.push(...compiled.params);
|
|
367
|
+
paramIndex = compiled.nextParamIndex;
|
|
368
|
+
if (compiled.impossible)
|
|
369
|
+
impossible = true;
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (key === "NOT") {
|
|
373
|
+
if (!isOperatorObject(value)) {
|
|
374
|
+
compileError("NOT must be a where object", {
|
|
375
|
+
...combinatorTableContext(table),
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
const compiled = compileWhereNode(manifest, table, value, dialect, paramIndex, columnRef, manifestIndex);
|
|
379
|
+
conditions.push(`NOT (${compiled.sql || "1=1"})`);
|
|
380
|
+
params.push(...compiled.params);
|
|
381
|
+
paramIndex = compiled.nextParamIndex;
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
const relation = relations.get(key);
|
|
385
|
+
if (relation) {
|
|
386
|
+
const compiled = compileRelationCondition(manifest, table, relation, value, dialect, paramIndex, manifestIndex);
|
|
387
|
+
if (compiled.sql)
|
|
388
|
+
conditions.push(compiled.sql);
|
|
389
|
+
params.push(...compiled.params);
|
|
390
|
+
paramIndex = compiled.nextParamIndex;
|
|
391
|
+
if (compiled.impossible)
|
|
392
|
+
impossible = true;
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const col = requireTsColumn(tableIndex, table, key, "where", "select");
|
|
396
|
+
const compiled = compileColumnCondition(col, value, dialect, paramIndex, columnRef);
|
|
397
|
+
if (compiled.sql)
|
|
398
|
+
conditions.push(compiled.sql);
|
|
399
|
+
params.push(...compiled.params);
|
|
400
|
+
paramIndex = compiled.nextParamIndex;
|
|
401
|
+
if (compiled.impossible)
|
|
402
|
+
impossible = true;
|
|
403
|
+
}
|
|
404
|
+
return compiledResult(conditions.join(" AND "), params, paramIndex, impossible);
|
|
405
|
+
}
|
|
406
|
+
export function compileWhere(manifest, table, where, dialect, startParamIndex = 1, manifestIndex, qualifyColumns = false, tableAlias) {
|
|
407
|
+
if (!where || Object.keys(where).length === 0) {
|
|
408
|
+
return { sql: "", params: [] };
|
|
409
|
+
}
|
|
410
|
+
const columnRef = tableAlias
|
|
411
|
+
? (col) => `${quoteIdentifier(tableAlias)}.${quoteIdentifier(col.sqlName)}`
|
|
412
|
+
: qualifyColumns
|
|
413
|
+
? qualifiedColumnRefForTable(table)
|
|
414
|
+
: defaultColumnRef;
|
|
415
|
+
const result = compileWhereNode(manifest, table, where, dialect, startParamIndex, columnRef, manifestIndex);
|
|
416
|
+
return {
|
|
417
|
+
sql: result.sql ? `WHERE ${result.sql}` : "",
|
|
418
|
+
params: result.params,
|
|
419
|
+
...(result.impossible ? { impossible: true } : {}),
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function logicalShapeKey(combinator, value) {
|
|
423
|
+
if (value.length === 0)
|
|
424
|
+
return `${combinator}:empty`;
|
|
425
|
+
return `${combinator}:${value
|
|
426
|
+
.filter((item) => !!item && typeof item === "object" && !Array.isArray(item))
|
|
427
|
+
.map((item) => whereShapeKey(item) || "{}")
|
|
428
|
+
.join(",")}`;
|
|
429
|
+
}
|
|
430
|
+
export function whereShapeKey(where) {
|
|
431
|
+
const parts = [];
|
|
432
|
+
for (const [key, value] of Object.entries(where)) {
|
|
433
|
+
if (key === "AND" && Array.isArray(value)) {
|
|
434
|
+
parts.push(logicalShapeKey("AND", value));
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (key === "OR" && Array.isArray(value)) {
|
|
438
|
+
parts.push(logicalShapeKey("OR", value));
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (key === "NOT" && isOperatorObject(value)) {
|
|
442
|
+
parts.push(`NOT:${whereShapeKey(value)}`);
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (isOperatorObject(value) && !(value instanceof Date)) {
|
|
446
|
+
const ops = Object.keys(value).sort();
|
|
447
|
+
if (ops.some((op) => op === "some" || op === "every" || op === "none")) {
|
|
448
|
+
const mode = ops.find((op) => op === "some" || op === "every" || op === "none");
|
|
449
|
+
const nested = value[mode ?? ""];
|
|
450
|
+
parts.push(`${key}:rel:${mode}:${isOperatorObject(nested) ? whereShapeKey(nested) : "{}"}`);
|
|
451
|
+
}
|
|
452
|
+
else if (ops.includes("in") || ops.includes("notIn")) {
|
|
453
|
+
const arr = value.in ?? value.notIn;
|
|
454
|
+
const len = Array.isArray(arr) ? arr.length : 0;
|
|
455
|
+
const op = ops.includes("in") ? "in" : "notIn";
|
|
456
|
+
parts.push(`${key}:${op}:${len}`);
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
const modePart = typeof value.mode === "string" ? `:mode:${value.mode}` : "";
|
|
460
|
+
const opsForKey = ops.filter((op) => op !== "mode");
|
|
461
|
+
parts.push(`${key}:${opsForKey.join(",")}${modePart}`);
|
|
462
|
+
}
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
parts.push(`${key}:eq`);
|
|
466
|
+
}
|
|
467
|
+
return parts.join("&");
|
|
468
|
+
}
|
|
469
|
+
function collectWhereParams(manifest, table, where, dialect, manifestIndex) {
|
|
470
|
+
const params = [];
|
|
471
|
+
function walk(node, columnRef) {
|
|
472
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
473
|
+
const relations = tableIndex?.effectiveRelationsByName ??
|
|
474
|
+
new Map(effectiveRelations(manifest, table).map((rel) => [
|
|
475
|
+
rel.name,
|
|
476
|
+
rel,
|
|
477
|
+
]));
|
|
478
|
+
for (const [key, value] of Object.entries(node)) {
|
|
479
|
+
if (key === "AND" || key === "OR") {
|
|
480
|
+
if (Array.isArray(value)) {
|
|
481
|
+
for (const item of value) {
|
|
482
|
+
if (item &&
|
|
483
|
+
typeof item === "object" &&
|
|
484
|
+
!Array.isArray(item)) {
|
|
485
|
+
walk(item, columnRef);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
if (key === "NOT" && isOperatorObject(value)) {
|
|
492
|
+
walk(value, columnRef);
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (relations.get(key)) {
|
|
496
|
+
const compiled = compileRelationCondition(manifest, table, relations.get(key), value, dialect, 1, manifestIndex);
|
|
497
|
+
params.push(...compiled.params);
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const col = columnByTsName(tableIndex, table, key);
|
|
501
|
+
if (!col)
|
|
502
|
+
continue;
|
|
503
|
+
const compiled = compileColumnCondition(col, value, dialect, 1, columnRef);
|
|
504
|
+
params.push(...compiled.params);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
walk(where, defaultColumnRef);
|
|
508
|
+
return params;
|
|
509
|
+
}
|
|
510
|
+
function whereValuesFingerprint(manifest, table, where, dialect, manifestIndex) {
|
|
511
|
+
return JSON.stringify(collectWhereParams(manifest, table, where, dialect, manifestIndex));
|
|
512
|
+
}
|
|
513
|
+
export function getCachedWhereClause(manifest, table, where, dialect, startParamIndex = 1, manifestIndex, qualifyColumns = false) {
|
|
514
|
+
if (!where || Object.keys(where).length === 0) {
|
|
515
|
+
return { sql: "", params: [] };
|
|
516
|
+
}
|
|
517
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
518
|
+
const shape = qualifyColumns
|
|
519
|
+
? `${whereShapeKey(where)}|qualified`
|
|
520
|
+
: whereShapeKey(where);
|
|
521
|
+
const shellCached = tableIndex?.whereClauseByShape.get(shape);
|
|
522
|
+
if (shellCached) {
|
|
523
|
+
return {
|
|
524
|
+
sql: shellCached.sql,
|
|
525
|
+
params: collectWhereParams(manifest, table, where, dialect, manifestIndex),
|
|
526
|
+
...(shellCached.impossible ? { impossible: true } : {}),
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
const fingerprint = whereValuesFingerprint(manifest, table, where, dialect, manifestIndex);
|
|
530
|
+
const cacheKey = `${shape}\0${fingerprint}`;
|
|
531
|
+
const cached = tableIndex?.whereClauseByFingerprint.get(cacheKey);
|
|
532
|
+
if (cached)
|
|
533
|
+
return cached;
|
|
534
|
+
const compiled = compileWhere(manifest, table, where, dialect, startParamIndex, manifestIndex, qualifyColumns);
|
|
535
|
+
tableIndex?.whereClauseByShape.set(shape, {
|
|
536
|
+
sql: compiled.sql,
|
|
537
|
+
...(compiled.impossible ? { impossible: true } : {}),
|
|
538
|
+
});
|
|
539
|
+
tableIndex?.whereClauseByFingerprint.set(cacheKey, compiled);
|
|
540
|
+
return compiled;
|
|
541
|
+
}
|
|
542
|
+
export function orderByShapeKey(orderBy, tableAlias) {
|
|
543
|
+
if (!orderBy || Object.keys(orderBy).length === 0)
|
|
544
|
+
return "";
|
|
545
|
+
const entries = Object.entries(orderBy)
|
|
546
|
+
.filter(([key]) => key !== "_count")
|
|
547
|
+
.map(([key, direction]) => typeof direction === "string"
|
|
548
|
+
? `${key}:${direction.toUpperCase()}`
|
|
549
|
+
: "")
|
|
550
|
+
.filter(Boolean);
|
|
551
|
+
if (entries.length === 0)
|
|
552
|
+
return "";
|
|
553
|
+
const base = entries.join("|");
|
|
554
|
+
return tableAlias ? `${base}|@${tableAlias}` : base;
|
|
555
|
+
}
|
|
556
|
+
export function getCachedOrderByClause(table, orderBy, tableAlias, manifestIndex) {
|
|
557
|
+
if (!orderBy || Object.keys(orderBy).length === 0)
|
|
558
|
+
return "";
|
|
559
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
560
|
+
const shape = orderByShapeKey(orderBy, tableAlias);
|
|
561
|
+
if (!shape)
|
|
562
|
+
return "";
|
|
563
|
+
if (!tableIndex) {
|
|
564
|
+
return compileOrderBy(table, orderBy, tableAlias, manifestIndex);
|
|
565
|
+
}
|
|
566
|
+
return getOrSetSqlCache(tableIndex.orderBySqlByShape, shape, () => compileOrderBy(table, orderBy, tableAlias, manifestIndex));
|
|
567
|
+
}
|
|
568
|
+
export function isImpossibleWhereSql(sql) {
|
|
569
|
+
if (!sql)
|
|
570
|
+
return false;
|
|
571
|
+
return /^1\s*=\s*0$/.test(sql.trim());
|
|
572
|
+
}
|
|
573
|
+
export function isImpossibleWhere(whereSql) {
|
|
574
|
+
if (!whereSql)
|
|
575
|
+
return false;
|
|
576
|
+
return isImpossibleWhereSql(whereSql.replace(/^WHERE\s+/i, ""));
|
|
577
|
+
}
|
|
578
|
+
export function compileOrderBy(table, orderBy, tableAlias, manifestIndex) {
|
|
579
|
+
if (!orderBy || Object.keys(orderBy).length === 0)
|
|
580
|
+
return "";
|
|
581
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
582
|
+
const prefix = tableAlias ? `${quoteIdentifier(tableAlias)}.` : "";
|
|
583
|
+
const parts = [];
|
|
584
|
+
for (const [tsKey, direction] of Object.entries(orderBy)) {
|
|
585
|
+
if (tsKey === "_count" || typeof direction !== "string")
|
|
586
|
+
continue;
|
|
587
|
+
const col = columnByTsName(tableIndex, table, tsKey);
|
|
588
|
+
if (!col)
|
|
589
|
+
continue;
|
|
590
|
+
const dir = direction.toUpperCase() === "DESC" ? "DESC" : "ASC";
|
|
591
|
+
parts.push(`${prefix}${quoteIdentifier(col.sqlName)} ${dir}`);
|
|
592
|
+
}
|
|
593
|
+
return parts.length > 0 ? `ORDER BY ${parts.join(", ")}` : "";
|
|
594
|
+
}
|
|
595
|
+
export function normalizeSelectColumns(select) {
|
|
596
|
+
if (!select)
|
|
597
|
+
return undefined;
|
|
598
|
+
if (Array.isArray(select))
|
|
599
|
+
return select;
|
|
600
|
+
return Object.entries(select)
|
|
601
|
+
.filter(([, enabled]) => enabled === true)
|
|
602
|
+
.map(([key]) => key);
|
|
603
|
+
}
|
|
604
|
+
/** Columns returned by default SELECT (omits `.hidden()` unless `includeHidden` or explicitly selected). */
|
|
605
|
+
export function columnsForOutput(tableIndex, table, select, includeHidden) {
|
|
606
|
+
if (select && select.length > 0) {
|
|
607
|
+
return columnsByTsNames(tableIndex, table, select);
|
|
608
|
+
}
|
|
609
|
+
if (includeHidden) {
|
|
610
|
+
return table.columns;
|
|
611
|
+
}
|
|
612
|
+
return table.columns.filter((col) => col.hidden !== true);
|
|
613
|
+
}
|
|
614
|
+
function aliasToTsName(expression, col) {
|
|
615
|
+
if (col.sqlName === col.tsName)
|
|
616
|
+
return expression;
|
|
617
|
+
return `${expression} AS ${quoteIdentifier(col.tsName)}`;
|
|
618
|
+
}
|
|
619
|
+
function selectExpression(col) {
|
|
620
|
+
if (col.kind === "fk") {
|
|
621
|
+
return aliasToTsName(quoteIdentifier(col.sqlName), col);
|
|
622
|
+
}
|
|
623
|
+
const plugin = getColumnType(col.kind);
|
|
624
|
+
if (plugin?.selectExpression) {
|
|
625
|
+
return aliasToTsName(plugin.selectExpression(col), col);
|
|
626
|
+
}
|
|
627
|
+
return aliasToTsName(quoteIdentifier(col.sqlName), col);
|
|
628
|
+
}
|
|
629
|
+
export function buildSelectColumns(table, select, manifestIndex, includeHidden, tableAlias) {
|
|
630
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
631
|
+
const cols = columnsForOutput(tableIndex, table, select, includeHidden);
|
|
632
|
+
const prefix = tableAlias ? `${quoteIdentifier(tableAlias)}.` : "";
|
|
633
|
+
return cols.map((c) => `${prefix}${selectExpression(c)}`).join(", ");
|
|
634
|
+
}
|
|
635
|
+
export function buildQualifiedSelectColumns(table, select, manifestIndex, includeHidden) {
|
|
636
|
+
const ref = tableRef(table);
|
|
637
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
638
|
+
const cols = columnsForOutput(tableIndex, table, select, includeHidden);
|
|
639
|
+
return cols.map((c) => `${ref}.${selectExpression(c)}`).join(", ");
|
|
640
|
+
}
|
|
641
|
+
export function buildFindByIdQuery(table, select, manifestIndex, includeHidden) {
|
|
642
|
+
const { sqlName } = requireScalarPrimaryKey(table);
|
|
643
|
+
const sqlCol = quoteIdentifier(sqlName);
|
|
644
|
+
const selectCols = buildSelectColumns(table, select, manifestIndex, includeHidden);
|
|
645
|
+
return `SELECT ${selectCols} FROM ${tableRef(table)} WHERE ${sqlCol} = $1`;
|
|
646
|
+
}
|
|
647
|
+
export function buildFindAllQuery(table) {
|
|
648
|
+
return `SELECT ${buildSelectColumns(table)} FROM ${tableRef(table)}`;
|
|
649
|
+
}
|
|
650
|
+
export function normalizeLimitOffset(value, label) {
|
|
651
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
|
|
652
|
+
compileError(`${label} must be a non-negative integer, got ${JSON.stringify(value)}`);
|
|
653
|
+
}
|
|
654
|
+
return value;
|
|
655
|
+
}
|
|
656
|
+
export function buildFindManyQuery(table, whereSql, orderSql, take, skip, distinctOn, extraSelectCols, joinClauses, manifestIndex, groupBySql, select, includeHidden) {
|
|
657
|
+
const hasJoins = Boolean(joinClauses && joinClauses.length > 0);
|
|
658
|
+
const tableIndex = getTableIndex(manifestIndex, table.accessor);
|
|
659
|
+
const selectCols = hasJoins
|
|
660
|
+
? buildQualifiedSelectColumns(table, select, manifestIndex, includeHidden)
|
|
661
|
+
: buildSelectColumns(table, select, manifestIndex, includeHidden);
|
|
662
|
+
let sql = "SELECT ";
|
|
663
|
+
if (distinctOn && distinctOn.length > 0) {
|
|
664
|
+
const distinctCols = columnsByTsNames(tableIndex, table, distinctOn)
|
|
665
|
+
.map((col) => hasJoins
|
|
666
|
+
? `${tableRef(table)}.${quoteIdentifier(col.sqlName)}`
|
|
667
|
+
: quoteIdentifier(col.sqlName))
|
|
668
|
+
.join(", ");
|
|
669
|
+
sql += `DISTINCT ON (${distinctCols}) `;
|
|
670
|
+
}
|
|
671
|
+
sql += selectCols;
|
|
672
|
+
if (extraSelectCols && extraSelectCols.length > 0) {
|
|
673
|
+
sql += `, ${extraSelectCols.join(", ")}`;
|
|
674
|
+
}
|
|
675
|
+
sql += ` FROM ${tableRef(table)}`;
|
|
676
|
+
if (joinClauses && joinClauses.length > 0) {
|
|
677
|
+
sql += ` ${joinClauses.join(" ")}`;
|
|
678
|
+
}
|
|
679
|
+
if (whereSql)
|
|
680
|
+
sql += ` ${whereSql}`;
|
|
681
|
+
if (groupBySql)
|
|
682
|
+
sql += ` ${groupBySql}`;
|
|
683
|
+
if (orderSql)
|
|
684
|
+
sql += ` ${orderSql}`;
|
|
685
|
+
if (take !== undefined) {
|
|
686
|
+
sql += ` LIMIT ${normalizeLimitOffset(take, "take")}`;
|
|
687
|
+
}
|
|
688
|
+
if (skip !== undefined) {
|
|
689
|
+
sql += ` OFFSET ${normalizeLimitOffset(skip, "skip")}`;
|
|
690
|
+
}
|
|
691
|
+
return sql;
|
|
692
|
+
}
|
|
693
|
+
export function buildPaginateQuery(table, whereSql, orderSql, take, extraSelectCols, joinClauses, manifestIndex, select, includeHidden) {
|
|
694
|
+
return buildFindManyQuery(table, whereSql, orderSql, take + 1, undefined, undefined, extraSelectCols, joinClauses, manifestIndex, undefined, select, includeHidden);
|
|
695
|
+
}
|
|
696
|
+
export function buildExistsQuery(table, whereSql) {
|
|
697
|
+
let sql = `SELECT 1 FROM ${tableRef(table)}`;
|
|
698
|
+
if (whereSql)
|
|
699
|
+
sql += ` ${whereSql}`;
|
|
700
|
+
sql += " LIMIT 1";
|
|
701
|
+
return sql;
|
|
702
|
+
}
|
|
703
|
+
export function getCachedFindManyQuery(tableIndex, signature, build) {
|
|
704
|
+
if (!tableIndex)
|
|
705
|
+
return build();
|
|
706
|
+
return getOrSetSqlCache(tableIndex.findManySqlBySignature, signature, build);
|
|
707
|
+
}
|
|
708
|
+
//# sourceMappingURL=compile-where.js.map
|