@seedprotocol/sdk 0.1.47 → 0.1.49
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/bin.js.map +1 -1
- package/dist/constants-BLctWkrn.js.map +1 -1
- package/dist/{index-DWf9Ls94.js → index-DMIKRod-.js} +3995 -3995
- package/dist/index-DMIKRod-.js.map +1 -0
- package/dist/{index-B2WbNudj.js → index-wKss7188.js} +10 -10
- package/dist/index-wKss7188.js.map +1 -0
- package/dist/main.js +7 -7
- package/dist/{seed.schema.config-mBqth17w.js → seed.schema.config-C0M8Rcti.js} +7 -7
- package/dist/seed.schema.config-C0M8Rcti.js.map +1 -0
- package/dist/src/AppStateSchema.ts +10 -0
- package/dist/src/Attestation.ts +21 -0
- package/dist/src/ConfigSchema.ts +15 -0
- package/dist/src/ItemProperty.ts +383 -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/Schema.ts +17 -0
- package/dist/src/SeedSchema.ts +29 -0
- package/dist/src/VersionSchema.ts +16 -0
- package/dist/src/actors.ts +280 -0
- package/dist/src/addModelsToDb.ts +150 -0
- package/dist/src/allItems.ts +23 -0
- package/dist/src/arweave.ts +37 -0
- package/dist/src/browser.app.db.config.ts +27 -0
- package/dist/src/browser.seed.db.config.ts +33 -0
- package/dist/src/browser.ts +30 -0
- package/dist/src/checkStatus.ts +50 -0
- package/dist/src/client.ts +85 -0
- package/dist/src/configureFs.ts +81 -0
- package/dist/src/connectToDb.ts +74 -0
- package/dist/src/connectionManager.ts +67 -0
- package/dist/src/constants.ts +118 -0
- package/dist/src/create.ts +39 -0
- package/dist/src/createItem.ts +15 -0
- package/dist/src/createItemMachine.ts +37 -0
- package/dist/src/createPublishAttempt.ts +16 -0
- package/dist/src/createSeeds.ts +24 -0
- package/dist/src/createVersion.ts +33 -0
- package/dist/src/db.ts +247 -0
- package/dist/src/dbMachine.ts +181 -0
- package/dist/src/deleteItem.ts +19 -0
- package/dist/src/download.ts +289 -0
- package/dist/src/drizzle.ts +82 -0
- package/dist/src/environment.ts +15 -0
- package/dist/src/eventBus.ts +5 -0
- package/dist/src/events.ts +14 -0
- package/dist/src/fetchDataFromEas.ts +90 -0
- package/dist/src/fetchDbData.ts +16 -0
- package/dist/src/fetchRelatedItems.ts +179 -0
- package/dist/src/fetchSeeds.ts +44 -0
- package/dist/src/fetchVersions.ts +41 -0
- package/dist/src/files.ts +16 -0
- package/dist/src/fragment-masking.ts +87 -0
- package/dist/src/fsProxy.ts +36 -0
- package/dist/src/getItem.ts +189 -0
- package/dist/src/getItemProperties.ts +162 -0
- package/dist/src/getItems.ts +75 -0
- package/dist/src/getMetadata.ts +40 -0
- package/dist/src/getModelSchemas.ts +88 -0
- package/dist/src/getSchemaForModel.ts +42 -0
- package/dist/src/getSeedData.ts +34 -0
- package/dist/src/getVersionData.ts +58 -0
- package/dist/src/getVersionsForVersionUids.ts +39 -0
- package/dist/src/globalMachine.ts +259 -0
- package/dist/src/gql.ts +113 -0
- package/dist/src/graphql.ts +3201 -0
- package/dist/src/helpers.ts +150 -0
- package/dist/src/hydrateExistingItem.ts +137 -0
- package/dist/src/hydrateFromDb.ts +254 -0
- package/dist/src/hydrateNewItem.ts +34 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.ts +73 -0
- package/dist/src/init.ts +61 -0
- package/dist/src/initialize.ts +127 -0
- package/dist/src/internalMachine.ts +220 -0
- package/dist/src/item.ts +324 -0
- package/dist/src/itemMachineAll.ts +158 -0
- package/dist/src/itemMachineSingle.ts +175 -0
- package/dist/src/loadAppDb.ts +47 -0
- package/dist/src/logger.ts +33 -0
- package/dist/src/machine.ts +55 -0
- package/dist/src/machines.ts +58 -0
- package/dist/src/migrate.ts +288 -0
- package/dist/src/model.ts +71 -0
- package/dist/src/modelClass.ts +19 -0
- package/dist/src/node.app.db.config.ts +40 -0
- package/dist/src/prepareDb.ts +34 -0
- package/dist/src/preparePublishRequestData.ts +81 -0
- package/dist/src/processItems.ts +71 -0
- package/dist/src/property.ts +161 -0
- package/dist/src/propertyMachine.ts +154 -0
- package/dist/src/publish.ts +31 -0
- package/dist/src/publishMachine.ts +77 -0
- package/dist/src/queries.ts +13 -0
- package/dist/src/read.ts +174 -0
- package/dist/src/recoverDeletedItem.ts +14 -0
- package/dist/src/request.ts +54 -0
- package/dist/src/requestAll.ts +157 -0
- package/dist/src/resolveRelatedValue.ts +348 -0
- package/dist/src/resolveRemoteStorage.ts +87 -0
- package/dist/src/save.ts +183 -0
- package/dist/src/saveConfig.ts +79 -0
- package/dist/src/saveDataToDb.ts +145 -0
- package/dist/src/saveMetadata.ts +18 -0
- package/dist/src/saveValueToDb.ts +94 -0
- package/dist/src/seed.schema.config.ts +25 -0
- package/dist/src/seed.ts +37 -0
- package/dist/src/seedData.ts +0 -0
- package/dist/src/seedProtocol.ts +17 -0
- package/dist/src/services.ts +359 -0
- package/dist/src/sqlWasmClient.ts +88 -0
- package/dist/src/syncDbWithEas.ts +686 -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/upload.ts +86 -0
- package/dist/src/validate.ts +42 -0
- package/dist/src/validateInput.ts +33 -0
- package/dist/src/validateItemData.ts +20 -0
- package/dist/src/waitForDb.ts +23 -0
- package/dist/src/wasm.d.ts +8300 -0
- package/dist/src/write.ts +366 -0
- package/dist/types/src/browser/db/read/getModelSchemas.d.ts.map +1 -1
- package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts +3 -1
- package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts.map +1 -1
- package/dist/types/src/browser/item/single/actors/saveDataToDb.d.ts +3 -3
- package/dist/types/src/browser/item/single/itemMachineSingle.d.ts +3 -3
- package/dist/types/src/browser/property/ItemProperty.d.ts +7 -7
- package/dist/types/src/browser/property/ItemProperty.d.ts.map +1 -1
- package/dist/types/src/browser/property/actors/resolveRemoteStorage.d.ts +3 -3
- package/dist/types/src/browser/react/trash.d.ts +1 -1
- package/dist/types/src/browser/react/trash.d.ts.map +1 -1
- package/dist/types/src/browser/services/db/dbMachine.d.ts +6 -6
- package/dist/types/src/browser/services/publish/publishMachine.d.ts +17 -17
- package/dist/types/src/types/machines.d.ts +4 -0
- package/dist/types/src/types/machines.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/index-B2WbNudj.js.map +0 -1
- package/dist/index-DWf9Ls94.js.map +0 -1
- package/dist/seed.schema.config-mBqth17w.js.map +0 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { itemMachineAll } from '@/browser/item/all/itemMachineAll'
|
|
3
|
+
import { Attestation } from '@/browser/gql/graphql'
|
|
4
|
+
import { eventEmitter } from '@/eventBus'
|
|
5
|
+
|
|
6
|
+
export const processItems = fromCallback<EventObject, typeof itemMachineAll>(
|
|
7
|
+
({ sendBack, input: { context } }) => {
|
|
8
|
+
const {
|
|
9
|
+
itemVersions,
|
|
10
|
+
itemSeeds,
|
|
11
|
+
ModelClass,
|
|
12
|
+
schemaUidsByModelName,
|
|
13
|
+
mostRecentPropertiesBySeedUid,
|
|
14
|
+
times,
|
|
15
|
+
// relatedProperties,
|
|
16
|
+
// relatedVersionsBySchemaUid,
|
|
17
|
+
// relatedVersionsBySeedUid,
|
|
18
|
+
} = context
|
|
19
|
+
|
|
20
|
+
if (!itemVersions || !itemSeeds) {
|
|
21
|
+
throw new Error('No itemVersions or itemSeeds found')
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const _processItems = async () => {
|
|
25
|
+
// For each itemSeed, find all the Versions
|
|
26
|
+
for (const itemSeed of itemSeeds.slice(8, 16)) {
|
|
27
|
+
const versionsForSeed = itemVersions.filter(
|
|
28
|
+
(version) => version.refUID === itemSeed.id,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
if (versionsForSeed.length === 0) {
|
|
32
|
+
continue
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Find the most recent Version for each Seed
|
|
36
|
+
const recentVersionsMap: { [seedId: string]: Attestation } = {}
|
|
37
|
+
versionsForSeed.forEach((version: Attestation) => {
|
|
38
|
+
const existingVersion = recentVersionsMap[version.refUID]
|
|
39
|
+
if (
|
|
40
|
+
!existingVersion ||
|
|
41
|
+
new Date(version.timeCreated * 1000) >
|
|
42
|
+
new Date(existingVersion.timeCreated * 1000)
|
|
43
|
+
) {
|
|
44
|
+
recentVersionsMap[version.refUID] = version
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_processItems().then(() => {
|
|
51
|
+
sendBack({ type: 'processItemsSuccess' })
|
|
52
|
+
const modelName = ModelClass.originalConstructor.name
|
|
53
|
+
eventEmitter.emit('item.requestAll', {
|
|
54
|
+
modelName,
|
|
55
|
+
})
|
|
56
|
+
eventEmitter.emit('service.save', {
|
|
57
|
+
modelName,
|
|
58
|
+
})
|
|
59
|
+
sendBack({
|
|
60
|
+
type: 'updateTimes',
|
|
61
|
+
times: {
|
|
62
|
+
...times,
|
|
63
|
+
processItems: {
|
|
64
|
+
start: null,
|
|
65
|
+
end: Date.now(),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
},
|
|
71
|
+
)
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { useImmer } from 'use-immer'
|
|
2
|
+
import { Item } from '@/browser/item'
|
|
3
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
4
|
+
import { useSelector } from '@xstate/react'
|
|
5
|
+
import debug from 'debug'
|
|
6
|
+
import { eventEmitter } from '@/eventBus'
|
|
7
|
+
import { ItemProperty } from '@/browser/property'
|
|
8
|
+
import { useGlobalServiceStatus } from '@/browser/react/services'
|
|
9
|
+
|
|
10
|
+
const logger = debug('app:react:property')
|
|
11
|
+
|
|
12
|
+
type UseItemPropertyProps = {
|
|
13
|
+
propertyName: string
|
|
14
|
+
seedLocalId?: string
|
|
15
|
+
seedUid?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type UseItemPropertyReturn = {
|
|
19
|
+
property: ItemProperty<any> | undefined
|
|
20
|
+
isInitialized: boolean
|
|
21
|
+
isReadingFromDb: boolean
|
|
22
|
+
value: any
|
|
23
|
+
status: string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type UseItemProperty = (props: UseItemPropertyProps) => UseItemPropertyReturn
|
|
27
|
+
|
|
28
|
+
export const useItemProperty: UseItemProperty = ({
|
|
29
|
+
propertyName,
|
|
30
|
+
seedLocalId,
|
|
31
|
+
seedUid,
|
|
32
|
+
}) => {
|
|
33
|
+
const [property, setProperty] = useState<ItemProperty<any> | undefined>()
|
|
34
|
+
const [isReadingFromDb, setIsReadingFromDb] = useState(false)
|
|
35
|
+
const [isInitialized, setIsInitialized] = useState(false)
|
|
36
|
+
|
|
37
|
+
// const isDbReady = useMemo(() => useIsDbReady(), [])
|
|
38
|
+
|
|
39
|
+
const { internalStatus } = useGlobalServiceStatus()
|
|
40
|
+
|
|
41
|
+
const value = useSelector(property?.getService(), (snapshot) => {
|
|
42
|
+
if (!snapshot || !snapshot.context) {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
return snapshot.context.renderValue || snapshot.context.propertyValue
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const status = useSelector(
|
|
49
|
+
property?.getService(),
|
|
50
|
+
(snapshot) => snapshot?.value as string,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
// useEffect(() => {
|
|
54
|
+
// if (property && property.value !== value) {
|
|
55
|
+
// readFromDb()
|
|
56
|
+
// }
|
|
57
|
+
// }, [property, value])
|
|
58
|
+
|
|
59
|
+
const readFromDb = useCallback(async () => {
|
|
60
|
+
if (
|
|
61
|
+
internalStatus !== 'ready' ||
|
|
62
|
+
isReadingFromDb ||
|
|
63
|
+
(!seedLocalId && !seedUid)
|
|
64
|
+
) {
|
|
65
|
+
return
|
|
66
|
+
}
|
|
67
|
+
setIsReadingFromDb(true)
|
|
68
|
+
const foundProperty = await ItemProperty.find({
|
|
69
|
+
propertyName,
|
|
70
|
+
seedLocalId,
|
|
71
|
+
seedUid,
|
|
72
|
+
})
|
|
73
|
+
setIsReadingFromDb(false)
|
|
74
|
+
if (!foundProperty) {
|
|
75
|
+
logger(
|
|
76
|
+
`[useItemPropertyTest] [readFromDb] no property found for Item.${seedLocalId}.${propertyName}`,
|
|
77
|
+
)
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
if (foundProperty.status === 'waitingForDb') {
|
|
81
|
+
foundProperty.getService().send({ type: 'waitForDbSuccess' })
|
|
82
|
+
}
|
|
83
|
+
setProperty(foundProperty)
|
|
84
|
+
setIsInitialized(true)
|
|
85
|
+
}, [internalStatus, isReadingFromDb])
|
|
86
|
+
|
|
87
|
+
// let count = 0
|
|
88
|
+
//
|
|
89
|
+
// const refresh = useCallback(() => {
|
|
90
|
+
// count++
|
|
91
|
+
// console.log('[useItemPropertyTest] [refresh] property', property)
|
|
92
|
+
// }, [property])
|
|
93
|
+
|
|
94
|
+
useEffect(() => {
|
|
95
|
+
if (internalStatus === 'ready') {
|
|
96
|
+
readFromDb()
|
|
97
|
+
}
|
|
98
|
+
}, [internalStatus])
|
|
99
|
+
|
|
100
|
+
// useEffect(() => {
|
|
101
|
+
// eventEmitter.addListener(
|
|
102
|
+
// `property.${seedUid || seedLocalId}.${propertyName}.update`,
|
|
103
|
+
// (event) => {
|
|
104
|
+
// refresh()
|
|
105
|
+
// },
|
|
106
|
+
// )
|
|
107
|
+
//
|
|
108
|
+
// return () => {
|
|
109
|
+
// eventEmitter.removeListener(
|
|
110
|
+
// `property.${seedUid || seedLocalId}.${propertyName}.update`,
|
|
111
|
+
// )
|
|
112
|
+
// }
|
|
113
|
+
// }, [])
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
property,
|
|
117
|
+
isInitialized,
|
|
118
|
+
isReadingFromDb,
|
|
119
|
+
value,
|
|
120
|
+
status,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
export const useItemProperties = (item?: Item<any>) => {
|
|
124
|
+
const [propertyObj, setPropertyObj] = useImmer({})
|
|
125
|
+
const [isListening, setIsListening] = useState(false)
|
|
126
|
+
|
|
127
|
+
const updatePropertyObj = useCallback(
|
|
128
|
+
(event) => {
|
|
129
|
+
if (!item) {
|
|
130
|
+
console.error('[XXXXXX] [updatePropertyObj] no item when expected')
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
const { propertyName, propertyValue } = event
|
|
134
|
+
if (!propertyName) {
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
setPropertyObj((draft) => {
|
|
138
|
+
draft[propertyName] = propertyValue
|
|
139
|
+
})
|
|
140
|
+
},
|
|
141
|
+
[item],
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
if (!item) {
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const eventKey = `item.${item.seedLocalId}.property.update`
|
|
150
|
+
|
|
151
|
+
eventEmitter.addListener(eventKey, updatePropertyObj)
|
|
152
|
+
|
|
153
|
+
return () => {
|
|
154
|
+
eventEmitter.removeListener(eventKey, updatePropertyObj)
|
|
155
|
+
}
|
|
156
|
+
}, [item])
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
properties: propertyObj,
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { assign, setup } from 'xstate'
|
|
2
|
+
import { PropertyMachineContext } from '@/types'
|
|
3
|
+
|
|
4
|
+
import { resolveRemoteStorage } from '@/browser/property/actors/resolveRemoteStorage'
|
|
5
|
+
import { waitForDb } from '@/browser/property/actors/waitForDb'
|
|
6
|
+
import { initialize } from '@/browser/property/actors/initialize'
|
|
7
|
+
import { resolveRelatedValue } from '@/browser/property/actors/resolveRelatedValue'
|
|
8
|
+
import { hydrateFromDb } from '@/browser/property/actors/hydrateFromDb'
|
|
9
|
+
import { saveValueToDb } from '@/browser/property/actors/saveValueToDb'
|
|
10
|
+
import { updateMachineContext } from '@/browser/helpers'
|
|
11
|
+
|
|
12
|
+
export const propertyMachine = setup({
|
|
13
|
+
types: {
|
|
14
|
+
context: {} as PropertyMachineContext,
|
|
15
|
+
},
|
|
16
|
+
actors: {
|
|
17
|
+
waitForDb,
|
|
18
|
+
hydrateFromDb,
|
|
19
|
+
initialize,
|
|
20
|
+
resolveRelatedValue,
|
|
21
|
+
resolveRemoteStorage,
|
|
22
|
+
saveValueToDb,
|
|
23
|
+
},
|
|
24
|
+
}).createMachine({
|
|
25
|
+
id: 'itemProperty',
|
|
26
|
+
initial: 'waitingForDb',
|
|
27
|
+
context: ({ input }) => input as PropertyMachineContext,
|
|
28
|
+
on: {
|
|
29
|
+
updatePropertyValue: {
|
|
30
|
+
target: '.resolvingRelatedValue',
|
|
31
|
+
guard: ({ context }) => !context.isDbReady,
|
|
32
|
+
actions: assign(({ event }) => {
|
|
33
|
+
return {
|
|
34
|
+
propertyValue: event.propertyValue,
|
|
35
|
+
}
|
|
36
|
+
}),
|
|
37
|
+
},
|
|
38
|
+
updateRenderValue: {
|
|
39
|
+
actions: assign(({ event, context }) => {
|
|
40
|
+
return {
|
|
41
|
+
renderValue: event.renderValue,
|
|
42
|
+
}
|
|
43
|
+
}),
|
|
44
|
+
},
|
|
45
|
+
save: {
|
|
46
|
+
actions: assign({
|
|
47
|
+
isSaving: true,
|
|
48
|
+
}),
|
|
49
|
+
target: '.saving',
|
|
50
|
+
},
|
|
51
|
+
updateContext: updateMachineContext,
|
|
52
|
+
},
|
|
53
|
+
states: {
|
|
54
|
+
idle: {},
|
|
55
|
+
waitingForDb: {
|
|
56
|
+
on: {
|
|
57
|
+
waitForDbSuccess: {
|
|
58
|
+
target: 'hydratingFromDb',
|
|
59
|
+
actions: assign({
|
|
60
|
+
isDbReady: true,
|
|
61
|
+
}),
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
invoke: {
|
|
65
|
+
src: 'waitForDb',
|
|
66
|
+
input: ({ context }) => ({ context }),
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
hydratingFromDb: {
|
|
70
|
+
on: {
|
|
71
|
+
hydrateFromDbSuccess: 'initializing',
|
|
72
|
+
},
|
|
73
|
+
invoke: {
|
|
74
|
+
src: 'hydrateFromDb',
|
|
75
|
+
input: ({ context }) => ({ context }),
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
initializing: {
|
|
79
|
+
on: {
|
|
80
|
+
initializeSuccess: 'idle',
|
|
81
|
+
isRelatedProperty: {
|
|
82
|
+
target: 'resolvingRelatedValue',
|
|
83
|
+
},
|
|
84
|
+
hasRemoteBackup: {
|
|
85
|
+
target: 'resolvingRemoteStorage',
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
invoke: {
|
|
89
|
+
src: 'initialize',
|
|
90
|
+
input: ({ context, event }) => ({ context, event }),
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
resolvingRelatedValue: {
|
|
94
|
+
on: {
|
|
95
|
+
resolvingRelatedValueSuccess: {
|
|
96
|
+
target: 'idle',
|
|
97
|
+
actions: assign({
|
|
98
|
+
resolvedDisplayValue: ({ event }) => event.resolvedDisplayValue,
|
|
99
|
+
resolvedValue: ({ event }) => event.resolvedValue,
|
|
100
|
+
}),
|
|
101
|
+
},
|
|
102
|
+
resolvingRelatedValueDone: {
|
|
103
|
+
target: 'idle',
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
invoke: {
|
|
107
|
+
src: 'resolveRelatedValue',
|
|
108
|
+
input: ({ context }) => ({ context }),
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
resolvingRemoteStorage: {
|
|
112
|
+
on: {
|
|
113
|
+
resolveRemoteStorageSuccess: {
|
|
114
|
+
target: 'idle',
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
invoke: {
|
|
118
|
+
src: 'resolveRemoteStorage',
|
|
119
|
+
input: ({ context }) => ({ context }),
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
saving: {
|
|
123
|
+
on: {
|
|
124
|
+
saveValueToDbSuccess: {
|
|
125
|
+
target: 'idle',
|
|
126
|
+
actions: assign({
|
|
127
|
+
isSaving: false,
|
|
128
|
+
}),
|
|
129
|
+
},
|
|
130
|
+
saveValueToDbFailure: {
|
|
131
|
+
target: 'idle',
|
|
132
|
+
actions: assign({
|
|
133
|
+
isSaving: false,
|
|
134
|
+
}),
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
invoke: {
|
|
138
|
+
src: 'saveValueToDb',
|
|
139
|
+
input: ({ context, event }) => ({ context, event }),
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
// conflict: {
|
|
145
|
+
// on: {
|
|
146
|
+
// resolveConflict: {
|
|
147
|
+
// target: 'saving',
|
|
148
|
+
// actions: assign({
|
|
149
|
+
// data: ({ context, event }) => event.output,
|
|
150
|
+
// }),
|
|
151
|
+
// },
|
|
152
|
+
// },
|
|
153
|
+
// },
|
|
154
|
+
})
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { getGlobalService } from '@/browser/services/global'
|
|
2
|
+
|
|
3
|
+
type PublishItemRequestEvent = {
|
|
4
|
+
modelName?: string
|
|
5
|
+
seedLocalId: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type PublishItemRequestHandler = (
|
|
9
|
+
event: PublishItemRequestEvent,
|
|
10
|
+
) => Promise<void>
|
|
11
|
+
|
|
12
|
+
export const publishItemRequestHandler: PublishItemRequestHandler = async ({
|
|
13
|
+
modelName,
|
|
14
|
+
seedLocalId,
|
|
15
|
+
}) => {
|
|
16
|
+
const globalService = getGlobalService()
|
|
17
|
+
globalService.subscribe((snapshot) => {
|
|
18
|
+
if (
|
|
19
|
+
!snapshot ||
|
|
20
|
+
!snapshot.context ||
|
|
21
|
+
!snapshot.context.publishItemService
|
|
22
|
+
) {
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
globalService.send({
|
|
27
|
+
type: 'publishItemRequest',
|
|
28
|
+
modelName,
|
|
29
|
+
seedLocalId,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { setup } from 'xstate'
|
|
2
|
+
import { validateItemData } from './actors/validateItemData'
|
|
3
|
+
import { PublishMachineStates } from '@/browser/services/internal/constants'
|
|
4
|
+
import { PublishMachineContext } from '@/types'
|
|
5
|
+
import { createPublishAttempt } from './actors/createPublishAttempt'
|
|
6
|
+
import { updateMachineContext } from '@/browser/helpers'
|
|
7
|
+
import { preparePublishRequestData } from '@/browser/services/publish/actors/preparePublishRequestData'
|
|
8
|
+
import { upload } from '@/browser/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 '@/browser/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
|
+
`)
|
package/dist/src/read.ts
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { PropertyData } from '@/types'
|
|
2
|
+
import { startCase } from 'lodash-es'
|
|
3
|
+
import { metadata, MetadataType } from '@/shared/seedSchema'
|
|
4
|
+
import { and, eq, max, sql } from 'drizzle-orm'
|
|
5
|
+
import { getAppDb, runQueryForStatement } from '@/browser/db/sqlWasmClient'
|
|
6
|
+
|
|
7
|
+
export const getPropertyData = async (
|
|
8
|
+
propertyName: string,
|
|
9
|
+
seedLocalId?: string,
|
|
10
|
+
seedUid?: string,
|
|
11
|
+
): Promise<PropertyData | undefined> => {
|
|
12
|
+
const appDb = getAppDb()
|
|
13
|
+
|
|
14
|
+
const queryBase = appDb.select().from(metadata)
|
|
15
|
+
|
|
16
|
+
if (seedLocalId) {
|
|
17
|
+
queryBase.where(
|
|
18
|
+
and(
|
|
19
|
+
eq(metadata.propertyName, propertyName),
|
|
20
|
+
eq(metadata.seedLocalId, seedLocalId),
|
|
21
|
+
),
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (seedUid) {
|
|
26
|
+
queryBase.where(
|
|
27
|
+
and(
|
|
28
|
+
eq(metadata.propertyName, propertyName),
|
|
29
|
+
eq(metadata.seedUid, seedUid),
|
|
30
|
+
),
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const rows = (await queryBase.orderBy(
|
|
35
|
+
sql.raw(`COALESCE(attestation_created_at, created_at) DESC`),
|
|
36
|
+
)) as MetadataType[]
|
|
37
|
+
|
|
38
|
+
if (!rows || rows.length === 0) {
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const row = rows[0]
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
...row,
|
|
46
|
+
itemModelName: startCase(row.modelType),
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const seedUidToStorageTransactionId = new Map<string, string>()
|
|
51
|
+
|
|
52
|
+
type GetStorageTransactionIdResults = {
|
|
53
|
+
storageTransactionId: string
|
|
54
|
+
}[]
|
|
55
|
+
|
|
56
|
+
export const getStorageTransactionIdForSeedUid = async (
|
|
57
|
+
seedUid: string,
|
|
58
|
+
): Promise<string | undefined> => {
|
|
59
|
+
if (seedUidToStorageTransactionId.has(seedUid)) {
|
|
60
|
+
return seedUidToStorageTransactionId.get(seedUid)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const appDb = getAppDb()
|
|
64
|
+
|
|
65
|
+
const results = (await appDb
|
|
66
|
+
.select({
|
|
67
|
+
storageTransactionId: metadata.propertyValue,
|
|
68
|
+
})
|
|
69
|
+
.from(metadata)
|
|
70
|
+
.where(
|
|
71
|
+
and(
|
|
72
|
+
eq(metadata.seedUid, seedUid),
|
|
73
|
+
eq(metadata.propertyName, 'storageTransactionId'),
|
|
74
|
+
),
|
|
75
|
+
)) as GetStorageTransactionIdResults
|
|
76
|
+
|
|
77
|
+
if (!results || results.length === 0) {
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
seedUidToStorageTransactionId.set(seedUid, results[0].storageTransactionId)
|
|
82
|
+
return results[0].storageTransactionId
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const getRelationValueData = async (
|
|
86
|
+
propertyValue: any,
|
|
87
|
+
): Promise<MetadataType | undefined> => {
|
|
88
|
+
const appDb = getAppDb()
|
|
89
|
+
|
|
90
|
+
const rows = (await appDb
|
|
91
|
+
.select({
|
|
92
|
+
propertyValue: metadata.propertyValue,
|
|
93
|
+
attestationCreatedAt: max(metadata.attestationCreatedAt),
|
|
94
|
+
refResolvedDisplayValue: metadata.refResolvedDisplayValue,
|
|
95
|
+
refResolvedValue: metadata.refResolvedValue,
|
|
96
|
+
refSeedType: metadata.refSeedType,
|
|
97
|
+
easDataType: metadata.easDataType,
|
|
98
|
+
})
|
|
99
|
+
.from(metadata)
|
|
100
|
+
.where(eq(metadata.propertyValue, propertyValue))) as MetadataType[]
|
|
101
|
+
|
|
102
|
+
if (!rows || rows.length === 0) {
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return rows[0]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type GetExistingItemProps = {
|
|
110
|
+
seedLocalId: string
|
|
111
|
+
seedUid: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
type GetExistingItemReturn = {
|
|
115
|
+
seedLocalId: string
|
|
116
|
+
seedUid: string
|
|
117
|
+
createdAt: string
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
type GetExistingItem = (
|
|
121
|
+
props: GetExistingItemProps,
|
|
122
|
+
) => Promise<GetExistingItemReturn | undefined>
|
|
123
|
+
|
|
124
|
+
export const getExistingItem: GetExistingItem = async ({
|
|
125
|
+
seedLocalId,
|
|
126
|
+
seedUid,
|
|
127
|
+
}) => {
|
|
128
|
+
const existingItemStatement = `SELECT local_id, uid, created_at
|
|
129
|
+
FROM seeds
|
|
130
|
+
WHERE (uid IS NOT NULL
|
|
131
|
+
AND uid != ''
|
|
132
|
+
AND uid != 'undefined'
|
|
133
|
+
AND uid != 'null'
|
|
134
|
+
AND uid != 'false'
|
|
135
|
+
AND uid != '0'
|
|
136
|
+
AND uid != 0
|
|
137
|
+
AND uid = '${seedUid}'
|
|
138
|
+
)
|
|
139
|
+
OR (
|
|
140
|
+
local_id IS NOT NULL
|
|
141
|
+
AND local_id != ''
|
|
142
|
+
AND local_id != 'undefined'
|
|
143
|
+
AND local_id != 'null'
|
|
144
|
+
AND local_id != 'false'
|
|
145
|
+
AND local_id != '0'
|
|
146
|
+
AND local_id != 0
|
|
147
|
+
AND local_id = '${seedLocalId}'
|
|
148
|
+
);
|
|
149
|
+
`
|
|
150
|
+
|
|
151
|
+
const { rows } = await runQueryForStatement(existingItemStatement)
|
|
152
|
+
|
|
153
|
+
let existingItem
|
|
154
|
+
|
|
155
|
+
if (rows && rows.length > 0) {
|
|
156
|
+
// We know the zero index is versionLocalId because we specifically asked for it first
|
|
157
|
+
// in the SELECT statement above. Same with the rest of the indexes.
|
|
158
|
+
let matchingRow = rows.find((row) => row[1] === seedUid)
|
|
159
|
+
|
|
160
|
+
if (!matchingRow) {
|
|
161
|
+
matchingRow = rows.find((row) => row[0] === seedLocalId)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (matchingRow) {
|
|
165
|
+
existingItem = {
|
|
166
|
+
seedLocalId: matchingRow[0],
|
|
167
|
+
seedUid: matchingRow[1],
|
|
168
|
+
createdAt: matchingRow[2],
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return existingItem
|
|
174
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getAppDb } from '@/browser/db/sqlWasmClient'
|
|
2
|
+
import { seeds } from '@/shared/seedSchema'
|
|
3
|
+
import { eq, or } from 'drizzle-orm'
|
|
4
|
+
|
|
5
|
+
export const recoverDeletedItem = async ({ seedLocalId, seedUid }) => {
|
|
6
|
+
const appDb = 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
|
+
}
|