@snowtop/ent 0.1.0-alpha160-test4 → 0.1.0-alpha160-test6
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/dist/package.json +64 -0
- package/{scripts → dist/scripts}/custom_compiler.js +0 -0
- package/{scripts → dist/scripts}/custom_graphql.js +0 -0
- package/package.json +48 -6
- package/src/action/action.ts +330 -0
- package/src/action/executor.ts +453 -0
- package/src/action/experimental_action.ts +277 -0
- package/src/action/index.ts +31 -0
- package/src/action/operations.ts +967 -0
- package/src/action/orchestrator.ts +1527 -0
- package/src/action/privacy.ts +37 -0
- package/src/action/relative_value.ts +242 -0
- package/src/action/transaction.ts +38 -0
- package/src/auth/auth.ts +77 -0
- package/src/auth/index.ts +8 -0
- package/src/core/base.ts +367 -0
- package/src/core/clause.ts +1065 -0
- package/src/core/config.ts +219 -0
- package/src/core/const.ts +5 -0
- package/src/core/context.ts +135 -0
- package/src/core/convert.ts +106 -0
- package/src/core/date.ts +23 -0
- package/src/core/db.ts +498 -0
- package/src/core/ent.ts +1740 -0
- package/src/core/global_schema.ts +49 -0
- package/src/core/loaders/assoc_count_loader.ts +99 -0
- package/src/core/loaders/assoc_edge_loader.ts +250 -0
- package/src/core/loaders/index.ts +12 -0
- package/src/core/loaders/loader.ts +66 -0
- package/src/core/loaders/object_loader.ts +489 -0
- package/src/core/loaders/query_loader.ts +314 -0
- package/src/core/loaders/raw_count_loader.ts +175 -0
- package/src/core/logger.ts +49 -0
- package/src/core/privacy.ts +660 -0
- package/src/core/query/assoc_query.ts +240 -0
- package/src/core/query/custom_clause_query.ts +174 -0
- package/src/core/query/custom_query.ts +302 -0
- package/src/core/query/index.ts +9 -0
- package/src/core/query/query.ts +674 -0
- package/src/core/query_impl.ts +32 -0
- package/src/core/viewer.ts +52 -0
- package/src/ent.code-workspace +73 -0
- package/src/graphql/builtins/connection.ts +25 -0
- package/src/graphql/builtins/edge.ts +16 -0
- package/src/graphql/builtins/node.ts +12 -0
- package/src/graphql/graphql.ts +891 -0
- package/src/graphql/graphql_field_helpers.ts +221 -0
- package/src/graphql/index.ts +42 -0
- package/src/graphql/mutations/union.ts +39 -0
- package/src/graphql/node_resolver.ts +122 -0
- package/src/graphql/query/connection_type.ts +113 -0
- package/src/graphql/query/edge_connection.ts +171 -0
- package/src/graphql/query/page_info.ts +34 -0
- package/src/graphql/query/shared_edge_connection.ts +287 -0
- package/src/graphql/scalars/orderby_direction.ts +13 -0
- package/src/graphql/scalars/time.ts +38 -0
- package/src/imports/dataz/example1/_auth.ts +51 -0
- package/src/imports/dataz/example1/_viewer.ts +35 -0
- package/src/imports/index.ts +213 -0
- package/src/index.ts +145 -0
- package/src/parse_schema/parse.ts +585 -0
- package/src/schema/base_schema.ts +224 -0
- package/src/schema/field.ts +1087 -0
- package/src/schema/index.ts +53 -0
- package/src/schema/json_field.ts +94 -0
- package/src/schema/schema.ts +1028 -0
- package/src/schema/struct_field.ts +234 -0
- package/src/schema/union_field.ts +105 -0
- package/src/scripts/custom_compiler.ts +331 -0
- package/src/scripts/custom_graphql.ts +550 -0
- package/src/scripts/migrate_v0.1.ts +41 -0
- package/src/scripts/move_types.ts +131 -0
- package/src/scripts/read_schema.ts +67 -0
- package/src/setupPackage.js +42 -0
- package/src/testutils/action/complex_schemas.ts +517 -0
- package/src/testutils/builder.ts +422 -0
- package/src/testutils/context/test_context.ts +25 -0
- package/src/testutils/db/fixture.ts +32 -0
- package/src/testutils/db/temp_db.ts +941 -0
- package/src/testutils/db/value.ts +294 -0
- package/src/testutils/db_mock.ts +351 -0
- package/src/testutils/db_time_zone.ts +40 -0
- package/src/testutils/ent-graphql-tests/index.ts +653 -0
- package/src/testutils/fake_comms.ts +50 -0
- package/src/testutils/fake_data/const.ts +64 -0
- package/src/testutils/fake_data/events_query.ts +145 -0
- package/src/testutils/fake_data/fake_contact.ts +150 -0
- package/src/testutils/fake_data/fake_event.ts +150 -0
- package/src/testutils/fake_data/fake_tag.ts +139 -0
- package/src/testutils/fake_data/fake_user.ts +232 -0
- package/src/testutils/fake_data/index.ts +1 -0
- package/src/testutils/fake_data/internal.ts +8 -0
- package/src/testutils/fake_data/tag_query.ts +56 -0
- package/src/testutils/fake_data/test_helpers.ts +388 -0
- package/src/testutils/fake_data/user_query.ts +524 -0
- package/src/testutils/fake_log.ts +52 -0
- package/src/testutils/mock_date.ts +10 -0
- package/src/testutils/mock_log.ts +39 -0
- package/src/testutils/parse_sql.ts +685 -0
- package/src/testutils/test_edge_global_schema.ts +49 -0
- package/src/testutils/write.ts +70 -0
- package/src/tsc/ast.ts +351 -0
- package/src/tsc/compilerOptions.ts +85 -0
- package/src/tsc/move_generated.ts +191 -0
- package/src/tsc/transform.ts +226 -0
- package/src/tsc/transform_action.ts +224 -0
- package/src/tsc/transform_ent.ts +66 -0
- package/src/tsc/transform_schema.ts +546 -0
- package/tsconfig.json +20 -0
- package/core/query/shared_assoc_test.d.ts +0 -2
- package/core/query/shared_assoc_test.js +0 -804
- package/core/query/shared_test.d.ts +0 -21
- package/core/query/shared_test.js +0 -736
- package/graphql/query/shared_assoc_test.d.ts +0 -1
- package/graphql/query/shared_assoc_test.js +0 -203
- /package/{action → dist/action}/action.d.ts +0 -0
- /package/{action → dist/action}/action.js +0 -0
- /package/{action → dist/action}/executor.d.ts +0 -0
- /package/{action → dist/action}/executor.js +0 -0
- /package/{action → dist/action}/experimental_action.d.ts +0 -0
- /package/{action → dist/action}/experimental_action.js +0 -0
- /package/{action → dist/action}/index.d.ts +0 -0
- /package/{action → dist/action}/index.js +0 -0
- /package/{action → dist/action}/operations.d.ts +0 -0
- /package/{action → dist/action}/operations.js +0 -0
- /package/{action → dist/action}/orchestrator.d.ts +0 -0
- /package/{action → dist/action}/orchestrator.js +0 -0
- /package/{action → dist/action}/privacy.d.ts +0 -0
- /package/{action → dist/action}/privacy.js +0 -0
- /package/{action → dist/action}/relative_value.d.ts +0 -0
- /package/{action → dist/action}/relative_value.js +0 -0
- /package/{action → dist/action}/transaction.d.ts +0 -0
- /package/{action → dist/action}/transaction.js +0 -0
- /package/{auth → dist/auth}/auth.d.ts +0 -0
- /package/{auth → dist/auth}/auth.js +0 -0
- /package/{auth → dist/auth}/index.d.ts +0 -0
- /package/{auth → dist/auth}/index.js +0 -0
- /package/{core → dist/core}/base.d.ts +0 -0
- /package/{core → dist/core}/base.js +0 -0
- /package/{core → dist/core}/clause.d.ts +0 -0
- /package/{core → dist/core}/clause.js +0 -0
- /package/{core → dist/core}/config.d.ts +0 -0
- /package/{core → dist/core}/config.js +0 -0
- /package/{core → dist/core}/const.d.ts +0 -0
- /package/{core → dist/core}/const.js +0 -0
- /package/{core → dist/core}/context.d.ts +0 -0
- /package/{core → dist/core}/context.js +0 -0
- /package/{core → dist/core}/convert.d.ts +0 -0
- /package/{core → dist/core}/convert.js +0 -0
- /package/{core → dist/core}/date.d.ts +0 -0
- /package/{core → dist/core}/date.js +0 -0
- /package/{core → dist/core}/db.d.ts +0 -0
- /package/{core → dist/core}/db.js +0 -0
- /package/{core → dist/core}/ent.d.ts +0 -0
- /package/{core → dist/core}/ent.js +0 -0
- /package/{core → dist/core}/global_schema.d.ts +0 -0
- /package/{core → dist/core}/global_schema.js +0 -0
- /package/{core → dist/core}/loaders/assoc_count_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/assoc_count_loader.js +0 -0
- /package/{core → dist/core}/loaders/assoc_edge_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/assoc_edge_loader.js +0 -0
- /package/{core → dist/core}/loaders/index.d.ts +0 -0
- /package/{core → dist/core}/loaders/index.js +0 -0
- /package/{core → dist/core}/loaders/loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/loader.js +0 -0
- /package/{core → dist/core}/loaders/object_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/object_loader.js +0 -0
- /package/{core → dist/core}/loaders/query_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/query_loader.js +0 -0
- /package/{core → dist/core}/loaders/raw_count_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/raw_count_loader.js +0 -0
- /package/{core → dist/core}/logger.d.ts +0 -0
- /package/{core → dist/core}/logger.js +0 -0
- /package/{core → dist/core}/privacy.d.ts +0 -0
- /package/{core → dist/core}/privacy.js +0 -0
- /package/{core → dist/core}/query/assoc_query.d.ts +0 -0
- /package/{core → dist/core}/query/assoc_query.js +0 -0
- /package/{core → dist/core}/query/custom_clause_query.d.ts +0 -0
- /package/{core → dist/core}/query/custom_clause_query.js +0 -0
- /package/{core → dist/core}/query/custom_query.d.ts +0 -0
- /package/{core → dist/core}/query/custom_query.js +0 -0
- /package/{core → dist/core}/query/index.d.ts +0 -0
- /package/{core → dist/core}/query/index.js +0 -0
- /package/{core → dist/core}/query/query.d.ts +0 -0
- /package/{core → dist/core}/query/query.js +0 -0
- /package/{core → dist/core}/query_impl.d.ts +0 -0
- /package/{core → dist/core}/query_impl.js +0 -0
- /package/{core → dist/core}/viewer.d.ts +0 -0
- /package/{core → dist/core}/viewer.js +0 -0
- /package/{graphql → dist/graphql}/builtins/connection.d.ts +0 -0
- /package/{graphql → dist/graphql}/builtins/connection.js +0 -0
- /package/{graphql → dist/graphql}/builtins/edge.d.ts +0 -0
- /package/{graphql → dist/graphql}/builtins/edge.js +0 -0
- /package/{graphql → dist/graphql}/builtins/node.d.ts +0 -0
- /package/{graphql → dist/graphql}/builtins/node.js +0 -0
- /package/{graphql → dist/graphql}/graphql.d.ts +0 -0
- /package/{graphql → dist/graphql}/graphql.js +0 -0
- /package/{graphql → dist/graphql}/graphql_field_helpers.d.ts +0 -0
- /package/{graphql → dist/graphql}/graphql_field_helpers.js +0 -0
- /package/{graphql → dist/graphql}/index.d.ts +0 -0
- /package/{graphql → dist/graphql}/index.js +0 -0
- /package/{graphql → dist/graphql}/mutations/union.d.ts +0 -0
- /package/{graphql → dist/graphql}/mutations/union.js +0 -0
- /package/{graphql → dist/graphql}/node_resolver.d.ts +0 -0
- /package/{graphql → dist/graphql}/node_resolver.js +0 -0
- /package/{graphql → dist/graphql}/query/connection_type.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/connection_type.js +0 -0
- /package/{graphql → dist/graphql}/query/edge_connection.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/edge_connection.js +0 -0
- /package/{graphql → dist/graphql}/query/page_info.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/page_info.js +0 -0
- /package/{graphql → dist/graphql}/query/shared_edge_connection.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/shared_edge_connection.js +0 -0
- /package/{graphql → dist/graphql}/scalars/orderby_direction.d.ts +0 -0
- /package/{graphql → dist/graphql}/scalars/orderby_direction.js +0 -0
- /package/{graphql → dist/graphql}/scalars/time.d.ts +0 -0
- /package/{graphql → dist/graphql}/scalars/time.js +0 -0
- /package/{imports → dist/imports}/dataz/example1/_auth.d.ts +0 -0
- /package/{imports → dist/imports}/dataz/example1/_auth.js +0 -0
- /package/{imports → dist/imports}/dataz/example1/_viewer.d.ts +0 -0
- /package/{imports → dist/imports}/dataz/example1/_viewer.js +0 -0
- /package/{imports → dist/imports}/index.d.ts +0 -0
- /package/{imports → dist/imports}/index.js +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{parse_schema → dist/parse_schema}/parse.d.ts +0 -0
- /package/{parse_schema → dist/parse_schema}/parse.js +0 -0
- /package/{schema → dist/schema}/base_schema.d.ts +0 -0
- /package/{schema → dist/schema}/base_schema.js +0 -0
- /package/{schema → dist/schema}/field.d.ts +0 -0
- /package/{schema → dist/schema}/field.js +0 -0
- /package/{schema → dist/schema}/index.d.ts +0 -0
- /package/{schema → dist/schema}/index.js +0 -0
- /package/{schema → dist/schema}/json_field.d.ts +0 -0
- /package/{schema → dist/schema}/json_field.js +0 -0
- /package/{schema → dist/schema}/schema.d.ts +0 -0
- /package/{schema → dist/schema}/schema.js +0 -0
- /package/{schema → dist/schema}/struct_field.d.ts +0 -0
- /package/{schema → dist/schema}/struct_field.js +0 -0
- /package/{schema → dist/schema}/union_field.d.ts +0 -0
- /package/{schema → dist/schema}/union_field.js +0 -0
- /package/{scripts → dist/scripts}/custom_compiler.d.ts +0 -0
- /package/{scripts → dist/scripts}/custom_graphql.d.ts +0 -0
- /package/{scripts → dist/scripts}/migrate_v0.1.d.ts +0 -0
- /package/{scripts → dist/scripts}/migrate_v0.1.js +0 -0
- /package/{scripts → dist/scripts}/move_types.d.ts +0 -0
- /package/{scripts → dist/scripts}/move_types.js +0 -0
- /package/{scripts → dist/scripts}/read_schema.d.ts +0 -0
- /package/{scripts → dist/scripts}/read_schema.js +0 -0
- /package/{testutils → dist/testutils}/action/complex_schemas.d.ts +0 -0
- /package/{testutils → dist/testutils}/action/complex_schemas.js +0 -0
- /package/{testutils → dist/testutils}/builder.d.ts +0 -0
- /package/{testutils → dist/testutils}/builder.js +0 -0
- /package/{testutils → dist/testutils}/context/test_context.d.ts +0 -0
- /package/{testutils → dist/testutils}/context/test_context.js +0 -0
- /package/{testutils → dist/testutils}/db/fixture.d.ts +0 -0
- /package/{testutils → dist/testutils}/db/fixture.js +0 -0
- /package/{testutils → dist/testutils}/db/temp_db.d.ts +0 -0
- /package/{testutils → dist/testutils}/db/temp_db.js +0 -0
- /package/{testutils → dist/testutils}/db/value.d.ts +0 -0
- /package/{testutils → dist/testutils}/db/value.js +0 -0
- /package/{testutils → dist/testutils}/db_mock.d.ts +0 -0
- /package/{testutils → dist/testutils}/db_mock.js +0 -0
- /package/{testutils → dist/testutils}/db_time_zone.d.ts +0 -0
- /package/{testutils → dist/testutils}/db_time_zone.js +0 -0
- /package/{testutils → dist/testutils}/ent-graphql-tests/index.d.ts +0 -0
- /package/{testutils → dist/testutils}/ent-graphql-tests/index.js +0 -0
- /package/{testutils → dist/testutils}/fake_comms.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_comms.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/const.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/const.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/events_query.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/events_query.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_contact.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_contact.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_event.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_event.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_tag.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_tag.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_user.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_user.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/index.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/index.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/internal.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/internal.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/tag_query.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/tag_query.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/test_helpers.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/test_helpers.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/user_query.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/user_query.js +0 -0
- /package/{testutils → dist/testutils}/fake_log.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_log.js +0 -0
- /package/{testutils → dist/testutils}/mock_date.d.ts +0 -0
- /package/{testutils → dist/testutils}/mock_date.js +0 -0
- /package/{testutils → dist/testutils}/mock_log.d.ts +0 -0
- /package/{testutils → dist/testutils}/mock_log.js +0 -0
- /package/{testutils → dist/testutils}/parse_sql.d.ts +0 -0
- /package/{testutils → dist/testutils}/parse_sql.js +0 -0
- /package/{testutils → dist/testutils}/test_edge_global_schema.d.ts +0 -0
- /package/{testutils → dist/testutils}/test_edge_global_schema.js +0 -0
- /package/{testutils → dist/testutils}/write.d.ts +0 -0
- /package/{testutils → dist/testutils}/write.js +0 -0
- /package/{tsc → dist/tsc}/ast.d.ts +0 -0
- /package/{tsc → dist/tsc}/ast.js +0 -0
- /package/{tsc → dist/tsc}/compilerOptions.d.ts +0 -0
- /package/{tsc → dist/tsc}/compilerOptions.js +0 -0
- /package/{tsc → dist/tsc}/move_generated.d.ts +0 -0
- /package/{tsc → dist/tsc}/move_generated.js +0 -0
- /package/{tsc → dist/tsc}/transform.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform.js +0 -0
- /package/{tsc → dist/tsc}/transform_action.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform_action.js +0 -0
- /package/{tsc → dist/tsc}/transform_ent.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform_ent.js +0 -0
- /package/{tsc → dist/tsc}/transform_schema.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform_schema.js +0 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
import DataLoader from "dataloader";
|
|
2
|
+
import {
|
|
3
|
+
LoadRowOptions,
|
|
4
|
+
ID,
|
|
5
|
+
Data,
|
|
6
|
+
SelectDataOptions,
|
|
7
|
+
Context,
|
|
8
|
+
Loader,
|
|
9
|
+
LoaderFactory,
|
|
10
|
+
PrimableLoader,
|
|
11
|
+
DataOptions,
|
|
12
|
+
} from "../base";
|
|
13
|
+
import { loadRow, loadRows } from "../ent";
|
|
14
|
+
import * as clause from "../clause";
|
|
15
|
+
import { log, logEnabled } from "../logger";
|
|
16
|
+
import { getCombinedClause } from "../clause";
|
|
17
|
+
|
|
18
|
+
import { getLoader, CacheMap, getCustomLoader } from "./loader";
|
|
19
|
+
import memoizee from "memoizee";
|
|
20
|
+
|
|
21
|
+
async function loadRowsForIDLoader<K, V = Data>(
|
|
22
|
+
options: SelectDataOptions,
|
|
23
|
+
ids: K[],
|
|
24
|
+
context?: Context,
|
|
25
|
+
) {
|
|
26
|
+
let col = options.key;
|
|
27
|
+
const cls = getCombinedClause(
|
|
28
|
+
options,
|
|
29
|
+
clause.DBTypeIn(col, ids, options.keyType || "uuid"),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
const rowOptions: LoadRowOptions = {
|
|
33
|
+
...options,
|
|
34
|
+
clause: cls,
|
|
35
|
+
context,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
let m = new Map<K, number>();
|
|
39
|
+
let result: (V | null)[] = [];
|
|
40
|
+
for (let i = 0; i < ids.length; i++) {
|
|
41
|
+
result.push(null);
|
|
42
|
+
// store the index....
|
|
43
|
+
m.set(ids[i], i);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const rows = (await loadRows(rowOptions)) as V[];
|
|
47
|
+
for (const row of rows) {
|
|
48
|
+
const id = row[col];
|
|
49
|
+
if (id === undefined) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`need to query for column ${col} when using an object loader because the query may not be sorted and we need the id to maintain sort order`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const idx = m.get(id);
|
|
55
|
+
if (idx === undefined) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`malformed query. got ${id} back but didn't query for it`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
result[idx] = row;
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function loadRowsForClauseLoader<
|
|
66
|
+
TQueryData extends Data = Data,
|
|
67
|
+
TResultData extends Data = TQueryData,
|
|
68
|
+
K = keyof TQueryData,
|
|
69
|
+
>(
|
|
70
|
+
options: SelectDataOptions,
|
|
71
|
+
clause: clause.Clause<TQueryData, K>,
|
|
72
|
+
): Promise<TResultData[]> {
|
|
73
|
+
const rowOptions: LoadRowOptions = {
|
|
74
|
+
...options,
|
|
75
|
+
// @ts-expect-error clause in LoadRowOptions doesn't take templatized version of Clause
|
|
76
|
+
clause: getCombinedClause(options, clause),
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return (await loadRows(rowOptions)) as TResultData[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function loadCountForClauseLoader<V extends Data = Data, K = keyof V>(
|
|
83
|
+
options: SelectDataOptions,
|
|
84
|
+
clause: clause.Clause<V, K>,
|
|
85
|
+
): Promise<number> {
|
|
86
|
+
const rowOptions: LoadRowOptions = {
|
|
87
|
+
...options,
|
|
88
|
+
// @ts-expect-error clause in LoadRowOptions doesn't take templatized version of Clause
|
|
89
|
+
clause: getCombinedClause(options, clause),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const row = await loadRow({
|
|
93
|
+
...rowOptions,
|
|
94
|
+
fields: ["count(*) as count"],
|
|
95
|
+
});
|
|
96
|
+
if (!row) {
|
|
97
|
+
return 0;
|
|
98
|
+
}
|
|
99
|
+
return parseInt(row.count, 10);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// optional clause...
|
|
103
|
+
// so ObjectLoaderFactory and createDataLoader need to take a new optional field which is a clause that's always added here
|
|
104
|
+
// and we need a disableTransform which skips loader completely and uses loadRow...
|
|
105
|
+
function createDataLoader(options: SelectDataOptions) {
|
|
106
|
+
const loaderOptions: DataLoader.Options<any, any> = {};
|
|
107
|
+
|
|
108
|
+
// if query logging is enabled, we should log what's happening with loader
|
|
109
|
+
if (logEnabled("query")) {
|
|
110
|
+
loaderOptions.cacheMap = new CacheMap(options);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return new DataLoader(async (ids: ID[]) => {
|
|
114
|
+
if (!ids.length) {
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// context not needed because we're creating a loader which has its own cache which is being used here
|
|
119
|
+
return loadRowsForIDLoader(options, ids);
|
|
120
|
+
}, loaderOptions);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
class clauseCacheMap {
|
|
124
|
+
private m = new Map();
|
|
125
|
+
|
|
126
|
+
constructor(private options: DataOptions, private count?: boolean) {}
|
|
127
|
+
|
|
128
|
+
get(key: clause.Clause) {
|
|
129
|
+
const key2 = key.instanceKey();
|
|
130
|
+
const ret = this.m.get(key2);
|
|
131
|
+
if (ret) {
|
|
132
|
+
log("cache", {
|
|
133
|
+
"dataloader-cache-hit": key2 + (this.count ? ":count" : ""),
|
|
134
|
+
"tableName": this.options.tableName,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return ret;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
set(key: clause.Clause, value: any) {
|
|
141
|
+
return this.m.set(key.instanceKey(), value);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
delete(key: clause.Clause) {
|
|
145
|
+
return this.m.delete(key.instanceKey());
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
clear() {
|
|
149
|
+
return this.m.clear();
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function createClauseDataLoder<
|
|
154
|
+
TQueryData extends Data = Data,
|
|
155
|
+
TResultData extends Data = TQueryData,
|
|
156
|
+
K = keyof TQueryData,
|
|
157
|
+
>(options: SelectDataOptions) {
|
|
158
|
+
return new DataLoader(
|
|
159
|
+
async (clauses: clause.Clause<TQueryData, K>[]) => {
|
|
160
|
+
if (!clauses.length) {
|
|
161
|
+
return [];
|
|
162
|
+
}
|
|
163
|
+
const ret: TResultData[][] = [];
|
|
164
|
+
for await (const clause of clauses) {
|
|
165
|
+
const data = await loadRowsForClauseLoader<TQueryData, TResultData, K>(
|
|
166
|
+
options,
|
|
167
|
+
clause,
|
|
168
|
+
);
|
|
169
|
+
ret.push(data);
|
|
170
|
+
}
|
|
171
|
+
return ret;
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
cacheMap: new clauseCacheMap(options),
|
|
175
|
+
},
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function createClauseCountDataLoader<V extends Data = Data, K = keyof V>(
|
|
180
|
+
options: SelectDataOptions,
|
|
181
|
+
) {
|
|
182
|
+
return new DataLoader(
|
|
183
|
+
async (clauses: clause.Clause<V, K>[]) => {
|
|
184
|
+
if (!clauses.length) {
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
const ret: number[] = [];
|
|
188
|
+
for await (const clause of clauses) {
|
|
189
|
+
const data = await loadCountForClauseLoader(options, clause);
|
|
190
|
+
ret.push(data);
|
|
191
|
+
}
|
|
192
|
+
return ret;
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
cacheMap: new clauseCacheMap(options, true),
|
|
196
|
+
},
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export class ObjectLoader<
|
|
201
|
+
TQueryData extends Data = Data,
|
|
202
|
+
TResultData extends Data = TQueryData,
|
|
203
|
+
K = keyof TQueryData,
|
|
204
|
+
> implements
|
|
205
|
+
Loader<ID, TResultData | null>,
|
|
206
|
+
Loader<clause.Clause<TQueryData, K>, TResultData[] | null>
|
|
207
|
+
{
|
|
208
|
+
private idLoader: DataLoader<ID, TResultData> | undefined;
|
|
209
|
+
private clauseLoader: DataLoader<
|
|
210
|
+
clause.Clause<TQueryData, K>,
|
|
211
|
+
TResultData[]
|
|
212
|
+
> | null;
|
|
213
|
+
|
|
214
|
+
private primedLoaders:
|
|
215
|
+
| Map<string, PrimableLoader<ID, TResultData | null>>
|
|
216
|
+
| undefined;
|
|
217
|
+
private memoizedInitPrime: () => void;
|
|
218
|
+
|
|
219
|
+
constructor(
|
|
220
|
+
private options: SelectDataOptions,
|
|
221
|
+
public context?: Context,
|
|
222
|
+
private toPrime?: ObjectLoaderFactory<TResultData>[],
|
|
223
|
+
) {
|
|
224
|
+
if (options.key === undefined) {
|
|
225
|
+
console.trace();
|
|
226
|
+
}
|
|
227
|
+
if (context) {
|
|
228
|
+
this.idLoader = createDataLoader(options);
|
|
229
|
+
this.clauseLoader = createClauseDataLoder(options);
|
|
230
|
+
}
|
|
231
|
+
this.memoizedInitPrime = memoizee(this.initPrime.bind(this));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
getOptions(): SelectDataOptions {
|
|
235
|
+
return this.options;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private initPrime() {
|
|
239
|
+
if (!this.context || !this.toPrime) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
let primedLoaders = new Map();
|
|
243
|
+
this.toPrime.forEach((prime) => {
|
|
244
|
+
const l2 = prime.createLoader(this.context);
|
|
245
|
+
if ((l2 as PrimableLoader<ID, TResultData | null>).prime === undefined) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
primedLoaders.set(prime.options.key, l2);
|
|
250
|
+
});
|
|
251
|
+
this.primedLoaders = primedLoaders;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async load(key: ID): Promise<TResultData | null>;
|
|
255
|
+
async load(key: clause.Clause<TQueryData, K>): Promise<TResultData[] | null>;
|
|
256
|
+
async load(
|
|
257
|
+
key: clause.Clause<TQueryData, K> | ID,
|
|
258
|
+
): Promise<TResultData | TResultData[] | null> {
|
|
259
|
+
if (typeof key === "string" || typeof key === "number") {
|
|
260
|
+
return this.loadID(key);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return this.loadClause(key);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
private async loadID(key: ID): Promise<TResultData | null> {
|
|
267
|
+
// simple case. we get parallelization etc
|
|
268
|
+
if (this.idLoader) {
|
|
269
|
+
this.memoizedInitPrime();
|
|
270
|
+
// prime the result if we got primable loaders
|
|
271
|
+
const result = await this.idLoader.load(key);
|
|
272
|
+
if (result && this.primedLoaders) {
|
|
273
|
+
for (const [key, loader] of this.primedLoaders) {
|
|
274
|
+
const value = result[key];
|
|
275
|
+
if (value !== undefined) {
|
|
276
|
+
loader.prime(result);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const cls = getCombinedClause(
|
|
285
|
+
this.options,
|
|
286
|
+
clause.Eq(this.options.key, key),
|
|
287
|
+
);
|
|
288
|
+
const rowOptions: LoadRowOptions = {
|
|
289
|
+
...this.options,
|
|
290
|
+
clause: cls,
|
|
291
|
+
context: this.context,
|
|
292
|
+
};
|
|
293
|
+
return loadRow(rowOptions) as Promise<TResultData | null>;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private async loadClause(
|
|
297
|
+
key: clause.Clause<TQueryData, K>,
|
|
298
|
+
): Promise<TResultData[] | null> {
|
|
299
|
+
if (this.clauseLoader) {
|
|
300
|
+
return this.clauseLoader.load(key);
|
|
301
|
+
}
|
|
302
|
+
return loadRowsForClauseLoader(this.options, key);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
clearAll() {
|
|
306
|
+
this.idLoader && this.idLoader.clearAll();
|
|
307
|
+
this.clauseLoader && this.clauseLoader.clearAll();
|
|
308
|
+
}
|
|
309
|
+
async loadMany(keys: ID[]): Promise<Array<TResultData | null>>;
|
|
310
|
+
async loadMany(
|
|
311
|
+
keys: clause.Clause<TQueryData, K>[],
|
|
312
|
+
): Promise<Array<TResultData[] | null>>;
|
|
313
|
+
async loadMany(
|
|
314
|
+
keys: ID[] | clause.Clause<TQueryData, K>[],
|
|
315
|
+
): Promise<Array<TResultData | TResultData[] | null>> {
|
|
316
|
+
if (!keys.length) {
|
|
317
|
+
return [];
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (typeof keys[0] === "string" || typeof keys[0] === "number") {
|
|
321
|
+
return this.loadIDMany(keys as ID[]);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return this.loadClauseMany(keys as clause.Clause<TQueryData, K>[]);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private loadIDMany(keys: ID[]): Promise<Array<TResultData | null>> {
|
|
328
|
+
if (this.idLoader) {
|
|
329
|
+
// @ts-expect-error TODO?
|
|
330
|
+
return this.idLoader.loadMany(keys);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return loadRowsForIDLoader(this.options, keys, this.context);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private async loadClauseMany(
|
|
337
|
+
keys: clause.Clause<TQueryData, K>[],
|
|
338
|
+
): Promise<Array<TResultData[] | null>> {
|
|
339
|
+
if (this.clauseLoader) {
|
|
340
|
+
// @ts-expect-error TODO?
|
|
341
|
+
return this.clauseLoader.loadMany(keys);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const res: TResultData[][] = [];
|
|
345
|
+
for await (const key of keys) {
|
|
346
|
+
const rows = await loadRowsForClauseLoader<TQueryData, TResultData, K>(
|
|
347
|
+
this.options,
|
|
348
|
+
key,
|
|
349
|
+
);
|
|
350
|
+
res.push(rows);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return res;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
prime(data: TResultData) {
|
|
357
|
+
// we have this data from somewhere else, prime it in the c
|
|
358
|
+
if (this.idLoader) {
|
|
359
|
+
const col = this.options.key;
|
|
360
|
+
const key = data[col];
|
|
361
|
+
this.idLoader.prime(key, data);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// prime this loader and any other loaders it's aware of
|
|
366
|
+
primeAll(data: TResultData) {
|
|
367
|
+
this.prime(data);
|
|
368
|
+
if (this.primedLoaders) {
|
|
369
|
+
for (const [key, loader] of this.primedLoaders) {
|
|
370
|
+
const value = data[key];
|
|
371
|
+
if (value !== undefined) {
|
|
372
|
+
loader.prime(data);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export class ObjectCountLoader<V extends Data = Data, K = keyof V>
|
|
380
|
+
implements Loader<clause.Clause<V, K>, number>
|
|
381
|
+
{
|
|
382
|
+
private loader: DataLoader<clause.Clause<V, K>, number> | null;
|
|
383
|
+
|
|
384
|
+
constructor(private options: SelectDataOptions, public context?: Context) {
|
|
385
|
+
if (context) {
|
|
386
|
+
this.loader = createClauseCountDataLoader(options);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
getOptions(): SelectDataOptions {
|
|
391
|
+
return this.options;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
async load(key: clause.Clause<V, K>): Promise<number> {
|
|
395
|
+
if (this.loader) {
|
|
396
|
+
return this.loader.load(key);
|
|
397
|
+
}
|
|
398
|
+
return loadCountForClauseLoader(this.options, key);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
clearAll() {
|
|
402
|
+
this.loader && this.loader.clearAll();
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
async loadMany(keys: clause.Clause<V, K>[]): Promise<Array<number>> {
|
|
406
|
+
if (!keys.length) {
|
|
407
|
+
return [];
|
|
408
|
+
}
|
|
409
|
+
if (this.loader) {
|
|
410
|
+
// @ts-expect-error
|
|
411
|
+
return this.loader.loadMany(keys);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const res: number[] = [];
|
|
415
|
+
for await (const key of keys) {
|
|
416
|
+
const r = await loadCountForClauseLoader(this.options, key);
|
|
417
|
+
res.push(r);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return res;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
interface ObjectLoaderOptions extends SelectDataOptions {
|
|
425
|
+
// needed when clause is a function...
|
|
426
|
+
instanceKey?: string;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// NOTE: if not querying for all columns
|
|
430
|
+
// have to query for the id field as one of the fields
|
|
431
|
+
// because it's used to maintain sort order of the queried ids
|
|
432
|
+
export class ObjectLoaderFactory<V extends Data = Data>
|
|
433
|
+
implements
|
|
434
|
+
LoaderFactory<ID, V | null>,
|
|
435
|
+
LoaderFactory<clause.Clause<V>, V[] | null>
|
|
436
|
+
{
|
|
437
|
+
name: string;
|
|
438
|
+
private toPrime: ObjectLoaderFactory<V>[] = [];
|
|
439
|
+
|
|
440
|
+
constructor(public options: ObjectLoaderOptions) {
|
|
441
|
+
let instanceKey = options.instanceKey || "";
|
|
442
|
+
if (typeof this.options.clause === "function") {
|
|
443
|
+
if (!options.instanceKey) {
|
|
444
|
+
throw new Error(
|
|
445
|
+
`need to pass an instanceKey to ObjectLoader if clause is a function`,
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
} else if (this.options.clause) {
|
|
449
|
+
instanceKey = this.options.clause.instanceKey();
|
|
450
|
+
}
|
|
451
|
+
this.name = `${options.tableName}:${options.key}:${instanceKey}`;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
createLoader(context?: Context): ObjectLoader<V> {
|
|
455
|
+
return getLoader(
|
|
456
|
+
this,
|
|
457
|
+
() => {
|
|
458
|
+
return new ObjectLoader(this.options, context, this.toPrime);
|
|
459
|
+
},
|
|
460
|
+
context,
|
|
461
|
+
) as ObjectLoader<V>;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
createTypedLoader<
|
|
465
|
+
TQueryData extends Data = Data,
|
|
466
|
+
TResultData extends Data = Data,
|
|
467
|
+
K = keyof TQueryData,
|
|
468
|
+
>(context?: Context): ObjectLoader<TQueryData, TResultData, K> {
|
|
469
|
+
const loader = this.createLoader(context);
|
|
470
|
+
return loader as unknown as ObjectLoader<TQueryData, TResultData, K>;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
createCountLoader<K = keyof V>(context?: Context): ObjectCountLoader<V, K> {
|
|
474
|
+
return getCustomLoader(
|
|
475
|
+
`${this.name}:count_loader`,
|
|
476
|
+
() => {
|
|
477
|
+
return new ObjectCountLoader(this.options, context);
|
|
478
|
+
},
|
|
479
|
+
context,
|
|
480
|
+
) as ObjectCountLoader<V, K>;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// keep track of loaders to prime. needs to be done not in the constructor
|
|
484
|
+
// because there's usually self references here
|
|
485
|
+
addToPrime(factory: ObjectLoaderFactory<V>): this {
|
|
486
|
+
this.toPrime.push(factory);
|
|
487
|
+
return this;
|
|
488
|
+
}
|
|
489
|
+
}
|