@peers-app/peers-sdk 0.1.4

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 (234) hide show
  1. package/README.md +1 -0
  2. package/dist/context/data-context.d.ts +31 -0
  3. package/dist/context/data-context.js +56 -0
  4. package/dist/context/index.d.ts +3 -0
  5. package/dist/context/index.js +19 -0
  6. package/dist/context/user-context-singleton.d.ts +11 -0
  7. package/dist/context/user-context-singleton.js +121 -0
  8. package/dist/context/user-context.d.ts +55 -0
  9. package/dist/context/user-context.js +205 -0
  10. package/dist/data/assistants.d.ts +68 -0
  11. package/dist/data/assistants.js +64 -0
  12. package/dist/data/change-tracking.d.ts +219 -0
  13. package/dist/data/change-tracking.js +119 -0
  14. package/dist/data/channels.d.ts +29 -0
  15. package/dist/data/channels.js +25 -0
  16. package/dist/data/data-locks.d.ts +37 -0
  17. package/dist/data/data-locks.js +180 -0
  18. package/dist/data/data-locks.test.d.ts +1 -0
  19. package/dist/data/data-locks.test.js +456 -0
  20. package/dist/data/device-sync-info.d.ts +19 -0
  21. package/dist/data/device-sync-info.js +24 -0
  22. package/dist/data/devices.d.ts +51 -0
  23. package/dist/data/devices.js +36 -0
  24. package/dist/data/embeddings.d.ts +47 -0
  25. package/dist/data/embeddings.js +36 -0
  26. package/dist/data/files/file-read-stream.d.ts +27 -0
  27. package/dist/data/files/file-read-stream.js +195 -0
  28. package/dist/data/files/file-write-stream.d.ts +20 -0
  29. package/dist/data/files/file-write-stream.js +113 -0
  30. package/dist/data/files/file.types.d.ts +47 -0
  31. package/dist/data/files/file.types.js +55 -0
  32. package/dist/data/files/files.d.ts +28 -0
  33. package/dist/data/files/files.js +127 -0
  34. package/dist/data/files/files.test.d.ts +1 -0
  35. package/dist/data/files/files.test.js +728 -0
  36. package/dist/data/files/index.d.ts +4 -0
  37. package/dist/data/files/index.js +23 -0
  38. package/dist/data/group-member-roles.d.ts +9 -0
  39. package/dist/data/group-member-roles.js +25 -0
  40. package/dist/data/group-members.d.ts +39 -0
  41. package/dist/data/group-members.js +68 -0
  42. package/dist/data/group-members.test.d.ts +1 -0
  43. package/dist/data/group-members.test.js +287 -0
  44. package/dist/data/group-permissions.d.ts +8 -0
  45. package/dist/data/group-permissions.js +73 -0
  46. package/dist/data/group-share.d.ts +50 -0
  47. package/dist/data/group-share.js +196 -0
  48. package/dist/data/groups.d.ts +50 -0
  49. package/dist/data/groups.js +73 -0
  50. package/dist/data/groups.test.d.ts +1 -0
  51. package/dist/data/groups.test.js +153 -0
  52. package/dist/data/index.d.ts +31 -0
  53. package/dist/data/index.js +47 -0
  54. package/dist/data/knowledge/knowledge-frames.d.ts +34 -0
  55. package/dist/data/knowledge/knowledge-frames.js +34 -0
  56. package/dist/data/knowledge/knowledge-links.d.ts +30 -0
  57. package/dist/data/knowledge/knowledge-links.js +25 -0
  58. package/dist/data/knowledge/knowledge-values.d.ts +35 -0
  59. package/dist/data/knowledge/knowledge-values.js +35 -0
  60. package/dist/data/knowledge/peer-types.d.ts +112 -0
  61. package/dist/data/knowledge/peer-types.js +27 -0
  62. package/dist/data/knowledge/predicates.d.ts +34 -0
  63. package/dist/data/knowledge/predicates.js +27 -0
  64. package/dist/data/messages.d.ts +57 -0
  65. package/dist/data/messages.js +97 -0
  66. package/dist/data/orm/client-proxy.data-source.d.ts +27 -0
  67. package/dist/data/orm/client-proxy.data-source.js +65 -0
  68. package/dist/data/orm/cursor.d.ts +25 -0
  69. package/dist/data/orm/cursor.js +47 -0
  70. package/dist/data/orm/cursor.test.d.ts +1 -0
  71. package/dist/data/orm/cursor.test.js +315 -0
  72. package/dist/data/orm/data-query.d.ts +96 -0
  73. package/dist/data/orm/data-query.js +208 -0
  74. package/dist/data/orm/data-query.mongo.d.ts +17 -0
  75. package/dist/data/orm/data-query.mongo.js +267 -0
  76. package/dist/data/orm/data-query.mongo.test.d.ts +1 -0
  77. package/dist/data/orm/data-query.mongo.test.js +398 -0
  78. package/dist/data/orm/data-query.sqlite.d.ts +14 -0
  79. package/dist/data/orm/data-query.sqlite.js +297 -0
  80. package/dist/data/orm/data-query.sqlite.test.d.ts +1 -0
  81. package/dist/data/orm/data-query.sqlite.test.js +377 -0
  82. package/dist/data/orm/data-query.test.d.ts +1 -0
  83. package/dist/data/orm/data-query.test.js +553 -0
  84. package/dist/data/orm/decorators.d.ts +6 -0
  85. package/dist/data/orm/decorators.js +21 -0
  86. package/dist/data/orm/dependency-injection.test.d.ts +1 -0
  87. package/dist/data/orm/dependency-injection.test.js +171 -0
  88. package/dist/data/orm/doc.d.ts +26 -0
  89. package/dist/data/orm/doc.js +124 -0
  90. package/dist/data/orm/event-registry.d.ts +24 -0
  91. package/dist/data/orm/event-registry.js +40 -0
  92. package/dist/data/orm/event-registry.test.d.ts +1 -0
  93. package/dist/data/orm/event-registry.test.js +44 -0
  94. package/dist/data/orm/factory.d.ts +8 -0
  95. package/dist/data/orm/factory.js +147 -0
  96. package/dist/data/orm/index.d.ts +16 -0
  97. package/dist/data/orm/index.js +32 -0
  98. package/dist/data/orm/multi-cursors.d.ts +11 -0
  99. package/dist/data/orm/multi-cursors.js +146 -0
  100. package/dist/data/orm/multi-cursors.test.d.ts +1 -0
  101. package/dist/data/orm/multi-cursors.test.js +455 -0
  102. package/dist/data/orm/sql-db.d.ts +6 -0
  103. package/dist/data/orm/sql-db.js +2 -0
  104. package/dist/data/orm/sql.data-source.d.ts +38 -0
  105. package/dist/data/orm/sql.data-source.js +379 -0
  106. package/dist/data/orm/sql.data-source.test.d.ts +1 -0
  107. package/dist/data/orm/sql.data-source.test.js +406 -0
  108. package/dist/data/orm/subscribable.data-source.d.ts +25 -0
  109. package/dist/data/orm/subscribable.data-source.js +72 -0
  110. package/dist/data/orm/table-container-events.test.d.ts +1 -0
  111. package/dist/data/orm/table-container-events.test.js +93 -0
  112. package/dist/data/orm/table-container.d.ts +39 -0
  113. package/dist/data/orm/table-container.js +96 -0
  114. package/dist/data/orm/table-definitions.system.d.ts +9 -0
  115. package/dist/data/orm/table-definitions.system.js +29 -0
  116. package/dist/data/orm/table-definitions.type.d.ts +19 -0
  117. package/dist/data/orm/table-definitions.type.js +2 -0
  118. package/dist/data/orm/table-dependencies.d.ts +32 -0
  119. package/dist/data/orm/table-dependencies.js +2 -0
  120. package/dist/data/orm/table.d.ts +42 -0
  121. package/dist/data/orm/table.event-source.test.d.ts +1 -0
  122. package/dist/data/orm/table.event-source.test.js +341 -0
  123. package/dist/data/orm/table.js +244 -0
  124. package/dist/data/orm/types.d.ts +20 -0
  125. package/dist/data/orm/types.js +115 -0
  126. package/dist/data/orm/types.test.d.ts +1 -0
  127. package/dist/data/orm/types.test.js +71 -0
  128. package/dist/data/package-permissions.d.ts +7 -0
  129. package/dist/data/package-permissions.js +18 -0
  130. package/dist/data/packages.d.ts +92 -0
  131. package/dist/data/packages.js +90 -0
  132. package/dist/data/peer-events/peer-event-handlers.d.ts +21 -0
  133. package/dist/data/peer-events/peer-event-handlers.js +28 -0
  134. package/dist/data/peer-events/peer-event-types.d.ts +119 -0
  135. package/dist/data/peer-events/peer-event-types.js +29 -0
  136. package/dist/data/peer-events/peer-events.d.ts +41 -0
  137. package/dist/data/peer-events/peer-events.js +102 -0
  138. package/dist/data/persistent-vars.d.ts +87 -0
  139. package/dist/data/persistent-vars.js +230 -0
  140. package/dist/data/tool-tests.d.ts +37 -0
  141. package/dist/data/tool-tests.js +27 -0
  142. package/dist/data/tools.d.ts +358 -0
  143. package/dist/data/tools.js +48 -0
  144. package/dist/data/user-permissions.d.ts +15 -0
  145. package/dist/data/user-permissions.js +39 -0
  146. package/dist/data/user-permissions.test.d.ts +1 -0
  147. package/dist/data/user-permissions.test.js +252 -0
  148. package/dist/data/users.d.ts +38 -0
  149. package/dist/data/users.js +73 -0
  150. package/dist/data/workflow-logs.d.ts +106 -0
  151. package/dist/data/workflow-logs.js +67 -0
  152. package/dist/data/workflow-runs.d.ts +103 -0
  153. package/dist/data/workflow-runs.js +313 -0
  154. package/dist/data/workflows.d.ts +16 -0
  155. package/dist/data/workflows.js +21 -0
  156. package/dist/device/connection.d.ts +41 -0
  157. package/dist/device/connection.js +249 -0
  158. package/dist/device/connection.test.d.ts +1 -0
  159. package/dist/device/connection.test.js +292 -0
  160. package/dist/device/device-election.d.ts +36 -0
  161. package/dist/device/device-election.js +137 -0
  162. package/dist/device/device.d.ts +22 -0
  163. package/dist/device/device.js +110 -0
  164. package/dist/device/device.test.d.ts +1 -0
  165. package/dist/device/device.test.js +203 -0
  166. package/dist/device/get-trust-level.d.ts +3 -0
  167. package/dist/device/get-trust-level.js +87 -0
  168. package/dist/device/socket.type.d.ts +20 -0
  169. package/dist/device/socket.type.js +15 -0
  170. package/dist/device/streamed-socket.d.ts +27 -0
  171. package/dist/device/streamed-socket.js +154 -0
  172. package/dist/device/streamed-socket.test.d.ts +1 -0
  173. package/dist/device/streamed-socket.test.js +44 -0
  174. package/dist/events.d.ts +35 -0
  175. package/dist/events.js +128 -0
  176. package/dist/index.d.ts +33 -0
  177. package/dist/index.js +50 -0
  178. package/dist/keys.d.ts +51 -0
  179. package/dist/keys.js +234 -0
  180. package/dist/keys.test.d.ts +1 -0
  181. package/dist/keys.test.js +215 -0
  182. package/dist/mentions.d.ts +9 -0
  183. package/dist/mentions.js +46 -0
  184. package/dist/observable.d.ts +19 -0
  185. package/dist/observable.js +112 -0
  186. package/dist/observable.test.d.ts +1 -0
  187. package/dist/observable.test.js +183 -0
  188. package/dist/package-loader/get-require.d.ts +10 -0
  189. package/dist/package-loader/get-require.js +31 -0
  190. package/dist/package-loader/index.d.ts +1 -0
  191. package/dist/package-loader/index.js +17 -0
  192. package/dist/package-loader/package-loader.d.ts +16 -0
  193. package/dist/package-loader/package-loader.js +102 -0
  194. package/dist/peers-ui/peers-ui.d.ts +15 -0
  195. package/dist/peers-ui/peers-ui.js +23 -0
  196. package/dist/peers-ui/peers-ui.types.d.ts +35 -0
  197. package/dist/peers-ui/peers-ui.types.js +3 -0
  198. package/dist/rpc-types.d.ts +45 -0
  199. package/dist/rpc-types.js +47 -0
  200. package/dist/serial-json.d.ts +5 -0
  201. package/dist/serial-json.js +186 -0
  202. package/dist/serial-json.test.d.ts +1 -0
  203. package/dist/serial-json.test.js +86 -0
  204. package/dist/system-ids.d.ts +6 -0
  205. package/dist/system-ids.js +10 -0
  206. package/dist/tools/index.d.ts +1 -0
  207. package/dist/tools/index.js +17 -0
  208. package/dist/tools/tools-factory.d.ts +5 -0
  209. package/dist/tools/tools-factory.js +34 -0
  210. package/dist/types/app-nav.d.ts +18 -0
  211. package/dist/types/app-nav.js +10 -0
  212. package/dist/types/assistant-runner-args.d.ts +9 -0
  213. package/dist/types/assistant-runner-args.js +2 -0
  214. package/dist/types/field-type.d.ts +37 -0
  215. package/dist/types/field-type.js +26 -0
  216. package/dist/types/peer-device.d.ts +40 -0
  217. package/dist/types/peer-device.js +14 -0
  218. package/dist/types/peers-package.d.ts +23 -0
  219. package/dist/types/peers-package.js +2 -0
  220. package/dist/types/workflow-logger.d.ts +2 -0
  221. package/dist/types/workflow-logger.js +2 -0
  222. package/dist/types/workflow-run-context.d.ts +12 -0
  223. package/dist/types/workflow-run-context.js +2 -0
  224. package/dist/types/workflow.d.ts +72 -0
  225. package/dist/types/workflow.js +24 -0
  226. package/dist/types/zod-types.d.ts +7 -0
  227. package/dist/types/zod-types.js +12 -0
  228. package/dist/users.query.d.ts +13 -0
  229. package/dist/users.query.js +134 -0
  230. package/dist/utils.d.ts +39 -0
  231. package/dist/utils.js +240 -0
  232. package/dist/utils.test.d.ts +1 -0
  233. package/dist/utils.test.js +140 -0
  234. package/package.json +50 -0
