@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.
- package/dist/{ArweaveClient-CmJYeuq1.js → ArweaveClient-C0g0es2v.js} +2 -2
- package/dist/{ArweaveClient-EXXrXUzz.js.map → ArweaveClient-C0g0es2v.js.map} +1 -1
- package/dist/{ArweaveClient-EXXrXUzz.js → ArweaveClient-CzkBgiZY.js} +2 -2
- package/dist/{ArweaveClient-CmJYeuq1.js.map → ArweaveClient-CzkBgiZY.js.map} +1 -1
- package/dist/{Db-CESKJH-G.js → Db-C4oog-0n.js} +3 -3
- package/dist/{Db-CESKJH-G.js.map → Db-C4oog-0n.js.map} +1 -1
- package/dist/{Db-DiTPSbQ8.js → Db-VPouegkr.js} +3 -3
- package/dist/{Db-DiTPSbQ8.js.map → Db-VPouegkr.js.map} +1 -1
- package/dist/{EasClient-BesHK2bO.js → EasClient-B9a3NsTi.js} +2 -2
- package/dist/{EasClient-BesHK2bO.js.map → EasClient-B9a3NsTi.js.map} +1 -1
- package/dist/{EasClient-Cop6WtWl.js → EasClient-DQ93i2ci.js} +2 -2
- package/dist/{EasClient-Cop6WtWl.js.map → EasClient-DQ93i2ci.js.map} +1 -1
- package/dist/{FileManager-DTzkncLh.js → FileManager-_ErxVuwb.js} +8 -5
- package/dist/FileManager-_ErxVuwb.js.map +1 -0
- package/dist/{Item-B5Tz-Y7t.js → Item-D9HFPxsK.js} +3 -3
- package/dist/{Item-B5Tz-Y7t.js.map → Item-D9HFPxsK.js.map} +1 -1
- package/dist/{ItemProperty-DBV0Iib_.js → ItemProperty-BdtvKrEy.js} +3 -3
- package/dist/{ItemProperty-DBV0Iib_.js.map → ItemProperty-BdtvKrEy.js.map} +1 -1
- package/dist/{QueryClient-C7a0NHCt.js → QueryClient-Bmnv_moN.js} +2 -2
- package/dist/{QueryClient-C7a0NHCt.js.map → QueryClient-Bmnv_moN.js.map} +1 -1
- package/dist/{QueryClient-BnwbHtqe.js → QueryClient-hzOtFe99.js} +2 -2
- package/dist/{QueryClient-BnwbHtqe.js.map → QueryClient-hzOtFe99.js.map} +1 -1
- package/dist/bin.js +5 -5
- package/dist/bin.js.map +1 -1
- package/dist/{constants-CWg2B3Au.js → constants-Bhl5dZpN.js} +4 -1
- package/dist/constants-Bhl5dZpN.js.map +1 -0
- package/dist/{index-CQ9FS10O.js → index-BAtjd9W-.js} +3 -3
- package/dist/index-BAtjd9W-.js.map +1 -0
- package/dist/{index-BHGJ-Es9.js → index-BSR5hv7Q.js} +59 -27
- package/dist/index-BSR5hv7Q.js.map +1 -0
- package/dist/main.js +2 -2
- package/dist/{seed.schema.config-DDzfQguf.js → seed.schema.config-BxJYabim.js} +3 -3
- package/dist/{seed.schema.config-DDzfQguf.js.map → seed.schema.config-BxJYabim.js.map} +1 -1
- package/dist/src/AppStateSchema.ts +10 -0
- package/dist/src/ArweaveClient.ts +13 -0
- package/dist/src/Attestation.ts +21 -0
- package/dist/src/BaseArweaveClient.ts +13 -0
- package/dist/src/BaseClientManager.ts +35 -0
- package/dist/src/BaseDb.ts +35 -0
- package/dist/src/BaseEasClient.ts +14 -0
- package/dist/src/BaseFileManager.ts +60 -0
- package/dist/src/BaseItem.ts +413 -0
- package/dist/src/BaseItemProperty.ts +416 -0
- package/dist/src/BaseQueryClient.ts +13 -0
- package/dist/src/ClientManager.ts +63 -0
- package/dist/src/ConfigSchema.ts +15 -0
- package/dist/src/Db.ts +82 -0
- package/dist/src/EasClient.ts +13 -0
- package/dist/src/FileDownloader.ts +63 -0
- package/dist/src/FileManager.ts +59 -0
- package/dist/src/IDb.ts +7 -0
- package/dist/src/IEasClient.ts +2 -0
- package/dist/src/IItem.ts +26 -0
- package/dist/src/IItemProperty.ts +28 -0
- package/dist/src/IQueryClient.ts +11 -0
- package/dist/src/ImageResizer.ts +81 -0
- package/dist/src/Item.ts +12 -0
- package/dist/src/ItemProperty.ts +9 -0
- package/dist/src/MetadataSchema.ts +28 -0
- package/dist/src/ModelSchema.ts +46 -0
- package/dist/src/ModelUidSchema.ts +16 -0
- package/dist/src/PropertyUidSchema.ts +16 -0
- package/dist/src/QueryClient.ts +26 -0
- package/dist/src/Schema.ts +17 -0
- package/dist/src/SeedProtocolSdk.d.ts +19 -0
- package/dist/src/SeedSchema.ts +29 -0
- package/dist/src/VersionSchema.ts +16 -0
- package/dist/src/actors.ts +295 -0
- package/dist/src/addModelsToDb.ts +152 -0
- package/dist/src/allItems.ts +31 -0
- package/dist/src/analyzeInput.ts +144 -0
- package/dist/src/browser.app.db.config.ts +27 -0
- package/dist/src/browser.ts +30 -0
- package/dist/src/checkStatus.ts +57 -0
- package/dist/src/clientManagerMachine.ts +59 -0
- package/dist/src/configureFs.ts +98 -0
- package/dist/src/connectToDb.ts +41 -0
- package/dist/src/connectionManager.ts +67 -0
- package/dist/src/constants.ts +125 -0
- package/dist/src/content-hash.ts +30 -0
- package/dist/src/create.ts +39 -0
- package/dist/src/createMetadata.ts +77 -0
- package/dist/src/createNewItem.ts +61 -0
- package/dist/src/createPublishAttempt.ts +16 -0
- package/dist/src/createSeed.ts +31 -0
- package/dist/src/createSeeds.ts +24 -0
- package/dist/src/createVersion.ts +33 -0
- package/dist/src/dbMachine.ts +182 -0
- package/dist/src/deleteItem.ts +19 -0
- package/dist/src/download.ts +208 -0
- package/dist/src/drizzle.ts +78 -0
- package/dist/src/eas.ts +40 -0
- package/dist/src/environment.ts +35 -0
- package/dist/src/eventBus.ts +5 -0
- package/dist/src/events.ts +14 -0
- package/dist/src/fetchDataFromEas.ts +94 -0
- package/dist/src/fetchDbData.ts +19 -0
- package/dist/src/fetchRelatedItems.ts +184 -0
- package/dist/src/fetchSeeds.ts +45 -0
- package/dist/src/fetchVersions.ts +49 -0
- package/dist/src/files.ts +81 -0
- package/dist/src/filesDownload.ts +326 -0
- package/dist/src/fragment-masking.ts +87 -0
- package/dist/src/getExistingItem.ts +60 -0
- package/dist/src/getItem.ts +32 -0
- package/dist/src/getItemData.ts +99 -0
- package/dist/src/getItemProperties.ts +56 -0
- package/dist/src/getItemProperty.ts +34 -0
- package/dist/src/getItems.ts +70 -0
- package/dist/src/getMetadata.ts +40 -0
- package/dist/src/getModelSchemas.ts +89 -0
- package/dist/src/getPropertyData.ts +50 -0
- package/dist/src/getPublishPayload.ts +355 -0
- package/dist/src/getPublishUploads.ts +207 -0
- package/dist/src/getRelationValueData.ts +27 -0
- package/dist/src/getSchemaForItemProperty.ts +68 -0
- package/dist/src/getSchemaForModel.ts +42 -0
- package/dist/src/getSchemaUidForModel.ts +32 -0
- package/dist/src/getSeedData.ts +34 -0
- package/dist/src/getSegmentedItemProperties.ts +65 -0
- package/dist/src/getStorageTransactionIdForSeedUid.ts +38 -0
- package/dist/src/getVersionData.ts +46 -0
- package/dist/src/getVersionsForVersionUids.ts +39 -0
- package/dist/src/globalMachine.ts +280 -0
- package/dist/src/gql.ts +118 -0
- package/dist/src/graphql.ts +3209 -0
- package/dist/src/helpers.ts +273 -0
- package/dist/src/hydrateExistingItem.ts +137 -0
- package/dist/src/hydrateFromDb.ts +270 -0
- package/dist/src/hydrateNewItem.ts +34 -0
- package/dist/src/imageResize.ts +507 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.ts +4 -0
- package/dist/src/initialize.ts +124 -0
- package/dist/src/internalMachine.ts +241 -0
- package/dist/src/itemMachineAll.ts +157 -0
- package/dist/src/itemMachineSingle.ts +158 -0
- package/dist/src/loadAppDb.ts +44 -0
- package/dist/src/machines.ts +66 -0
- package/dist/src/metadataLatest.ts +34 -0
- package/dist/src/migrate.ts +57 -0
- package/dist/src/model.ts +72 -0
- package/dist/src/modelClass.ts +19 -0
- package/dist/src/node.app.db.config.ts +41 -0
- package/dist/src/prepareDb.ts +29 -0
- package/dist/src/preparePublishRequestData.ts +81 -0
- package/dist/src/processItems.ts +74 -0
- package/dist/src/property.ts +156 -0
- package/dist/src/propertyMachine.ts +202 -0
- package/dist/src/publish.ts +28 -0
- package/dist/src/publishMachine.ts +77 -0
- package/dist/src/queries.ts +13 -0
- package/dist/src/recoverDeletedItem.ts +14 -0
- package/dist/src/reload.ts +33 -0
- package/dist/src/request.ts +45 -0
- package/dist/src/requestAll.ts +157 -0
- package/dist/src/resolveRelatedValue.ts +367 -0
- package/dist/src/resolveRemoteStorage.ts +88 -0
- package/dist/src/saveAppState.ts +50 -0
- package/dist/src/saveConfig.ts +106 -0
- package/dist/src/saveDataToDb.ts +145 -0
- package/dist/src/saveImageSrc.ts +242 -0
- package/dist/src/saveItemStorage.ts +156 -0
- package/dist/src/saveMetadata.ts +18 -0
- package/dist/src/savePublishService.ts +30 -0
- package/dist/src/saveRelation.ts +112 -0
- package/dist/src/seed.schema.config.ts +25 -0
- package/dist/src/seed.ts +10 -0
- package/dist/src/seedProtocol.ts +17 -0
- package/dist/src/services.ts +365 -0
- package/dist/src/setAddresses.ts +16 -0
- package/dist/src/sqlWasmClient.ts +88 -0
- package/dist/src/syncDbWithEas.ts +628 -0
- package/dist/src/trash.ts +29 -0
- package/dist/src/ts-to-proto.ts +101 -0
- package/dist/src/types.ts +12 -0
- package/dist/src/updateItemPropertyValue.ts +243 -0
- package/dist/src/updateMachineContext.ts +21 -0
- package/dist/src/updateMetadata.ts +59 -0
- package/dist/src/upload.ts +86 -0
- package/dist/src/validate.ts +45 -0
- package/dist/src/validateInput.ts +43 -0
- package/dist/src/validateItemData.ts +20 -0
- package/dist/src/versionData.ts +24 -0
- package/dist/src/waitForDb.ts +25 -0
- package/dist/src/waitForFiles.ts +41 -0
- package/dist/src/wasm.d.ts +8300 -0
- package/dist/types/scripts/bin.d.ts.map +1 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/browser/helpers/FileManager.d.ts.map +1 -1
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts +1 -0
- package/dist/types/src/helpers/FileManager/BaseFileManager.d.ts.map +1 -1
- package/dist/types/src/node/helpers/FileManager.d.ts +2 -1
- package/dist/types/src/node/helpers/FileManager.d.ts.map +1 -1
- package/package.json +3 -3
- package/dist/FileManager-DTzkncLh.js.map +0 -1
- package/dist/constants-CWg2B3Au.js.map +0 -1
- package/dist/index-BHGJ-Es9.js.map +0 -1
- package/dist/index-CQ9FS10O.js.map +0 -1
- package/dist/package.json +0 -189
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { DbServiceContext, FromCallbackInput, SqliteWasmResult } from '@/types'
|
|
3
|
+
import {
|
|
4
|
+
BROWSER_FS_TOP_DIR,
|
|
5
|
+
DB_MIGRATING_SUCCESS,
|
|
6
|
+
} from '@/services/internal/constants'
|
|
7
|
+
import debug from 'debug'
|
|
8
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
9
|
+
import fs from '@zenfs/core'
|
|
10
|
+
import { FileManager } from '@/browser/helpers/FileManager'
|
|
11
|
+
import { isBrowser } from '@/helpers/environment'
|
|
12
|
+
|
|
13
|
+
const logger = debug('app:services:db:actors:migrate')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export const migrate = fromCallback<
|
|
18
|
+
EventObject,
|
|
19
|
+
FromCallbackInput<DbServiceContext>
|
|
20
|
+
>(({ sendBack, input: { context } }) => {
|
|
21
|
+
const { pathToDbDir, dbId, dbName } = context
|
|
22
|
+
|
|
23
|
+
logger('[db/actors] migrate context', context)
|
|
24
|
+
|
|
25
|
+
let journalExists = false
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
const _checkForFiles = async (): Promise<void> => {
|
|
29
|
+
const journalPath = `/${pathToDbDir}/meta/_journal.json`
|
|
30
|
+
|
|
31
|
+
journalExists = await fs.promises.exists(journalPath)
|
|
32
|
+
|
|
33
|
+
if (!journalExists && isBrowser()) {
|
|
34
|
+
await FileManager.waitForFile(journalPath, 500, 60000)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const _migrate = async (): Promise<void> => {
|
|
39
|
+
await BaseDb.migrate(pathToDbDir, dbName, dbId)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_checkForFiles()
|
|
43
|
+
.then(() => {
|
|
44
|
+
if (!isBrowser()) {
|
|
45
|
+
return _migrate()
|
|
46
|
+
}
|
|
47
|
+
if (journalExists) {
|
|
48
|
+
return _migrate()
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
.then(() => {
|
|
52
|
+
console.log('sending db migrating success')
|
|
53
|
+
sendBack({ type: DB_MIGRATING_SUCCESS, dbName })
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
return () => { }
|
|
57
|
+
})
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { PropertyStates, PropertyValue } from './property'
|
|
2
|
+
import { Actor, AnyActorLogic } from 'xstate'
|
|
3
|
+
import { Static } from '@sinclair/typebox'
|
|
4
|
+
import { IModelClass, TModelSchema } from '@/schema'
|
|
5
|
+
import { BaseItem } from '@/Item/BaseItem'
|
|
6
|
+
|
|
7
|
+
export type ModelDefinitions = {
|
|
8
|
+
[modelName: string]: ModelClassType
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ModelStatus = (propName: string) => keyof PropertyStates
|
|
12
|
+
|
|
13
|
+
export type ExcludedKeys = 'states' | 'status'
|
|
14
|
+
|
|
15
|
+
type ExcludeKeys<T, K> = {
|
|
16
|
+
[P in keyof T as Exclude<P, K>]: T[P]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ModelClassType = {
|
|
20
|
+
originalConstructor: () => void
|
|
21
|
+
schema: ModelSchema
|
|
22
|
+
schemaUid?: string
|
|
23
|
+
create: (values: ModelValues<any>) => Promise<BaseItem<any>>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export type ModelValues<T extends Record<string, any>> = BaseItem<any> & {
|
|
27
|
+
schema: ModelSchema
|
|
28
|
+
ModelClass?: ModelClassType
|
|
29
|
+
[key: string & keyof T]: PropertyValue
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type StatesMap<T> = Map<string, Actor<T extends AnyActorLogic ? T : never>>
|
|
33
|
+
|
|
34
|
+
// export type ModelSchema = {
|
|
35
|
+
// [key: string]: TObject
|
|
36
|
+
// }
|
|
37
|
+
|
|
38
|
+
export type ModelSchema = Partial<Static<typeof TModelSchema>>
|
|
39
|
+
|
|
40
|
+
export type ModelConstructor = <
|
|
41
|
+
T extends { new(...args: any[]): IModelClass },
|
|
42
|
+
>(
|
|
43
|
+
constructor: T,
|
|
44
|
+
) => T & IModelClass
|
|
45
|
+
|
|
46
|
+
// export type ModelConstructor = Static<TModelConstructor>
|
|
47
|
+
|
|
48
|
+
export interface ModelProperty {
|
|
49
|
+
propertyLocalId?: string
|
|
50
|
+
name: string
|
|
51
|
+
dataType:
|
|
52
|
+
| 'string'
|
|
53
|
+
| 'bytes32'
|
|
54
|
+
| 'uint8'
|
|
55
|
+
| 'uint256'
|
|
56
|
+
| 'bool'
|
|
57
|
+
| 'address'
|
|
58
|
+
| 'bytes'
|
|
59
|
+
| 'int8'
|
|
60
|
+
| 'int256'
|
|
61
|
+
| 'int'
|
|
62
|
+
| 'bytes32[]'
|
|
63
|
+
modelSchemaUids: string[]
|
|
64
|
+
modelLocalId?: string
|
|
65
|
+
schemaName?: string
|
|
66
|
+
schemaUid: string
|
|
67
|
+
schemaDefinition: string
|
|
68
|
+
relatedModelSchemaUid?: string
|
|
69
|
+
relatedModelLocalId?: string
|
|
70
|
+
createdAt?: number
|
|
71
|
+
updatedAt?: number
|
|
72
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ModelClassType } from '@/types'
|
|
2
|
+
|
|
3
|
+
const modelStore = new Map<string, ModelClassType>()
|
|
4
|
+
|
|
5
|
+
export const getModels = (): Record<string, ModelClassType> => {
|
|
6
|
+
return Object.fromEntries(modelStore)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const getModel = (modelName: string): ModelClassType | undefined => {
|
|
10
|
+
return modelStore.get(modelName)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const getModelNames = (): string[] => {
|
|
14
|
+
return Array.from(modelStore.keys())
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const setModel = (modelName: string, model: ModelClassType) => {
|
|
18
|
+
modelStore.set(modelName, model)
|
|
19
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { defineConfig } from 'drizzle-kit'
|
|
2
|
+
import dotenv from 'dotenv'
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
import { DrizzleConfig } from 'drizzle-orm'
|
|
6
|
+
|
|
7
|
+
dotenv.config()
|
|
8
|
+
|
|
9
|
+
let sdkRoot = './node_modules/@seedprotocol/sdk'
|
|
10
|
+
|
|
11
|
+
if (process.env.IS_SEED_DEV) {
|
|
12
|
+
sdkRoot = './src'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let dotSeedDir = path.join(process.cwd(), '.seed')
|
|
16
|
+
|
|
17
|
+
if (process.env.IS_SEED_DEV) {
|
|
18
|
+
dotSeedDir = path.join(
|
|
19
|
+
process.cwd(),
|
|
20
|
+
'__tests__',
|
|
21
|
+
'__mocks__',
|
|
22
|
+
'node',
|
|
23
|
+
'project',
|
|
24
|
+
'.seed',
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let schemaDir = `${sdkRoot}/dist/seedSchema/*.ts`
|
|
29
|
+
|
|
30
|
+
if (process.env.IS_SEED_DEV) {
|
|
31
|
+
schemaDir = `${sdkRoot}/seedSchema/*.ts`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default defineConfig({
|
|
35
|
+
schema: schemaDir,
|
|
36
|
+
dialect: 'sqlite',
|
|
37
|
+
out: `${dotSeedDir}/db`,
|
|
38
|
+
dbCredentials: {
|
|
39
|
+
url: `${dotSeedDir}/db/app_db.sqlite3`,
|
|
40
|
+
},
|
|
41
|
+
}) as DrizzleConfig
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
2
|
+
import { InternalMachineContext } from '@/types'
|
|
3
|
+
import { FromCallbackInput } from '@/types'
|
|
4
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export const prepareDb = fromCallback<
|
|
8
|
+
EventObject,
|
|
9
|
+
FromCallbackInput<InternalMachineContext>
|
|
10
|
+
>(({ sendBack, input: { context } }) => {
|
|
11
|
+
const { filesDir } = context
|
|
12
|
+
|
|
13
|
+
if (!filesDir) {
|
|
14
|
+
throw new Error('filesDir is required')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const _prepareDb = async (): Promise<void> => {
|
|
18
|
+
const appDb = await BaseDb.prepareDb(filesDir)
|
|
19
|
+
if (appDb) {
|
|
20
|
+
sendBack({ type: 'prepareDbSuccess'})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
_prepareDb().then(() => {
|
|
26
|
+
return
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
})
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { FromCallbackInput, PublishMachineContext } from '@/types'
|
|
3
|
+
import { models as modelsTable, modelUids } from '@/seedSchema'
|
|
4
|
+
import { BaseDb } from '@/db/Db/BaseDb'
|
|
5
|
+
import { eq } from 'drizzle-orm'
|
|
6
|
+
import { getModelSchemas } from '@/db/read/getModelSchemas'
|
|
7
|
+
import { BaseItem } from '@/Item/BaseItem'
|
|
8
|
+
|
|
9
|
+
export const preparePublishRequestData = fromCallback<
|
|
10
|
+
EventObject,
|
|
11
|
+
FromCallbackInput<PublishMachineContext>
|
|
12
|
+
>(({ sendBack, input: { context } }) => {
|
|
13
|
+
const { localId } = context
|
|
14
|
+
|
|
15
|
+
const _preparePublishRequestData = async () => {
|
|
16
|
+
const item = await BaseItem.find({ seedLocalId: localId })
|
|
17
|
+
|
|
18
|
+
if (!item) {
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const appDb = BaseDb.getAppDb()
|
|
23
|
+
|
|
24
|
+
const modelsRows = await appDb
|
|
25
|
+
.select({
|
|
26
|
+
modelName: modelsTable.name,
|
|
27
|
+
schemaUid: modelUids.uid,
|
|
28
|
+
})
|
|
29
|
+
.from(modelsTable)
|
|
30
|
+
.leftJoin(modelUids, eq(modelUids.modelId, modelsTable.id))
|
|
31
|
+
.where(eq(modelsTable.name, 'Version'))
|
|
32
|
+
|
|
33
|
+
if (!modelsRows || modelsRows.length === 0) {
|
|
34
|
+
sendBack({ type: 'preparePublishRequestDataError' })
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const versionSchemaUid = modelsRows[0].schemaUid
|
|
39
|
+
|
|
40
|
+
const editedProperties = await item.getEditedProperties()
|
|
41
|
+
|
|
42
|
+
const { modelSchemas, schemaStringToModelRecord } = await getModelSchemas({
|
|
43
|
+
sdkConfigDb: appDb,
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
// const dataEncoder = new SchemaEncoder(modelProperty.schemaDefinition,)
|
|
47
|
+
// const encodedData = dataEncoder.encodeData(data,)
|
|
48
|
+
//
|
|
49
|
+
// itemData.listOfAttestations.push({
|
|
50
|
+
// schema : modelProperty.schemaUid,
|
|
51
|
+
// data : [
|
|
52
|
+
// {
|
|
53
|
+
// ...defaultAttestationData,
|
|
54
|
+
// data : encodedData,
|
|
55
|
+
// },
|
|
56
|
+
// ],
|
|
57
|
+
// },)
|
|
58
|
+
|
|
59
|
+
const publishRequestData = {
|
|
60
|
+
seedIsRevocable: false,
|
|
61
|
+
seedSchemaUid: item.schemaUid,
|
|
62
|
+
seedUid: item.seedUid,
|
|
63
|
+
versionSchemaUid,
|
|
64
|
+
versionUid: item.latestVersionUid,
|
|
65
|
+
listOfAttestations: [],
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
sendBack({
|
|
69
|
+
type: 'updateContext',
|
|
70
|
+
...publishRequestData,
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
return true
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
_preparePublishRequestData().then((success) => {
|
|
77
|
+
if (success) {
|
|
78
|
+
sendBack({ type: 'preparePublishRequestDataSuccess' })
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
})
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { AllItemsMachineContext, FromCallbackInput } from '@/types'
|
|
3
|
+
import { Attestation } from '@/graphql/gql/graphql'
|
|
4
|
+
import { eventEmitter } from '@/eventBus'
|
|
5
|
+
|
|
6
|
+
export const processItems = fromCallback<
|
|
7
|
+
EventObject,
|
|
8
|
+
FromCallbackInput<AllItemsMachineContext>
|
|
9
|
+
>(
|
|
10
|
+
({ sendBack, input: { context } }) => {
|
|
11
|
+
const {
|
|
12
|
+
itemVersions,
|
|
13
|
+
itemSeeds,
|
|
14
|
+
ModelClass,
|
|
15
|
+
schemaUidsByModelName,
|
|
16
|
+
mostRecentPropertiesBySeedUid,
|
|
17
|
+
times,
|
|
18
|
+
// relatedProperties,
|
|
19
|
+
// relatedVersionsBySchemaUid,
|
|
20
|
+
// relatedVersionsBySeedUid,
|
|
21
|
+
} = context
|
|
22
|
+
|
|
23
|
+
if (!itemVersions || !itemSeeds) {
|
|
24
|
+
throw new Error('No itemVersions or itemSeeds found')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const _processItems = async () => {
|
|
28
|
+
// For each itemSeed, find all the Versions
|
|
29
|
+
for (const itemSeed of itemSeeds.slice(8, 16)) {
|
|
30
|
+
const versionsForSeed = itemVersions.filter(
|
|
31
|
+
(version) => version.refUID === itemSeed.id,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
if (versionsForSeed.length === 0) {
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Find the most recent Version for each Seed
|
|
39
|
+
const recentVersionsMap: { [seedId: string]: Attestation } = {}
|
|
40
|
+
versionsForSeed.forEach((version: Attestation) => {
|
|
41
|
+
const existingVersion = recentVersionsMap[version.refUID]
|
|
42
|
+
if (
|
|
43
|
+
!existingVersion ||
|
|
44
|
+
new Date(version.timeCreated * 1000) >
|
|
45
|
+
new Date(existingVersion.timeCreated * 1000)
|
|
46
|
+
) {
|
|
47
|
+
recentVersionsMap[version.refUID] = version
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_processItems().then(() => {
|
|
54
|
+
sendBack({ type: 'processItemsSuccess' })
|
|
55
|
+
const modelName = ModelClass.originalConstructor.name
|
|
56
|
+
eventEmitter.emit('item.requestAll', {
|
|
57
|
+
modelName,
|
|
58
|
+
})
|
|
59
|
+
eventEmitter.emit('service.save', {
|
|
60
|
+
modelName,
|
|
61
|
+
})
|
|
62
|
+
sendBack({
|
|
63
|
+
type: 'updateTimes',
|
|
64
|
+
times: {
|
|
65
|
+
...times,
|
|
66
|
+
processItems: {
|
|
67
|
+
start: null,
|
|
68
|
+
end: Date.now(),
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
},
|
|
74
|
+
)
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { useImmer } from 'use-immer'
|
|
2
|
+
import { Item } from '@/browser/Item'
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
4
|
+
import { useSelector } from '@xstate/react'
|
|
5
|
+
import debug from 'debug'
|
|
6
|
+
import { eventEmitter } from '@/eventBus'
|
|
7
|
+
import { ItemProperty } from '@/browser/ItemProperty/ItemProperty'
|
|
8
|
+
import { useGlobalServiceStatus } from '@/browser/react/services'
|
|
9
|
+
import { IItemProperty } from '@/interfaces'
|
|
10
|
+
|
|
11
|
+
const logger = debug('app:react:property')
|
|
12
|
+
|
|
13
|
+
type UseItemPropertyProps = {
|
|
14
|
+
propertyName: string
|
|
15
|
+
seedLocalId?: string
|
|
16
|
+
seedUid?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type UseItemPropertyReturn = {
|
|
20
|
+
property: IItemProperty<any> | undefined
|
|
21
|
+
isInitialized: boolean
|
|
22
|
+
value: any
|
|
23
|
+
status: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type UseItemProperty = (props: UseItemPropertyProps | undefined) => UseItemPropertyReturn
|
|
27
|
+
|
|
28
|
+
export const useItemProperty: UseItemProperty = (props = {
|
|
29
|
+
propertyName: '',
|
|
30
|
+
seedLocalId: '',
|
|
31
|
+
seedUid: '',
|
|
32
|
+
}) => {
|
|
33
|
+
|
|
34
|
+
const { propertyName, seedLocalId, seedUid } = props
|
|
35
|
+
|
|
36
|
+
const [property, setProperty] = useState<IItemProperty<any> | undefined>()
|
|
37
|
+
const [isInitialized, setIsInitialized] = useState(false)
|
|
38
|
+
|
|
39
|
+
const { internalStatus } = useGlobalServiceStatus()
|
|
40
|
+
|
|
41
|
+
const isReadingDb = useRef(false)
|
|
42
|
+
|
|
43
|
+
const value = useSelector(property?.getService(), (snapshot) => {
|
|
44
|
+
if (!snapshot || !snapshot.context) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
return snapshot.context.renderValue || snapshot.context.propertyValue
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const status = useSelector(
|
|
51
|
+
property?.getService(),
|
|
52
|
+
(snapshot) => snapshot?.value as string,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
const readFromDb = useCallback(async () => {
|
|
56
|
+
if (
|
|
57
|
+
internalStatus !== 'ready' ||
|
|
58
|
+
isReadingDb.current ||
|
|
59
|
+
(!seedLocalId && !seedUid)
|
|
60
|
+
) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
isReadingDb.current = true
|
|
64
|
+
const foundProperty = await ItemProperty.find({
|
|
65
|
+
propertyName,
|
|
66
|
+
seedLocalId,
|
|
67
|
+
seedUid,
|
|
68
|
+
})
|
|
69
|
+
if (!foundProperty) {
|
|
70
|
+
logger(
|
|
71
|
+
`[useItemPropertyTest] [readFromDb] no property found for Item.${seedLocalId}.${propertyName}`,
|
|
72
|
+
)
|
|
73
|
+
isReadingDb.current = false
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
if (foundProperty.status === 'waitingForDb') {
|
|
77
|
+
foundProperty.getService().send({ type: 'waitForDbSuccess' })
|
|
78
|
+
}
|
|
79
|
+
setProperty(foundProperty)
|
|
80
|
+
setIsInitialized(true)
|
|
81
|
+
isReadingDb.current = false
|
|
82
|
+
}, [internalStatus, props])
|
|
83
|
+
|
|
84
|
+
const listenerRef = useRef(readFromDb)
|
|
85
|
+
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
listenerRef.current = readFromDb
|
|
88
|
+
}, [readFromDb, props])
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (internalStatus === 'ready') {
|
|
92
|
+
readFromDb()
|
|
93
|
+
}
|
|
94
|
+
}, [internalStatus])
|
|
95
|
+
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
eventEmitter.addListener(
|
|
98
|
+
`property.${seedUid || seedLocalId}.${propertyName}.update`,
|
|
99
|
+
() => {
|
|
100
|
+
listenerRef.current()
|
|
101
|
+
},
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
eventEmitter.removeListener(
|
|
106
|
+
`property.${seedUid || seedLocalId}.${propertyName}.update`,
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
}, [])
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
property,
|
|
113
|
+
isInitialized,
|
|
114
|
+
value,
|
|
115
|
+
status,
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
export const useItemProperties = (item?: Item<any>) => {
|
|
119
|
+
const [propertyObj, setPropertyObj] = useImmer({})
|
|
120
|
+
const [isListening, setIsListening] = useState(false)
|
|
121
|
+
|
|
122
|
+
const updatePropertyObj = useCallback(
|
|
123
|
+
(event) => {
|
|
124
|
+
if (!item) {
|
|
125
|
+
console.error('[XXXXXX] [updatePropertyObj] no item when expected')
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
const { propertyName, propertyValue } = event
|
|
129
|
+
if (!propertyName) {
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
setPropertyObj((draft) => {
|
|
133
|
+
draft[propertyName] = propertyValue
|
|
134
|
+
})
|
|
135
|
+
},
|
|
136
|
+
[item],
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
if (!item) {
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const eventKey = `item.${item.seedLocalId}.property.update`
|
|
145
|
+
|
|
146
|
+
eventEmitter.addListener(eventKey, updatePropertyObj)
|
|
147
|
+
|
|
148
|
+
return () => {
|
|
149
|
+
eventEmitter.removeListener(eventKey, updatePropertyObj)
|
|
150
|
+
}
|
|
151
|
+
}, [item])
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
properties: propertyObj,
|
|
155
|
+
}
|
|
156
|
+
}
|