@eleven-am/pondsocket 0.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.
Files changed (64) hide show
  1. package/.eslintrc.js +28 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/pondsocket.iml +12 -0
  4. package/LICENSE +674 -0
  5. package/base.d.ts +1 -0
  6. package/base.js +17 -0
  7. package/client.d.ts +1 -0
  8. package/client.js +17 -0
  9. package/index.d.ts +1 -0
  10. package/index.js +17 -0
  11. package/jest.config.js +11 -0
  12. package/package.json +48 -0
  13. package/pondBase/baseClass.d.ts +37 -0
  14. package/pondBase/baseClass.js +111 -0
  15. package/pondBase/baseClass.test.js +73 -0
  16. package/pondBase/enums.d.ts +9 -0
  17. package/pondBase/enums.js +14 -0
  18. package/pondBase/index.d.ts +6 -0
  19. package/pondBase/index.js +22 -0
  20. package/pondBase/pondBase.d.ts +41 -0
  21. package/pondBase/pondBase.js +60 -0
  22. package/pondBase/pondBase.test.js +101 -0
  23. package/pondBase/pubSub.d.ts +73 -0
  24. package/pondBase/pubSub.js +138 -0
  25. package/pondBase/pubSub.test.js +309 -0
  26. package/pondBase/simpleBase.d.ts +131 -0
  27. package/pondBase/simpleBase.js +211 -0
  28. package/pondBase/simpleBase.test.js +153 -0
  29. package/pondBase/types.d.ts +2 -0
  30. package/pondBase/types.js +2 -0
  31. package/pondClient/channel.d.ts +66 -0
  32. package/pondClient/channel.js +152 -0
  33. package/pondClient/index.d.ts +2 -0
  34. package/pondClient/index.js +18 -0
  35. package/pondClient/socket.d.ts +42 -0
  36. package/pondClient/socket.js +116 -0
  37. package/pondSocket/channel.d.ts +134 -0
  38. package/pondSocket/channel.js +287 -0
  39. package/pondSocket/channel.test.js +377 -0
  40. package/pondSocket/channelMiddleWare.d.ts +26 -0
  41. package/pondSocket/channelMiddleWare.js +36 -0
  42. package/pondSocket/endpoint.d.ts +90 -0
  43. package/pondSocket/endpoint.js +323 -0
  44. package/pondSocket/endpoint.test.js +513 -0
  45. package/pondSocket/enums.d.ts +19 -0
  46. package/pondSocket/enums.js +25 -0
  47. package/pondSocket/index.d.ts +7 -0
  48. package/pondSocket/index.js +23 -0
  49. package/pondSocket/pondChannel.d.ts +79 -0
  50. package/pondSocket/pondChannel.js +219 -0
  51. package/pondSocket/pondChannel.test.js +430 -0
  52. package/pondSocket/pondResponse.d.ts +25 -0
  53. package/pondSocket/pondResponse.js +120 -0
  54. package/pondSocket/pondSocket.d.ts +47 -0
  55. package/pondSocket/pondSocket.js +94 -0
  56. package/pondSocket/server.test.js +136 -0
  57. package/pondSocket/socketMiddleWare.d.ts +6 -0
  58. package/pondSocket/socketMiddleWare.js +32 -0
  59. package/pondSocket/types.d.ts +74 -0
  60. package/pondSocket/types.js +2 -0
  61. package/socket.d.ts +1 -0
  62. package/socket.js +17 -0
  63. package/tsconfig.eslint.json +5 -0
  64. package/tsconfig.json +90 -0