@@ -0,0 +1,219 @@
1
+ import { z } from "zod";
2
+ import { ISqlDb, SQLDataSource } from "./orm";
3
+ import { Emitter, Event } from "../events";
4
+ declare const insertChange: z.ZodObject<{
5
+ changeId: z.ZodEffects<z.ZodString, string, string>;
6
+ changeType: z.ZodUnion<[z.ZodUnion<[z.ZodLiteral<"insert">, z.ZodLiteral<"snapshot">]>, z.ZodLiteral<"restore">]>;
7
+ timestamp: z.ZodNumber;
8
+ timestampApplied: z.ZodNumber;
9
+ tableName: z.ZodString;
10
+ recordId: z.ZodString;
11
+ newRecord: z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>;
12
+ }, "strip", z.ZodTypeAny, {
13
+ changeId: string;
14
+ changeType: "insert" | "snapshot" | "restore";
15
+ timestamp: number;
16
+ timestampApplied: number;
17
+ tableName: string;
18
+ recordId: string;
19
+ newRecord: {} & {
20
+ [k: string]: any;
21
+ };
22
+ }, {
23
+ changeId: string;
24
+ changeType: "insert" | "snapshot" | "restore";
25
+ timestamp: number;
26
+ timestampApplied: number;
27
+ tableName: string;
28
+ recordId: string;
29
+ newRecord: {} & {
30
+ [k: string]: any;
31
+ };
32
+ }>;
33
+ export type IChangeInsert = z.infer<typeof insertChange>;
34
+ declare const deleteChange: z.ZodObject<{
35
+ changeId: z.ZodEffects<z.ZodString, string, string>;
36
+ changeType: z.ZodLiteral<"delete">;
37
+ timestamp: z.ZodNumber;
38
+ timestampApplied: z.ZodNumber;
39
+ tableName: z.ZodString;
40
+ recordId: z.ZodString;
41
+ oldRecord: z.ZodOptional<z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>>;
42
+ }, "strip", z.ZodTypeAny, {
43
+ changeId: string;
44
+ changeType: "delete";
45
+ timestamp: number;
46
+ timestampApplied: number;
47
+ tableName: string;
48
+ recordId: string;
49
+ oldRecord?: z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
50
+ }, {
51
+ changeId: string;
52
+ changeType: "delete";
53
+ timestamp: number;
54
+ timestampApplied: number;
55
+ tableName: string;
56
+ recordId: string;
57
+ oldRecord?: z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
58
+ }>;
59
+ export type IChangeDelete = z.infer<typeof deleteChange>;
60
+ declare const updateChange: z.ZodObject<{
61
+ changeId: z.ZodEffects<z.ZodString, string, string>;
62
+ changeType: z.ZodLiteral<"update">;
63
+ timestamp: z.ZodNumber;
64
+ timestampApplied: z.ZodNumber;
65
+ tableName: z.ZodString;
66
+ recordId: z.ZodString;
67
+ newRecord: z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>;
68
+ jsonDiff: z.ZodArray<z.ZodAny, "many">;
69
+ }, "strip", z.ZodTypeAny, {
70
+ changeId: string;
71
+ changeType: "update";
72
+ timestamp: number;
73
+ timestampApplied: number;
74
+ tableName: string;
75
+ recordId: string;
76
+ newRecord: {} & {
77
+ [k: string]: any;
78
+ };
79
+ jsonDiff: any[];
80
+ }, {
81
+ changeId: string;
82
+ changeType: "update";
83
+ timestamp: number;
84
+ timestampApplied: number;
85
+ tableName: string;
86
+ recordId: string;
87
+ newRecord: {} & {
88
+ [k: string]: any;
89
+ };
90
+ jsonDiff: any[];
91
+ }>;
92
+ export type IChangeUpdate = z.infer<typeof updateChange>;
93
+ declare const changeSchema: z.ZodUnion<[z.ZodObject<{
94
+ changeId: z.ZodEffects<z.ZodString, string, string>;
95
+ changeType: z.ZodUnion<[z.ZodUnion<[z.ZodLiteral<"insert">, z.ZodLiteral<"snapshot">]>, z.ZodLiteral<"restore">]>;
96
+ timestamp: z.ZodNumber;
97
+ timestampApplied: z.ZodNumber;
98
+ tableName: z.ZodString;
99
+ recordId: z.ZodString;
100
+ newRecord: z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>;
101
+ }, "strip", z.ZodTypeAny, {
102
+ changeId: string;
103
+ changeType: "insert" | "snapshot" | "restore";
104
+ timestamp: number;
105
+ timestampApplied: number;
106
+ tableName: string;
107
+ recordId: string;
108
+ newRecord: {} & {
109
+ [k: string]: any;
110
+ };
111
+ }, {
112
+ changeId: string;
113
+ changeType: "insert" | "snapshot" | "restore";
114
+ timestamp: number;
115
+ timestampApplied: number;
116
+ tableName: string;
117
+ recordId: string;
118
+ newRecord: {} & {
119
+ [k: string]: any;
120
+ };
121
+ }>, z.ZodObject<{
122
+ changeId: z.ZodEffects<z.ZodString, string, string>;
123
+ changeType: z.ZodLiteral<"delete">;
124
+ timestamp: z.ZodNumber;
125
+ timestampApplied: z.ZodNumber;
126
+ tableName: z.ZodString;
127
+ recordId: z.ZodString;
128
+ oldRecord: z.ZodOptional<z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>>;
129
+ }, "strip", z.ZodTypeAny, {
130
+ changeId: string;
131
+ changeType: "delete";
132
+ timestamp: number;
133
+ timestampApplied: number;
134
+ tableName: string;
135
+ recordId: string;
136
+ oldRecord?: z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
137
+ }, {
138
+ changeId: string;
139
+ changeType: "delete";
140
+ timestamp: number;
141
+ timestampApplied: number;
142
+ tableName: string;
143
+ recordId: string;
144
+ oldRecord?: z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
145
+ }>, z.ZodObject<{
146
+ changeId: z.ZodEffects<z.ZodString, string, string>;
147
+ changeType: z.ZodLiteral<"update">;
148
+ timestamp: z.ZodNumber;
149
+ timestampApplied: z.ZodNumber;
150
+ tableName: z.ZodString;
151
+ recordId: z.ZodString;
152
+ newRecord: z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>;
153
+ jsonDiff: z.ZodArray<z.ZodAny, "many">;
154
+ }, "strip", z.ZodTypeAny, {
155
+ changeId: string;
156
+ changeType: "update";
157
+ timestamp: number;
158
+ timestampApplied: number;
159
+ tableName: string;
160
+ recordId: string;
161
+ newRecord: {} & {
162
+ [k: string]: any;
163
+ };
164
+ jsonDiff: any[];
165
+ }, {
166
+ changeId: string;
167
+ changeType: "update";
168
+ timestamp: number;
169
+ timestampApplied: number;
170
+ tableName: string;
171
+ recordId: string;
172
+ newRecord: {} & {
173
+ [k: string]: any;
174
+ };
175
+ jsonDiff: any[];
176
+ }>]>;
177
+ export type IChange = z.infer<typeof changeSchema>;
178
+ export declare const changeTrackingSchema: z.ZodObject<{
179
+ changeId: z.ZodEffects<z.ZodString, string, string>;
180
+ changeType: z.ZodEnum<["insert", "snapshot", "update", "delete", "restore"]>;
181
+ timestamp: z.ZodNumber;
182
+ timestampApplied: z.ZodNumber;
183
+ tableName: z.ZodString;
184
+ recordId: z.ZodString;
185
+ oldRecord: z.ZodOptional<z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>>;
186
+ newRecord: z.ZodOptional<z.ZodObject<{}, "strip", z.ZodAny, z.objectOutputType<{}, z.ZodAny, "strip">, z.objectInputType<{}, z.ZodAny, "strip">>>;
187
+ jsonDiff: z.ZodOptional<z.ZodArray<z.ZodAny, "many">>;
188
+ }, "strip", z.ZodTypeAny, {
189
+ changeId: string;
190
+ changeType: "update" | "insert" | "delete" | "snapshot" | "restore";
191
+ timestamp: number;
192
+ timestampApplied: number;
193
+ tableName: string;
194
+ recordId: string;
195
+ newRecord?: z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
196
+ oldRecord?: z.objectOutputType<{}, z.ZodAny, "strip"> | undefined;
197
+ jsonDiff?: any[] | undefined;
198
+ }, {
199
+ changeId: string;
200
+ changeType: "update" | "insert" | "delete" | "snapshot" | "restore";
201
+ timestamp: number;
202
+ timestampApplied: number;
203
+ tableName: string;
204
+ recordId: string;
205
+ newRecord?: z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
206
+ oldRecord?: z.objectInputType<{}, z.ZodAny, "strip"> | undefined;
207
+ jsonDiff?: any[] | undefined;
208
+ }>;
209
+ export type IChangeAny = z.infer<typeof changeTrackingSchema>;
210
+ export declare class ChangeTrackingTable extends SQLDataSource<IChange> {
211
+ constructor({ db, }: {
212
+ db: ISqlDb;
213
+ });
214
+ readonly dataChangedEmitter: Emitter<IChange>;
215
+ readonly dataChanged: Event<IChange>;
216
+ filterToDeletedIds(recordIds: string[]): Promise<string[]>;
217
+ filterToMissingIds(recordIds: string[]): Promise<string[]>;
218
+ }
219
+ export {};
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChangeTrackingTable = exports.changeTrackingSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ const zod_types_1 = require("../types/zod-types");
6
+ const orm_1 = require("./orm");
7
+ const events_1 = require("../events");
8
+ const utils_1 = require("../utils");
9
+ const changeType = zod_1.z.enum(['insert', 'snapshot', 'update', 'delete', 'restore']);
10
+ const insertChange = zod_1.z.object({
11
+ changeId: zod_types_1.zodPeerId,
12
+ changeType: zod_1.z.literal('insert').or(zod_1.z.literal('snapshot')).or(zod_1.z.literal('restore')),
13
+ timestamp: zod_1.z.number(),
14
+ timestampApplied: zod_1.z.number(),
15
+ tableName: zod_1.z.string(),
16
+ recordId: zod_1.z.string(),
17
+ newRecord: zod_types_1.zodAnyObject,
18
+ });
19
+ const deleteChange = zod_1.z.object({
20
+ changeId: zod_types_1.zodPeerId,
21
+ changeType: zod_1.z.literal('delete'),
22
+ timestamp: zod_1.z.number(),
23
+ timestampApplied: zod_1.z.number(),
24
+ tableName: zod_1.z.string(),
25
+ recordId: zod_1.z.string(),
26
+ oldRecord: zod_types_1.zodAnyObject.optional(),
27
+ });
28
+ const updateChange = zod_1.z.object({
29
+ changeId: zod_types_1.zodPeerId,
30
+ changeType: zod_1.z.literal('update'),
31
+ timestamp: zod_1.z.number(),
32
+ timestampApplied: zod_1.z.number(),
33
+ tableName: zod_1.z.string(),
34
+ recordId: zod_1.z.string(),
35
+ newRecord: zod_types_1.zodAnyObject,
36
+ jsonDiff: zod_1.z.array(zod_1.z.any()),
37
+ });
38
+ const changeSchema = zod_1.z.union([insertChange, deleteChange, updateChange]);
39
+ exports.changeTrackingSchema = zod_1.z.object({
40
+ changeId: zod_types_1.zodPeerId,
41
+ changeType: changeType,
42
+ timestamp: zod_1.z.number(),
43
+ timestampApplied: zod_1.z.number(),
44
+ tableName: zod_1.z.string(),
45
+ recordId: zod_1.z.string(),
46
+ oldRecord: zod_types_1.zodAnyObject.optional(),
47
+ newRecord: zod_types_1.zodAnyObject.optional(),
48
+ jsonDiff: zod_1.z.array(zod_1.z.any()).optional(),
49
+ });
50
+ const changeTrackingTableMetaData = {
51
+ name: 'ChangeTracking',
52
+ primaryKeyName: 'changeId',
53
+ description: 'Change tracking table',
54
+ fields: (0, orm_1.schemaToFields)(exports.changeTrackingSchema),
55
+ indexes: [
56
+ { fields: ['tableName'] },
57
+ { fields: ['recordId'] },
58
+ { fields: ['changeType'] },
59
+ { fields: ['timestamp'] },
60
+ { fields: ['timestampApplied'] },
61
+ ],
62
+ localOnly: true,
63
+ };
64
+ class ChangeTrackingTable extends orm_1.SQLDataSource {
65
+ constructor({ db, }) {
66
+ super(db, changeTrackingTableMetaData, changeSchema);
67
+ const tableName = changeTrackingTableMetaData.name;
68
+ this.dataChangedEmitter = new events_1.Emitter(tableName + "_DataChanged_" + (0, utils_1.newid)());
69
+ // @ts-ignore
70
+ this.dataChanged = this.dataChangedEmitter.event;
71
+ // TODO turn this back on to observe changes and make it performant
72
+ // this.dataChanged.subscribe((change) => {
73
+ // console.log("Data changed:", change);
74
+ // });
75
+ }
76
+ dataChangedEmitter;
77
+ dataChanged;
78
+ async filterToDeletedIds(recordIds) {
79
+ await this.initTable();
80
+ // find all changes who have been deleted
81
+ const deletedRecordsResult = await this.db.all(`
82
+ WITH InsertsAndDeletes AS (
83
+ SELECT
84
+ recordId,
85
+ MAX(timestamp) as last_timestamp
86
+ FROM "${this.tableName}"
87
+ WHERE
88
+ recordId IN (${recordIds.map(() => '?').join(',')}) AND
89
+ changeType IN ('restore', 'delete')
90
+ GROUP BY recordId
91
+ )
92
+ SELECT
93
+ ct.recordId
94
+ FROM "${this.tableName}" ct
95
+ JOIN InsertsAndDeletes iad ON ct.recordId = iad.recordId AND ct.timestamp = iad.last_timestamp
96
+ WHERE
97
+ ct.changeType = 'delete'
98
+ `, recordIds);
99
+ return deletedRecordsResult.map(d => d.recordId);
100
+ }
101
+ async filterToMissingIds(recordIds) {
102
+ await this.initTable();
103
+ const missingRecordsResult = await this.db.all(`
104
+ WITH InputRecordIds(recordId) AS (
105
+ VALUES
106
+ ${recordIds.map(() => '(?)').join(',\n ')}
107
+ )
108
+ SELECT
109
+ i.recordId
110
+ FROM InputRecordIds AS i
111
+ LEFT JOIN "${this.tableName}" AS existing
112
+ ON i.recordId = existing.recordId
113
+ WHERE existing.recordId IS NULL;
114
+ `, recordIds);
115
+ let missingIds = missingRecordsResult.map(d => d.recordId);
116
+ return missingIds;
117
+ }
118
+ }
119
+ exports.ChangeTrackingTable = ChangeTrackingTable;
@@ -0,0 +1,29 @@
1
+ import { z } from "zod";
2
+ import type { DataContext } from "../context/data-context";
3
+ export declare const channelSchema: z.ZodObject<{
4
+ channelId: z.ZodString;
5
+ name: z.ZodString;
6
+ description: z.ZodString;
7
+ createdAt: z.ZodDate;
8
+ owningGroupId: z.ZodString;
9
+ }, "strip", z.ZodTypeAny, {
10
+ name: string;
11
+ description: string;
12
+ createdAt: Date;
13
+ channelId: string;
14
+ owningGroupId: string;
15
+ }, {
16
+ name: string;
17
+ description: string;
18
+ createdAt: Date;
19
+ channelId: string;
20
+ owningGroupId: string;
21
+ }>;
22
+ export type IChannel = z.infer<typeof channelSchema>;
23
+ export declare function Channels(dataContext?: DataContext): import("./orm").Table<{
24
+ name: string;
25
+ description: string;
26
+ createdAt: Date;
27
+ channelId: string;
28
+ owningGroupId: string;
29
+ }>;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.channelSchema = void 0;
4
+ exports.Channels = Channels;
5
+ const zod_1 = require("zod");
6
+ const types_1 = require("./orm/types");
7
+ const user_context_singleton_1 = require("../context/user-context-singleton");
8
+ const table_definitions_system_1 = require("./orm/table-definitions.system");
9
+ exports.channelSchema = zod_1.z.object({
10
+ channelId: zod_1.z.string(),
11
+ name: zod_1.z.string(),
12
+ description: zod_1.z.string(),
13
+ createdAt: zod_1.z.date(),
14
+ owningGroupId: zod_1.z.string(),
15
+ });
16
+ const metaData = {
17
+ name: 'Channels',
18
+ description: 'Different channels of communication for peers and assistants',
19
+ primaryKeyName: 'channelId',
20
+ fields: (0, types_1.schemaToFields)(exports.channelSchema),
21
+ };
22
+ (0, table_definitions_system_1.registerSystemTableDefinition)(metaData, exports.channelSchema);
23
+ function Channels(dataContext) {
24
+ return (0, user_context_singleton_1.getTableContainer)(dataContext).getTable(metaData, exports.channelSchema);
25
+ }
@@ -0,0 +1,37 @@
1
+ import { z } from "zod";
2
+ import { IPeerDevice } from "../types/peer-device";
3
+ import { ITableMetaData, Table, ITableDependencies } from "./orm";
4
+ export declare const dataLockSchema: z.ZodObject<{
5
+ dataLockId: z.ZodEffects<z.ZodString, string, string>;
6
+ recordId: z.ZodString;
7
+ lockedUntil: z.ZodNumber;
8
+ acknowledged: z.ZodOptional<z.ZodNumber>;
9
+ }, "strip", z.ZodTypeAny, {
10
+ recordId: string;
11
+ dataLockId: string;
12
+ lockedUntil: number;
13
+ acknowledged?: number | undefined;
14
+ }, {
15
+ recordId: string;
16
+ dataLockId: string;
17
+ lockedUntil: number;
18
+ acknowledged?: number | undefined;
19
+ }>;
20
+ export type IDataLock = z.infer<typeof dataLockSchema>;
21
+ export declare class DataLocksTable extends Table<IDataLock> {
22
+ readonly DEFAULT_LOCK_TIME_MS = 300000;
23
+ readonly DEFAULT_TIMEOUT_MS = 20000;
24
+ readonly DEAD_PERIOD_MS = 10000;
25
+ peerDevice: IPeerDevice | undefined;
26
+ constructor(metaData: ITableMetaData, deps: ITableDependencies);
27
+ getCurrentLock(recordId: string): Promise<IDataLock | undefined>;
28
+ releaseLock(dataLock: IDataLock): Promise<void>;
29
+ createLock(recordId: string, lockTimeMs?: number): Promise<IDataLock>;
30
+ acquireLock(lock: IDataLock, timeoutMs?: number): Promise<IDataLock | undefined>;
31
+ acquireLock(recordId: string, timeoutMs?: number, lockTimeMs?: number): Promise<IDataLock | undefined>;
32
+ renewLock(dataLock: IDataLock, lockTimeMs?: number): Promise<IDataLock | undefined>;
33
+ private countLockAcknowledgements;
34
+ private lockStatus;
35
+ private waitForLockConfirmedAndCurrent;
36
+ }
37
+ export declare function DataLocks(dataContext?: any): DataLocksTable;
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DataLocksTable = exports.dataLockSchema = void 0;
4
+ exports.DataLocks = DataLocks;
5
+ const lodash_1 = require("lodash");
6
+ const zod_1 = require("zod");
7
+ const zod_types_1 = require("../types/zod-types");
8
+ const utils_1 = require("../utils");
9
+ const orm_1 = require("./orm");
10
+ const context_1 = require("../context");
11
+ const table_definitions_system_1 = require("./orm/table-definitions.system");
12
+ /*
13
+ This works by waiting for a majority of peers to acknowledge the lock.
14
+ Once that happens it is considered "well propagated" and it also assumes older
15
+ locks from other devices have all been synced but that's not actually guaranteed.
16
+
17
+ This system works okay for a few peers but it won't scale well when _all_ the peers are trying to
18
+ do something like emit a scheduled event at the same time. They'll all try to acquire the lock at the same time
19
+ and even if this works, it'll create a ton of traffic and contention on the locks table.
20
+
21
+ The issue with emitting scheduled events might be an edge case but it still feels like the locking problem
22
+ should be solved in a more robust and scalable way.
23
+
24
+ We already have an election system for preferred connections. This feels like it could be solved in a similar way,
25
+ maybe by just using the existing elections. I also suspect there are going to be other problems that would
26
+ benefit greatly from utilizing an elected coordinator for these kinds of problems that require coordination
27
+
28
+ */
29
+ exports.dataLockSchema = zod_1.z.object({
30
+ dataLockId: zod_types_1.zodPeerId,
31
+ recordId: zod_1.z.string(),
32
+ lockedUntil: zod_1.z.number(),
33
+ acknowledged: zod_1.z.number().optional(), // timestamp of acknowledgment
34
+ });
35
+ const dataLocksTableMetaData = {
36
+ name: 'DataLocks',
37
+ primaryKeyName: 'dataLockId',
38
+ description: 'Data locks table to track exclusive write access to records in other tables.',
39
+ fields: (0, orm_1.schemaToFields)(exports.dataLockSchema),
40
+ indexes: [
41
+ { fields: ['recordId'] },
42
+ ],
43
+ };
44
+ class DataLocksTable extends orm_1.Table {
45
+ DEFAULT_LOCK_TIME_MS = 300_000; // 5 minutes
46
+ DEFAULT_TIMEOUT_MS = 20_000; // 20 seconds
47
+ DEAD_PERIOD_MS = 10_000; // The cannot be renewed in it's last 10 seconds to avoid races
48
+ peerDevice;
49
+ constructor(metaData, deps) {
50
+ super(metaData, deps);
51
+ // signal to other peers when we become aware of a new data lock
52
+ this.dataChanged.subscribe(async (evt) => {
53
+ const dataLock = evt.dataObject;
54
+ if (evt.op === 'insert') {
55
+ dataLock.acknowledged = (0, utils_1.getTimestamp)();
56
+ this.dataSource.update(dataLock);
57
+ }
58
+ });
59
+ }
60
+ async getCurrentLock(recordId) {
61
+ const locks = await this.list({
62
+ recordId,
63
+ lockedUntil: { $gt: Date.now() }, // Only consider locks that are still valid
64
+ });
65
+ return (0, lodash_1.sortBy)(locks, 'dataLockId')[0];
66
+ }
67
+ async releaseLock(dataLock) {
68
+ // Remove the lock from the database
69
+ await this.delete(dataLock);
70
+ }
71
+ async createLock(recordId, lockTimeMs = this.DEFAULT_LOCK_TIME_MS) {
72
+ const newLock = {
73
+ dataLockId: (0, utils_1.newid)(),
74
+ recordId,
75
+ lockedUntil: Date.now() + lockTimeMs,
76
+ };
77
+ return await this.insert(newLock);
78
+ }
79
+ async acquireLock(recordIdOrLock, timeoutMs = this.DEFAULT_TIMEOUT_MS, lockTimeMs = this.DEFAULT_LOCK_TIME_MS) {
80
+ const lock = typeof recordIdOrLock === 'string'
81
+ ? await this.createLock(recordIdOrLock, lockTimeMs)
82
+ : recordIdOrLock;
83
+ const recordId = lock.recordId;
84
+ const confirmedLock = await this.waitForLockConfirmedAndCurrent(lock, timeoutMs);
85
+ if (confirmedLock) {
86
+ await this.renewLock(confirmedLock, lockTimeMs);
87
+ const currentLock = await this.getCurrentLock(recordId);
88
+ if (!currentLock || currentLock.dataLockId !== confirmedLock.dataLockId) {
89
+ console.warn(`Lock acquisition failed for ${recordId}`);
90
+ await this.releaseLock(confirmedLock); // clean up unconfirmed lock
91
+ return undefined; // Return undefined if the lock could not be renewed
92
+ }
93
+ return currentLock;
94
+ }
95
+ else {
96
+ await this.releaseLock(lock); // clean up unconfirmed lock
97
+ return undefined; // Return undefined if the lock could not be acquired
98
+ }
99
+ }
100
+ async renewLock(dataLock, lockTimeMs = this.DEFAULT_LOCK_TIME_MS) {
101
+ if (dataLock.lockedUntil < Date.now() - this.DEAD_PERIOD_MS) {
102
+ console.warn(`Lock renewal rejected for expired lock ${dataLock.dataLockId} for ${dataLock.recordId}`);
103
+ return undefined;
104
+ }
105
+ dataLock.lockedUntil = Date.now() + lockTimeMs;
106
+ const renewedLock = await this.update(dataLock);
107
+ let currentLock = await this.getCurrentLock(dataLock.recordId);
108
+ if (!currentLock || currentLock.dataLockId !== dataLock.dataLockId) {
109
+ // currently we don't allow renewing a lock if it is not the current lock - this could change in the future if needed
110
+ console.warn(`Lock renewal failed for ${dataLock.dataLockId} on ${dataLock.recordId}`);
111
+ return undefined;
112
+ }
113
+ return renewedLock;
114
+ }
115
+ async countLockAcknowledgements(lock) {
116
+ if (!this.peerDevice)
117
+ throw new Error(`Peer device not set for DataLocksTable`);
118
+ const updates = await this.peerDevice.listChanges({
119
+ tableName: dataLocksTableMetaData.name,
120
+ recordId: lock.dataLockId,
121
+ changeType: 'update',
122
+ });
123
+ const acknowledgedUpdates = updates.map(u => u.newRecord.acknowledged).filter(a => a !== undefined);
124
+ // NOTE this could potentially miscount if two peers acknowledge the lock with the same timestamp (very unlikely)
125
+ return (0, lodash_1.uniq)(acknowledgedUpdates).length;
126
+ }
127
+ async lockStatus(lock) {
128
+ if (!this.peerDevice)
129
+ throw new Error(`Peer device not set for DataLocksTable`);
130
+ const connectionCount = (await this.peerDevice.getNetworkInfo()).connections.length;
131
+ const ackCnt = await this.countLockAcknowledgements(lock);
132
+ const acksNeeded = Math.ceil((connectionCount * 0.7) + 1); // 70% of the connections and +1 for the local device
133
+ if (ackCnt >= acksNeeded) {
134
+ return 'confirmed';
135
+ }
136
+ return 'pending';
137
+ }
138
+ async waitForLockConfirmedAndCurrent(lock, timeoutMs = this.DEFAULT_TIMEOUT_MS) {
139
+ const startTime = Date.now();
140
+ let resolveLock;
141
+ const confirmPromise = new Promise(async (resolve) => {
142
+ resolveLock = resolve;
143
+ });
144
+ const subscription = this.dataChanged.subscribe(async (evt) => {
145
+ if (evt.dataObject.recordId === lock.recordId) {
146
+ checkLockStatus();
147
+ }
148
+ });
149
+ const checkLockStatus = async () => {
150
+ const status = await this.lockStatus(lock);
151
+ if (status === 'confirmed') {
152
+ const currentLock = await this.getCurrentLock(lock.recordId);
153
+ if (currentLock?.dataLockId === lock.dataLockId) {
154
+ subscription.unsubscribe();
155
+ clearTimeout(timeoutId);
156
+ resolveLock(lock);
157
+ return;
158
+ }
159
+ console.debug(`Lock confirmed but not current for ${lock.recordId}`);
160
+ }
161
+ if (Date.now() - startTime > timeoutMs) {
162
+ console.warn(`Lock confirmation timed out for ${lock.recordId}`);
163
+ subscription.unsubscribe();
164
+ clearTimeout(timeoutId);
165
+ resolveLock(undefined);
166
+ }
167
+ };
168
+ const timeoutId = setTimeout(() => {
169
+ checkLockStatus(); // Check lock status after timeout
170
+ }, timeoutMs + 1);
171
+ checkLockStatus(); // Initial check for lock status
172
+ return confirmPromise;
173
+ }
174
+ }
175
+ exports.DataLocksTable = DataLocksTable;
176
+ (0, table_definitions_system_1.registerSystemTableDefinition)(dataLocksTableMetaData, exports.dataLockSchema, DataLocksTable);
177
+ function DataLocks(dataContext) {
178
+ const tableFactory = (0, context_1.getTableContainer)(dataContext);
179
+ return tableFactory.getTable(dataLocksTableMetaData);
180
+ }
@@ -0,0 +1 @@
1
+ export {};