grafio-mongo 1.0.1 → 1.1.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 CHANGED
@@ -157,7 +157,7 @@ try {
157
157
 
158
158
  ## Graph Operations
159
159
 
160
- All graph operations from grafio are available when using MongoDB storage. See the [grafio documentation](https://github.com/witspry/grafio) for the complete API reference.
160
+ All graph operations from grafio are available when using MongoDB storage. See the [grafio documentation](https://github.com/satyajugran/grafio) for the complete API reference.
161
161
 
162
162
  ### Example Operations
163
163
 
@@ -9,5 +9,6 @@ export declare class MongoGraphFactory implements IGraphFactory {
9
9
  ensureIndexes(): Promise<void>;
10
10
  forGraph(graphId?: string): Graph;
11
11
  fromGraphData(data: GraphData, graphId?: string): Promise<Graph>;
12
+ private _wrapWithCacheIfEnabled;
12
13
  }
13
14
  export {};
@@ -15,19 +15,36 @@ class MongoGraphFactory {
15
15
  await probe.ensureIndexes();
16
16
  }
17
17
  forGraph(graphId = 'default') {
18
- const provider = new MongoStorageProvider_1.MongoStorageProvider(this._db, { ...this._opts, graphId });
18
+ const mongoProvider = new MongoStorageProvider_1.MongoStorageProvider(this._db, { ...this._opts, graphId });
19
+ const provider = this._wrapWithCacheIfEnabled(mongoProvider, graphId);
19
20
  return new grafio_1.Graph(provider);
20
21
  }
21
22
  async fromGraphData(data, graphId = 'default') {
22
- const provider = new MongoStorageProvider_1.MongoStorageProvider(this._db, { ...this._opts, graphId });
23
- const graph = new grafio_1.Graph(provider);
23
+ const mongoProvider = new MongoStorageProvider_1.MongoStorageProvider(this._db, { ...this._opts, graphId });
24
+ const provider = this._wrapWithCacheIfEnabled(mongoProvider, graphId);
24
25
  const filteredData = {
25
26
  graphId,
26
27
  nodes: data.graphId === undefined || data.graphId === graphId ? data.nodes : [],
27
28
  edges: data.graphId === undefined || data.graphId === graphId ? data.edges : [],
28
29
  };
30
+ const graph = new grafio_1.Graph(provider);
29
31
  await grafio_1.Graph.importJSON(filteredData, provider);
30
32
  return graph;
31
33
  }
34
+ _wrapWithCacheIfEnabled(mongoProvider, graphId) {
35
+ if (!grafio_1.GraphManager.isInitialized()) {
36
+ return mongoProvider;
37
+ }
38
+ const manager = grafio_1.GraphManager.getInstance();
39
+ const cacheManager = manager.getCacheManager();
40
+ if (!cacheManager) {
41
+ return mongoProvider;
42
+ }
43
+ const cacheConfig = manager.getConfig()?.cache;
44
+ if (!cacheConfig) {
45
+ return mongoProvider;
46
+ }
47
+ return new grafio_1.CachedStorageProvider(mongoProvider, graphId, cacheManager, cacheConfig);
48
+ }
32
49
  }
33
50
  exports.MongoGraphFactory = MongoGraphFactory;
@@ -1,5 +1,5 @@
1
1
  import type { Db } from 'mongodb';
2
- import { IStorageProvider, NodeData, ITransactionHandle, EdgeData, GraphData } from 'grafio';
2
+ import { IStorageProvider, IOrderBy, NodeData, ITransactionHandle, EdgeData, GraphData } from 'grafio';
3
3
  export interface MongoStorageProviderOptions {
4
4
  graphId?: string;
5
5
  nodesCollection?: string;
@@ -19,7 +19,7 @@ export declare class MongoStorageProvider implements IStorageProvider {
19
19
  deleteNode(id: string, transaction?: ITransactionHandle): Promise<void>;
20
20
  hasNode(id: string, transaction?: ITransactionHandle): Promise<boolean>;
21
21
  getNode(id: string, transaction?: ITransactionHandle): Promise<NodeData | undefined>;
22
- getAllNodes(limit?: number, transaction?: ITransactionHandle): Promise<NodeData[]>;
22
+ getAllNodes(limit?: number, orderBy?: IOrderBy, transaction?: ITransactionHandle): Promise<NodeData[]>;
23
23
  getNodesByType(type: string, transaction?: ITransactionHandle): Promise<NodeData[]>;
24
24
  getNodesByProperty(key: string, value: unknown, nodeType?: string, transaction?: ITransactionHandle): Promise<NodeData[]>;
25
25
  getEdgesByProperty(key: string, value: unknown, edgeType?: string, transaction?: ITransactionHandle): Promise<EdgeData[]>;
@@ -27,7 +27,7 @@ export declare class MongoStorageProvider implements IStorageProvider {
27
27
  deleteEdge(id: string, transaction?: ITransactionHandle): Promise<void>;
28
28
  hasEdge(id: string, transaction?: ITransactionHandle): Promise<boolean>;
29
29
  getEdge(id: string, transaction?: ITransactionHandle): Promise<EdgeData | undefined>;
30
- getAllEdges(transaction?: ITransactionHandle): Promise<EdgeData[]>;
30
+ getAllEdges(limit?: number, orderBy?: IOrderBy, transaction?: ITransactionHandle): Promise<EdgeData[]>;
31
31
  getEdgesByType(type: string, transaction?: ITransactionHandle): Promise<EdgeData[]>;
32
32
  getEdgesBySource(nodeId: string, type?: string, transaction?: ITransactionHandle): Promise<EdgeData[]>;
33
33
  getEdgesByTarget(nodeId: string, type?: string, transaction?: ITransactionHandle): Promise<EdgeData[]>;
@@ -39,12 +39,21 @@ class MongoStorageProvider {
39
39
  ]);
40
40
  }
41
41
  async insertNode(node, transaction) {
42
+ const now = Date.now();
43
+ if (node.createdOn === undefined) {
44
+ node.createdOn = now;
45
+ }
46
+ if (node.updatedOn === undefined) {
47
+ node.updatedOn = now;
48
+ }
42
49
  const session = transaction?.context;
43
50
  try {
44
51
  await this._nodes.insertOne({
45
52
  id: node.id,
46
53
  graphId: this._graphId,
47
54
  type: node.type,
55
+ createdOn: node.createdOn,
56
+ updatedOn: node.updatedOn,
48
57
  properties: node.properties,
49
58
  }, { session });
50
59
  }
@@ -68,10 +77,12 @@ class MongoStorageProvider {
68
77
  const doc = await this._nodes.findOne({ graphId: this._graphId, id }, { session });
69
78
  return doc ? this._docToNode(doc) : undefined;
70
79
  }
71
- async getAllNodes(limit, transaction) {
80
+ async getAllNodes(limit, orderBy, transaction) {
72
81
  const session = transaction?.context;
73
82
  const nodes = [];
74
83
  const cursor = this._nodes.find({ graphId: this._graphId }, { session }).batchSize(this._batchSize);
84
+ if (orderBy)
85
+ cursor.sort(orderBy.field, orderBy.direction);
75
86
  if (limit)
76
87
  cursor.limit(limit);
77
88
  for await (const doc of cursor) {
@@ -109,6 +120,13 @@ class MongoStorageProvider {
109
120
  return docs.map(d => this._docToEdge(d));
110
121
  }
111
122
  async insertEdge(edge, transaction) {
123
+ const now = Date.now();
124
+ if (edge.createdOn === undefined) {
125
+ edge.createdOn = now;
126
+ }
127
+ if (edge.updatedOn === undefined) {
128
+ edge.updatedOn = now;
129
+ }
112
130
  const session = transaction?.context;
113
131
  try {
114
132
  await this._edges.insertOne({
@@ -117,6 +135,8 @@ class MongoStorageProvider {
117
135
  sourceId: edge.sourceId,
118
136
  targetId: edge.targetId,
119
137
  type: edge.type,
138
+ createdOn: edge.createdOn,
139
+ updatedOn: edge.updatedOn,
120
140
  properties: edge.properties,
121
141
  }, { session });
122
142
  }
@@ -140,10 +160,14 @@ class MongoStorageProvider {
140
160
  const doc = await this._edges.findOne({ graphId: this._graphId, id }, { session });
141
161
  return doc ? this._docToEdge(doc) : undefined;
142
162
  }
143
- async getAllEdges(transaction) {
163
+ async getAllEdges(limit, orderBy, transaction) {
144
164
  const session = transaction?.context;
145
165
  const edges = [];
146
166
  const cursor = this._edges.find({ graphId: this._graphId }, { session }).batchSize(this._batchSize);
167
+ if (orderBy)
168
+ cursor.sort(orderBy.field, orderBy.direction);
169
+ if (limit)
170
+ cursor.limit(limit);
147
171
  for await (const doc of cursor) {
148
172
  edges.push(this._docToEdge(doc));
149
173
  }
@@ -236,6 +260,8 @@ class MongoStorageProvider {
236
260
  id: n.id,
237
261
  graphId: this._graphId,
238
262
  type: n.type,
263
+ createdOn: n.createdOn ?? Date.now(),
264
+ updatedOn: n.updatedOn ?? Date.now(),
239
265
  properties: n.properties,
240
266
  }));
241
267
  for (let i = 0; i < nodeDocs.length; i += this._batchSize) {
@@ -249,6 +275,8 @@ class MongoStorageProvider {
249
275
  sourceId: e.sourceId,
250
276
  targetId: e.targetId,
251
277
  type: e.type,
278
+ createdOn: e.createdOn ?? Date.now(),
279
+ updatedOn: e.updatedOn ?? Date.now(),
252
280
  properties: e.properties,
253
281
  }));
254
282
  for (let i = 0; i < edgeDocs.length; i += this._batchSize) {
@@ -296,7 +324,7 @@ class MongoStorageProvider {
296
324
  }
297
325
  const session = transaction?.context;
298
326
  const collection = target === 'node' ? this._nodes : this._edges;
299
- const result = await collection.updateOne({ graphId: this._graphId, id, [`properties.${key}`]: { $exists: false } }, { $set: { [`properties.${key}`]: value } }, { session });
327
+ const result = await collection.updateOne({ graphId: this._graphId, id, [`properties.${key}`]: { $exists: false } }, { $set: { [`properties.${key}`]: value, updatedOn: Date.now() } }, { session });
300
328
  if (result.matchedCount === 0) {
301
329
  const record = target === 'node' ? await this.getNode(id, transaction) : await this.getEdge(id, transaction);
302
330
  if (!record) {
@@ -311,7 +339,7 @@ class MongoStorageProvider {
311
339
  }
312
340
  const session = transaction?.context;
313
341
  const collection = target === 'node' ? this._nodes : this._edges;
314
- const result = await collection.updateOne({ graphId: this._graphId, id, [`properties.${key}`]: { $exists: true } }, { $set: { [`properties.${key}`]: value } }, { session });
342
+ const result = await collection.updateOne({ graphId: this._graphId, id, [`properties.${key}`]: { $exists: true } }, { $set: { [`properties.${key}`]: value, updatedOn: Date.now() } }, { session });
315
343
  if (result.matchedCount === 0) {
316
344
  const record = target === 'node' ? await this.getNode(id, transaction) : await this.getEdge(id, transaction);
317
345
  if (!record) {
@@ -332,7 +360,7 @@ class MongoStorageProvider {
332
360
  throw new errors_1.EdgeNotFoundError(id);
333
361
  }
334
362
  }
335
- await collection.updateOne({ graphId: this._graphId, id }, { $unset: { [`properties.${key}`]: '' } }, { session });
363
+ await collection.updateOne({ graphId: this._graphId, id }, { $unset: { [`properties.${key}`]: '' }, $set: { updatedOn: Date.now() } }, { session });
336
364
  }
337
365
  async clearProperties(target, id, transaction) {
338
366
  const session = transaction?.context;
@@ -398,6 +426,8 @@ class MongoStorageProvider {
398
426
  return {
399
427
  id: doc.id,
400
428
  type: doc.type,
429
+ createdOn: doc.createdOn,
430
+ updatedOn: doc.updatedOn,
401
431
  properties: doc.properties,
402
432
  };
403
433
  }
@@ -407,6 +437,8 @@ class MongoStorageProvider {
407
437
  sourceId: doc.sourceId,
408
438
  targetId: doc.targetId,
409
439
  type: doc.type,
440
+ createdOn: doc.createdOn,
441
+ updatedOn: doc.updatedOn,
410
442
  properties: doc.properties,
411
443
  };
412
444
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,9 @@
1
1
  export { MongoStorageProvider } from './MongoStorageProvider';
2
2
  export type { MongoStorageProviderOptions } from './MongoStorageProvider';
3
3
  export { MongoGraphFactory } from './MongoGraphFactory';
4
+ export { GraphManager } from 'grafio';
5
+ export type { GraphManagerConfig, CacheConfig, CacheStats } from 'grafio';
6
+ export { CachedStorageProvider, CacheManager } from 'grafio';
7
+ export type { EvictionStrategy, PreloadStrategy, CacheStoreType } from 'grafio';
8
+ export { InMemoryCache } from 'grafio';
9
+ export type { ICacheProvider } from 'grafio';
package/dist/index.js CHANGED
@@ -1,7 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoGraphFactory = exports.MongoStorageProvider = void 0;
3
+ exports.InMemoryCache = exports.CacheManager = exports.CachedStorageProvider = exports.GraphManager = exports.MongoGraphFactory = exports.MongoStorageProvider = void 0;
4
4
  var MongoStorageProvider_1 = require("./MongoStorageProvider");
5
5
  Object.defineProperty(exports, "MongoStorageProvider", { enumerable: true, get: function () { return MongoStorageProvider_1.MongoStorageProvider; } });
6
6
  var MongoGraphFactory_1 = require("./MongoGraphFactory");
7
7
  Object.defineProperty(exports, "MongoGraphFactory", { enumerable: true, get: function () { return MongoGraphFactory_1.MongoGraphFactory; } });
8
+ var grafio_1 = require("grafio");
9
+ Object.defineProperty(exports, "GraphManager", { enumerable: true, get: function () { return grafio_1.GraphManager; } });
10
+ var grafio_2 = require("grafio");
11
+ Object.defineProperty(exports, "CachedStorageProvider", { enumerable: true, get: function () { return grafio_2.CachedStorageProvider; } });
12
+ Object.defineProperty(exports, "CacheManager", { enumerable: true, get: function () { return grafio_2.CacheManager; } });
13
+ var grafio_3 = require("grafio");
14
+ Object.defineProperty(exports, "InMemoryCache", { enumerable: true, get: function () { return grafio_3.InMemoryCache; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grafio-mongo",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "MongoDB storage backend for grafio",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,13 +15,13 @@
15
15
 
16
16
  },
17
17
  "peerDependencies": {
18
- "grafio": ">=6.0.6",
18
+ "grafio": ">=6.1.0",
19
19
  "mongodb": ">=5.0.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/jest": "^29.5.0",
23
23
  "@types/node": "^25.6.0",
24
- "grafio": "^6.0.6",
24
+ "grafio": "^6.1.0",
25
25
  "cross-env": "^10.1.0",
26
26
  "jest": "^29.5.0",
27
27
  "rimraf": "^5.0.0",