@seedprotocol/sdk 0.2.51 → 0.2.52

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 (205) hide show
  1. package/dist/{ArweaveClient-D1TsTBoj.js → ArweaveClient-D5iJisYr.js} +2 -2
  2. package/dist/{ArweaveClient-DpdXUcVJ.js.map → ArweaveClient-D5iJisYr.js.map} +1 -1
  3. package/dist/{ArweaveClient-DpdXUcVJ.js → ArweaveClient-HEIIuOfB.js} +2 -2
  4. package/dist/{ArweaveClient-D1TsTBoj.js.map → ArweaveClient-HEIIuOfB.js.map} +1 -1
  5. package/dist/{Db-quJxmvHH.js → Db-D-BsZYlN.js} +3 -3
  6. package/dist/{Db-quJxmvHH.js.map → Db-D-BsZYlN.js.map} +1 -1
  7. package/dist/{Db-DMJ0v3gO.js → Db-Dpsbk_T7.js} +3 -3
  8. package/dist/{Db-DMJ0v3gO.js.map → Db-Dpsbk_T7.js.map} +1 -1
  9. package/dist/{EasClient-ClZDuT3o.js → EasClient-D3wKgt0o.js} +2 -2
  10. package/dist/{EasClient-DfnGY_2X.js.map → EasClient-D3wKgt0o.js.map} +1 -1
  11. package/dist/{EasClient-DfnGY_2X.js → EasClient-jqkT2ZBt.js} +2 -2
  12. package/dist/{EasClient-ClZDuT3o.js.map → EasClient-jqkT2ZBt.js.map} +1 -1
  13. package/dist/{FileManager-texI7UwL.js → FileManager-4mWYO3Mt.js} +3 -5
  14. package/dist/FileManager-4mWYO3Mt.js.map +1 -0
  15. package/dist/{Item-D4n439Ei.js → Item-CqWdJsyg.js} +3 -3
  16. package/dist/{Item-D4n439Ei.js.map → Item-CqWdJsyg.js.map} +1 -1
  17. package/dist/{ItemProperty-Be5ymRVT.js → ItemProperty-BpBW3IWx.js} +3 -3
  18. package/dist/{ItemProperty-Be5ymRVT.js.map → ItemProperty-BpBW3IWx.js.map} +1 -1
  19. package/dist/{QueryClient-B1HkAaJo.js → QueryClient-CBOP3OoU.js} +2 -2
  20. package/dist/{QueryClient-B1HkAaJo.js.map → QueryClient-CBOP3OoU.js.map} +1 -1
  21. package/dist/{QueryClient-BPoc9eBn.js → QueryClient-bKb3SB0W.js} +2 -2
  22. package/dist/{QueryClient-BPoc9eBn.js.map → QueryClient-bKb3SB0W.js.map} +1 -1
  23. package/dist/bin.js +5 -5
  24. package/dist/{constants-5PxfAeqj.js → constants-CIDbABDz.js} +2 -2
  25. package/dist/{constants-5PxfAeqj.js.map → constants-CIDbABDz.js.map} +1 -1
  26. package/dist/{index-BeKRizLf.js → index-BRxO1tLl.js} +257 -145
  27. package/dist/index-BRxO1tLl.js.map +1 -0
  28. package/dist/{index-BLla2o05.js → index-d8Qsot6w.js} +3 -3
  29. package/dist/index-d8Qsot6w.js.map +1 -0
  30. package/dist/main.js +2 -2
  31. package/dist/package.json +189 -0
  32. package/dist/{seed.schema.config-CMpV_hyS.js → seed.schema.config-CPRn6KVL.js} +3 -3
  33. package/dist/{seed.schema.config-CMpV_hyS.js.map → seed.schema.config-CPRn6KVL.js.map} +1 -1
  34. package/dist/types/src/client/BaseClientManager.d.ts +7 -0
  35. package/dist/types/src/client/BaseClientManager.d.ts.map +1 -0
  36. package/dist/types/src/client/actions/setAddresses.d.ts +2 -0
  37. package/dist/types/src/client/actions/setAddresses.d.ts.map +1 -0
  38. package/dist/types/src/client/actors/initialize.d.ts +4 -0
  39. package/dist/types/src/client/actors/initialize.d.ts.map +1 -0
  40. package/dist/types/src/client/actors/saveAppState.d.ts +5 -0
  41. package/dist/types/src/client/actors/saveAppState.d.ts.map +1 -0
  42. package/dist/types/src/client/clientManagerMachine.d.ts +19 -0
  43. package/dist/types/src/client/clientManagerMachine.d.ts.map +1 -0
  44. package/dist/types/src/client/index.d.ts +41 -0
  45. package/dist/types/src/client/index.d.ts.map +1 -0
  46. package/dist/types/src/client.d.ts.map +1 -1
  47. package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
  48. package/dist/types/src/services/internal/actors/validateInput.d.ts.map +1 -1
  49. package/dist/types/src/types/machines.d.ts +5 -0
  50. package/dist/types/src/types/machines.d.ts.map +1 -1
  51. package/package.json +2 -2
  52. package/dist/FileManager-texI7UwL.js.map +0 -1
  53. package/dist/index-BLla2o05.js.map +0 -1
  54. package/dist/index-BeKRizLf.js.map +0 -1
  55. package/dist/src/AppStateSchema.ts +0 -10
  56. package/dist/src/ArweaveClient.ts +0 -13
  57. package/dist/src/Attestation.ts +0 -21
  58. package/dist/src/BaseArweaveClient.ts +0 -13
  59. package/dist/src/BaseDb.ts +0 -35
  60. package/dist/src/BaseEasClient.ts +0 -13
  61. package/dist/src/BaseFileManager.ts +0 -56
  62. package/dist/src/BaseItem.ts +0 -413
  63. package/dist/src/BaseItemProperty.ts +0 -416
  64. package/dist/src/BaseQueryClient.ts +0 -13
  65. package/dist/src/ConfigSchema.ts +0 -15
  66. package/dist/src/Db.ts +0 -78
  67. package/dist/src/EasClient.ts +0 -13
  68. package/dist/src/FileDownloader.ts +0 -63
  69. package/dist/src/FileManager.ts +0 -57
  70. package/dist/src/IDb.ts +0 -7
  71. package/dist/src/IEasClient.ts +0 -2
  72. package/dist/src/IItem.ts +0 -26
  73. package/dist/src/IItemProperty.ts +0 -28
  74. package/dist/src/IQueryClient.ts +0 -4
  75. package/dist/src/ImageResizer.ts +0 -81
  76. package/dist/src/Item.ts +0 -12
  77. package/dist/src/ItemProperty.ts +0 -9
  78. package/dist/src/MetadataSchema.ts +0 -28
  79. package/dist/src/ModelSchema.ts +0 -46
  80. package/dist/src/ModelUidSchema.ts +0 -16
  81. package/dist/src/PropertyUidSchema.ts +0 -16
  82. package/dist/src/QueryClient.ts +0 -22
  83. package/dist/src/Schema.ts +0 -17
  84. package/dist/src/SeedProtocolSdk.d.ts +0 -19
  85. package/dist/src/SeedSchema.ts +0 -29
  86. package/dist/src/VersionSchema.ts +0 -16
  87. package/dist/src/actors.ts +0 -295
  88. package/dist/src/addModelsToDb.ts +0 -152
  89. package/dist/src/allItems.ts +0 -31
  90. package/dist/src/analyzeInput.ts +0 -144
  91. package/dist/src/browser.app.db.config.ts +0 -27
  92. package/dist/src/browser.ts +0 -30
  93. package/dist/src/checkStatus.ts +0 -50
  94. package/dist/src/client.ts +0 -148
  95. package/dist/src/configureFs.ts +0 -94
  96. package/dist/src/connectToDb.ts +0 -41
  97. package/dist/src/connectionManager.ts +0 -67
  98. package/dist/src/constants.ts +0 -125
  99. package/dist/src/content-hash.ts +0 -30
  100. package/dist/src/create.ts +0 -39
  101. package/dist/src/createMetadata.ts +0 -77
  102. package/dist/src/createNewItem.ts +0 -61
  103. package/dist/src/createPublishAttempt.ts +0 -16
  104. package/dist/src/createSeed.ts +0 -31
  105. package/dist/src/createSeeds.ts +0 -24
  106. package/dist/src/createVersion.ts +0 -33
  107. package/dist/src/dbMachine.ts +0 -182
  108. package/dist/src/deleteItem.ts +0 -19
  109. package/dist/src/download.ts +0 -202
  110. package/dist/src/drizzle.ts +0 -78
  111. package/dist/src/eas.ts +0 -40
  112. package/dist/src/environment.ts +0 -35
  113. package/dist/src/eventBus.ts +0 -5
  114. package/dist/src/events.ts +0 -14
  115. package/dist/src/fetchDataFromEas.ts +0 -94
  116. package/dist/src/fetchDbData.ts +0 -19
  117. package/dist/src/fetchRelatedItems.ts +0 -184
  118. package/dist/src/fetchSeeds.ts +0 -52
  119. package/dist/src/fetchVersions.ts +0 -49
  120. package/dist/src/files.ts +0 -81
  121. package/dist/src/filesDownload.ts +0 -326
  122. package/dist/src/fragment-masking.ts +0 -87
  123. package/dist/src/getExistingItem.ts +0 -60
  124. package/dist/src/getItem.ts +0 -32
  125. package/dist/src/getItemData.ts +0 -99
  126. package/dist/src/getItemProperties.ts +0 -56
  127. package/dist/src/getItemProperty.ts +0 -34
  128. package/dist/src/getItems.ts +0 -70
  129. package/dist/src/getMetadata.ts +0 -40
  130. package/dist/src/getModelSchemas.ts +0 -89
  131. package/dist/src/getPropertyData.ts +0 -50
  132. package/dist/src/getPublishPayload.ts +0 -355
  133. package/dist/src/getPublishUploads.ts +0 -207
  134. package/dist/src/getRelationValueData.ts +0 -27
  135. package/dist/src/getSchemaForItemProperty.ts +0 -68
  136. package/dist/src/getSchemaForModel.ts +0 -42
  137. package/dist/src/getSchemaUidForModel.ts +0 -32
  138. package/dist/src/getSeedData.ts +0 -34
  139. package/dist/src/getSegmentedItemProperties.ts +0 -65
  140. package/dist/src/getStorageTransactionIdForSeedUid.ts +0 -38
  141. package/dist/src/getVersionData.ts +0 -46
  142. package/dist/src/getVersionsForVersionUids.ts +0 -39
  143. package/dist/src/globalMachine.ts +0 -280
  144. package/dist/src/gql.ts +0 -118
  145. package/dist/src/graphql.ts +0 -3209
  146. package/dist/src/helpers.ts +0 -273
  147. package/dist/src/hydrateExistingItem.ts +0 -137
  148. package/dist/src/hydrateFromDb.ts +0 -270
  149. package/dist/src/hydrateNewItem.ts +0 -34
  150. package/dist/src/imageResize.ts +0 -507
  151. package/dist/src/index.d.ts +0 -5
  152. package/dist/src/index.ts +0 -4
  153. package/dist/src/initialize.ts +0 -124
  154. package/dist/src/internalMachine.ts +0 -238
  155. package/dist/src/itemMachineAll.ts +0 -157
  156. package/dist/src/itemMachineSingle.ts +0 -158
  157. package/dist/src/loadAppDb.ts +0 -47
  158. package/dist/src/machines.ts +0 -60
  159. package/dist/src/metadataLatest.ts +0 -34
  160. package/dist/src/migrate.ts +0 -53
  161. package/dist/src/model.ts +0 -72
  162. package/dist/src/modelClass.ts +0 -19
  163. package/dist/src/node.app.db.config.ts +0 -41
  164. package/dist/src/prepareDb.ts +0 -29
  165. package/dist/src/preparePublishRequestData.ts +0 -81
  166. package/dist/src/processItems.ts +0 -74
  167. package/dist/src/property.ts +0 -156
  168. package/dist/src/propertyMachine.ts +0 -202
  169. package/dist/src/publish.ts +0 -28
  170. package/dist/src/publishMachine.ts +0 -77
  171. package/dist/src/queries.ts +0 -13
  172. package/dist/src/recoverDeletedItem.ts +0 -14
  173. package/dist/src/reload.ts +0 -33
  174. package/dist/src/request.ts +0 -45
  175. package/dist/src/requestAll.ts +0 -157
  176. package/dist/src/resolveRelatedValue.ts +0 -367
  177. package/dist/src/resolveRemoteStorage.ts +0 -88
  178. package/dist/src/saveAppState.ts +0 -19
  179. package/dist/src/saveConfig.ts +0 -88
  180. package/dist/src/saveDataToDb.ts +0 -145
  181. package/dist/src/saveImageSrc.ts +0 -242
  182. package/dist/src/saveItemStorage.ts +0 -156
  183. package/dist/src/saveMetadata.ts +0 -18
  184. package/dist/src/savePublishService.ts +0 -30
  185. package/dist/src/saveRelation.ts +0 -112
  186. package/dist/src/seed.schema.config.ts +0 -25
  187. package/dist/src/seed.ts +0 -10
  188. package/dist/src/seedProtocol.ts +0 -17
  189. package/dist/src/services.ts +0 -365
  190. package/dist/src/sqlWasmClient.ts +0 -88
  191. package/dist/src/syncDbWithEas.ts +0 -628
  192. package/dist/src/trash.ts +0 -29
  193. package/dist/src/ts-to-proto.ts +0 -101
  194. package/dist/src/types.ts +0 -12
  195. package/dist/src/updateItemPropertyValue.ts +0 -243
  196. package/dist/src/updateMachineContext.ts +0 -21
  197. package/dist/src/updateMetadata.ts +0 -59
  198. package/dist/src/upload.ts +0 -86
  199. package/dist/src/validate.ts +0 -45
  200. package/dist/src/validateInput.ts +0 -47
  201. package/dist/src/validateItemData.ts +0 -20
  202. package/dist/src/versionData.ts +0 -24
  203. package/dist/src/waitForDb.ts +0 -25
  204. package/dist/src/waitForFiles.ts +0 -34
  205. package/dist/src/wasm.d.ts +0 -8300
