@livequery/mongoose 2.0.25 → 2.0.27

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.
@@ -334,6 +334,8 @@ export class MongoQuery {
334
334
  ];
335
335
  }
336
336
  static #build_cursor_paging($sort, req) {
337
+ if (req.options[':after'] || req.options[':before'] || req.options[':around']) {
338
+ }
337
339
  const { pipelines, summary } = this.#parse_summary(req);
338
340
  const limit = this.#get_limit(req);
339
341
  return [
@@ -1,22 +1,26 @@
1
1
  import { RouteOptions } from "./RouteOptions.js";
2
- import { LivequeryRequest, LivequeryBaseEntity, WebsocketSyncPayload, DatabaseEvent } from '@livequery/types';
3
- import { Observable } from "rxjs";
2
+ import { LivequeryRequest, WebsocketSyncPayload, DatabaseEvent } from '@livequery/types';
3
+ import { Observable, Subject } from "rxjs";
4
4
  import { Connection } from 'mongoose';
5
- export type MongooseDatasourceOptions<T> = Array<T & {
5
+ export type LivequeryDatasourceOptions<T> = Array<T & {
6
6
  refs: string[];
7
7
  }>;
8
- export type LivequeryDatasource<Options = {}, StreamPayload = {}, Connection = any> = {
9
- init(routes: MongooseDatasourceOptions<Options>, connections: {
10
- [name: string]: Connection;
11
- }): Promise<void>;
8
+ export type LivequeryDatasourceProps<OptionsType = {}, RawDataChangeType = {}, InjectType = any> = {
9
+ routes: LivequeryDatasourceOptions<OptionsType>;
10
+ deps: {
11
+ [name: string]: InjectType;
12
+ };
13
+ rawChanges: Observable<RawDataChangeType>;
14
+ };
15
+ export type LivequeryDatasource<OptionsType = {}, RawDataChangeType = {}, InjectType = any> = {
16
+ init(props: LivequeryDatasourceProps<OptionsType, RawDataChangeType, InjectType>): any;
12
17
  query(query: LivequeryRequest): any;
13
- enable_realtime?: (stream: Observable<StreamPayload>) => Observable<WebsocketSyncPayload>;
18
+ $: Observable<WebsocketSyncPayload>;
14
19
  };
15
20
  export declare class MongooseDatasource implements LivequeryDatasource<RouteOptions, DatabaseEvent, Connection> {
16
21
  #private;
17
- init(routes: MongooseDatasourceOptions<RouteOptions>, connections: {
18
- [name: string]: Connection;
19
- }): Promise<void>;
22
+ readonly $: Subject<WebsocketSyncPayload>;
23
+ init({ deps, rawChanges, routes }: LivequeryDatasourceProps<RouteOptions, DatabaseEvent, Connection>): void;
20
24
  query(query: LivequeryRequest): Promise<import("mongodb").DeleteResult | import("mongoose").UpdateWriteOpResult | {
21
25
  items: any[];
22
26
  summary: any;
@@ -41,12 +45,4 @@ export declare class MongooseDatasource implements LivequeryDatasource<RouteOpti
41
45
  } | {
42
46
  item: any;
43
47
  }>;
44
- enable_realtime<T extends LivequeryBaseEntity = LivequeryBaseEntity>(d: Observable<DatabaseEvent>): Observable<{
45
- new_data: LivequeryBaseEntity;
46
- old_data: LivequeryBaseEntity;
47
- table: string;
48
- type: "added" | "removed" | "modified";
49
- old_ref: string;
50
- new_ref: string;
51
- }>;
52
48
  }
@@ -1,5 +1,5 @@
1
1
  import { Cursor } from './Cursor.js';
2
- import { map, mergeAll } from "rxjs";
2
+ import { map, mergeAll, Subject, pipe } from "rxjs";
3
3
  import { MongoQuery } from "./MongoQuery.js";
4
4
  import { ObjectId } from 'bson';
5
5
  export class MongooseDatasource {
@@ -7,8 +7,10 @@ export class MongooseDatasource {
7
7
  #routes = new Map();
8
8
  #realtime_collections = new Map();
9
9
  #conections = {};
10
- async init(routes, connections) {
11
- this.#conections = { ...connections };
10
+ $ = new Subject;
11
+ init({ deps, rawChanges, routes }) {
12
+ this.#conections = { ...deps };
13
+ rawChanges.pipe(this.#stream()).subscribe(this.$);
12
14
  for (const { refs, ...options } of routes) {
13
15
  for (const ref of refs) {
14
16
  const schema_ref = ref.replaceAll(':', '');
@@ -153,55 +155,55 @@ export class MongooseDatasource {
153
155
  }, {});
154
156
  return await model.deleteOne(keys);
155
157
  }
156
- enable_realtime(d) {
157
- function parse_refs(ref, o, n) {
158
- const keys = ref.split('/').map((k, i) => {
159
- if (i % 2 == 0)
160
- return { v: { old_ref: k, new_ref: k } };
161
- if (Array.isArray(o[k]) || Array.isArray(n[k])) {
162
- return [
163
- ...Array.isArray(o[k]) ? o[k] : [],
164
- ...Array.isArray(n[k]) ? n[k] : []
165
- ].reduce((p, c) => {
166
- p.set(c, {
167
- old_ref: Array.isArray(o[k]) && o[k].includes(c) ? c : null,
168
- new_ref: Array.isArray(n[k]) && n[k].includes(c) ? c : null,
169
- });
170
- return p;
171
- }, new Map());
172
- }
158
+ #caculateRef(arr) {
159
+ const v = arr[0];
160
+ const x = v instanceof Map ? [...v] : Object.entries(v);
161
+ return x.map(([k, w]) => {
162
+ const { old_ref, new_ref } = w;
163
+ const next = arr.slice(1);
164
+ if (next.length == 0)
165
+ return { old_ref, new_ref };
166
+ const n = this.#caculateRef(next);
167
+ return n.map(nn => {
173
168
  return {
174
- v: {
175
- old_ref: o[k],
176
- new_ref: n[k]
177
- }
169
+ old_ref: `${old_ref}/${nn.old_ref}`,
170
+ new_ref: `${new_ref}/${nn.new_ref}`
178
171
  };
179
172
  });
180
- function parse(arr) {
181
- const v = arr[0];
182
- const x = v instanceof Map ? [...v] : Object.entries(v);
183
- return x.map(([k, w]) => {
184
- const { old_ref, new_ref } = w;
185
- const next = arr.slice(1);
186
- if (next.length == 0)
187
- return { old_ref, new_ref };
188
- const n = parse(next);
189
- return n.map(nn => {
190
- return {
191
- old_ref: `${old_ref}/${nn.old_ref}`,
192
- new_ref: `${new_ref}/${nn.new_ref}`
193
- };
173
+ }).flat(2);
174
+ }
175
+ #parse(ref, o, n) {
176
+ const keys = ref.split('/').map((k, i) => {
177
+ if (i % 2 == 0)
178
+ return { v: { old_ref: k, new_ref: k } };
179
+ if (Array.isArray(o[k]) || Array.isArray(n[k])) {
180
+ return [
181
+ ...Array.isArray(o[k]) ? o[k] : [],
182
+ ...Array.isArray(n[k]) ? n[k] : []
183
+ ].reduce((p, c) => {
184
+ p.set(c, {
185
+ old_ref: Array.isArray(o[k]) && o[k].includes(c) ? c : null,
186
+ new_ref: Array.isArray(n[k]) && n[k].includes(c) ? c : null,
194
187
  });
195
- }).flat(2);
188
+ return p;
189
+ }, new Map());
196
190
  }
197
- return parse(keys);
198
- }
199
- return d.pipe(map((event) => {
191
+ return {
192
+ v: {
193
+ old_ref: o[k],
194
+ new_ref: n[k]
195
+ }
196
+ };
197
+ });
198
+ return this.#caculateRef(keys);
199
+ }
200
+ #stream() {
201
+ return pipe(map((event) => {
200
202
  const refs = this.#realtime_collections.get(event.table);
201
203
  if (!refs)
202
204
  return [];
203
205
  return [...refs].map(ref => {
204
- const refs_list = parse_refs(ref, event.old_data, event.new_data);
206
+ const refs_list = this.#parse(ref, event.old_data, event.new_data);
205
207
  return refs_list.map(({ new_ref, old_ref }) => ({
206
208
  new_data: event.new_data,
207
209
  old_data: event.old_data,
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "repository": {
7
7
  "url": "git@github.com:livequery/mongoose.git"
8
8
  },
9
- "version": "2.0.25",
9
+ "version": "2.0.27",
10
10
  "description": "Mongoose datasource mapping for @livequery ecosystem",
11
11
  "main": "./build/src/index.js",
12
12
  "types": "./build/src/index.d.ts",