@seedprotocol/sdk 0.3.0 → 0.3.1

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 (200) hide show
  1. package/dist/{ArweaveClient-CmJYeuq1.js → ArweaveClient-C0g0es2v.js} +2 -2
  2. package/dist/{ArweaveClient-EXXrXUzz.js.map → ArweaveClient-C0g0es2v.js.map} +1 -1
  3. package/dist/{ArweaveClient-EXXrXUzz.js → ArweaveClient-CzkBgiZY.js} +2 -2
  4. package/dist/{ArweaveClient-CmJYeuq1.js.map → ArweaveClient-CzkBgiZY.js.map} +1 -1
  5. package/dist/{Db-CESKJH-G.js → Db-C4oog-0n.js} +3 -3
  6. package/dist/{Db-CESKJH-G.js.map → Db-C4oog-0n.js.map} +1 -1
  7. package/dist/{Db-DiTPSbQ8.js → Db-VPouegkr.js} +3 -3
  8. package/dist/{Db-DiTPSbQ8.js.map → Db-VPouegkr.js.map} +1 -1
  9. package/dist/{EasClient-BesHK2bO.js → EasClient-B9a3NsTi.js} +2 -2
  10. package/dist/{EasClient-BesHK2bO.js.map → EasClient-B9a3NsTi.js.map} +1 -1
  11. package/dist/{EasClient-Cop6WtWl.js → EasClient-DQ93i2ci.js} +2 -2
  12. package/dist/{EasClient-Cop6WtWl.js.map → EasClient-DQ93i2ci.js.map} +1 -1
  13. package/dist/{FileManager-DTzkncLh.js → FileManager-_ErxVuwb.js} +8 -5
  14. package/dist/FileManager-_ErxVuwb.js.map +1 -0
  15. package/dist/{Item-B5Tz-Y7t.js → Item-D9HFPxsK.js} +3 -3
  16. package/dist/{Item-B5Tz-Y7t.js.map → Item-D9HFPxsK.js.map} +1 -1
  17. package/dist/{ItemProperty-DBV0Iib_.js → ItemProperty-BdtvKrEy.js} +3 -3
  18. package/dist/{ItemProperty-DBV0Iib_.js.map → ItemProperty-BdtvKrEy.js.map} +1 -1
  19. package/dist/{QueryClient-C7a0NHCt.js → QueryClient-Bmnv_moN.js} +2 -2
  20. package/dist/{QueryClient-C7a0NHCt.js.map → QueryClient-Bmnv_moN.js.map} +1 -1
  21. package/dist/{QueryClient-BnwbHtqe.js → QueryClient-hzOtFe99.js} +2 -2
  22. package/dist/{QueryClient-BnwbHtqe.js.map → QueryClient-hzOtFe99.js.map} +1 -1
  23. package/dist/bin.js +5 -5
  24. package/dist/bin.js.map +1 -1
  25. package/dist/{constants-CWg2B3Au.js → constants-Bhl5dZpN.js} +4 -1
  26. package/dist/constants-Bhl5dZpN.js.map +1 -0
  27. package/dist/{index-CQ9FS10O.js → index-BAtjd9W-.js} +3 -3
  28. package/dist/index-BAtjd9W-.js.map +1 -0
  29. package/dist/{index-BHGJ-Es9.js → index-BSR5hv7Q.js} +59 -27
  30. package/dist/index-BSR5hv7Q.js.map +1 -0
  31. package/dist/main.js +2 -2
  32. package/dist/{seed.schema.config-DDzfQguf.js → seed.schema.config-BxJYabim.js} +3 -3
  33. package/dist/{seed.schema.config-DDzfQguf.js.map → seed.schema.config-BxJYabim.js.map} +1 -1
  34. package/dist/src/AppStateSchema.ts +10 -0
  35. package/dist/src/ArweaveClient.ts +13 -0
  36. package/dist/src/Attestation.ts +21 -0
  37. package/dist/src/BaseArweaveClient.ts +13 -0
  38. package/dist/src/BaseClientManager.ts +35 -0
  39. package/dist/src/BaseDb.ts +35 -0
  40. package/dist/src/BaseEasClient.ts +14 -0
  41. package/dist/src/BaseFileManager.ts +60 -0
  42. package/dist/src/BaseItem.ts +413 -0
  43. package/dist/src/BaseItemProperty.ts +416 -0
  44. package/dist/src/BaseQueryClient.ts +13 -0
  45. package/dist/src/ClientManager.ts +63 -0
  46. package/dist/src/ConfigSchema.ts +15 -0
  47. package/dist/src/Db.ts +82 -0
  48. package/dist/src/EasClient.ts +13 -0
  49. package/dist/src/FileDownloader.ts +63 -0
  50. package/dist/src/FileManager.ts +59 -0
  51. package/dist/src/IDb.ts +7 -0
  52. package/dist/src/IEasClient.ts +2 -0
  53. package/dist/src/IItem.ts +26 -0
  54. package/dist/src/IItemProperty.ts +28 -0
  55. package/dist/src/IQueryClient.ts +11 -0
  56. package/dist/src/ImageResizer.ts +81 -0
  57. package/dist/src/Item.ts +12 -0
  58. package/dist/src/ItemProperty.ts +9 -0
  59. package/dist/src/MetadataSchema.ts +28 -0
  60. package/dist/src/ModelSchema.ts +46 -0
  61. package/dist/src/ModelUidSchema.ts +16 -0
  62. package/dist/src/PropertyUidSchema.ts +16 -0
  63. package/dist/src/QueryClient.ts +26 -0
  64. package/dist/src/Schema.ts +17 -0
  65. package/dist/src/SeedProtocolSdk.d.ts +19 -0
  66. package/dist/src/SeedSchema.ts +29 -0
  67. package/dist/src/VersionSchema.ts +16 -0
  68. package/dist/src/actors.ts +295 -0
  69. package/dist/src/addModelsToDb.ts +152 -0
  70. package/dist/src/allItems.ts +31 -0
  71. package/dist/src/analyzeInput.ts +144 -0
  72. package/dist/src/browser.app.db.config.ts +27 -0
  73. package/dist/src/browser.ts +30 -0
  74. package/dist/src/checkStatus.ts +57 -0
  75. package/dist/src/clientManagerMachine.ts +59 -0
  76. package/dist/src/configureFs.ts +98 -0
  77. package/dist/src/connectToDb.ts +41 -0
  78. package/dist/src/connectionManager.ts +67 -0
  79. package/dist/src/constants.ts +125 -0
  80. package/dist/src/content-hash.ts +30 -0
  81. package/dist/src/create.ts +39 -0
  82. package/dist/src/createMetadata.ts +77 -0
  83. package/dist/src/createNewItem.ts +61 -0
  84. package/dist/src/createPublishAttempt.ts +16 -0
  85. package/dist/src/createSeed.ts +31 -0
  86. package/dist/src/createSeeds.ts +24 -0
  87. package/dist/src/createVersion.ts +33 -0
  88. package/dist/src/dbMachine.ts +182 -0
  89. package/dist/src/deleteItem.ts +19 -0
  90. package/dist/src/download.ts +208 -0
  91. package/dist/src/drizzle.ts +78 -0
  92. package/dist/src/eas.ts +40 -0
  93. package/dist/src/environment.ts +35 -0
  94. package/dist/src/eventBus.ts +5 -0
  95. package/dist/src/events.ts +14 -0
  96. package/dist/src/fetchDataFromEas.ts +94 -0
  97. package/dist/src/fetchDbData.ts +19 -0
  98. package/dist/src/fetchRelatedItems.ts +184 -0
  99. package/dist/src/fetchSeeds.ts +45 -0
  100. package/dist/src/fetchVersions.ts +49 -0
  101. package/dist/src/files.ts +81 -0
  102. package/dist/src/filesDownload.ts +326 -0
  103. package/dist/src/fragment-masking.ts +87 -0
  104. package/dist/src/getExistingItem.ts +60 -0
  105. package/dist/src/getItem.ts +32 -0
  106. package/dist/src/getItemData.ts +99 -0
  107. package/dist/src/getItemProperties.ts +56 -0
  108. package/dist/src/getItemProperty.ts +34 -0
  109. package/dist/src/getItems.ts +70 -0
  110. package/dist/src/getMetadata.ts +40 -0
  111. package/dist/src/getModelSchemas.ts +89 -0
  112. package/dist/src/getPropertyData.ts +50 -0
  113. package/dist/src/getPublishPayload.ts +355 -0
  114. package/dist/src/getPublishUploads.ts +207 -0
  115. package/dist/src/getRelationValueData.ts +27 -0
  116. package/dist/src/getSchemaForItemProperty.ts +68 -0
  117. package/dist/src/getSchemaForModel.ts +42 -0
  118. package/dist/src/getSchemaUidForModel.ts +32 -0
  119. package/dist/src/getSeedData.ts +34 -0
  120. package/dist/src/getSegmentedItemProperties.ts +65 -0
  121. package/dist/src/getStorageTransactionIdForSeedUid.ts +38 -0
  122. package/dist/src/getVersionData.ts +46 -0
  123. package/dist/src/getVersionsForVersionUids.ts +39 -0
  124. package/dist/src/globalMachine.ts +280 -0
  125. package/dist/src/gql.ts +118 -0
  126. package/dist/src/graphql.ts +3209 -0
  127. package/dist/src/helpers.ts +273 -0
  128. package/dist/src/hydrateExistingItem.ts +137 -0
  129. package/dist/src/hydrateFromDb.ts +270 -0
  130. package/dist/src/hydrateNewItem.ts +34 -0
  131. package/dist/src/imageResize.ts +507 -0
  132. package/dist/src/index.d.ts +5 -0
  133. package/dist/src/index.ts +4 -0
  134. package/dist/src/initialize.ts +124 -0
  135. package/dist/src/internalMachine.ts +241 -0
  136. package/dist/src/itemMachineAll.ts +157 -0
  137. package/dist/src/itemMachineSingle.ts +158 -0
  138. package/dist/src/loadAppDb.ts +44 -0
  139. package/dist/src/machines.ts +66 -0
  140. package/dist/src/metadataLatest.ts +34 -0
  141. package/dist/src/migrate.ts +57 -0
  142. package/dist/src/model.ts +72 -0
  143. package/dist/src/modelClass.ts +19 -0
  144. package/dist/src/node.app.db.config.ts +41 -0
  145. package/dist/src/prepareDb.ts +29 -0
  146. package/dist/src/preparePublishRequestData.ts +81 -0
  147. package/dist/src/processItems.ts +74 -0
  148. package/dist/src/property.ts +156 -0
  149. package/dist/src/propertyMachine.ts +202 -0
  150. package/dist/src/publish.ts +28 -0
  151. package/dist/src/publishMachine.ts +77 -0
  152. package/dist/src/queries.ts +13 -0
  153. package/dist/src/recoverDeletedItem.ts +14 -0
  154. package/dist/src/reload.ts +33 -0
  155. package/dist/src/request.ts +45 -0
  156. package/dist/src/requestAll.ts +157 -0
  157. package/dist/src/resolveRelatedValue.ts +367 -0
  158. package/dist/src/resolveRemoteStorage.ts +88 -0
  159. package/dist/src/saveAppState.ts +50 -0
  160. package/dist/src/saveConfig.ts +106 -0
  161. package/dist/src/saveDataToDb.ts +145 -0
  162. package/dist/src/saveImageSrc.ts +242 -0
  163. package/dist/src/saveItemStorage.ts +156 -0
  164. package/dist/src/saveMetadata.ts +18 -0
  165. package/dist/src/savePublishService.ts +30 -0
  166. package/dist/src/saveRelation.ts +112 -0
  167. package/dist/src/seed.schema.config.ts +25 -0
  168. package/dist/src/seed.ts +10 -0
  169. package/dist/src/seedProtocol.ts +17 -0
  170. package/dist/src/services.ts +365 -0
  171. package/dist/src/setAddresses.ts +16 -0
  172. package/dist/src/sqlWasmClient.ts +88 -0
  173. package/dist/src/syncDbWithEas.ts +628 -0
  174. package/dist/src/trash.ts +29 -0
  175. package/dist/src/ts-to-proto.ts +101 -0
  176. package/dist/src/types.ts +12 -0
  177. package/dist/src/updateItemPropertyValue.ts +243 -0
  178. package/dist/src/updateMachineContext.ts +21 -0
  179. package/dist/src/updateMetadata.ts +59 -0
  180. package/dist/src/upload.ts +86 -0
  181. package/dist/src/validate.ts +45 -0
  182. package/dist/src/validateInput.ts +43 -0
  183. package/dist/src/validateItemData.ts +20 -0
  184. package/dist/src/versionData.ts +24 -0
  185. package/dist/src/waitForDb.ts +25 -0
  186. package/dist/src/waitForFiles.ts +41 -0
  187. package/dist/src/wasm.d.ts +8300 -0
  188. package/dist/types/scripts/bin.d.ts.map +1 -1
  189. package/dist/types/src/browser/helpers/FileManager.d.ts +2 -1
  190. package/dist/types/src/browser/helpers/FileManager.d.ts.map +1 -1
  191. package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts +1 -0
  192. package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
  193. package/dist/types/src/node/helpers/FileManager.d.ts +2 -1
  194. package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
  195. package/package.json +3 -3
  196. package/dist/FileManager-DTzkncLh.js.map +0 -1
  197. package/dist/constants-CWg2B3Au.js.map +0 -1
  198. package/dist/index-BHGJ-Es9.js.map +0 -1
  199. package/dist/index-CQ9FS10O.js.map +0 -1
  200. package/dist/package.json +0 -189
