@snowtop/ent 0.1.0-alpha85 → 0.1.0-alpha88
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/core/base.d.ts +6 -6
- package/core/context.d.ts +3 -3
- package/core/context.js +18 -5
- package/core/ent.d.ts +4 -1
- package/core/ent.js +174 -149
- package/core/loaders/object_loader.d.ts +2 -2
- package/core/loaders/object_loader.js +45 -58
- package/index.d.ts +1 -1
- package/index.js +3 -2
- package/package.json +1 -1
package/core/base.d.ts
CHANGED
|
@@ -5,12 +5,12 @@ export interface Loader<T, V> {
|
|
|
5
5
|
loadMany?(keys: T[]): Promise<(V | null)[]>;
|
|
6
6
|
clearAll(): any;
|
|
7
7
|
}
|
|
8
|
-
interface LoaderWithLoadMany<T, V> extends Loader<T, V> {
|
|
9
|
-
loadMany(keys: T[]): Promise<
|
|
8
|
+
export interface LoaderWithLoadMany<T, V> extends Loader<T, V> {
|
|
9
|
+
loadMany(keys: T[]): Promise<V[]>;
|
|
10
10
|
}
|
|
11
|
-
export interface LoaderFactory<
|
|
11
|
+
export interface LoaderFactory<K, V> {
|
|
12
12
|
name: string;
|
|
13
|
-
createLoader(context?: Context): Loader<
|
|
13
|
+
createLoader(context?: Context): Loader<K, V>;
|
|
14
14
|
}
|
|
15
15
|
interface LoaderFactoryWithLoaderMany<T, V> extends LoaderFactory<T, V> {
|
|
16
16
|
createLoader(context?: Context): LoaderWithLoadMany<T, V>;
|
|
@@ -25,12 +25,12 @@ export interface PrimableLoader<T, V> extends Loader<T, V> {
|
|
|
25
25
|
}
|
|
26
26
|
interface cache {
|
|
27
27
|
getLoader<T, V>(name: string, create: () => Loader<T, V>): Loader<T, V>;
|
|
28
|
+
getLoaderWithLoadMany<T, V>(name: string, create: () => LoaderWithLoadMany<T, V>): LoaderWithLoadMany<T, V>;
|
|
28
29
|
getCachedRows(options: queryOptions): Data[] | null;
|
|
29
30
|
getCachedRow(options: queryOptions): Data | null;
|
|
30
31
|
primeCache(options: queryOptions, rows: Data[]): void;
|
|
31
32
|
primeCache(options: queryOptions, rows: Data): void;
|
|
32
33
|
clearCache(): void;
|
|
33
|
-
getEntCache(): Map<string, Ent | Error | null>;
|
|
34
34
|
}
|
|
35
35
|
interface queryOptions {
|
|
36
36
|
fields: string[];
|
|
@@ -97,7 +97,7 @@ interface LoadableEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer>
|
|
|
97
97
|
loaderFactory: LoaderFactoryWithOptions;
|
|
98
98
|
ent: EntConstructor<TEnt, TViewer>;
|
|
99
99
|
}
|
|
100
|
-
interface LoaderFactoryWithOptions extends LoaderFactoryWithLoaderMany<any, Data | null> {
|
|
100
|
+
export interface LoaderFactoryWithOptions extends LoaderFactoryWithLoaderMany<any, Data | null> {
|
|
101
101
|
options?: SelectDataOptions;
|
|
102
102
|
}
|
|
103
103
|
export interface LoadEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer> extends LoadableEntOptions<TEnt, TViewer>, SelectBaseDataOptions {
|
package/core/context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { Viewer, Data, Loader,
|
|
2
|
+
import { Viewer, Data, Loader, LoaderWithLoadMany } from "./base";
|
|
3
3
|
import { IncomingMessage, ServerResponse } from "http";
|
|
4
4
|
import * as clause from "./clause";
|
|
5
5
|
import { Context } from "./base";
|
|
@@ -11,17 +11,17 @@ export interface RequestContext<TViewer extends Viewer = Viewer> extends Context
|
|
|
11
11
|
}
|
|
12
12
|
export declare class ContextCache {
|
|
13
13
|
loaders: Map<string, Loader<any, any>>;
|
|
14
|
+
loaderWithLoadMany: Map<string, LoaderWithLoadMany<any, any>>;
|
|
14
15
|
getLoader<T, V>(name: string, create: () => Loader<T, V>): Loader<T, V>;
|
|
16
|
+
getLoaderWithLoadMany<T, V>(name: string, create: () => LoaderWithLoadMany<T, V>): LoaderWithLoadMany<T, V>;
|
|
15
17
|
private itemMap;
|
|
16
18
|
private listMap;
|
|
17
|
-
private entCache;
|
|
18
19
|
private getkey;
|
|
19
20
|
getCachedRows(options: queryOptions): Data[] | null;
|
|
20
21
|
getCachedRow(options: queryOptions): Data | null;
|
|
21
22
|
primeCache(options: queryOptions, rows: Data[]): void;
|
|
22
23
|
primeCache(options: queryOptions, rows: Data): void;
|
|
23
24
|
clearCache(): void;
|
|
24
|
-
getEntCache(): Map<string, Error | Ent<any> | null>;
|
|
25
25
|
}
|
|
26
26
|
interface queryOptions {
|
|
27
27
|
fields: string[];
|
package/core/context.js
CHANGED
|
@@ -5,10 +5,11 @@ const logger_1 = require("./logger");
|
|
|
5
5
|
class ContextCache {
|
|
6
6
|
constructor() {
|
|
7
7
|
this.loaders = new Map();
|
|
8
|
+
// we should eventually combine the two but better for typing to be separate for now
|
|
9
|
+
this.loaderWithLoadMany = new Map();
|
|
8
10
|
// we have a per-table map to make it easier to purge and have less things to compare with
|
|
9
11
|
this.itemMap = new Map();
|
|
10
12
|
this.listMap = new Map();
|
|
11
|
-
this.entCache = new Map();
|
|
12
13
|
}
|
|
13
14
|
getLoader(name, create) {
|
|
14
15
|
let l = this.loaders.get(name);
|
|
@@ -20,6 +21,16 @@ class ContextCache {
|
|
|
20
21
|
this.loaders.set(name, l);
|
|
21
22
|
return l;
|
|
22
23
|
}
|
|
24
|
+
getLoaderWithLoadMany(name, create) {
|
|
25
|
+
let l = this.loaderWithLoadMany.get(name);
|
|
26
|
+
if (l) {
|
|
27
|
+
return l;
|
|
28
|
+
}
|
|
29
|
+
(0, logger_1.log)("debug", `new context-aware loader created for ${name}`);
|
|
30
|
+
l = create();
|
|
31
|
+
this.loaderWithLoadMany.set(name, l);
|
|
32
|
+
return l;
|
|
33
|
+
}
|
|
23
34
|
// tableName is ignored bcos already indexed on that
|
|
24
35
|
// maybe we just want to store sql queries???
|
|
25
36
|
getkey(options) {
|
|
@@ -80,13 +91,15 @@ class ContextCache {
|
|
|
80
91
|
// but may have some benefits by explicitily doing so?
|
|
81
92
|
loader.clearAll();
|
|
82
93
|
}
|
|
94
|
+
for (const [_key, loader] of this.loaderWithLoadMany) {
|
|
95
|
+
// may not need this since we're clearing the loaders themselves...
|
|
96
|
+
// but may have some benefits by explicitily doing so?
|
|
97
|
+
loader.clearAll();
|
|
98
|
+
}
|
|
83
99
|
this.loaders.clear();
|
|
100
|
+
this.loaderWithLoadMany.clear();
|
|
84
101
|
this.itemMap.clear();
|
|
85
102
|
this.listMap.clear();
|
|
86
|
-
this.entCache.clear();
|
|
87
|
-
}
|
|
88
|
-
getEntCache() {
|
|
89
|
-
return this.entCache;
|
|
90
103
|
}
|
|
91
104
|
}
|
|
92
105
|
exports.ContextCache = ContextCache;
|
package/core/ent.d.ts
CHANGED
|
@@ -56,6 +56,9 @@ export declare type CustomQuery = string | parameterizedQueryOptions | clause.Cl
|
|
|
56
56
|
* }) // doesn't change the query
|
|
57
57
|
*/
|
|
58
58
|
export declare function loadCustomData(options: SelectCustomDataOptions, query: CustomQuery, context: Context | undefined): Promise<Data[]>;
|
|
59
|
+
interface CustomCountOptions extends DataOptions {
|
|
60
|
+
}
|
|
61
|
+
export declare function loadCustomCount(options: CustomCountOptions, query: CustomQuery, context: Context | undefined): Promise<number>;
|
|
59
62
|
export declare function loadDerivedEnt<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, data: Data, loader: new (viewer: TViewer, data: Data) => TEnt): Promise<TEnt | null>;
|
|
60
63
|
export declare function loadDerivedEntX<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, data: Data, loader: new (viewer: TViewer, data: Data) => TEnt): Promise<TEnt>;
|
|
61
64
|
export declare function loadRowX(options: LoadRowOptions): Promise<Data>;
|
|
@@ -225,7 +228,7 @@ interface loadEdgeForIDOptions<T extends AssocEdge> extends loadCustomEdgesOptio
|
|
|
225
228
|
}
|
|
226
229
|
export declare function loadEdgeForID2<T extends AssocEdge>(options: loadEdgeForIDOptions<T>): Promise<T | undefined>;
|
|
227
230
|
export declare function loadNodesByEdge<T extends Ent>(viewer: Viewer, id1: ID, edgeType: string, options: LoadEntOptions<T>): Promise<T[]>;
|
|
228
|
-
export declare function applyPrivacyPolicyForRow<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, options: LoadEntOptions<TEnt, TViewer>, row: Data
|
|
231
|
+
export declare function applyPrivacyPolicyForRow<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, options: LoadEntOptions<TEnt, TViewer>, row: Data): Promise<TEnt | null>;
|
|
229
232
|
export declare function applyPrivacyPolicyForRows<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, rows: Data[], options: LoadEntOptions<TEnt, TViewer>): Promise<Map<ID, TEnt>>;
|
|
230
233
|
export declare function getEdgeTypeInGroup<T extends string>(viewer: Viewer, id1: ID, id2: ID, m: Map<T, string>): Promise<[T, AssocEdge] | undefined>;
|
|
231
234
|
export {};
|
package/core/ent.js
CHANGED
|
@@ -22,8 +22,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
22
22
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.
|
|
26
|
-
exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.applyPrivacyPolicyForRow = exports.loadNodesByEdge = void 0;
|
|
25
|
+
exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadCustomEdges = exports.getEdgeClauseAndFields = exports.loadEdges = exports.defaultEdgeQueryOptions = exports.DefaultLimit = exports.loadEdgeDatas = exports.loadEdgeData = exports.assocEdgeLoader = exports.AssocEdgeData = exports.getCursor = exports.AssocEdge = exports.DeleteNodeOperation = exports.deleteRowsSync = exports.deleteRows = exports.editRowSync = exports.editRow = exports.buildUpdateQuery = exports.createRowSync = exports.createRow = exports.buildInsertQuery = exports.EdgeOperation = exports.__hasGlobalSchema = exports.clearGlobalSchema = exports.setGlobalSchema = exports.EditNodeOperation = exports.RawQueryOperation = exports.buildGroupQuery = exports.buildQuery = exports.loadRows = exports.performRawQuery = exports.loadRow = exports.loadRowX = exports.loadDerivedEntX = exports.loadDerivedEnt = exports.loadCustomCount = exports.loadCustomData = exports.loadCustomEnts = exports.loadEntsFromClause = exports.loadEntsList = exports.loadEnts = exports.loadEntXFromClause = exports.loadEntFromClause = exports.loadEntXViaKey = exports.loadEntX = exports.loadEntViaKey = exports.loadEnt = exports.getEntKey = void 0;
|
|
26
|
+
exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.applyPrivacyPolicyForRow = exports.loadNodesByEdge = exports.loadEdgeForID2 = void 0;
|
|
27
27
|
const db_1 = __importStar(require("./db"));
|
|
28
28
|
const privacy_1 = require("./privacy");
|
|
29
29
|
const clause = __importStar(require("./clause"));
|
|
@@ -57,6 +57,34 @@ class cacheMap {
|
|
|
57
57
|
return this.m.clear();
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
+
class entCacheMap {
|
|
61
|
+
constructor(viewer, options) {
|
|
62
|
+
this.viewer = viewer;
|
|
63
|
+
this.options = options;
|
|
64
|
+
this.m = new Map();
|
|
65
|
+
this.logEnabled = false;
|
|
66
|
+
this.logEnabled = (0, logger_1.logEnabled)("cache");
|
|
67
|
+
}
|
|
68
|
+
get(id) {
|
|
69
|
+
const ret = this.m.get(id);
|
|
70
|
+
if (this.logEnabled && ret) {
|
|
71
|
+
const key = getEntKey(this.viewer, id, this.options);
|
|
72
|
+
(0, logger_1.log)("cache", {
|
|
73
|
+
"ent-cache-hit": key,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return ret;
|
|
77
|
+
}
|
|
78
|
+
set(key, value) {
|
|
79
|
+
return this.m.set(key, value);
|
|
80
|
+
}
|
|
81
|
+
delete(key) {
|
|
82
|
+
return this.m.delete(key);
|
|
83
|
+
}
|
|
84
|
+
clear() {
|
|
85
|
+
return this.m.clear();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
60
88
|
function createDataLoader(options) {
|
|
61
89
|
const loaderOptions = {};
|
|
62
90
|
// if query logging is enabled, we should log what's happening with loader
|
|
@@ -87,60 +115,116 @@ function createDataLoader(options) {
|
|
|
87
115
|
return result;
|
|
88
116
|
}, loaderOptions);
|
|
89
117
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
function entFromCacheMaybe(viewer, id, options) {
|
|
96
|
-
const cache = viewer.context?.cache?.getEntCache();
|
|
97
|
-
if (!cache) {
|
|
98
|
-
return {};
|
|
99
|
-
}
|
|
100
|
-
const key = getEntKey(viewer, id, options);
|
|
101
|
-
const r = cache.get(key);
|
|
102
|
-
if (r !== undefined) {
|
|
103
|
-
(0, logger_1.log)("cache", {
|
|
104
|
-
"ent-cache-hit": key,
|
|
105
|
-
});
|
|
118
|
+
// used to wrap errors that would eventually be thrown in ents
|
|
119
|
+
// not an Error because DataLoader automatically rejects that
|
|
120
|
+
class ErrorWrapper {
|
|
121
|
+
constructor(error) {
|
|
122
|
+
this.error = error;
|
|
106
123
|
}
|
|
107
|
-
return {
|
|
108
|
-
key,
|
|
109
|
-
ent: r instanceof Error ? undefined : r,
|
|
110
|
-
error: r instanceof Error ? r : undefined,
|
|
111
|
-
cache,
|
|
112
|
-
};
|
|
113
124
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
125
|
+
function createEntLoader(viewer, options, map) {
|
|
126
|
+
// share the cache across loaders even if we create a new instance
|
|
127
|
+
const loaderOptions = {};
|
|
128
|
+
loaderOptions.cacheMap = map;
|
|
129
|
+
return new dataloader_1.default(async (ids) => {
|
|
130
|
+
if (!ids.length) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
let result = [];
|
|
134
|
+
const loader = options.loaderFactory.createLoader(viewer.context);
|
|
135
|
+
const rows = await loader.loadMany(ids);
|
|
136
|
+
// this is a loader which should return the same order based on passed-in ids
|
|
137
|
+
// so let's depend on that...
|
|
138
|
+
for (let idx = 0; idx < rows.length; idx++) {
|
|
139
|
+
const row = rows[idx];
|
|
140
|
+
// db error
|
|
141
|
+
if (row instanceof Error) {
|
|
142
|
+
result[idx] = row;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
else if (!row) {
|
|
146
|
+
result[idx] = new ErrorWrapper(new Error(`couldn't find row for value ${ids[idx]}`));
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const r = await applyPrivacyPolicyForRowImpl(viewer, options, row);
|
|
150
|
+
if (r instanceof Error) {
|
|
151
|
+
result[idx] = new ErrorWrapper(r);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
result[idx] = r;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
}, loaderOptions);
|
|
121
160
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
161
|
+
class EntLoader {
|
|
162
|
+
constructor(viewer, options) {
|
|
163
|
+
this.viewer = viewer;
|
|
164
|
+
this.options = options;
|
|
165
|
+
this.map = new entCacheMap(viewer, options);
|
|
166
|
+
this.loader = createEntLoader(this.viewer, this.options, this.map);
|
|
126
167
|
}
|
|
127
|
-
|
|
128
|
-
|
|
168
|
+
getMap() {
|
|
169
|
+
return this.map;
|
|
129
170
|
}
|
|
130
|
-
|
|
131
|
-
|
|
171
|
+
async load(id) {
|
|
172
|
+
return this.loader.load(id);
|
|
173
|
+
}
|
|
174
|
+
async loadMany(ids) {
|
|
175
|
+
return this.loader.loadMany(ids);
|
|
176
|
+
}
|
|
177
|
+
prime(id, ent) {
|
|
178
|
+
this.loader.prime(id, ent);
|
|
179
|
+
}
|
|
180
|
+
clear(id) {
|
|
181
|
+
this.loader.clear(id);
|
|
182
|
+
}
|
|
183
|
+
clearAll() {
|
|
184
|
+
this.loader.clearAll();
|
|
132
185
|
}
|
|
133
|
-
return ent;
|
|
134
186
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return info.ent;
|
|
187
|
+
function getEntLoader(viewer, options) {
|
|
188
|
+
if (!viewer.context?.cache) {
|
|
189
|
+
return new EntLoader(viewer, options);
|
|
139
190
|
}
|
|
140
|
-
const
|
|
141
|
-
return
|
|
191
|
+
const name = `ent-loader:${viewer.instanceKey()}:${options.loaderFactory.name}`;
|
|
192
|
+
return viewer.context.cache.getLoaderWithLoadMany(name, () => new EntLoader(viewer, options));
|
|
193
|
+
}
|
|
194
|
+
function getEntKey(viewer, id, options) {
|
|
195
|
+
return `${viewer.instanceKey()}:${options.loaderFactory.name}:${id}`;
|
|
196
|
+
}
|
|
197
|
+
exports.getEntKey = getEntKey;
|
|
198
|
+
async function loadEnt(viewer, id, options) {
|
|
199
|
+
const r = await getEntLoader(viewer, options).load(id);
|
|
200
|
+
return r instanceof ErrorWrapper ? null : r;
|
|
142
201
|
}
|
|
143
202
|
exports.loadEnt = loadEnt;
|
|
203
|
+
async function applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options,
|
|
204
|
+
// can pass in loader when calling this for multi-id cases...
|
|
205
|
+
loader) {
|
|
206
|
+
if (!loader) {
|
|
207
|
+
loader = getEntLoader(viewer, options);
|
|
208
|
+
}
|
|
209
|
+
// TODO every row.id needs to be audited...
|
|
210
|
+
// https://github.com/lolopinto/ent/issues/1064
|
|
211
|
+
const id = row.id;
|
|
212
|
+
// we should check the ent loader cache to see if this is already there
|
|
213
|
+
// TODO hmm... we eventually need a custom data-loader for this too so that it's all done correctly if there's a complicated fetch deep down in graphql
|
|
214
|
+
const result = loader.getMap().get(id);
|
|
215
|
+
if (result !== undefined) {
|
|
216
|
+
return result;
|
|
217
|
+
}
|
|
218
|
+
const r = await applyPrivacyPolicyForRowImpl(viewer, options, row);
|
|
219
|
+
if (r instanceof Error) {
|
|
220
|
+
loader.prime(id, new ErrorWrapper(r));
|
|
221
|
+
return new ErrorWrapper(r);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
loader.prime(id, r);
|
|
225
|
+
return r;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
144
228
|
// this is the same implementation-wise (right now) as loadEnt. it's just clearer that it's not loaded via ID.
|
|
145
229
|
// used for load via email address etc
|
|
146
230
|
async function loadEntViaKey(viewer, key, options) {
|
|
@@ -150,29 +234,16 @@ async function loadEntViaKey(viewer, key, options) {
|
|
|
150
234
|
if (!row) {
|
|
151
235
|
return null;
|
|
152
236
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const info = entFromCacheMaybe(viewer, row.id, options);
|
|
156
|
-
if (info.ent !== undefined) {
|
|
157
|
-
return info.ent;
|
|
158
|
-
}
|
|
159
|
-
return applyPrivacyPolicyForRowAndStoreInCache(viewer, options, row, info);
|
|
237
|
+
const r = await applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options);
|
|
238
|
+
return r instanceof ErrorWrapper ? null : r;
|
|
160
239
|
}
|
|
161
240
|
exports.loadEntViaKey = loadEntViaKey;
|
|
162
241
|
async function loadEntX(viewer, id, options) {
|
|
163
|
-
const
|
|
164
|
-
if (
|
|
165
|
-
throw
|
|
242
|
+
const r = await getEntLoader(viewer, options).load(id);
|
|
243
|
+
if (r instanceof ErrorWrapper) {
|
|
244
|
+
throw r.error;
|
|
166
245
|
}
|
|
167
|
-
|
|
168
|
-
return info.ent;
|
|
169
|
-
}
|
|
170
|
-
const row = await options.loaderFactory.createLoader(viewer.context).load(id);
|
|
171
|
-
if (!row) {
|
|
172
|
-
// todo make this better
|
|
173
|
-
throw new Error(`${options.loaderFactory.name}: couldn't find row for value ${id}`);
|
|
174
|
-
}
|
|
175
|
-
return applyPrivacyPolicyForRowAndStoreInCacheX(viewer, options, row, info);
|
|
246
|
+
return r;
|
|
176
247
|
}
|
|
177
248
|
exports.loadEntX = loadEntX;
|
|
178
249
|
async function loadEntXViaKey(viewer, key, options) {
|
|
@@ -183,14 +254,11 @@ async function loadEntXViaKey(viewer, key, options) {
|
|
|
183
254
|
// todo make this better
|
|
184
255
|
throw new Error(`${options.loaderFactory.name}: couldn't find row for value ${key}`);
|
|
185
256
|
}
|
|
186
|
-
const
|
|
187
|
-
if (
|
|
188
|
-
throw
|
|
189
|
-
}
|
|
190
|
-
if (info.ent !== undefined && info.ent !== null) {
|
|
191
|
-
return info.ent;
|
|
257
|
+
const r = await applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options);
|
|
258
|
+
if (r instanceof ErrorWrapper) {
|
|
259
|
+
throw r.error;
|
|
192
260
|
}
|
|
193
|
-
return
|
|
261
|
+
return r;
|
|
194
262
|
}
|
|
195
263
|
exports.loadEntXViaKey = loadEntXViaKey;
|
|
196
264
|
/**
|
|
@@ -203,6 +271,9 @@ async function loadEntFromClause(viewer, options, clause) {
|
|
|
203
271
|
context: viewer.context,
|
|
204
272
|
};
|
|
205
273
|
const row = await loadRow(rowOptions);
|
|
274
|
+
if (row === null) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
206
277
|
return applyPrivacyPolicyForRow(viewer, options, row);
|
|
207
278
|
}
|
|
208
279
|
exports.loadEntFromClause = loadEntFromClause;
|
|
@@ -228,62 +299,15 @@ async function loadEnts(viewer, options, ...ids) {
|
|
|
228
299
|
}
|
|
229
300
|
// result
|
|
230
301
|
let m = new Map();
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const key = getEntKey(viewer, id, options);
|
|
236
|
-
const ent = cache.get(key);
|
|
237
|
-
if (ent !== undefined) {
|
|
238
|
-
(0, logger_1.log)("cache", {
|
|
239
|
-
"ent-cache-hit": key,
|
|
240
|
-
});
|
|
241
|
-
if (ent === null) {
|
|
242
|
-
// TODO this should return null if not loadable...
|
|
243
|
-
// https://github.com/lolopinto/ent/issues/1070
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
// @ts-ignore
|
|
247
|
-
m.set(id, ent);
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
toFetch.push(id);
|
|
251
|
-
}
|
|
302
|
+
const ret = await getEntLoader(viewer, options).loadMany(ids);
|
|
303
|
+
for (const r of ret) {
|
|
304
|
+
if (r instanceof Error) {
|
|
305
|
+
throw r;
|
|
252
306
|
}
|
|
253
|
-
|
|
254
|
-
else {
|
|
255
|
-
toFetch = ids;
|
|
256
|
-
}
|
|
257
|
-
// all in ent cache!
|
|
258
|
-
if (!toFetch.length) {
|
|
259
|
-
return m;
|
|
260
|
-
}
|
|
261
|
-
const l = options.loaderFactory.createLoader(viewer.context);
|
|
262
|
-
const rows = await l.loadMany(toFetch);
|
|
263
|
-
let rows2 = [];
|
|
264
|
-
for (const row of rows) {
|
|
265
|
-
if (!row) {
|
|
307
|
+
if (r instanceof ErrorWrapper) {
|
|
266
308
|
continue;
|
|
267
309
|
}
|
|
268
|
-
|
|
269
|
-
throw row;
|
|
270
|
-
}
|
|
271
|
-
rows2.push(row);
|
|
272
|
-
}
|
|
273
|
-
const m2 = await applyPrivacyPolicyForRows(viewer, rows2, options);
|
|
274
|
-
for (const row of rows2) {
|
|
275
|
-
const id = row[options.loaderFactory.options?.key || "id"];
|
|
276
|
-
const ent = m2.get(id);
|
|
277
|
-
if (cache) {
|
|
278
|
-
// put back in cache...
|
|
279
|
-
// store null for rows that can't be seen
|
|
280
|
-
cache.set(getEntKey(viewer, id, options), ent ?? null);
|
|
281
|
-
}
|
|
282
|
-
if (ent !== undefined) {
|
|
283
|
-
// TODO this should return null if not loadable...?
|
|
284
|
-
// TODO https://github.com/lolopinto/ent/issues/1070
|
|
285
|
-
m.set(id, ent);
|
|
286
|
-
}
|
|
310
|
+
m.set(r.id, r);
|
|
287
311
|
}
|
|
288
312
|
return m;
|
|
289
313
|
}
|
|
@@ -320,23 +344,16 @@ exports.loadEntsFromClause = loadEntsFromClause;
|
|
|
320
344
|
async function loadCustomEnts(viewer, options, query) {
|
|
321
345
|
const rows = await loadCustomData(options, query, viewer.context);
|
|
322
346
|
const result = new Array(rows.length);
|
|
347
|
+
if (!rows.length) {
|
|
348
|
+
return [];
|
|
349
|
+
}
|
|
350
|
+
const entLoader = getEntLoader(viewer, options);
|
|
323
351
|
await Promise.all(rows.map(async (row, idx) => {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const info = entFromCacheMaybe(viewer, row.id, options);
|
|
327
|
-
if (info.ent !== undefined) {
|
|
328
|
-
if (info.ent === null) {
|
|
329
|
-
// we're done here
|
|
330
|
-
return;
|
|
331
|
-
}
|
|
332
|
-
// @ts-ignore
|
|
333
|
-
result[idx] = info.ent;
|
|
352
|
+
const r = await applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options, entLoader);
|
|
353
|
+
if (r instanceof ErrorWrapper) {
|
|
334
354
|
return;
|
|
335
355
|
}
|
|
336
|
-
|
|
337
|
-
if (privacyEnt) {
|
|
338
|
-
result[idx] = privacyEnt;
|
|
339
|
-
}
|
|
356
|
+
result[idx] = r;
|
|
340
357
|
}));
|
|
341
358
|
// filter ents that aren't visible because of privacy
|
|
342
359
|
return result.filter((r) => r !== undefined);
|
|
@@ -387,6 +404,20 @@ async function loadCustomData(options, query, context) {
|
|
|
387
404
|
return rows;
|
|
388
405
|
}
|
|
389
406
|
exports.loadCustomData = loadCustomData;
|
|
407
|
+
// NOTE: if you use a raw query or paramterized query with this,
|
|
408
|
+
// you should use `SELECT count(*) as count...`
|
|
409
|
+
async function loadCustomCount(options, query, context) {
|
|
410
|
+
// TODO also need to loaderify this in case we're querying for this a lot...
|
|
411
|
+
const rows = await loadCustomDataImpl({
|
|
412
|
+
...options,
|
|
413
|
+
fields: ["count(1) as count"],
|
|
414
|
+
}, query, context);
|
|
415
|
+
if (rows.length) {
|
|
416
|
+
return parseInt(rows[0].count);
|
|
417
|
+
}
|
|
418
|
+
return 0;
|
|
419
|
+
}
|
|
420
|
+
exports.loadCustomCount = loadCustomCount;
|
|
390
421
|
function isPrimableLoader(loader) {
|
|
391
422
|
return loader != undefined;
|
|
392
423
|
}
|
|
@@ -457,14 +488,11 @@ exports.loadDerivedEntX = loadDerivedEntX;
|
|
|
457
488
|
// everything calls into this two so should be fine
|
|
458
489
|
// TODO is there a smarter way to not instantiate two objects here?
|
|
459
490
|
async function applyPrivacyPolicyForEnt(viewer, ent, data, fieldPrivacyOptions) {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
return doFieldPrivacy(viewer, ent, data, fieldPrivacyOptions);
|
|
464
|
-
}
|
|
465
|
-
return error;
|
|
491
|
+
const error = await (0, privacy_1.applyPrivacyPolicyImpl)(viewer, ent.getPrivacyPolicy(), ent);
|
|
492
|
+
if (error === null) {
|
|
493
|
+
return doFieldPrivacy(viewer, ent, data, fieldPrivacyOptions);
|
|
466
494
|
}
|
|
467
|
-
return
|
|
495
|
+
return error;
|
|
468
496
|
}
|
|
469
497
|
async function applyPrivacyPolicyForEntX(viewer, ent, data, options) {
|
|
470
498
|
const r = await applyPrivacyPolicyForEnt(viewer, ent, data, options);
|
|
@@ -1505,9 +1533,6 @@ async function applyPrivacyPolicyForRow(viewer, options, row) {
|
|
|
1505
1533
|
}
|
|
1506
1534
|
exports.applyPrivacyPolicyForRow = applyPrivacyPolicyForRow;
|
|
1507
1535
|
async function applyPrivacyPolicyForRowImpl(viewer, options, row) {
|
|
1508
|
-
if (!row) {
|
|
1509
|
-
return null;
|
|
1510
|
-
}
|
|
1511
1536
|
const ent = new options.ent(viewer, row);
|
|
1512
1537
|
return applyPrivacyPolicyForEnt(viewer, ent, row, options);
|
|
1513
1538
|
}
|
|
@@ -11,7 +11,7 @@ export declare class ObjectLoader<T> implements Loader<T, Data | null> {
|
|
|
11
11
|
private initPrime;
|
|
12
12
|
load(key: T): Promise<Data | null>;
|
|
13
13
|
clearAll(): void;
|
|
14
|
-
loadMany(keys: T[]): Promise<Data
|
|
14
|
+
loadMany(keys: T[]): Promise<Array<Data | null>>;
|
|
15
15
|
prime(data: Data): void;
|
|
16
16
|
primeAll(data: Data): void;
|
|
17
17
|
}
|
|
@@ -24,6 +24,6 @@ export declare class ObjectLoaderFactory<T> implements LoaderFactory<T, Data | n
|
|
|
24
24
|
private toPrime;
|
|
25
25
|
constructor(options: ObjectLoaderOptions);
|
|
26
26
|
createLoader(context?: Context): ObjectLoader<T>;
|
|
27
|
-
addToPrime(factory: ObjectLoaderFactory<T>):
|
|
27
|
+
addToPrime(factory: ObjectLoaderFactory<T>): this;
|
|
28
28
|
}
|
|
29
29
|
export {};
|
|
@@ -29,6 +29,47 @@ const clause = __importStar(require("../clause"));
|
|
|
29
29
|
const logger_1 = require("../logger");
|
|
30
30
|
const loader_1 = require("./loader");
|
|
31
31
|
const memoizee_1 = __importDefault(require("memoizee"));
|
|
32
|
+
async function loadRowsForLoader(options, ids, context) {
|
|
33
|
+
let col = options.key;
|
|
34
|
+
let cls = clause.In(col, ...ids);
|
|
35
|
+
if (options.clause) {
|
|
36
|
+
let optionClause;
|
|
37
|
+
if (typeof options.clause === "function") {
|
|
38
|
+
optionClause = options.clause();
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
optionClause = options.clause;
|
|
42
|
+
}
|
|
43
|
+
if (optionClause) {
|
|
44
|
+
cls = clause.And(cls, optionClause);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const rowOptions = {
|
|
48
|
+
...options,
|
|
49
|
+
clause: cls,
|
|
50
|
+
context,
|
|
51
|
+
};
|
|
52
|
+
let m = new Map();
|
|
53
|
+
let result = [];
|
|
54
|
+
for (let i = 0; i < ids.length; i++) {
|
|
55
|
+
result.push(null);
|
|
56
|
+
// store the index....
|
|
57
|
+
m.set(ids[i], i);
|
|
58
|
+
}
|
|
59
|
+
const rows = await (0, ent_1.loadRows)(rowOptions);
|
|
60
|
+
for (const row of rows) {
|
|
61
|
+
const id = row[col];
|
|
62
|
+
if (id === undefined) {
|
|
63
|
+
throw new Error(`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`);
|
|
64
|
+
}
|
|
65
|
+
const idx = m.get(id);
|
|
66
|
+
if (idx === undefined) {
|
|
67
|
+
throw new Error(`malformed query. got ${id} back but didn't query for it`);
|
|
68
|
+
}
|
|
69
|
+
result[idx] = row;
|
|
70
|
+
}
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
32
73
|
// optional clause...
|
|
33
74
|
// so ObjectLoaderFactory and createDataLoader need to take a new optional field which is a clause that's always added here
|
|
34
75
|
// and we need a disableTransform which skips loader completely and uses loadRow...
|
|
@@ -42,45 +83,8 @@ function createDataLoader(options) {
|
|
|
42
83
|
if (!ids.length) {
|
|
43
84
|
return [];
|
|
44
85
|
}
|
|
45
|
-
let col = options.key;
|
|
46
|
-
let cls = clause.In(col, ...ids);
|
|
47
|
-
if (options.clause) {
|
|
48
|
-
let optionClause;
|
|
49
|
-
if (typeof options.clause === "function") {
|
|
50
|
-
optionClause = options.clause();
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
optionClause = options.clause;
|
|
54
|
-
}
|
|
55
|
-
if (optionClause) {
|
|
56
|
-
cls = clause.And(cls, optionClause);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
const rowOptions = {
|
|
60
|
-
...options,
|
|
61
|
-
clause: cls,
|
|
62
|
-
};
|
|
63
|
-
let m = new Map();
|
|
64
|
-
let result = [];
|
|
65
|
-
for (let i = 0; i < ids.length; i++) {
|
|
66
|
-
result.push(null);
|
|
67
|
-
// store the index....
|
|
68
|
-
m.set(ids[i], i);
|
|
69
|
-
}
|
|
70
86
|
// context not needed because we're creating a loader which has its own cache which is being used here
|
|
71
|
-
|
|
72
|
-
for (const row of rows) {
|
|
73
|
-
const id = row[col];
|
|
74
|
-
if (id === undefined) {
|
|
75
|
-
throw new Error(`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`);
|
|
76
|
-
}
|
|
77
|
-
const idx = m.get(id);
|
|
78
|
-
if (idx === undefined) {
|
|
79
|
-
throw new Error(`malformed query. got ${id} back but didn't query for it`);
|
|
80
|
-
}
|
|
81
|
-
result[idx] = row;
|
|
82
|
-
}
|
|
83
|
-
return result;
|
|
87
|
+
return loadRowsForLoader(options, ids);
|
|
84
88
|
}, loaderOptions);
|
|
85
89
|
}
|
|
86
90
|
class ObjectLoader {
|
|
@@ -147,7 +151,7 @@ class ObjectLoader {
|
|
|
147
151
|
clause: cls,
|
|
148
152
|
context: this.context,
|
|
149
153
|
};
|
|
150
|
-
return
|
|
154
|
+
return (0, ent_1.loadRow)(rowOptions);
|
|
151
155
|
}
|
|
152
156
|
clearAll() {
|
|
153
157
|
this.loader && this.loader.clearAll();
|
|
@@ -156,25 +160,7 @@ class ObjectLoader {
|
|
|
156
160
|
if (this.loader) {
|
|
157
161
|
return await this.loader.loadMany(keys);
|
|
158
162
|
}
|
|
159
|
-
|
|
160
|
-
if (this.options.clause) {
|
|
161
|
-
let optionClause;
|
|
162
|
-
if (typeof this.options.clause === "function") {
|
|
163
|
-
optionClause = this.options.clause();
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
optionClause = this.options.clause;
|
|
167
|
-
}
|
|
168
|
-
if (optionClause) {
|
|
169
|
-
cls = clause.And(cls, optionClause);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
const rowOptions = {
|
|
173
|
-
...this.options,
|
|
174
|
-
clause: cls,
|
|
175
|
-
context: this.context,
|
|
176
|
-
};
|
|
177
|
-
return await (0, ent_1.loadRows)(rowOptions);
|
|
163
|
+
return loadRowsForLoader(this.options, keys, this.context);
|
|
178
164
|
}
|
|
179
165
|
prime(data) {
|
|
180
166
|
// we have this data from somewhere else, prime it in the c
|
|
@@ -225,6 +211,7 @@ class ObjectLoaderFactory {
|
|
|
225
211
|
// because there's usually self references here
|
|
226
212
|
addToPrime(factory) {
|
|
227
213
|
this.toPrime.push(factory);
|
|
214
|
+
return this;
|
|
228
215
|
}
|
|
229
216
|
}
|
|
230
217
|
exports.ObjectLoaderFactory = ObjectLoaderFactory;
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from "./core/base";
|
|
2
|
-
export { loadEnt, loadCustomData, loadCustomEnts, loadEntX, loadEnts, CustomQuery, loadDerivedEnt, loadDerivedEntX, loadEntViaKey, loadEntXViaKey, performRawQuery, loadRowX, loadRow, loadRows, DataOperation, EditNodeOptions, EditNodeOperation, RawQueryOperation, EdgeOperation, DeleteNodeOperation, AssocEdge, AssocEdgeInputOptions, AssocEdgeInput, AssocEdgeData, loadEdgeData, loadEdgeDatas, loadEdges, loadUniqueEdge, loadUniqueNode, loadRawEdgeCountX, loadEdgeForID2, loadNodesByEdge, getEdgeTypeInGroup, setGlobalSchema, } from "./core/ent";
|
|
2
|
+
export { loadEnt, loadCustomData, loadCustomEnts, loadCustomCount, loadEntX, loadEnts, CustomQuery, loadDerivedEnt, loadDerivedEntX, loadEntViaKey, loadEntXViaKey, performRawQuery, loadRowX, loadRow, loadRows, DataOperation, EditNodeOptions, EditNodeOperation, RawQueryOperation, EdgeOperation, DeleteNodeOperation, AssocEdge, AssocEdgeInputOptions, AssocEdgeInput, AssocEdgeData, loadEdgeData, loadEdgeDatas, loadEdges, loadUniqueEdge, loadUniqueNode, loadRawEdgeCountX, loadEdgeForID2, loadNodesByEdge, getEdgeTypeInGroup, setGlobalSchema, } from "./core/ent";
|
|
3
3
|
import DB from "./core/db";
|
|
4
4
|
export * from "./core/loaders";
|
|
5
5
|
export { DB };
|
package/index.js
CHANGED
|
@@ -25,13 +25,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
25
25
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
26
26
|
};
|
|
27
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
-
exports.
|
|
29
|
-
exports.setLogLevels = exports.loadConfig = exports.LoggedOutViewer = exports.IDViewer = exports.ContextCache = exports.query = exports.AllowIfViewerHasIdentityPrivacyPolicy = exports.AllowIfViewerPrivacyPolicy = exports.AllowIfSubPolicyAllowsRule = exports.AllowIfConditionAppliesRule = exports.AlwaysDenyPrivacyPolicy = exports.AlwaysAllowPrivacyPolicy = exports.applyPrivacyPolicyX = exports.applyPrivacyPolicy = exports.DelayedResultRule = exports.DenyIfEntIsVisiblePolicy = exports.AllowIfEntIsVisiblePolicy = exports.DenyIfEntIsNotVisibleRule = exports.DenyIfEntIsVisibleRule = exports.AllowIfEntIsNotVisibleRule = exports.AllowIfEntIsVisibleRule = exports.DenyIfViewerOutboundEdgeDoesNotExistRule = exports.DenyIfViewerInboundEdgeDoesNotExistRule = exports.DenyIfEdgeDoesNotExistRule = void 0;
|
|
28
|
+
exports.DenyIfViewerInboundEdgeExistsRule = exports.DenyIfEdgeExistsRule = exports.AllowIfViewerOutboundEdgeExistsRule = exports.AllowIfViewerInboundEdgeExistsRule = exports.AllowIfEdgeExistsRule = exports.DenyIfViewerEqualsRule = exports.AllowIfViewerEqualsRule = exports.DenyIfEntPropertyIsRule = exports.AllowIfEntPropertyIsRule = exports.AllowIfViewerIsEntPropertyRule = exports.AllowIfViewerIsRule = exports.AllowIfFuncRule = exports.AllowIfViewerRule = exports.AllowIfHasIdentity = exports.DenyIfLoggedOutRule = exports.DenyIfLoggedInRule = exports.AlwaysDenyRule = exports.AlwaysAllowRule = exports.EntPrivacyError = exports.DB = exports.setGlobalSchema = exports.getEdgeTypeInGroup = exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadEdges = exports.loadEdgeDatas = exports.loadEdgeData = exports.AssocEdgeData = exports.AssocEdge = exports.DeleteNodeOperation = exports.EdgeOperation = exports.RawQueryOperation = exports.EditNodeOperation = exports.loadRows = exports.loadRow = exports.loadRowX = exports.performRawQuery = exports.loadEntXViaKey = exports.loadEntViaKey = exports.loadDerivedEntX = exports.loadDerivedEnt = exports.loadEnts = exports.loadEntX = exports.loadCustomCount = exports.loadCustomEnts = exports.loadCustomData = exports.loadEnt = void 0;
|
|
29
|
+
exports.setLogLevels = exports.loadConfig = exports.LoggedOutViewer = exports.IDViewer = exports.ContextCache = exports.query = exports.AllowIfViewerHasIdentityPrivacyPolicy = exports.AllowIfViewerPrivacyPolicy = exports.AllowIfSubPolicyAllowsRule = exports.AllowIfConditionAppliesRule = exports.AlwaysDenyPrivacyPolicy = exports.AlwaysAllowPrivacyPolicy = exports.applyPrivacyPolicyX = exports.applyPrivacyPolicy = exports.DelayedResultRule = exports.DenyIfEntIsVisiblePolicy = exports.AllowIfEntIsVisiblePolicy = exports.DenyIfEntIsNotVisibleRule = exports.DenyIfEntIsVisibleRule = exports.AllowIfEntIsNotVisibleRule = exports.AllowIfEntIsVisibleRule = exports.DenyIfViewerOutboundEdgeDoesNotExistRule = exports.DenyIfViewerInboundEdgeDoesNotExistRule = exports.DenyIfEdgeDoesNotExistRule = exports.DenyIfViewerOutboundEdgeExistsRule = void 0;
|
|
30
30
|
__exportStar(require("./core/base"), exports);
|
|
31
31
|
var ent_1 = require("./core/ent");
|
|
32
32
|
Object.defineProperty(exports, "loadEnt", { enumerable: true, get: function () { return ent_1.loadEnt; } });
|
|
33
33
|
Object.defineProperty(exports, "loadCustomData", { enumerable: true, get: function () { return ent_1.loadCustomData; } });
|
|
34
34
|
Object.defineProperty(exports, "loadCustomEnts", { enumerable: true, get: function () { return ent_1.loadCustomEnts; } });
|
|
35
|
+
Object.defineProperty(exports, "loadCustomCount", { enumerable: true, get: function () { return ent_1.loadCustomCount; } });
|
|
35
36
|
Object.defineProperty(exports, "loadEntX", { enumerable: true, get: function () { return ent_1.loadEntX; } });
|
|
36
37
|
Object.defineProperty(exports, "loadEnts", { enumerable: true, get: function () { return ent_1.loadEnts; } });
|
|
37
38
|
Object.defineProperty(exports, "loadDerivedEnt", { enumerable: true, get: function () { return ent_1.loadDerivedEnt; } });
|