@@ -1,10 +0,0 @@
1
- import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
2
-
3
- export const appState = sqliteTable('appState', {
4
- key: text('key').unique(),
5
- value: text('value'),
6
- createdAt: int('created_at'),
7
- updatedAt: int('updated_at'),
8
- })
9
-
10
- export type appStateType = appState.$inferSelect
@@ -1,13 +0,0 @@
1
- import { BaseArweaveClient } from '@/helpers/ArweaveClient/BaseArweaveClient';
2
- import { ARWEAVE_ENDPOINT } from '@/services/internal/constants';
3
- import { GraphQLClient } from 'graphql-request';
4
-
5
- class ArweaveClient extends BaseArweaveClient {
6
- static getArweaveClient(): GraphQLClient {
7
- return new GraphQLClient(ARWEAVE_ENDPOINT);
8
- }
9
- }
10
-
11
- BaseArweaveClient.setPlatformClass(ArweaveClient);
12
-
13
- export { ArweaveClient };
@@ -1,21 +0,0 @@
1
- import { graphql, } from '../gql'
2
-
3
- export const ATTESTATION_FIELDS = graphql(/* GraphQL */ `
4
- fragment attestationFields on Attestation {
5
- id
6
- decodedDataJson
7
- attester
8
- schema {
9
- schemaNames {
10
- name
11
- }
12
- }
13
- refUID
14
- revoked
15
- schemaId
16
- txid
17
- timeCreated
18
- time
19
- isOffchain
20
- }
21
- `,)
@@ -1,13 +0,0 @@
1
- import { GraphQLClient } from 'graphql-request'
2
-
3
- export abstract class BaseArweaveClient {
4
- static PlatformClass: typeof BaseArweaveClient
5
-
6
- static setPlatformClass(platformClass: typeof BaseArweaveClient) {
7
- this.PlatformClass = platformClass
8
- }
9
-
10
- static getArweaveClient(): GraphQLClient {
11
- return this.PlatformClass.getArweaveClient()
12
- }
13
- }
@@ -1,35 +0,0 @@
1
- import { IDb } from '@/interfaces/IDb'
2
- import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core'
3
-
4
- export abstract class BaseDb implements IDb {
5
-
6
- constructor() {
7
- }
8
-
9
- static PlatformClass: typeof BaseDb
10
-
11
- static setPlatformClass(platformClass: typeof BaseDb) {
12
- this.PlatformClass = platformClass
13
- }
14
-
15
- static getAppDb(): BaseSQLiteDatabase {
16
- return this.PlatformClass.getAppDb()
17
- }
18
-
19
- static isAppDbReady(): boolean {
20
- return this.PlatformClass.isAppDbReady()
21
- }
22
-
23
- static prepareDb(filesDir: string): Promise<BaseSQLiteDatabase> {
24
- return this.PlatformClass.prepareDb(filesDir)
25
- }
26
-
27
- static connectToDb(pathToDir: string, dbName: string): Promise<unknown> {
28
- return this.PlatformClass.connectToDb(pathToDir, dbName)
29
- }
30
-
31
- static async migrate(pathToDbDir: string, dbName: string, dbId: string): Promise<void> {
32
- return this.PlatformClass.migrate(pathToDbDir, dbName, dbId)
33
- }
34
-
35
- }
@@ -1,13 +0,0 @@
1
- import { GraphQLClient } from 'graphql-request'
2
-
3
- export abstract class BaseEasClient {
4
- static PlatformClass: typeof BaseEasClient
5
-
6
- static setPlatformClass(platformClass: typeof BaseEasClient) {
7
- this.PlatformClass = platformClass
8
- }
9
-
10
- static getEasClient(): GraphQLClient {
11
- return this.PlatformClass.getEasClient()
12
- }
13
- }
@@ -1,56 +0,0 @@
1
- export abstract class BaseFileManager {
2
- private static fileSystemInitialized = false
3
- private static initializing = false
4
-
5
- static PlatformClass: typeof BaseFileManager
6
-
7
- static setPlatformClass( platformClass: typeof BaseFileManager ) {
8
- this.PlatformClass = platformClass
9
- }
10
-
11
- static async initializeFileSystem(): Promise<void> {
12
- if (this.initializing || this.fileSystemInitialized) {
13
- return Promise.resolve()
14
- }
15
- this.initializing = true
16
- await this.PlatformClass.initializeFileSystem()
17
- this.fileSystemInitialized = true
18
- this.initializing = false
19
- }
20
-
21
- static getContentUrlFromPath( path: string ): Promise<string | undefined> {
22
- return this.PlatformClass.getContentUrlFromPath(path)
23
- }
24
-
25
- static downloadAllFiles( {
26
- transactionIds,
27
- arweaveHost,
28
- excludedTransactions,
29
- }: DownloadAllFilesParams ): Promise<void> {
30
- return this.PlatformClass.downloadAllFiles({ transactionIds, arweaveHost, excludedTransactions })
31
- }
32
-
33
- static resizeImage( { filePath, width, height }: ResizeImageParams ): Promise<void> {
34
- return this.PlatformClass.resizeImage({ filePath, width, height })
35
- }
36
-
37
- static resizeAllImages( { width, height }: ResizeAllImagesParams ): Promise<void> {
38
- return this.PlatformClass.resizeAllImages({ width, height })
39
- }
40
-
41
- static pathExists(filePath: string): Promise<boolean> {
42
- return this.PlatformClass.pathExists(filePath)
43
- }
44
-
45
- static createDirIfNotExists(filePath: string): Promise<void> {
46
- return this.PlatformClass.createDirIfNotExists(filePath)
47
- }
48
-
49
- static async waitForFile(filePath: string): Promise<boolean> {
50
- return this.PlatformClass.waitForFile(filePath)
51
- }
52
-
53
- static async saveFile(filePath: string, content: string | Blob | ArrayBuffer): Promise<void> {
54
- return this.PlatformClass.saveFile(filePath, content)
55
- }
56
- }
@@ -1,413 +0,0 @@
1
- import { IItem, IItemProperty } from '@/interfaces'
2
- import { itemMachineSingle } from '@/Item/service/itemMachineSingle'
3
- import { internalPropertyNames } from '@/helpers/constants'
4
- import { VersionsType } from '@/seedSchema/VersionSchema'
5
- import { getModel } from '@/stores/modelClass'
6
-
7
- import {
8
- CreatePropertyInstanceProps,
9
- ItemData,
10
- ItemFindProps,
11
- ModelSchema,
12
- ModelValues,
13
- NewItemProps,
14
- PropertyData
15
- } from '@/types'
16
-
17
- import { immerable } from 'immer'
18
- import { BehaviorSubject } from 'rxjs'
19
- import { ActorRefFrom, Subscription, createActor } from 'xstate'
20
- import { eventEmitter } from '@/eventBus'
21
- import pluralize from 'pluralize'
22
- import { orderBy, startCase } from 'lodash-es'
23
- import { waitForEvent } from '@/events'
24
- import { getItemData } from '@/db/read/getItemData'
25
- import { getItemsData } from '@/db/read/getItems'
26
- import { BaseItemProperty } from '@/ItemProperty/BaseItemProperty'
27
- import { getItemProperties } from '@/db/read/getItemProperties'
28
- import { getPublishUploads } from '@/db/read/getPublishUploads'
29
- import { getPublishPayload } from '@/db/read/getPublishPayload'
30
- import { createNewItem } from '@/db/write/createNewItem'
31
-
32
-
33
- export abstract class BaseItem<T extends ModelValues<ModelSchema>> implements IItem<T> {
34
-
35
- protected static instanceCache: Map<string, { instance: BaseItem<any>; refCount: number }> = new Map();
36
- protected _subscription: Subscription | undefined;
37
- protected readonly _storageTransactionId: string | undefined;
38
- [immerable] = true;
39
- protected _propertiesSubject: BehaviorSubject<Record<string, IItemProperty<any>>> = new BehaviorSubject({});
40
- protected readonly _service: ActorRefFrom<typeof itemMachineSingle>;
41
-
42
- constructor(initialValues: NewItemProps<T>) {
43
-
44
- const {
45
- modelName,
46
- seedUid,
47
- schemaUid,
48
- seedLocalId,
49
- latestVersionLocalId,
50
- latestVersionUid,
51
- } = initialValues
52
-
53
- const ModelClass = getModel(modelName)
54
-
55
- if (
56
- ModelClass &&
57
- Object.keys(ModelClass?.schema).includes('storageTransactionId') &&
58
- initialValues.storageTransactionId
59
- ) {
60
- this._storageTransactionId = initialValues.storageTransactionId
61
- }
62
-
63
- this._service = createActor(itemMachineSingle, {
64
- input: {
65
- seedLocalId,
66
- seedUid,
67
- schemaUid,
68
- ModelClass,
69
- latestVersionLocalId,
70
- latestVersionUid,
71
- storageTransactionId: this._storageTransactionId,
72
- },
73
- })
74
-
75
- this._subscription = this._service.subscribe((snapshot) => {
76
- const { context } = snapshot
77
-
78
- if (
79
- !context ||
80
- !context.propertyInstances ||
81
- context.propertyInstances.size === 0
82
- ) {
83
- return
84
- }
85
-
86
- const propertiesObj: Record<string, IItemProperty<any>> = {}
87
-
88
- for (const [key, propertyInstance] of context.propertyInstances) {
89
- if (typeof key !== 'string' || internalPropertyNames.includes(key)) {
90
- propertiesObj[key.toString()] = propertyInstance
91
- continue
92
- }
93
-
94
- let transformedKey: string = key as string
95
-
96
- if (propertyInstance.alias) {
97
- transformedKey = propertyInstance.alias
98
- }
99
-
100
- if (!propertyInstance.alias && key.endsWith('Ids')) {
101
- transformedKey = key.slice(0, -3) // Remove 'Ids'
102
- transformedKey = pluralize(transformedKey)
103
- }
104
-
105
- if (!propertyInstance.alias && key.endsWith('Id')) {
106
- transformedKey = key.slice(0, -2) // Remove 'Id'
107
- }
108
-
109
- propertiesObj[transformedKey] = propertyInstance
110
- }
111
-
112
- this._propertiesSubject.next(propertiesObj)
113
- // eventEmitter.emit(`item.${modelName}.${seedUid || seedLocalId}.update`)
114
- })
115
-
116
- this._service.start()
117
-
118
- const definedKeys: string[] = ['ModelClass']
119
-
120
- const itemPropertyBase: Partial<CreatePropertyInstanceProps> = {
121
- seedLocalId,
122
- seedUid,
123
- versionLocalId: latestVersionLocalId,
124
- versionUid: latestVersionUid,
125
- modelName: modelName,
126
- }
127
-
128
- if (ModelClass && ModelClass.schema) {
129
- const schema = ModelClass.schema
130
-
131
- for (const [propertyName, propertyRecordSchema] of Object.entries(
132
- schema,
133
- )) {
134
- if (!propertyRecordSchema) {
135
- throw new Error(`Property ${propertyName} has no definition`)
136
- }
137
-
138
- this._createPropertyInstance({
139
- ...itemPropertyBase,
140
- propertyName,
141
- propertyValue: initialValues[propertyName as keyof T],
142
- })
143
-
144
- definedKeys.push(propertyName)
145
-
146
- if (
147
- propertyRecordSchema.dataType === 'Relation' &&
148
- !propertyName.endsWith('Id')
149
- ) {
150
- definedKeys.push(`${propertyName}Id`)
151
- }
152
-
153
- if (
154
- propertyRecordSchema.dataType === 'List' &&
155
- !propertyName.endsWith('Ids')
156
- ) {
157
- const singularPropertyName = pluralize.singular(propertyName)
158
- const propertyNameForSchema = `${singularPropertyName}${propertyRecordSchema.ref}Ids`
159
- definedKeys.push(propertyNameForSchema)
160
- }
161
- }
162
- }
163
-
164
- ; (Object.keys(initialValues) as Array<string & keyof Partial<T>>).forEach(
165
- (key) => {
166
- // If we already defined it, that means it was in the schema
167
- if (definedKeys.includes(key)) {
168
- return
169
- }
170
-
171
- this._createPropertyInstance({
172
- ...itemPropertyBase,
173
- propertyName: key,
174
- propertyValue: initialValues[key],
175
- })
176
- },
177
- )
178
- }
179
-
180
- static PlatformClass: typeof BaseItem
181
-
182
- static setPlatformClass(platformClass: typeof BaseItem) {
183
- this.PlatformClass = platformClass
184
- }
185
-
186
- static async create<T extends ModelValues<ModelSchema>>(
187
- props: Partial<ItemData>,
188
- ): Promise<BaseItem<any>> {
189
- if (!props.modelName && props.type) {
190
- props.modelName = startCase(props.type)
191
- }
192
- if (props.seedUid || props.seedLocalId) {
193
- const seedId = (props.seedUid || props.seedLocalId) as string
194
- if (this.instanceCache.has(seedId)) {
195
- const { instance, refCount } = this.instanceCache.get(seedId)!
196
- this.instanceCache.set(seedId, {
197
- instance,
198
- refCount: refCount + 1,
199
- })
200
- for (const [propertyName, propertyValue] of Object.entries(props)) {
201
- const propertyInstances = instance.getService().getSnapshot()
202
- .context.propertyInstances
203
- if (!propertyInstances || !propertyInstances.has(propertyName)) {
204
- continue
205
- }
206
- const propertyInstance = propertyInstances.get(propertyName)
207
- if (!propertyInstance) {
208
- continue
209
- }
210
- propertyInstance.getService().send({
211
- type: 'updateContext',
212
- propertyValue,
213
- })
214
- }
215
- return instance
216
- }
217
- if (!this.instanceCache.has(seedId)) {
218
- const newInstance = new this(props)
219
- this.instanceCache.set(seedId, {
220
- instance: newInstance,
221
- refCount: 1,
222
- })
223
- return newInstance
224
- }
225
- }
226
- if (!props.modelName) {
227
- throw new Error('Model name is required to create an item')
228
- }
229
- const { seedLocalId, versionLocalId, } = await createNewItem({
230
- modelName: props.modelName,
231
- ...props,
232
- })
233
- props.seedLocalId = seedLocalId
234
- props.latestVersionLocalId = versionLocalId
235
- const newInstance = new this(props)
236
- this.instanceCache.set(newInstance.seedUid || newInstance.seedLocalId, {
237
- instance: newInstance,
238
- refCount: 1,
239
- })
240
- return newInstance
241
- }
242
-
243
- static async find({
244
- modelName,
245
- seedLocalId,
246
- seedUid,
247
- }: ItemFindProps): Promise<IItem<any> | undefined> {
248
- if (!seedLocalId && !seedUid) {
249
- return
250
- }
251
- const itemData = await getItemData({
252
- modelName,
253
- seedLocalId,
254
- seedUid,
255
- })
256
-
257
- if (!itemData) {
258
- console.error('No item data found', { modelName, seedLocalId, seedUid })
259
- return
260
- }
261
-
262
- return BaseItem.create({
263
- ...itemData,
264
- modelName,
265
- })
266
- }
267
-
268
- static async all(
269
- modelName?: string,
270
- deleted?: boolean,
271
- ): Promise<BaseItem<any>[]> {
272
- const itemsData = await getItemsData({ modelName, deleted })
273
- const itemInstances: BaseItem<any>[] = []
274
- for (const itemData of itemsData) {
275
- itemInstances.push(
276
- await BaseItem.create({
277
- ...itemData,
278
- modelName,
279
- }),
280
- )
281
- }
282
-
283
- return orderBy(itemInstances, ['createdAt'], ['desc'])
284
- }
285
-
286
- protected _createPropertyInstance(props: Partial<CreatePropertyInstanceProps>) {
287
- if (this._storageTransactionId) {
288
- props.storageTransactionId = this._storageTransactionId
289
- }
290
-
291
- const propertyInstance = BaseItemProperty.create(props)
292
-
293
- if (!propertyInstance || !props.propertyName) {
294
- return
295
- }
296
-
297
- this._service.send({
298
- type: 'addPropertyInstance',
299
- propertyName: props.propertyName,
300
- propertyInstance,
301
- })
302
-
303
- Object.defineProperty(this, props.propertyName, {
304
- get: () => propertyInstance.value,
305
- set: (value) => (propertyInstance.value = value),
306
- enumerable: true,
307
- })
308
- }
309
-
310
-
311
- static async publish(item: IItem<any>): Promise<void> {
312
- await waitForEvent({
313
- req: {
314
- eventLabel: `item.${item.seedLocalId}.publish.request`,
315
- data: {
316
- seedLocalId: item.seedLocalId,
317
- },
318
- },
319
- res: {
320
- eventLabel: `item.${item.seedLocalId}.publish.success`,
321
- },
322
- })
323
- }
324
-
325
- subscribe = (callback: (itemProps: any) => void): Subscription => {
326
- return this._service.subscribe((snapshot) => {
327
- callback(snapshot.context)
328
- })
329
- }
330
-
331
- getService = (): ActorRefFrom<typeof itemMachineSingle> => {
332
- return this._service
333
- }
334
-
335
- getEditedProperties = async (): Promise<PropertyData[]> => {
336
- return await getItemProperties({
337
- seedLocalId: this.seedLocalId,
338
- edited: true,
339
- })
340
- }
341
-
342
- publish = async (): Promise<void> => {
343
- await waitForEvent({
344
- req: {
345
- eventLabel: `item.publish.request`,
346
- data: {
347
- seedLocalId: this.seedLocalId,
348
- },
349
- },
350
- res: {
351
- eventLabel: `item.${this.seedLocalId}.publish.success`,
352
- },
353
- })
354
- }
355
-
356
- getPublishUploads = async () => {
357
- return await getPublishUploads(this)
358
- }
359
-
360
- getPublishPayload = async (uploadedTransactions: any[]) => {
361
- return await getPublishPayload(this, uploadedTransactions)
362
- }
363
-
364
- get serviceContext() {
365
- const snapshot = this._service.getSnapshot()
366
- return (snapshot as any).context || {}
367
- }
368
-
369
- get seedLocalId(): string {
370
- return this.serviceContext.seedLocalId as string
371
- }
372
-
373
- get seedUid(): string | undefined {
374
- return this.serviceContext.seedUid
375
- }
376
-
377
- get schemaUid(): string {
378
- return this.serviceContext.schemaUid as string
379
- }
380
-
381
- get latestVersionUid(): VersionsType {
382
- return this.serviceContext.latestVersionUid as VersionsType
383
- }
384
-
385
- get latestVersionLocalId(): string {
386
- return this.serviceContext.latestVersionLocalId as string
387
- }
388
-
389
- get modelName(): string {
390
- return this.serviceContext.modelName as string
391
- }
392
-
393
- get properties(): Record<string, IItemProperty<any>> {
394
- return this._propertiesSubject.value
395
- }
396
-
397
- get attestationCreatedAt(): number {
398
- return this.serviceContext.attestationCreatedAt as number
399
- }
400
-
401
- get versionsCount(): number {
402
- return this.serviceContext.versionsCount as number
403
- }
404
-
405
- get lastVersionPublishedAt(): number {
406
- return this.serviceContext.lastVersionPublishedAt as number
407
- }
408
-
409
- unload(): void {
410
- this._subscription?.unsubscribe()
411
- this._service.stop()
412
- }
413
- }