@seedprotocol/sdk 0.1.46 → 0.1.48
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-BEzB8REh.js → index-DMIKRod-.js} +3684 -3660
- package/dist/index-DMIKRod-.js.map +1 -0
- package/dist/{index-ChGsdGPJ.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-jKpK-lR6.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 +10 -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 +21 -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/events/item/create.d.ts.map +1 -1
- package/dist/types/src/browser/events/item/publish.d.ts.map +1 -1
- package/dist/types/src/browser/events/item/requestAll.d.ts.map +1 -1
- package/dist/types/src/browser/events/item/syncDbWithEas.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/hydrateFromDb.d.ts.map +1 -1
- package/dist/types/src/browser/property/actors/initialize.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 +1 -1
- package/dist/index-BEzB8REh.js.map +0 -1
- package/dist/index-ChGsdGPJ.js.map +0 -1
- package/dist/seed.schema.config-jKpK-lR6.js.map +0 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { itemMachineAll } from '@/browser/item/all/itemMachineAll'
|
|
3
|
+
|
|
4
|
+
export const fetchDbData = fromCallback<EventObject, typeof itemMachineAll>(
|
|
5
|
+
({ sendBack, input: { context } }) => {
|
|
6
|
+
const { modelNamePlural, times } = context
|
|
7
|
+
|
|
8
|
+
const _fetchDbData = async (): Promise<void> => {}
|
|
9
|
+
|
|
10
|
+
_fetchDbData().then(() => {
|
|
11
|
+
sendBack({ type: 'fetchDbDataSuccess' })
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
return () => {}
|
|
15
|
+
},
|
|
16
|
+
)
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import {
|
|
3
|
+
GET_ALL_PROPERTIES_FOR_ALL_VERSIONS,
|
|
4
|
+
GET_SEED_IDS,
|
|
5
|
+
GET_VERSIONS,
|
|
6
|
+
} from '@/browser/item/queries'
|
|
7
|
+
import {
|
|
8
|
+
models as modelsTable,
|
|
9
|
+
modelUids,
|
|
10
|
+
PropertyType,
|
|
11
|
+
} from '@/shared/seedSchema'
|
|
12
|
+
import { Attestation } from '@/browser/gql/graphql'
|
|
13
|
+
import {
|
|
14
|
+
AllItemsMachineContext,
|
|
15
|
+
FromCallbackInput,
|
|
16
|
+
ModelClassType,
|
|
17
|
+
} from '@/types'
|
|
18
|
+
import { getAddressesFromDb } from '@/shared/helpers/db'
|
|
19
|
+
import { eq } from 'drizzle-orm'
|
|
20
|
+
import { easClient, queryClient } from '@/browser/helpers'
|
|
21
|
+
import { getAppDb } from '@/browser/db/sqlWasmClient'
|
|
22
|
+
|
|
23
|
+
export const fetchRelatedItems = fromCallback<
|
|
24
|
+
EventObject,
|
|
25
|
+
FromCallbackInput<AllItemsMachineContext>
|
|
26
|
+
>(({ sendBack, input: { context } }) => {
|
|
27
|
+
const { ModelClass, modelName } = context
|
|
28
|
+
|
|
29
|
+
const appDb = getAppDb()
|
|
30
|
+
|
|
31
|
+
const relatedProperties = new Map<string, PropertyType>()
|
|
32
|
+
const relatedVersionsBySeedUid = new Map<string, Attestation[]>()
|
|
33
|
+
const schemaUidsByModelName = new Map<string, string>()
|
|
34
|
+
const mostRecentVersionsBySeedUid = new Map<string, Attestation>()
|
|
35
|
+
const mostRecentPropertiesBySeedUid = new Map<string, Attestation[]>()
|
|
36
|
+
const seedUidsByMostRecentVersionUid = new Map<string, string>()
|
|
37
|
+
|
|
38
|
+
const _fetchRelatedItems = async () => {
|
|
39
|
+
// Get related properties
|
|
40
|
+
for (const [propertyName, propertyDef] of Object.entries(
|
|
41
|
+
(ModelClass as ModelClassType).schema,
|
|
42
|
+
)) {
|
|
43
|
+
if (propertyDef && propertyDef.ref && propertyDef.refModelId) {
|
|
44
|
+
relatedProperties.set(propertyName, propertyDef)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const addresses = await getAddressesFromDb(appDb)
|
|
49
|
+
|
|
50
|
+
// Get the models they point to from sdkConfigDb
|
|
51
|
+
for (const [propertyName, propertyDef] of relatedProperties.entries()) {
|
|
52
|
+
const relatedModelQuery = await appDb
|
|
53
|
+
.select({
|
|
54
|
+
id: modelsTable.id,
|
|
55
|
+
name: modelsTable.name,
|
|
56
|
+
uid: modelUids.uid,
|
|
57
|
+
})
|
|
58
|
+
.from(modelsTable)
|
|
59
|
+
.leftJoin(modelUids, eq(modelsTable.id, modelUids.modelId))
|
|
60
|
+
.where(eq(modelsTable.id, propertyDef.refModelId))
|
|
61
|
+
.limit(1)
|
|
62
|
+
|
|
63
|
+
if (relatedModelQuery && relatedModelQuery.length > 0) {
|
|
64
|
+
const relatedModel = relatedModelQuery[0]
|
|
65
|
+
const relatedModelUid = relatedModel.uid
|
|
66
|
+
// Exclude the current model's schemaUid since we already have its versions
|
|
67
|
+
if (relatedModelUid && relatedModelUid !== ModelClass.schemaUid) {
|
|
68
|
+
schemaUidsByModelName.set(relatedModel.name, relatedModelUid)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const queryKey = [`getRelatedSeedIds${modelName}`]
|
|
74
|
+
|
|
75
|
+
const { itemSeedIds: relatedSeedIdAttestations } =
|
|
76
|
+
await queryClient.fetchQuery({
|
|
77
|
+
queryKey,
|
|
78
|
+
queryFn: async () =>
|
|
79
|
+
easClient.request(GET_SEED_IDS, {
|
|
80
|
+
where: {
|
|
81
|
+
schema: {
|
|
82
|
+
is: {
|
|
83
|
+
id: {
|
|
84
|
+
in: Array.from(schemaUidsByModelName.values()),
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
attester: {
|
|
89
|
+
in: addresses,
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
}),
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
const relatedSeedIds = relatedSeedIdAttestations.map((seed) => seed.id)
|
|
96
|
+
|
|
97
|
+
const { itemVersions: relatedVersions } = await queryClient.fetchQuery({
|
|
98
|
+
queryKey: [`getRelatedVersions${modelName}`],
|
|
99
|
+
queryFn: async () =>
|
|
100
|
+
easClient.request(GET_VERSIONS, {
|
|
101
|
+
where: {
|
|
102
|
+
refUID: {
|
|
103
|
+
in: relatedSeedIds,
|
|
104
|
+
},
|
|
105
|
+
attester: {
|
|
106
|
+
in: addresses,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
}),
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
// Index versions by seedUid
|
|
113
|
+
for (const version of relatedVersions) {
|
|
114
|
+
const existingVersionsForSeedUid =
|
|
115
|
+
relatedVersionsBySeedUid.get(version.refUID) || []
|
|
116
|
+
existingVersionsForSeedUid.push(version)
|
|
117
|
+
relatedVersionsBySeedUid.set(version.refUID, existingVersionsForSeedUid)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Sort the indexed versions by timeCreated and index the most recent
|
|
121
|
+
for (const [
|
|
122
|
+
seedUid,
|
|
123
|
+
versionsForSeed,
|
|
124
|
+
] of relatedVersionsBySeedUid.entries()) {
|
|
125
|
+
const versionsForSeedSorted = versionsForSeed.sort(
|
|
126
|
+
(a: Attestation, b: Attestation) => {
|
|
127
|
+
return a.timeCreated - b.timeCreated
|
|
128
|
+
},
|
|
129
|
+
)
|
|
130
|
+
relatedVersionsBySeedUid.set(seedUid, versionsForSeedSorted)
|
|
131
|
+
mostRecentVersionsBySeedUid.set(seedUid, versionsForSeedSorted[0])
|
|
132
|
+
seedUidsByMostRecentVersionUid.set(versionsForSeedSorted[0].id, seedUid)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Extract the ids of the most recent versions
|
|
136
|
+
const mostRecentVersionIds = Array.from(
|
|
137
|
+
mostRecentVersionsBySeedUid.values(),
|
|
138
|
+
).map((version) => version.id)
|
|
139
|
+
|
|
140
|
+
const { allProperties } = await queryClient.fetchQuery({
|
|
141
|
+
queryKey: [`getAllProperties${modelName}`],
|
|
142
|
+
queryFn: async () =>
|
|
143
|
+
easClient.request(GET_ALL_PROPERTIES_FOR_ALL_VERSIONS, {
|
|
144
|
+
where: {
|
|
145
|
+
refUID: {
|
|
146
|
+
in: mostRecentVersionIds,
|
|
147
|
+
},
|
|
148
|
+
attester: {
|
|
149
|
+
in: addresses,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
for (const propertyAttestation of allProperties) {
|
|
156
|
+
const seedUidForProperty = seedUidsByMostRecentVersionUid.get(
|
|
157
|
+
propertyAttestation.refUID,
|
|
158
|
+
)
|
|
159
|
+
const existingPropertiesForSeedUid =
|
|
160
|
+
mostRecentPropertiesBySeedUid.get(seedUidForProperty!) || []
|
|
161
|
+
existingPropertiesForSeedUid.push(propertyAttestation)
|
|
162
|
+
mostRecentPropertiesBySeedUid.set(
|
|
163
|
+
seedUidForProperty!,
|
|
164
|
+
existingPropertiesForSeedUid,
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
_fetchRelatedItems().then(() => {
|
|
170
|
+
sendBack({
|
|
171
|
+
type: 'fetchRelatedItemsSuccess',
|
|
172
|
+
mostRecentPropertiesBySeedUid,
|
|
173
|
+
relatedVersionsBySeedUid,
|
|
174
|
+
relatedProperties,
|
|
175
|
+
schemaUidsByModelName,
|
|
176
|
+
})
|
|
177
|
+
return
|
|
178
|
+
})
|
|
179
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { GET_SEEDS } from '@/browser/item/queries'
|
|
3
|
+
import { itemMachineAll } from '@/browser/item/all/itemMachineAll'
|
|
4
|
+
import { Attestation } from '@/browser/gql/graphql'
|
|
5
|
+
import { easClient, queryClient } from '@/browser/helpers'
|
|
6
|
+
import debug from 'debug'
|
|
7
|
+
|
|
8
|
+
const logger = debug('app:allItemsActors:fetchSeeds')
|
|
9
|
+
|
|
10
|
+
export const fetchSeeds = fromCallback<EventObject, typeof itemMachineAll>(
|
|
11
|
+
({ sendBack, input: { context } }) => {
|
|
12
|
+
const { queryVariables, modelName } = context
|
|
13
|
+
|
|
14
|
+
if (!queryVariables) {
|
|
15
|
+
throw new Error('No queryVariables found')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let itemSeeds: Attestation[] | undefined
|
|
19
|
+
|
|
20
|
+
const _fetchSeeds = async () => {
|
|
21
|
+
const queryKey = [`getSeeds${modelName}`]
|
|
22
|
+
|
|
23
|
+
const cachedResults = queryClient.getQueryData(queryKey)
|
|
24
|
+
|
|
25
|
+
logger(
|
|
26
|
+
`[allItemsActors] [fetchSeeds] cachedResults ${Date.now()}`,
|
|
27
|
+
cachedResults,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const results = await queryClient.fetchQuery({
|
|
31
|
+
queryKey,
|
|
32
|
+
queryFn: async () => easClient.request(GET_SEEDS, queryVariables),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
itemSeeds = results.itemSeeds
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_fetchSeeds().then(() => {
|
|
39
|
+
sendBack({ type: 'fetchSeedsSuccess', itemSeeds })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return () => {}
|
|
43
|
+
},
|
|
44
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { GET_VERSIONS } from '@/browser/item/queries'
|
|
3
|
+
import { itemMachineAll } from '@/browser/item/all/itemMachineAll'
|
|
4
|
+
import { Attestation } from '@/browser/gql/graphql'
|
|
5
|
+
import { easClient, queryClient } from '@/browser/helpers'
|
|
6
|
+
|
|
7
|
+
export const fetchVersions = fromCallback<EventObject, typeof itemMachineAll>(
|
|
8
|
+
({ sendBack, input: { context } }) => {
|
|
9
|
+
const { itemSeeds, modelName } = context
|
|
10
|
+
|
|
11
|
+
if (!itemSeeds) {
|
|
12
|
+
throw new Error('No queryVariables found')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
let itemVersions: Attestation[] | undefined
|
|
16
|
+
|
|
17
|
+
const _fetchVersions = async () => {
|
|
18
|
+
const seedIds = itemSeeds.map((seed) => seed.id)
|
|
19
|
+
|
|
20
|
+
const results = await queryClient.fetchQuery({
|
|
21
|
+
queryKey: [`getVersions${modelName}`],
|
|
22
|
+
queryFn: async () =>
|
|
23
|
+
easClient.request(GET_VERSIONS, {
|
|
24
|
+
where: {
|
|
25
|
+
refUID: {
|
|
26
|
+
in: seedIds,
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
}),
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
itemVersions = results.itemVersions
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_fetchVersions().then(() => {
|
|
36
|
+
sendBack({ type: 'fetchVersionsSuccess', itemVersions })
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
return () => {}
|
|
40
|
+
},
|
|
41
|
+
)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const listFilesInOPFSRoot = async () => {
|
|
2
|
+
// Get the root directory handle
|
|
3
|
+
const rootDirHandle = await navigator.storage.getDirectory()
|
|
4
|
+
|
|
5
|
+
// Initialize an array to hold the file names
|
|
6
|
+
let fileNames = []
|
|
7
|
+
|
|
8
|
+
// Create an async iterator to loop through directory entries
|
|
9
|
+
for await (const entry of rootDirHandle.values()) {
|
|
10
|
+
if (entry.kind === 'file') {
|
|
11
|
+
fileNames.push(entry.name)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return fileNames
|
|
16
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
3
|
+
import { FragmentDefinitionNode } from 'graphql';
|
|
4
|
+
import { Incremental } from './graphql';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
|
|
8
|
+
infer TType,
|
|
9
|
+
any
|
|
10
|
+
>
|
|
11
|
+
? [TType] extends [{ ' $fragmentName'?: infer TKey }]
|
|
12
|
+
? TKey extends string
|
|
13
|
+
? { ' $fragmentRefs'?: { [key in TKey]: TType } }
|
|
14
|
+
: never
|
|
15
|
+
: never
|
|
16
|
+
: never;
|
|
17
|
+
|
|
18
|
+
// return non-nullable if `fragmentType` is non-nullable
|
|
19
|
+
export function useFragment<TType>(
|
|
20
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
21
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
|
|
22
|
+
): TType;
|
|
23
|
+
// return nullable if `fragmentType` is undefined
|
|
24
|
+
export function useFragment<TType>(
|
|
25
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
26
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | undefined
|
|
27
|
+
): TType | undefined;
|
|
28
|
+
// return nullable if `fragmentType` is nullable
|
|
29
|
+
export function useFragment<TType>(
|
|
30
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
31
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null
|
|
32
|
+
): TType | null;
|
|
33
|
+
// return nullable if `fragmentType` is nullable or undefined
|
|
34
|
+
export function useFragment<TType>(
|
|
35
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
36
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
|
|
37
|
+
): TType | null | undefined;
|
|
38
|
+
// return array of non-nullable if `fragmentType` is array of non-nullable
|
|
39
|
+
export function useFragment<TType>(
|
|
40
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
41
|
+
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>>
|
|
42
|
+
): Array<TType>;
|
|
43
|
+
// return array of nullable if `fragmentType` is array of nullable
|
|
44
|
+
export function useFragment<TType>(
|
|
45
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
46
|
+
fragmentType: Array<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
47
|
+
): Array<TType> | null | undefined;
|
|
48
|
+
// return readonly array of non-nullable if `fragmentType` is array of non-nullable
|
|
49
|
+
export function useFragment<TType>(
|
|
50
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
51
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
|
|
52
|
+
): ReadonlyArray<TType>;
|
|
53
|
+
// return readonly array of nullable if `fragmentType` is array of nullable
|
|
54
|
+
export function useFragment<TType>(
|
|
55
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
56
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
57
|
+
): ReadonlyArray<TType> | null | undefined;
|
|
58
|
+
export function useFragment<TType>(
|
|
59
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
60
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | Array<FragmentType<DocumentTypeDecoration<TType, any>>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
61
|
+
): TType | Array<TType> | ReadonlyArray<TType> | null | undefined {
|
|
62
|
+
return fragmentType as any;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
export function makeFragmentData<
|
|
67
|
+
F extends DocumentTypeDecoration<any, any>,
|
|
68
|
+
FT extends ResultOf<F>
|
|
69
|
+
>(data: FT, _fragment: F): FragmentType<F> {
|
|
70
|
+
return data as FragmentType<F>;
|
|
71
|
+
}
|
|
72
|
+
export function isFragmentReady<TQuery, TFrag>(
|
|
73
|
+
queryNode: DocumentTypeDecoration<TQuery, any>,
|
|
74
|
+
fragmentNode: TypedDocumentNode<TFrag>,
|
|
75
|
+
data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
|
|
76
|
+
): data is FragmentType<typeof fragmentNode> {
|
|
77
|
+
const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
|
|
78
|
+
?.deferredFields;
|
|
79
|
+
|
|
80
|
+
if (!deferredFields) return true;
|
|
81
|
+
|
|
82
|
+
const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
|
|
83
|
+
const fragName = fragDef?.name?.value;
|
|
84
|
+
|
|
85
|
+
const fields = (fragName && deferredFields[fragName]) || [];
|
|
86
|
+
return fields.length > 0 && fields.every(field => data && field in data);
|
|
87
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// File: fsProxy.js
|
|
2
|
+
let actualFs = {} // Empty placeholder for the real implementation
|
|
3
|
+
|
|
4
|
+
let resolveFsReady
|
|
5
|
+
const fsReady = new Promise((resolve) => {
|
|
6
|
+
resolveFsReady = resolve
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
// Create a proxy object that intercepts all accesses to `fs`
|
|
10
|
+
const fsProxy = new Proxy(actualFs, {
|
|
11
|
+
get(target, prop) {
|
|
12
|
+
if (prop.endsWith('Sync')) {
|
|
13
|
+
return (...args) => {
|
|
14
|
+
actualFs[prop](...args)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return async (...args) => {
|
|
19
|
+
await fsReady
|
|
20
|
+
if (typeof actualFs[prop] === 'function') {
|
|
21
|
+
return actualFs[prop](...args)
|
|
22
|
+
} else {
|
|
23
|
+
return actualFs[prop]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
const setFsImplementation = (configuredFs) => {
|
|
30
|
+
Object.assign(actualFs, configuredFs)
|
|
31
|
+
resolveFsReady() // Resolve the promise to signal fs is ready
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { setFsImplementation }
|
|
35
|
+
|
|
36
|
+
export default fsProxy
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { Item } from '@/browser/item'
|
|
2
|
+
|
|
3
|
+
import { getItemProperties } from '@/browser/db/read/getItemProperties'
|
|
4
|
+
import { getSeedData } from '@/browser/db/read/getSeedData'
|
|
5
|
+
import debug from 'debug'
|
|
6
|
+
import { ItemData } from '@/types'
|
|
7
|
+
import { getAppDb } from '../sqlWasmClient'
|
|
8
|
+
import { seeds, versions } from '@/shared/seedSchema'
|
|
9
|
+
import {
|
|
10
|
+
and,
|
|
11
|
+
count,
|
|
12
|
+
eq,
|
|
13
|
+
getTableColumns,
|
|
14
|
+
isNull,
|
|
15
|
+
max,
|
|
16
|
+
or,
|
|
17
|
+
SQL,
|
|
18
|
+
} from 'drizzle-orm'
|
|
19
|
+
|
|
20
|
+
const logger = debug('app:db:queries:getItem')
|
|
21
|
+
|
|
22
|
+
type GetItemDataFromDbParams = {
|
|
23
|
+
modelName?: string
|
|
24
|
+
seedLocalId?: string
|
|
25
|
+
seedUid?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type GetItemDataFromDb = (
|
|
29
|
+
params: GetItemDataFromDbParams,
|
|
30
|
+
) => Promise<ItemData | undefined>
|
|
31
|
+
|
|
32
|
+
export const getItemDataFromDb: GetItemDataFromDb = async ({
|
|
33
|
+
modelName,
|
|
34
|
+
seedLocalId,
|
|
35
|
+
seedUid,
|
|
36
|
+
}) => {
|
|
37
|
+
if (!seedLocalId && !seedUid) {
|
|
38
|
+
throw new Error('[db/queries] [getItem] no seedLocalId or seedUid')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (seedUid && !seedLocalId) {
|
|
42
|
+
const seedData = await getSeedData({ seedUid })
|
|
43
|
+
if (!seedData) {
|
|
44
|
+
logger('[db/queries] [getItem] no seedData seedUid', seedUid)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
seedLocalId = seedData.localId
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const appDb = getAppDb()
|
|
51
|
+
|
|
52
|
+
const { localId, uid, ...rest } = getTableColumns(seeds)
|
|
53
|
+
|
|
54
|
+
const whereClauses: SQL[] = []
|
|
55
|
+
|
|
56
|
+
if (modelName) {
|
|
57
|
+
whereClauses.push(eq(seeds.type, modelName.toLowerCase()))
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const uidWhereClause: SQL = seedUid
|
|
61
|
+
? eq(seeds.uid, seedUid)
|
|
62
|
+
: isNull(seeds.uid)
|
|
63
|
+
const localWhereClause: SQL = seedLocalId
|
|
64
|
+
? eq(seeds.localId, seedLocalId)
|
|
65
|
+
: isNull(seeds.localId)
|
|
66
|
+
|
|
67
|
+
whereClauses.push(or(localWhereClause, uidWhereClause) as SQL)
|
|
68
|
+
|
|
69
|
+
const latestVersions = appDb.$with('latestVersions').as(
|
|
70
|
+
appDb
|
|
71
|
+
.select({
|
|
72
|
+
localId: versions.localId,
|
|
73
|
+
uid: versions.uid,
|
|
74
|
+
seedLocalId: versions.seedLocalId,
|
|
75
|
+
seedUid: versions.seedUid,
|
|
76
|
+
})
|
|
77
|
+
.from(versions)
|
|
78
|
+
.groupBy(versions.seedLocalId),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
const itemDataRows = await appDb
|
|
82
|
+
.with(latestVersions)
|
|
83
|
+
.select({
|
|
84
|
+
...rest,
|
|
85
|
+
seedLocalId: seeds.localId,
|
|
86
|
+
seedUid: seeds.uid,
|
|
87
|
+
versionsCount: count(versions.localId),
|
|
88
|
+
lastVersionPublishedAt: max(versions.attestationCreatedAt),
|
|
89
|
+
latestVersionUid: latestVersions.uid,
|
|
90
|
+
})
|
|
91
|
+
.from(seeds)
|
|
92
|
+
.leftJoin(versions, eq(versions.seedLocalId, seeds.localId))
|
|
93
|
+
.leftJoin(
|
|
94
|
+
latestVersions,
|
|
95
|
+
eq(versions.seedLocalId, latestVersions.seedLocalId),
|
|
96
|
+
)
|
|
97
|
+
.where(and(...whereClauses))
|
|
98
|
+
.groupBy(seeds.localId)
|
|
99
|
+
|
|
100
|
+
if (!itemDataRows || itemDataRows.length === 0) {
|
|
101
|
+
logger('[db/queries] [getItemDataFromDb] no itemDataRows')
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let itemData = itemDataRows[0]
|
|
106
|
+
|
|
107
|
+
const propertiesData = await getItemProperties({ seedLocalId, seedUid })
|
|
108
|
+
|
|
109
|
+
// const initObj: ItemData = {
|
|
110
|
+
// seedLocalId,
|
|
111
|
+
// seedUid,
|
|
112
|
+
// modelName,
|
|
113
|
+
// }
|
|
114
|
+
|
|
115
|
+
if (!propertiesData || propertiesData.length === 0) {
|
|
116
|
+
return itemData
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const firstPropertyData = propertiesData[0]
|
|
120
|
+
|
|
121
|
+
for (const propertyData of propertiesData) {
|
|
122
|
+
const propertyName = propertyData.propertyName
|
|
123
|
+
|
|
124
|
+
const propertyValue = propertyData.propertyValue
|
|
125
|
+
|
|
126
|
+
// TODO: Find a better place for the property data below
|
|
127
|
+
// Since initObj is used to initialize an Item, the following values
|
|
128
|
+
// just overwrite each other for each property since they are Property
|
|
129
|
+
// specific.
|
|
130
|
+
|
|
131
|
+
// const refSeedType = propertyDbValues[11]
|
|
132
|
+
// if (refSeedType) {
|
|
133
|
+
// initObj.refSeedType = refSeedType
|
|
134
|
+
// }
|
|
135
|
+
// const refValueType = propertyDbValues[12]
|
|
136
|
+
// if (refValueType) {
|
|
137
|
+
// initObj.refValueType = refValueType
|
|
138
|
+
// }
|
|
139
|
+
//
|
|
140
|
+
// if (
|
|
141
|
+
// refSeedType &&
|
|
142
|
+
// refValueType === 'list' &&
|
|
143
|
+
// propertyName.endsWith('Ids')
|
|
144
|
+
// ) {
|
|
145
|
+
// logger('[db/queries] [getItemDataFromDb] propertyName', propertyName)
|
|
146
|
+
// }
|
|
147
|
+
|
|
148
|
+
itemData[propertyName] = propertyValue
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return itemData
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
type GetItemParams = {
|
|
155
|
+
modelName?: string
|
|
156
|
+
seedLocalId?: string
|
|
157
|
+
seedUid?: string
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
type GetItem = (params: GetItemParams) => Promise<Item<any> | undefined>
|
|
161
|
+
|
|
162
|
+
export const getItem: GetItem = async ({ modelName, seedLocalId, seedUid }) => {
|
|
163
|
+
const itemInitObj = await getItemDataFromDb({
|
|
164
|
+
modelName,
|
|
165
|
+
seedLocalId,
|
|
166
|
+
seedUid,
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
if (!itemInitObj) {
|
|
170
|
+
console.error(
|
|
171
|
+
`[db/queries] [getItem] no itemInitObj modelName: ${modelName} seedLocalId: ${seedLocalId} seedUid: ${seedUid}`,
|
|
172
|
+
)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (!itemInitObj.seedLocalId) {
|
|
177
|
+
console.error(
|
|
178
|
+
`[db/queries] [getItem] no itemInitObj.seedLocalId modelName: ${modelName} seedLocalId: ${seedLocalId} seedUid: ${seedUid}`,
|
|
179
|
+
)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
console.log(
|
|
184
|
+
`[${modelName}.${seedLocalId}] [getItem] Creating Item`,
|
|
185
|
+
itemInitObj,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
return Item.create(itemInitObj)
|
|
189
|
+
}
|