package/base.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./pondBase"), exports);
package/client.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './pondClient';
package/client.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./pondClient"), exports);
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './pondSocket';
package/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./pondSocket"), exports);
package/jest.config.js ADDED
@@ -0,0 +1,11 @@
1
+ module.exports = {
2
+ transform: {
3
+ '^.+\\.ts?$': 'ts-jest',
4
+ },
5
+ moduleFileExtensions: ['ts', 'js', 'json', 'node', 'd.ts'],
6
+ collectCoverage: true,
7
+ collectCoverageFrom: [
8
+ "src/**/*.{js,jsx,ts,tsx}",
9
+ "!src/**/*.d.ts"
10
+ ],
11
+ };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@eleven-am/pondsocket",
3
+ "version": "0.1.0",
4
+ "description": "PondSocket is a fast simple socket server",
5
+ "keywords": [
6
+ "socket",
7
+ "server",
8
+ "ws",
9
+ "websocket",
10
+ "pubsub",
11
+ "presence",
12
+ "realtime",
13
+ "realtime server"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "scripts": {
19
+ "test": "jest --coverage",
20
+ "build": "tsc"
21
+ },
22
+ "author": "Roy OSSAI",
23
+ "license": "GPL-3.0",
24
+ "main": "./index.js",
25
+ "types": "./index.d.ts",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/Eleven-am/pondSocket.git"
29
+ },
30
+ "dependencies": {
31
+ "rxjs": "^7.5.6",
32
+ "ws": "^8.8.1"
33
+ },
34
+ "devDependencies": {
35
+ "@types/jest": "^29.1.2",
36
+ "@types/node": "^16.10.3",
37
+ "@types/ws": "^8.5.3",
38
+ "@typescript-eslint/eslint-plugin": "^5.40.1",
39
+ "@typescript-eslint/parser": "^5.40.1",
40
+ "eslint": "^8.25.0",
41
+ "jest": "^29.0.1",
42
+ "process": "^0.11.10",
43
+ "superwstest": "^2.0.3",
44
+ "ts-jest": "^29.0.3",
45
+ "ts-node": "^10.9.1",
46
+ "typescript": "^4.8.4"
47
+ }
48
+ }
@@ -0,0 +1,37 @@
1
+ import {default_t, PondPath} from "./types";
2
+
3
+ export interface Resolver {
4
+ params: default_t<string>;
5
+ query: default_t<string>;
6
+ address: string;
7
+ }
8
+
9
+ export declare class BaseClass {
10
+
11
+ /**
12
+ * @desc compares string to string | regex
13
+ * @param string - the string to compare to the pattern
14
+ * @param pattern - the pattern to compare to the string
15
+ */
16
+ compareStringToPattern(string: string, pattern: string | RegExp): boolean;
17
+
18
+ /**
19
+ * @desc Checks if the given object is empty
20
+ * @param obj - the object to check
21
+ */
22
+ isObjectEmpty(obj: object): boolean;
23
+
24
+ /**
25
+ * @desc Generates a pond request resolver object
26
+ * @param path - the path to resolve
27
+ * @param address - the address to resolve
28
+ */
29
+ generateEventRequest(path: PondPath, address: string): Resolver | null;
30
+
31
+ /**
32
+ * @desc Compares if two objects are equal
33
+ * @param obj1 - the first object
34
+ * @param obj2 - the second object
35
+ */
36
+ areEqual<T>(obj1: T, obj2: T): boolean;
37
+ }
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseClass = void 0;
4
+ class BaseClass {
5
+ /**
6
+ * @desc checks if the pattern is matchable
7
+ * @param pattern - the pattern to check
8
+ */
9
+ static isPatternMatchable(pattern) {
10
+ return typeof pattern === 'string' && pattern.includes(':');
11
+ }
12
+ /**
13
+ * @desc compares string to string | regex
14
+ * @param string - the string to compare to the pattern
15
+ * @param pattern - the pattern to compare to the string
16
+ */
17
+ compareStringToPattern(string, pattern) {
18
+ if (typeof pattern === 'string')
19
+ return string.split('?')[0] === pattern;
20
+ else
21
+ return pattern.test(string);
22
+ }
23
+ /**
24
+ * @desc Checks if the given object is empty
25
+ * @param obj - the object to check
26
+ */
27
+ isObjectEmpty(obj) {
28
+ return Object.keys(obj).length === 0;
29
+ }
30
+ /**
31
+ * @desc Generates a pond request resolver object
32
+ * @param path - the path to resolve
33
+ * @param address - the address to resolve
34
+ */
35
+ generateEventRequest(path, address) {
36
+ const match = this._matchStringToPattern(address, path);
37
+ if (match)
38
+ return {
39
+ params: match, query: this._parseQueries(address), address: address
40
+ };
41
+ return null;
42
+ }
43
+ /**
44
+ * @desc Compares if two objects are equal
45
+ * @param obj1 - the first object
46
+ * @param obj2 - the second object
47
+ */
48
+ areEqual(obj1, obj2) {
49
+ return JSON.stringify(obj1) === JSON.stringify(obj2);
50
+ }
51
+ /**
52
+ * @desc Creates an object from the params of a path
53
+ * @param path - the path to create the object from
54
+ *
55
+ * @example
56
+ * /api/id?name=abc should return { name: 'abc' }
57
+ * /api/id?name=abc&age=123 should return { name: 'abc', age: '123' }
58
+ */
59
+ _parseQueries(path) {
60
+ const obj = {};
61
+ const params = path.split('?')[1];
62
+ if (params) {
63
+ params.split('&').forEach(param => {
64
+ const [key, value] = param.split('=');
65
+ obj[key] = value;
66
+ });
67
+ }
68
+ return obj;
69
+ }
70
+ /**
71
+ * @desc Returns the {key: value} matches of a string
72
+ * @param string - the string to create the regex from
73
+ * @param pattern - the pattern to match
74
+ *
75
+ * @example
76
+ * /api/:id should match /api/123 and return { id: 123 }
77
+ * /api/:id/:name should match /api/123/abc and return { id: 123, name: abc }
78
+ * hello:id should match hello:123 and return { id: 123 }
79
+ * @private
80
+ */
81
+ _matchString(string, pattern) {
82
+ const replace = pattern.replace(/:[^/]+/g, '([^/]+)');
83
+ const regExp = new RegExp(`^${replace}$`);
84
+ const matches = string.split('?')[0].match(regExp);
85
+ if (matches) {
86
+ const keys = pattern.match(/:[^/]+/g);
87
+ if (keys) {
88
+ const obj = {};
89
+ keys.forEach((key, index) => {
90
+ obj[key.replace(':', '')] = matches[index + 1].replace(/\?.*$/, '');
91
+ });
92
+ return obj;
93
+ }
94
+ }
95
+ return null;
96
+ }
97
+ /**
98
+ * @desc matches a string to a pattern and returns its params if any
99
+ * @param string - the string to match
100
+ * @param pattern - the pattern to match to
101
+ */
102
+ _matchStringToPattern(string, pattern) {
103
+ if (BaseClass.isPatternMatchable(pattern))
104
+ return this._matchString(string, pattern);
105
+ const valid = this.compareStringToPattern(string, pattern);
106
+ if (valid)
107
+ return {};
108
+ return null;
109
+ }
110
+ }
111
+ exports.BaseClass = BaseClass;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const baseClass_1 = require("./baseClass");
4
+ describe('BaseClass', () => {
5
+ const baseClass = new baseClass_1.BaseClass();
6
+ it('should return true when object is empty', () => {
7
+ expect(baseClass.isObjectEmpty({})).toBe(true);
8
+ });
9
+ it('should return false when object is not empty', () => {
10
+ expect(baseClass.isObjectEmpty({ test: 5 })).toBe(false);
11
+ });
12
+ it('should return true if a string matches a regex | string', () => {
13
+ const regex = new RegExp(/^test/);
14
+ expect(baseClass.compareStringToPattern('test', regex)).toBe(true);
15
+ const string = 'test';
16
+ expect(baseClass.compareStringToPattern('test', string)).toBe(true);
17
+ });
18
+ it('should return false if a string does not match a regex | string', () => {
19
+ const regex = new RegExp(/^test$/);
20
+ expect(baseClass.compareStringToPattern('test2', regex)).toBe(false);
21
+ const string = 'test';
22
+ expect(baseClass.compareStringToPattern('test2', string)).toBe(false);
23
+ });
24
+ it('should return the params of a string matching the pattern', () => {
25
+ const pattern = '/test/:id';
26
+ const secondPattern = '/test/:id/:id2';
27
+ const string = '/test/5';
28
+ const secondString = '/test/5/6';
29
+ expect(baseClass['_matchString'](string, pattern)).toEqual({ id: '5' });
30
+ expect(baseClass['_matchString'](secondString, secondPattern)).toEqual({ id: '5', id2: '6' });
31
+ // this function fails if the pattern is not a string or regex
32
+ expect(baseClass['_matchString'](secondString, pattern)).toEqual(null);
33
+ // But will return null if the string is smaller than the pattern
34
+ expect(baseClass['_matchString'](string, secondPattern)).toEqual(null);
35
+ //it should also match patterns without the slash
36
+ const thirdPattern = 'test:id';
37
+ const thirdString = 'test5';
38
+ expect(baseClass['_matchString'](thirdString, thirdPattern)).toEqual({ id: '5' });
39
+ });
40
+ it('should return the query of string', () => {
41
+ const string = '/test/5?test=5';
42
+ const secondString = '/test/5?test=5&test2=6';
43
+ expect(baseClass['_parseQueries'](string)).toEqual({ test: '5' });
44
+ expect(baseClass['_parseQueries'](secondString)).toEqual({ test: '5', test2: '6' });
45
+ });
46
+ it('should return true if an object matches another object', () => {
47
+ const object = { test: 5 };
48
+ const secondObject = { test: 5, test2: 6 };
49
+ expect(baseClass.areEqual(object, object)).toBe(true);
50
+ expect(baseClass.areEqual(object, secondObject)).toBe(false);
51
+ });
52
+ it('should return null if the string does not match the pattern', () => {
53
+ const pattern = 'pondSocket';
54
+ const string = '/test2/5';
55
+ expect(baseClass['_matchStringToPattern'](string, pattern)).toBe(null);
56
+ });
57
+ it('should return the params of a string matching the pattern', () => {
58
+ const pattern = 'pondSocket';
59
+ const string = 'pondSocket';
60
+ expect(baseClass['_matchStringToPattern'](string, pattern)).toEqual({});
61
+ });
62
+ it('should generateEventRequest', () => {
63
+ const pattern = 'pondSocket:test';
64
+ const string = 'pondSockethello?test=5&test2=6';
65
+ expect(baseClass.generateEventRequest(pattern, string)).toEqual({
66
+ address: string,
67
+ params: { test: 'hello' },
68
+ query: { test: '5', test2: '6' }
69
+ });
70
+ const unMatchingString = 'pondXocket2hello?test=5&test2=6';
71
+ expect(baseClass.generateEventRequest(pattern, unMatchingString)).toEqual(null);
72
+ });
73
+ });
@@ -0,0 +1,9 @@
1
+ export declare enum ResponsePicker {
2
+ POND = "POND",
3
+ CHANNEL = "CHANNEL"
4
+ }
5
+ export declare enum PondBaseActions {
6
+ ADD_TO_POND = "ADD_TO_POND",
7
+ REMOVE_FROM_POND = "REMOVE_FROM_POND",
8
+ UPDATE_IN_POND = "UPDATE_IN_POND"
9
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PondBaseActions = exports.ResponsePicker = void 0;
4
+ var ResponsePicker;
5
+ (function (ResponsePicker) {
6
+ ResponsePicker["POND"] = "POND";
7
+ ResponsePicker["CHANNEL"] = "CHANNEL";
8
+ })(ResponsePicker = exports.ResponsePicker || (exports.ResponsePicker = {}));
9
+ var PondBaseActions;
10
+ (function (PondBaseActions) {
11
+ PondBaseActions["ADD_TO_POND"] = "ADD_TO_POND";
12
+ PondBaseActions["REMOVE_FROM_POND"] = "REMOVE_FROM_POND";
13
+ PondBaseActions["UPDATE_IN_POND"] = "UPDATE_IN_POND";
14
+ })(PondBaseActions = exports.PondBaseActions || (exports.PondBaseActions = {}));
@@ -0,0 +1,6 @@
1
+ export * from './baseClass';
2
+ export * from './pondBase';
3
+ export * from './pubSub';
4
+ export * from './types';
5
+ export * from './enums';
6
+ export * from './simpleBase';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./baseClass"), exports);
18
+ __exportStar(require("./pondBase"), exports);
19
+ __exportStar(require("./pubSub"), exports);
20
+ __exportStar(require("./types"), exports);
21
+ __exportStar(require("./enums"), exports);
22
+ __exportStar(require("./simpleBase"), exports);
@@ -0,0 +1,41 @@
1
+ import {Subscription} from "./pubSub";
2
+ import {SimpleBase} from "./simpleBase";
3
+ import {PondBaseActions} from "./enums";
4
+
5
+ declare type ExtractSameValueType<A, B, C extends keyof A> = {
6
+ [K in keyof B]: B[K] extends A[C] ? A[C] extends B[K] ? K : never : never;
7
+ }[keyof B];
8
+
9
+ export declare class PondBase<T> extends SimpleBase<T> {
10
+
11
+ constructor();
12
+
13
+ /**
14
+ * @des Generate a key for a new document
15
+ */
16
+ private get _nanoid();
17
+
18
+ /**
19
+ * @desc Subscribe to the database
20
+ * @param handler - The handler to call when the database is updated
21
+ */
22
+ subscribe(handler: (docs: T[], change: T | null, action: PondBaseActions) => void): Subscription;
23
+
24
+ /**
25
+ * @desc Add a document to the database
26
+ * @param doc - The document to add
27
+ */
28
+ addDoc(doc: T): import("./simpleBase").PondDocument<T>;
29
+
30
+ /**
31
+ * @desc Left join two ponds on a key on this pond and a foreign key on the other pond
32
+ * @param pond - The pond to join with
33
+ * @param key - The key to join on
34
+ * @param foreignKey - The foreign key to join on
35
+ */
36
+ leftJoin<U, A extends keyof T, B extends ExtractSameValueType<T, U, A>>(pond: PondBase<U>, key: A, foreignKey: B): PondBase<T & {
37
+ [K in A]: U | null;
38
+ }>;
39
+ }
40
+
41
+ export {};
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PondBase = void 0;
4
+ const pubSub_1 = require("./pubSub");
5
+ const simpleBase_1 = require("./simpleBase");
6
+ const enums_1 = require("./enums");
7
+ class PondBase extends simpleBase_1.SimpleBase {
8
+ constructor() {
9
+ const broadcast = new pubSub_1.Broadcast();
10
+ super((data) => broadcast.publish(data));
11
+ this._broadcast = broadcast;
12
+ }
13
+ /**
14
+ * @des Generate a key for a new document
15
+ */
16
+ get _nanoid() {
17
+ let id = '';
18
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
19
+ for (let i = 0; i < 21; i++) {
20
+ id += chars.charAt(Math.floor(Math.random() * chars.length));
21
+ }
22
+ return id;
23
+ }
24
+ /**
25
+ * @desc Subscribe to the database
26
+ * @param handler - The handler to call when the database is updated
27
+ */
28
+ subscribe(handler) {
29
+ return this._broadcast.subscribe((data) => {
30
+ let change = enums_1.PondBaseActions.UPDATE_IN_POND;
31
+ if (data.oldValue === null)
32
+ change = enums_1.PondBaseActions.ADD_TO_POND;
33
+ else if (data.currentValue === null)
34
+ change = enums_1.PondBaseActions.REMOVE_FROM_POND;
35
+ handler(Object.values(this._getDB()), data.currentValue || data.oldValue, change);
36
+ });
37
+ }
38
+ /**
39
+ * @desc Add a document to the database
40
+ * @param doc - The document to add
41
+ */
42
+ addDoc(doc) {
43
+ return super.set(this._nanoid, doc);
44
+ }
45
+ /**
46
+ * @desc Left join two ponds on a key on this pond and a foreign key on the other pond
47
+ * @param pond - The pond to join with
48
+ * @param key - The key to join on
49
+ * @param foreignKey - The foreign key to join on
50
+ */
51
+ leftJoin(pond, key, foreignKey) {
52
+ const newPond = new PondBase();
53
+ for (const doc of this) {
54
+ const foreignDoc = pond.find((d) => d[foreignKey] === doc.doc[key]);
55
+ newPond.set(doc.id, { ...doc.doc, [key]: (foreignDoc === null || foreignDoc === void 0 ? void 0 : foreignDoc.doc) || null });
56
+ }
57
+ return newPond;
58
+ }
59
+ }
60
+ exports.PondBase = PondBase;
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const pondBase_1 = require("./pondBase");
4
+ const enums_1 = require("./enums");
5
+ describe('PondBase', () => {
6
+ it('should be able to take in a subscriber', () => {
7
+ const base = new pondBase_1.PondBase();
8
+ expect(base['_broadcast']['_subscribers'].size).toBe(0);
9
+ const mockSubscriber = jest.fn();
10
+ base.subscribe(mockSubscriber);
11
+ expect(base['_broadcast']['_subscribers'].size).toBe(1);
12
+ });
13
+ it('should be able to remove a subscriber', () => {
14
+ const base = new pondBase_1.PondBase();
15
+ expect(base['_broadcast']['_subscribers'].size).toBe(0);
16
+ const mockSubscriber = jest.fn();
17
+ const subscription = base.subscribe(mockSubscriber);
18
+ expect(base['_broadcast']['_subscribers'].size).toBe(1);
19
+ subscription.unsubscribe();
20
+ expect(base['_broadcast']['_subscribers'].size).toBe(0);
21
+ });
22
+ it('should fire the subscriber when a document is added', () => {
23
+ const base = new pondBase_1.PondBase();
24
+ const mockSubscriber = jest.fn();
25
+ base.subscribe(mockSubscriber);
26
+ base.set('test', { name: 'test' });
27
+ expect(mockSubscriber).toBeCalled();
28
+ expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
29
+ });
30
+ it('should fire the subscriber when a document is removed', () => {
31
+ const base = new pondBase_1.PondBase();
32
+ const mockSubscriber = jest.fn();
33
+ base.subscribe(mockSubscriber);
34
+ const data = base.set('test', { name: 'test' });
35
+ expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
36
+ mockSubscriber.mockClear();
37
+ data.removeDoc();
38
+ expect(mockSubscriber).toBeCalledWith([], { name: "test" }, enums_1.PondBaseActions.REMOVE_FROM_POND);
39
+ });
40
+ it('should fire the subscriber when a document is updated', () => {
41
+ const base = new pondBase_1.PondBase();
42
+ const mockSubscriber = jest.fn();
43
+ base.subscribe(mockSubscriber);
44
+ const data = base.set('test', { name: 'test' });
45
+ expect(mockSubscriber).toBeCalledWith([{ name: "test" }], { name: "test" }, enums_1.PondBaseActions.ADD_TO_POND);
46
+ mockSubscriber.mockClear();
47
+ data.updateDoc({ name: 'test2' });
48
+ expect(mockSubscriber).toBeCalledWith([{ name: "test2" }], { name: "test2" }, enums_1.PondBaseActions.UPDATE_IN_POND);
49
+ });
50
+ it('should add a document to the database', () => {
51
+ var _a, _b;
52
+ const base = new pondBase_1.PondBase();
53
+ const data = base.addDoc({ name: 'test' });
54
+ expect(data.id).toBeDefined();
55
+ expect((_a = base.get(data.id)) === null || _a === void 0 ? void 0 : _a.doc).toEqual(data.doc);
56
+ expect((_b = base.get(data.id)) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
57
+ });
58
+ it('should left join with another pond', () => {
59
+ const owners = new pondBase_1.PondBase();
60
+ const pets = new pondBase_1.PondBase();
61
+ owners.addDoc({ name: 'test', age: 10 });
62
+ owners.set('test2', { name: 'test2', age: 12 });
63
+ pets.addDoc({ name: 'test', owner: 'test' });
64
+ pets.addDoc({ name: 'test2', owner: 'test2' });
65
+ pets.set('test3', { name: 'test3', owner: 'test3' });
66
+ const joined = pets.leftJoin(owners, 'owner', 'name');
67
+ expect([...joined].map(({ doc }) => doc)).toEqual([
68
+ { name: 'test', owner: { name: 'test', age: 10 } },
69
+ { name: 'test2', owner: { name: 'test2', age: 12 } },
70
+ { name: 'test3', owner: null }
71
+ ]);
72
+ });
73
+ it('should be able to get the keys', () => {
74
+ const base = new pondBase_1.PondBase();
75
+ base.set('test', { name: 'test' });
76
+ base.set('test2', { name: 'test2' });
77
+ expect(base.keys).toEqual(['test', 'test2']);
78
+ });
79
+ it('should be able to get the values', () => {
80
+ const base = new pondBase_1.PondBase();
81
+ base.addDoc({ name: 'test' });
82
+ base.set('test2', { name: 'test2' });
83
+ expect(base.values).toEqual([{ name: 'test' }, { name: 'test2' }]);
84
+ });
85
+ it('should be able to upsert a document', () => {
86
+ var _a, _b;
87
+ const base = new pondBase_1.PondBase();
88
+ base.upsert('test', { name: 'test' });
89
+ expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
90
+ base.upsert('test', { name: 'test2' });
91
+ expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test2' });
92
+ });
93
+ it('should be able to getOrCreate a document with a function', () => {
94
+ var _a, _b;
95
+ const base = new pondBase_1.PondBase();
96
+ base.getOrCreate('test', () => ({ name: 'test' }));
97
+ expect((_a = base.get('test')) === null || _a === void 0 ? void 0 : _a.doc).toEqual({ name: 'test' });
98
+ base.getOrCreate('test', () => ({ name: 'test2' }));
99
+ expect((_b = base.get('test')) === null || _b === void 0 ? void 0 : _b.doc).toEqual({ name: 'test' });
100
+ });
101
+ });