@@ -0,0 +1,202 @@
1
+ import { assign, setup } from 'xstate'
2
+ import { PropertyMachineContext } from '@/types'
3
+
4
+ import { resolveRemoteStorage } from '@/ItemProperty/service/actors/resolveRemoteStorage'
5
+ import { waitForDb } from '@/ItemProperty/service/actors/waitForDb'
6
+ import { initialize } from '@/ItemProperty/service/actors/initialize'
7
+ import { resolveRelatedValue } from '@/ItemProperty/service/actors/resolveRelatedValue'
8
+ import { hydrateFromDb } from '@/ItemProperty/service/actors/hydrateFromDb'
9
+ import {
10
+ saveImageSrc,
11
+ saveItemStorage,
12
+ saveRelation,
13
+ } from '@/ItemProperty/service/actors/saveValueToDb'
14
+ import { analyzeInput } from '@/ItemProperty/service/actors/saveValueToDb/analyzeInput' // import { updateMachineContext } from '@/helpers'
15
+ // import { updateMachineContext } from '@/helpers'
16
+
17
+ export const propertyMachine = setup({
18
+ types: {
19
+ context: {} as PropertyMachineContext,
20
+ input: {} as PropertyMachineContext,
21
+ },
22
+ // actions: {
23
+ // updateContext: updateMachineContext,
24
+ // },
25
+ actors: {
26
+ waitForDb,
27
+ hydrateFromDb,
28
+ initialize,
29
+ resolveRelatedValue,
30
+ resolveRemoteStorage,
31
+ analyzeInput,
32
+ saveImageSrc,
33
+ saveRelation,
34
+ saveItemStorage,
35
+ },
36
+ }).createMachine({
37
+ id: 'itemProperty',
38
+ initial: 'waitingForDb',
39
+ context: ({ input }) => input as PropertyMachineContext,
40
+ on: {
41
+ // reload: '.hydratingFromDb',
42
+ save: {
43
+ actions: assign({
44
+ isSaving: true,
45
+ }),
46
+ target: '.saving',
47
+ },
48
+ updateContext: {
49
+ actions: assign(({ context, event }) => {
50
+ const newContext = Object.assign({}, context)
51
+
52
+ for (let i = 0; i < Object.keys(event).length; i++) {
53
+ const key = Object.keys(event)[i]
54
+ if (key === 'type') {
55
+ continue
56
+ }
57
+ newContext[key] = event[key]
58
+ }
59
+ return newContext
60
+ }),
61
+ },
62
+ },
63
+ states: {
64
+ idle: {},
65
+ waitingForDb: {
66
+ on: {
67
+ waitForDbSuccess: {
68
+ target: 'hydratingFromDb',
69
+ actions: assign({
70
+ isDbReady: true,
71
+ }),
72
+ },
73
+ },
74
+ invoke: {
75
+ src: 'waitForDb',
76
+ input: ({ context }) => ({ context }),
77
+ },
78
+ },
79
+ hydratingFromDb: {
80
+ on: {
81
+ hydrateFromDbSuccess: 'initializing',
82
+ },
83
+ invoke: {
84
+ src: 'hydrateFromDb',
85
+ input: ({ context }) => ({ context }),
86
+ },
87
+ },
88
+ initializing: {
89
+ on: {
90
+ initializeSuccess: 'idle',
91
+ isRelatedProperty: {
92
+ target: 'resolvingRelatedValue',
93
+ },
94
+ hasRemoteBackup: {
95
+ target: 'resolvingRemoteStorage',
96
+ },
97
+ },
98
+ invoke: {
99
+ src: 'initialize',
100
+ input: ({ context, event }) => ({ context, event }),
101
+ },
102
+ },
103
+ resolvingRelatedValue: {
104
+ on: {
105
+ resolvingRelatedValueSuccess: {
106
+ target: 'idle',
107
+ actions: assign({
108
+ refResolvedDisplayValue: ({ event }) =>
109
+ event.refResolvedDisplayValue,
110
+ refResolvedValue: ({ event }) => event.refResolvedValue,
111
+ }),
112
+ },
113
+ resolvingRelatedValueDone: {
114
+ target: 'idle',
115
+ },
116
+ },
117
+ invoke: {
118
+ src: 'resolveRelatedValue',
119
+ input: ({ context }) => ({ context }),
120
+ },
121
+ },
122
+ resolvingRemoteStorage: {
123
+ on: {
124
+ resolveRemoteStorageSuccess: {
125
+ target: 'idle',
126
+ },
127
+ },
128
+ invoke: {
129
+ src: 'resolveRemoteStorage',
130
+ input: ({ context }) => ({ context }),
131
+ },
132
+ },
133
+ saving: {
134
+ initial: 'analyzingInput',
135
+ states: {
136
+ analyzingInput: {
137
+ on: {
138
+ saveValueToDbSuccess: {
139
+ target: 'doneSaving',
140
+ },
141
+ saveImageSrc: 'savingImageSrc',
142
+ saveRelation: 'savingRelation',
143
+ saveItemStorage: 'savingItemStorage',
144
+ },
145
+ invoke: {
146
+ src: 'analyzeInput',
147
+ input: ({ context, event }) => ({ context, event }),
148
+ },
149
+ },
150
+ savingImageSrc: {
151
+ on: {
152
+ saveImageSrcSuccess: 'doneSaving',
153
+ },
154
+ invoke: {
155
+ src: 'saveImageSrc',
156
+ input: ({ context, event }) => ({ context, event }),
157
+ },
158
+ },
159
+ savingRelation: {
160
+ on: {
161
+ saveRelationSuccess: 'doneSaving',
162
+ },
163
+ invoke: {
164
+ src: 'saveRelation',
165
+ input: ({ context, event }) => ({ context, event }),
166
+ },
167
+ },
168
+ savingItemStorage: {
169
+ on: {
170
+ saveItemStorageSuccess: 'doneSaving',
171
+ },
172
+ invoke: {
173
+ src: 'saveItemStorage',
174
+ input: ({ context, event }) => ({ context, event }),
175
+ },
176
+ },
177
+ doneSaving: {
178
+ type: 'final',
179
+ },
180
+ },
181
+ onDone: {
182
+ target: 'idle',
183
+ actions: assign(({ context }) => {
184
+ return {
185
+ isSaving: false,
186
+ }
187
+ }),
188
+ },
189
+ },
190
+ },
191
+
192
+ // conflict: {
193
+ // on: {
194
+ // resolveConflict: {
195
+ // target: 'saving',
196
+ // actions: assign({
197
+ // data: ({ context, event }) => event.output,
198
+ // }),
199
+ // },
200
+ // },
201
+ // },
202
+ })
@@ -0,0 +1,28 @@
1
+ import { getGlobalService } from '@/services/global'
2
+
3
+ type PublishItemRequestEvent = {
4
+ seedLocalId: string
5
+ }
6
+
7
+ type PublishItemRequestHandler = (
8
+ event: PublishItemRequestEvent,
9
+ ) => Promise<void>
10
+
11
+ export const publishItemRequestHandler: PublishItemRequestHandler = async ({
12
+ seedLocalId,
13
+ }) => {
14
+ const globalService = getGlobalService()
15
+ globalService.subscribe((snapshot) => {
16
+ if (
17
+ !snapshot ||
18
+ !snapshot.context ||
19
+ !snapshot.context.publishItemService
20
+ ) {
21
+ return
22
+ }
23
+ })
24
+ globalService.send({
25
+ type: 'publishItemRequest',
26
+ seedLocalId,
27
+ })
28
+ }
@@ -0,0 +1,77 @@
1
+ import { setup } from 'xstate'
2
+ import { validateItemData } from './actors/validateItemData'
3
+ import { PublishMachineStates } from '@/services/internal/constants'
4
+ import { PublishMachineContext } from '@/types'
5
+ import { createPublishAttempt } from './actors/createPublishAttempt'
6
+ import { updateMachineContext } from '@/helpers/updateMachineContext'
7
+ import { preparePublishRequestData } from '@/services/publish/actors/preparePublishRequestData'
8
+ import { upload } from '@/services/publish/actors/upload'
9
+
10
+ const {
11
+ VALIDATING_ITEM_DATA,
12
+ CREATING_PUBLISH_ATTEMPT,
13
+ PREPARING_PUBLISH_REQUEST_DATA,
14
+ UPLOADING,
15
+ PUBLISHING,
16
+ IDLE,
17
+ } = PublishMachineStates
18
+
19
+ export const publishMachine = setup({
20
+ types: {
21
+ context: {} as PublishMachineContext,
22
+ input: {} as PublishMachineContext,
23
+ },
24
+ actors: {
25
+ validateItemData,
26
+ createPublishAttempt,
27
+ upload,
28
+ preparePublishRequestData,
29
+ },
30
+ }).createMachine({
31
+ id: 'publish',
32
+ initial: VALIDATING_ITEM_DATA,
33
+ context: ({ input }) => input,
34
+ on: {
35
+ updateContext: updateMachineContext,
36
+ },
37
+ states: {
38
+ [VALIDATING_ITEM_DATA]: {
39
+ on: {
40
+ validateItemDataSuccess: [CREATING_PUBLISH_ATTEMPT],
41
+ },
42
+ invoke: {
43
+ src: 'validateItemData',
44
+ input: ({ context }) => ({ context }),
45
+ },
46
+ },
47
+ [CREATING_PUBLISH_ATTEMPT]: {
48
+ on: {
49
+ createPublishAttemptSuccess: [UPLOADING],
50
+ },
51
+ invoke: {
52
+ src: 'createPublishAttempt',
53
+ input: ({ context }) => ({ context }),
54
+ },
55
+ },
56
+ [UPLOADING]: {
57
+ on: {
58
+ uploadingSuccess: [PREPARING_PUBLISH_REQUEST_DATA],
59
+ },
60
+ invoke: {
61
+ src: 'upload',
62
+ input: ({ context }) => ({ context }),
63
+ },
64
+ },
65
+ [PREPARING_PUBLISH_REQUEST_DATA]: {
66
+ on: {
67
+ preparePublishRequestDataSuccess: [UPLOADING],
68
+ },
69
+ invoke: {
70
+ src: 'preparePublishRequestData',
71
+ input: ({ context }) => ({ context }),
72
+ },
73
+ },
74
+ [PUBLISHING]: {},
75
+ [IDLE]: {},
76
+ },
77
+ })
@@ -0,0 +1,13 @@
1
+ import { graphql } from '@/graphql/gql'
2
+
3
+ export const GET_SCHEMA_UIDS_FOR_MODELS = graphql(/* GraphQL */ `
4
+ query GetSchemaUids($where: SchemaWhereInput!) {
5
+ schemaUids: schemata(where: $where) {
6
+ id
7
+ schema
8
+ schemaNames {
9
+ name
10
+ }
11
+ }
12
+ }
13
+ `)
@@ -0,0 +1,14 @@
1
+ import { BaseDb } from '@/db/Db/BaseDb'
2
+ import { seeds } from '@/seedSchema'
3
+ import { eq, or } from 'drizzle-orm'
4
+
5
+ export const recoverDeletedItem = async ({ seedLocalId, seedUid }: { seedLocalId: string, seedUid: string }) => {
6
+ const appDb = BaseDb.getAppDb()
7
+
8
+ await appDb
9
+ .update(seeds)
10
+ .set({
11
+ _markedForDeletion: 0,
12
+ })
13
+ .where(or(eq(seeds.localId, seedLocalId), eq(seeds.uid, seedUid)))
14
+ }
@@ -0,0 +1,33 @@
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { FromCallbackInput } from '@/types/machines'
3
+ import { ItemMachineContext } from '@/types/item'
4
+
5
+ export const reload = fromCallback<
6
+ EventObject,
7
+ FromCallbackInput<ItemMachineContext<any>>
8
+ >(({ sendBack, input: { context } }) => {
9
+ const { propertyInstances } = context
10
+
11
+ const _reload = async () => {
12
+ if (!propertyInstances) {
13
+ return
14
+ }
15
+
16
+ for (const propertyInstance of propertyInstances.values()) {
17
+ if (propertyInstance) {
18
+ const propertyRecordSchema = propertyInstance.propertyDef
19
+ if (
20
+ propertyRecordSchema &&
21
+ propertyRecordSchema.storageType &&
22
+ propertyRecordSchema.storageType === 'ItemStorage'
23
+ ) {
24
+ propertyInstance.getService().send({ type: 'reload' })
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ _reload().then(() => {
31
+ sendBack({ type: 'reloadSuccess' })
32
+ })
33
+ })
@@ -0,0 +1,45 @@
1
+ import { getItem } from '@/db/read/getItem'
2
+ import { eventEmitter } from '@/eventBus'
3
+
4
+ const activeItemRequests = new Set<string>()
5
+
6
+ const queue = new Map<string, Record<string, unknown>>()
7
+
8
+ export const itemRequestHandler = async (event) => {
9
+ const { seedLocalId, seedUid, modelName, eventId } = event
10
+
11
+ if (activeItemRequests.has(seedLocalId)) {
12
+ queue.set(seedLocalId, event)
13
+ return
14
+ }
15
+
16
+ const item = await getItem({
17
+ modelName,
18
+ seedLocalId,
19
+ seedUid,
20
+ })
21
+
22
+ if (!item) {
23
+ console.warn(
24
+ `[item/events] [itemRequestHandler] no item for seedLocalId ${seedLocalId} or versionUid ${seedUid}`,
25
+ )
26
+ if (eventId) {
27
+ eventEmitter.emit(`item.${modelName}.${seedLocalId}.response`, {
28
+ item,
29
+ eventId,
30
+ })
31
+ }
32
+ return
33
+ }
34
+
35
+ eventEmitter.emit(`item.${modelName}.${seedLocalId}.response`, {
36
+ item,
37
+ eventId,
38
+ })
39
+
40
+ queue.delete(seedLocalId)
41
+ if (queue.size > 0 && queue.has(seedLocalId)) {
42
+ const nextEvent = queue.get(seedLocalId)
43
+ await itemRequestHandler(nextEvent)
44
+ }
45
+ }
@@ -0,0 +1,157 @@
1
+ import { eventEmitter } from '@/eventBus'
2
+ import { getItemsData } from '@/db/read/getItems'
3
+ import debug from 'debug'
4
+ import { getModel } from '@/stores/modelClass'
5
+ import { BaseItem } from '@/Item/BaseItem'
6
+
7
+ const logger = debug('app:events:requestAll')
8
+
9
+ const cache = new Map<string, Map<string, BaseItem<any>>>()
10
+
11
+ let modelCount = 0
12
+
13
+ const handleRequestAll = async (event) => {
14
+ const { modelName, eventId } = event
15
+
16
+ let modelItemsCache
17
+
18
+ if (cache.has(modelName)) {
19
+ modelItemsCache = cache.get(modelName)
20
+ }
21
+
22
+ if (modelItemsCache && modelName === 'Identity' && modelCount <= 1) {
23
+ // Send what we have while we update the cache
24
+ eventEmitter.emit(`item.${modelName}.requestAll.response`, {
25
+ items: Array.from(modelItemsCache.values()),
26
+ eventId,
27
+ })
28
+ modelCount++
29
+ }
30
+
31
+ if (!modelItemsCache) {
32
+ modelItemsCache = new Map()
33
+ }
34
+
35
+ const itemsData = await getItemsData({ modelName })
36
+
37
+ const cacheKeys = Array.from(modelItemsCache.keys())
38
+
39
+ const keysInDb = []
40
+
41
+ const returnItems = []
42
+
43
+ const ModelClass = getModel(modelName)
44
+
45
+ for (const itemData of itemsData) {
46
+ returnItems.push(
47
+ await BaseItem.create({
48
+ ...itemData,
49
+ modelName,
50
+ }),
51
+ )
52
+ }
53
+
54
+ if (modelName === 'Identity' && modelCount <= 1) {
55
+ logger(
56
+ `[XXXXXX] [handleRequestAll] ${ModelClass?.originalConstructor.name}:`,
57
+ returnItems.length,
58
+ )
59
+ eventEmitter.emit(`item.${modelName}.requestAll.response`, {
60
+ items: returnItems,
61
+ eventId,
62
+ })
63
+ }
64
+
65
+ for (const returnItem of returnItems) {
66
+ const itemCacheKey = `${returnItem.seedLocalId}_${returnItem.seedUid || ''}`
67
+ keysInDb.push(itemCacheKey)
68
+ const existingCacheKey = cacheKeys.find((ck) => ck.includes(itemCacheKey))
69
+
70
+ if (!existingCacheKey) {
71
+ modelItemsCache.set(itemCacheKey, returnItem)
72
+ continue
73
+ }
74
+
75
+ if (existingCacheKey) {
76
+ const existingItem = modelItemsCache.get(existingCacheKey)
77
+ if (!existingItem) {
78
+ console.error(
79
+ '[events/requestAll] [getItemsDataFromDb] no existingItem for cache key',
80
+ existingCacheKey,
81
+ modelItemsCache,
82
+ )
83
+ continue
84
+ }
85
+ for (const [key, value] of Object.entries(returnItem)) {
86
+ if (key === 'seedLocalId' || key === 'seedUid') {
87
+ continue
88
+ }
89
+ existingItem[key] = value
90
+ }
91
+ modelItemsCache.set(existingCacheKey, existingItem)
92
+ }
93
+ }
94
+
95
+ // Remove any stale items from the cache that were deleted from the DB
96
+ if (keysInDb.length !== cacheKeys.length) {
97
+ for (const cacheKey of cacheKeys) {
98
+ if (!keysInDb.includes(cacheKey)) {
99
+ modelItemsCache.delete(cacheKey)
100
+ }
101
+ }
102
+ }
103
+
104
+ cache.set(modelName, modelItemsCache)
105
+
106
+ if (modelName === 'Identity' && modelCount <= 1) {
107
+ logger(
108
+ `[XXXXXX] [getItemsDataFromDb] ${modelName} responding with`,
109
+ Array.from(modelItemsCache.values()),
110
+ )
111
+ eventEmitter.emit(`item.${modelName}.requestAll.response`, {
112
+ items: Array.from(modelItemsCache.values()),
113
+ eventId,
114
+ })
115
+ }
116
+ }
117
+
118
+ export const itemRequestAllHandler = async (event) => {
119
+ logger('[events/requestAll] Request all items', event)
120
+ await handleRequestAll(event)
121
+ // eventQueue.push(event, (err) => {
122
+ // if (err) {
123
+ // console.error(err)
124
+ // }
125
+ // if (!err) {
126
+ // logger('[events/requestAll] Request all items done without error')
127
+ // }
128
+ // })
129
+ }
130
+
131
+ // export const getItemCache = () => {
132
+ // return itemCache
133
+ // }
134
+
135
+ type CreateItemCacheKeyParams = {
136
+ seedLocalId?: string
137
+ seedUid?: string
138
+ }
139
+
140
+ type CreateItemCacheKey = (
141
+ itemData: CreateItemCacheKeyParams,
142
+ ) => string | undefined
143
+
144
+ export const createItemCacheKey: CreateItemCacheKey = (itemData) => {
145
+ if (!itemData || (!itemData.seedLocalId && !itemData.seedUid)) {
146
+ return
147
+ }
148
+ return `${itemData.seedLocalId}_${itemData.seedUid || ''}`
149
+ }
150
+
151
+ // export const updateItemCache = (itemCacheKey: string, item: Item<any>) => {
152
+ // itemCache.set(itemCacheKey, item)
153
+ // }
154
+ //
155
+ // export const deleteItemFromCache = (itemCacheKey: string) => {
156
+ // itemCache.delete(itemCacheKey)
157
+ // }