@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,29 @@
|
|
|
1
|
+
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
|
|
2
|
+
|
|
3
|
+
export const seeds = sqliteTable(
|
|
4
|
+
'seeds',
|
|
5
|
+
{
|
|
6
|
+
localId: text('local_id').unique(),
|
|
7
|
+
uid: text('uid'),
|
|
8
|
+
schemaUid: text('schema_uid'),
|
|
9
|
+
type: text('type'),
|
|
10
|
+
attestationRaw: text('attestation_raw'),
|
|
11
|
+
attestationCreatedAt: int('attestation_created_at'),
|
|
12
|
+
createdAt: int('created_at'),
|
|
13
|
+
updatedAt: int('updated_at'),
|
|
14
|
+
_markedForDeletion: int('_marked_for_deletion'),
|
|
15
|
+
},
|
|
16
|
+
// {
|
|
17
|
+
// triggers: [
|
|
18
|
+
// sql<string>`CREATE TRIGGER IF NOT EXISTS seeds_created_at_trigger
|
|
19
|
+
// BEFORE INSERT
|
|
20
|
+
// ON seeds
|
|
21
|
+
// FOR EACH ROW
|
|
22
|
+
// BEGIN
|
|
23
|
+
// SELECT strftime('%s', 'now') * 1000 INTO NEW.created_at;
|
|
24
|
+
// END;`,
|
|
25
|
+
// ],
|
|
26
|
+
// },
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
export type SeedType = seeds.$inferSelect
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
|
|
2
|
+
|
|
3
|
+
export const versions = sqliteTable('versions', {
|
|
4
|
+
localId: text('local_id').unique(),
|
|
5
|
+
uid: text('uid'),
|
|
6
|
+
seedLocalId: text('seed_local_id'),
|
|
7
|
+
seedUid: text('seed_uid'),
|
|
8
|
+
seedType: text('seed_type'),
|
|
9
|
+
note: text('note'),
|
|
10
|
+
createdAt: int('created_at'),
|
|
11
|
+
updatedAt: int('updated_at'),
|
|
12
|
+
attestationCreatedAt: int('attestation_created_at'),
|
|
13
|
+
attestationRaw: text('attestation_raw'),
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export type VersionsType = versions.$inferSelect
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { fetchAllFilesMachine } from '@/browser/schema/file/fetchAll/index'
|
|
3
|
+
import { ARWEAVE_HOST } from '@/browser/services/internal/constants'
|
|
4
|
+
import { GET_FILES_METADATA } from '@/browser/schema/file/queries'
|
|
5
|
+
import { getArweave } from '../arweave'
|
|
6
|
+
import { fs } from '@zenfs/core'
|
|
7
|
+
import { getImageDataType, getMimeType, identifyString } from '@/shared/helpers'
|
|
8
|
+
import { appState } from 'src/shared/seedSchema'
|
|
9
|
+
import { eq } from 'drizzle-orm'
|
|
10
|
+
import { easClient, queryClient } from '@/browser/helpers'
|
|
11
|
+
import debug from 'debug'
|
|
12
|
+
import { getAppDb } from '@/browser/db/sqlWasmClient'
|
|
13
|
+
import { writeAppState } from '@/browser/db/write'
|
|
14
|
+
|
|
15
|
+
const logger = debug('app:file:actors:fetchAll')
|
|
16
|
+
|
|
17
|
+
type FileType = {
|
|
18
|
+
mimeType: string
|
|
19
|
+
extension: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Map of common MIME types to file extensions
|
|
23
|
+
const fileTypeMap: Record<string, FileType> = {
|
|
24
|
+
'image/jpeg': { mimeType: 'image/jpeg', extension: '.jpg' },
|
|
25
|
+
'image/png': { mimeType: 'image/png', extension: '.png' },
|
|
26
|
+
'application/json': { mimeType: 'application/json', extension: '.json' },
|
|
27
|
+
'text/plain': { mimeType: 'text/plain', extension: '.txt' },
|
|
28
|
+
// Add more MIME types and file extensions as needed
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const fetchAllMetadataRecords = fromCallback<
|
|
32
|
+
EventObject,
|
|
33
|
+
typeof fetchAllFilesMachine
|
|
34
|
+
>(({ sendBack, receive, input: { context, event } }) => {
|
|
35
|
+
const { addresses } = context
|
|
36
|
+
|
|
37
|
+
const _fetchAllMetadataRecords = async () => {
|
|
38
|
+
const { filesMetadata } = await queryClient.fetchQuery({
|
|
39
|
+
queryKey: ['getFilesMetadata', ...addresses],
|
|
40
|
+
queryFn: async () =>
|
|
41
|
+
easClient.request(GET_FILES_METADATA, {
|
|
42
|
+
where: {
|
|
43
|
+
attester: {
|
|
44
|
+
in: addresses,
|
|
45
|
+
},
|
|
46
|
+
schema: {
|
|
47
|
+
is: {
|
|
48
|
+
id: {
|
|
49
|
+
equals:
|
|
50
|
+
'0x55fdefb36fcbbaebeb7d6b41dc3a1a9666e4e42154267c889de064faa7ede517',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
}),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
return filesMetadata
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_fetchAllMetadataRecords().then((filesMetadata) => {
|
|
62
|
+
sendBack({ type: 'fetchingAllMetadataRecordsSuccess', filesMetadata })
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return () => {}
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
export const fetchAllBinaryData = fromCallback<
|
|
69
|
+
EventObject,
|
|
70
|
+
typeof fetchAllFilesMachine
|
|
71
|
+
>(({ sendBack, input: { context } }) => {
|
|
72
|
+
const { filesMetadata, addresses } = context
|
|
73
|
+
|
|
74
|
+
const _fetchAllBinaryData = async () => {
|
|
75
|
+
if (!(await fs.promises.exists('/files'))) {
|
|
76
|
+
await fs.promises.mkdir('/files', { recursive: true })
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!(await fs.promises.exists('/files/html'))) {
|
|
80
|
+
await fs.promises.mkdir('/files/html', { recursive: true })
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!(await fs.promises.exists('/files/json'))) {
|
|
84
|
+
await fs.promises.mkdir('/files/json', { recursive: true })
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!(await fs.promises.exists('/files/images'))) {
|
|
88
|
+
await fs.promises.mkdir('/files/images', { recursive: true })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const appDb = getAppDb()
|
|
92
|
+
|
|
93
|
+
if (!appDb) {
|
|
94
|
+
logger('[fetchAll/actors] [fetchAllBinaryData] seedDb not available')
|
|
95
|
+
return []
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (const fileMetadata of filesMetadata) {
|
|
99
|
+
const json = JSON.parse(fileMetadata.decodedDataJson)
|
|
100
|
+
const transactionId = json[0].value.value
|
|
101
|
+
|
|
102
|
+
const excludedTransactionsQuery = await appDb
|
|
103
|
+
.select()
|
|
104
|
+
.from(appState)
|
|
105
|
+
.where(eq(appState.key, 'excludedTransactions'))
|
|
106
|
+
|
|
107
|
+
let excludedTransactions = new Set<string>()
|
|
108
|
+
|
|
109
|
+
if (excludedTransactionsQuery && excludedTransactionsQuery.length === 1) {
|
|
110
|
+
const valueString = excludedTransactionsQuery[0].value
|
|
111
|
+
if (valueString) {
|
|
112
|
+
const excludedTransactionsArray = JSON.parse(valueString)
|
|
113
|
+
excludedTransactions = new Set(excludedTransactionsArray)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (excludedTransactions.has(transactionId)) {
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const arweave = getArweave()
|
|
122
|
+
|
|
123
|
+
if (!arweave) {
|
|
124
|
+
logger('[fetchAll/actors] [fetchAllBinaryData] arweave not available')
|
|
125
|
+
return []
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
const res = await fetch(
|
|
130
|
+
`https://${ARWEAVE_HOST}/tx/${transactionId}/status`,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
if (res.status !== 200) {
|
|
134
|
+
logger(
|
|
135
|
+
`[fetchAll/actors] [fetchAllBinaryData] error fetching transaction data for ${transactionId}`,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
excludedTransactions.add(transactionId)
|
|
139
|
+
|
|
140
|
+
await writeAppState(
|
|
141
|
+
'excludedTransactions',
|
|
142
|
+
JSON.stringify(Array.from(excludedTransactions)),
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
logger(
|
|
146
|
+
'[fetchAll/actors] [fetchAllBinaryData] updated excludedTransactions:',
|
|
147
|
+
excludedTransactions,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
continue
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const dataString = await arweave.transactions
|
|
154
|
+
.getData(transactionId, {
|
|
155
|
+
decode: true,
|
|
156
|
+
string: true,
|
|
157
|
+
})
|
|
158
|
+
.catch((error) => {
|
|
159
|
+
logger(
|
|
160
|
+
`[fetchAll/actors] [fetchAllBinaryData] error fetching transaction data for ${transactionId}`,
|
|
161
|
+
error,
|
|
162
|
+
)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
const dataUint8Array = await arweave.transactions.getData(transactionId)
|
|
166
|
+
// let buffer
|
|
167
|
+
//
|
|
168
|
+
// if (dataUint8Array && dataUint8Array instanceof Uint8Array) {
|
|
169
|
+
// }
|
|
170
|
+
|
|
171
|
+
let contentType = identifyString(dataString)
|
|
172
|
+
if (
|
|
173
|
+
contentType !== 'json' &&
|
|
174
|
+
contentType !== 'base64' &&
|
|
175
|
+
contentType !== 'html'
|
|
176
|
+
) {
|
|
177
|
+
const possibleImageType = getImageDataType(dataString)
|
|
178
|
+
if (!possibleImageType) {
|
|
179
|
+
logger(
|
|
180
|
+
`[fetchAll/actors] [fetchAllBinaryData] transaction ${transactionId} data not in expected format: ${possibleImageType}`,
|
|
181
|
+
)
|
|
182
|
+
continue
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
contentType = possibleImageType
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (contentType === 'url') {
|
|
189
|
+
const url = dataString as string
|
|
190
|
+
const response = await fetch(url)
|
|
191
|
+
if (!response.ok) {
|
|
192
|
+
throw new Error(`Failed to fetch image: ${response.statusText}`)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Get the image as a Blob
|
|
196
|
+
const blob = await response.blob()
|
|
197
|
+
const buffer = await blob.arrayBuffer()
|
|
198
|
+
const bufferUint8Array = new Uint8Array(buffer)
|
|
199
|
+
|
|
200
|
+
// Extract the file extension from the URL
|
|
201
|
+
const extensionMatch = url.match(
|
|
202
|
+
/\.(jpg|jpeg|png|gif|bmp|webp|svg)$/i,
|
|
203
|
+
)
|
|
204
|
+
if (!extensionMatch) {
|
|
205
|
+
throw new Error(
|
|
206
|
+
'Unable to determine the file extension from the URL.',
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
const fileExtension = extensionMatch[0] // e.g., ".jpg"
|
|
210
|
+
|
|
211
|
+
// Set the file name (you can customize this)
|
|
212
|
+
// const fileNameFromUrl = `${transactionId}${fileExtension}`
|
|
213
|
+
|
|
214
|
+
await fs.promises.writeFile(
|
|
215
|
+
`/files/images/${transactionId}`,
|
|
216
|
+
bufferUint8Array,
|
|
217
|
+
{
|
|
218
|
+
encoding: 'binary',
|
|
219
|
+
},
|
|
220
|
+
)
|
|
221
|
+
|
|
222
|
+
continue
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const mimeType = getMimeType(dataString as string)
|
|
226
|
+
|
|
227
|
+
let fileName = transactionId
|
|
228
|
+
|
|
229
|
+
if (contentType === 'base64') {
|
|
230
|
+
if (mimeType) {
|
|
231
|
+
fileName += `.${mimeType}`
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// Remove the Base64 header if it exists (e.g., "data:image/png;base64,")
|
|
235
|
+
const base64Data = dataString.split(',').pop() || ''
|
|
236
|
+
|
|
237
|
+
// Decode the Base64 string to binary
|
|
238
|
+
const binaryString = atob(base64Data)
|
|
239
|
+
const length = binaryString.length
|
|
240
|
+
const binaryData = new Uint8Array(length)
|
|
241
|
+
|
|
242
|
+
for (let i = 0; i < length; i++) {
|
|
243
|
+
binaryData[i] = binaryString.charCodeAt(i)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
await fs.promises.writeFile(`/files/images/${fileName}`, binaryData, {
|
|
247
|
+
encoding: 'binary',
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
// if (dataUint8Array && dataUint8Array instanceof Uint8Array) {
|
|
251
|
+
// await fs.promises.writeFile(
|
|
252
|
+
// `/files/images/${fileName}`,
|
|
253
|
+
// dataUint8Array,
|
|
254
|
+
// )
|
|
255
|
+
// }
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (contentType === 'html') {
|
|
259
|
+
fileName += '.html'
|
|
260
|
+
await fs.promises.writeFile(`/files/html/${fileName}`, dataString)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (contentType === 'json') {
|
|
264
|
+
fileName += '.json'
|
|
265
|
+
await fs.promises.writeFile(`/files/json/${fileName}`, dataString)
|
|
266
|
+
}
|
|
267
|
+
} catch (error) {
|
|
268
|
+
logger(error)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
return []
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
_fetchAllBinaryData().then((binaryData) => {
|
|
276
|
+
sendBack({ type: 'fetchingAllBinaryDataSuccess', binaryData })
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
return () => {}
|
|
280
|
+
})
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { models as modelsTable, modelUids } from '@/shared/seedSchema'
|
|
3
|
+
import { eq } from 'drizzle-orm'
|
|
4
|
+
import { toSnakeCase } from '@/shared/helpers'
|
|
5
|
+
import { easClient, queryClient } from '@/browser/helpers'
|
|
6
|
+
import { getAppDb } from '@/browser/db/sqlWasmClient'
|
|
7
|
+
import { GLOBAL_ADDING_MODELS_TO_DB_SUCCESS } from '@/browser/services/internal/constants'
|
|
8
|
+
import { eventEmitter } from '@/eventBus'
|
|
9
|
+
import { FromCallbackInput, GlobalMachineContext } from '@/types'
|
|
10
|
+
import debug from 'debug'
|
|
11
|
+
import { GET_SCHEMAS } from '@/browser/item/queries'
|
|
12
|
+
|
|
13
|
+
const logger = debug('app:services:global:actors:addModelsToDb')
|
|
14
|
+
|
|
15
|
+
export const addModelsToDb = fromCallback<
|
|
16
|
+
EventObject,
|
|
17
|
+
FromCallbackInput<GlobalMachineContext>
|
|
18
|
+
>(({ sendBack, input: { context } }) => {
|
|
19
|
+
const { models } = context
|
|
20
|
+
|
|
21
|
+
const _addModelsToDb = async () => {
|
|
22
|
+
const appDb = getAppDb()
|
|
23
|
+
|
|
24
|
+
if (!models) {
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { models: SeedModels } = await import(
|
|
29
|
+
'@/shared/configs/seed.schema.config'
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
const allModels = {
|
|
33
|
+
...SeedModels,
|
|
34
|
+
...models,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let hasModelsInDb = false
|
|
38
|
+
const schemaDefsByModelName = new Map<
|
|
39
|
+
string,
|
|
40
|
+
{
|
|
41
|
+
dbId: number
|
|
42
|
+
schemaDef: string
|
|
43
|
+
}
|
|
44
|
+
>()
|
|
45
|
+
|
|
46
|
+
for (const [modelName, _] of Object.entries(allModels)) {
|
|
47
|
+
logger(
|
|
48
|
+
'[helpers/db] [addModelsToInternalDb] starting modelName:',
|
|
49
|
+
modelName,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
let foundModel
|
|
53
|
+
|
|
54
|
+
const foundModelsQuery = await appDb
|
|
55
|
+
.select()
|
|
56
|
+
.from(modelsTable)
|
|
57
|
+
.where(eq(modelsTable.name, modelName))
|
|
58
|
+
|
|
59
|
+
if (!foundModelsQuery || foundModelsQuery.length === 0) {
|
|
60
|
+
await appDb.insert(modelsTable).values({
|
|
61
|
+
name: modelName,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
logger('[global/actors] [addModelsToDb] inserted model:', modelName)
|
|
65
|
+
const foundModels = await appDb
|
|
66
|
+
.select({
|
|
67
|
+
id: modelsTable.id,
|
|
68
|
+
name: modelsTable.name,
|
|
69
|
+
uid: modelUids.uid,
|
|
70
|
+
})
|
|
71
|
+
.from(modelsTable)
|
|
72
|
+
.leftJoin(modelUids, eq(modelsTable.id, modelUids.modelId))
|
|
73
|
+
.where(eq(modelsTable.name, modelName))
|
|
74
|
+
.limit(1)
|
|
75
|
+
|
|
76
|
+
foundModel = foundModels[0]
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (foundModelsQuery && foundModelsQuery.length > 0) {
|
|
80
|
+
foundModel = foundModelsQuery[0]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!foundModel) {
|
|
84
|
+
hasModelsInDb = false
|
|
85
|
+
break
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
schemaDefsByModelName.set(modelName, {
|
|
89
|
+
dbId: foundModel.id,
|
|
90
|
+
schemaDef: `bytes32 ${toSnakeCase(modelName)}`,
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!hasModelsInDb) {
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const schemaDefs = Array.from(schemaDefsByModelName.values()).map(
|
|
99
|
+
({ schemaDef }) => schemaDef,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
const { schemas } = await queryClient.fetchQuery({
|
|
103
|
+
queryKey: [`getSchemasVersion`],
|
|
104
|
+
queryFn: async () =>
|
|
105
|
+
easClient.request(GET_SCHEMAS, {
|
|
106
|
+
where: {
|
|
107
|
+
schema: {
|
|
108
|
+
in: schemaDefs,
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
}),
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
if (!schemas || schemas.length === 0) {
|
|
115
|
+
throw new Error(`No schemas found`)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
for (const schema of schemas) {
|
|
119
|
+
const modelId = Array.from(schemaDefsByModelName.values()).find(
|
|
120
|
+
({ schemaDef }) => schemaDef === schema.schema,
|
|
121
|
+
)?.dbId
|
|
122
|
+
|
|
123
|
+
if (!modelId) {
|
|
124
|
+
throw new Error(`No modelId found for schema ${schema.schema}`)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
await appDb
|
|
128
|
+
.insert(modelUids)
|
|
129
|
+
.values({
|
|
130
|
+
modelId,
|
|
131
|
+
uid: schema.id,
|
|
132
|
+
})
|
|
133
|
+
.onConflictDoNothing()
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
_addModelsToDb().then((hasModelsInDb) => {
|
|
138
|
+
sendBack({ type: GLOBAL_ADDING_MODELS_TO_DB_SUCCESS })
|
|
139
|
+
if (hasModelsInDb) {
|
|
140
|
+
}
|
|
141
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
142
|
+
const service = context[`${modelName}Service`]
|
|
143
|
+
service.send({ type: 'modelsFound' })
|
|
144
|
+
}
|
|
145
|
+
eventEmitter.emit('syncDbWithEas')
|
|
146
|
+
return
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
return () => {}
|
|
150
|
+
})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getGlobalService } from '@/browser/services/global'
|
|
2
|
+
import { writeAppState } from '@/browser/db/write'
|
|
3
|
+
|
|
4
|
+
type SaveServiceEvent = {
|
|
5
|
+
modelName: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const saveServiceHandler = async (event: SaveServiceEvent) => {
|
|
9
|
+
const globalService = getGlobalService()
|
|
10
|
+
|
|
11
|
+
if (!globalService) {
|
|
12
|
+
return
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const { modelName } = event
|
|
16
|
+
|
|
17
|
+
const service = globalService.getSnapshot().context[`${modelName}Service`]
|
|
18
|
+
|
|
19
|
+
await writeAppState(
|
|
20
|
+
`snapshot__${modelName}`,
|
|
21
|
+
JSON.stringify(service.getPersistedSnapshot()),
|
|
22
|
+
)
|
|
23
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import Arweave from 'arweave'
|
|
2
|
+
|
|
3
|
+
export const getArweave = (): Arweave | undefined => {
|
|
4
|
+
if (
|
|
5
|
+
typeof window === 'undefined' ||
|
|
6
|
+
!Arweave ||
|
|
7
|
+
(!Object.keys(Arweave).includes('init') &&
|
|
8
|
+
!Object.keys(Arweave).includes('default'))
|
|
9
|
+
) {
|
|
10
|
+
return
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (process.env.NODE_ENV === 'production') {
|
|
14
|
+
return Arweave.init({
|
|
15
|
+
host: process.env.NEXT_PUBLIC_ARWEAVE_HOST,
|
|
16
|
+
protocol: 'https',
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// return Arweave.init({
|
|
21
|
+
// host : 'localhost',
|
|
22
|
+
// port : 1984,
|
|
23
|
+
// protocol : 'http',
|
|
24
|
+
// },)
|
|
25
|
+
|
|
26
|
+
if (Object.keys(Arweave).includes('default')) {
|
|
27
|
+
return Arweave.default.init({
|
|
28
|
+
host: 'permagate.io',
|
|
29
|
+
protocol: 'https',
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return Arweave.init({
|
|
34
|
+
host: 'permagate.io',
|
|
35
|
+
protocol: 'https',
|
|
36
|
+
})
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineConfig } from 'drizzle-kit'
|
|
2
|
+
import dotenv from 'dotenv'
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
|
|
6
|
+
dotenv.config()
|
|
7
|
+
|
|
8
|
+
let dotSeedDir = path.join(process.cwd(), '.seed')
|
|
9
|
+
|
|
10
|
+
if (process.env.IS_SEED_DEV) {
|
|
11
|
+
dotSeedDir = path.join(
|
|
12
|
+
process.cwd(),
|
|
13
|
+
'__tests__',
|
|
14
|
+
'__mocks__',
|
|
15
|
+
'project',
|
|
16
|
+
'.seed',
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default defineConfig({
|
|
21
|
+
schema: [`${dotSeedDir}/app/schema/*Schema.ts`],
|
|
22
|
+
dialect: 'sqlite',
|
|
23
|
+
out: `${dotSeedDir}/app/db`,
|
|
24
|
+
dbCredentials: {
|
|
25
|
+
url: `${dotSeedDir}/app/db/app_db.sqlite3`,
|
|
26
|
+
},
|
|
27
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineConfig } from 'drizzle-kit'
|
|
2
|
+
import dotenv from 'dotenv'
|
|
3
|
+
import process from 'node:process'
|
|
4
|
+
import path from 'path'
|
|
5
|
+
|
|
6
|
+
dotenv.config()
|
|
7
|
+
|
|
8
|
+
let sdkRoot = './node_modules/@seedprotocol/sdk'
|
|
9
|
+
|
|
10
|
+
if (process.env.IS_SEED_DEV) {
|
|
11
|
+
sdkRoot = './src'
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let dotSeedDir = path.join(process.cwd(), '.seed')
|
|
15
|
+
|
|
16
|
+
if (process.env.IS_SEED_DEV) {
|
|
17
|
+
dotSeedDir = path.join(
|
|
18
|
+
process.cwd(),
|
|
19
|
+
'__tests__',
|
|
20
|
+
'__mocks__',
|
|
21
|
+
'project',
|
|
22
|
+
'.seed',
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default defineConfig({
|
|
27
|
+
schema: `${sdkRoot}/browser/db/seedSchema/*Schema.ts`,
|
|
28
|
+
dialect: 'sqlite',
|
|
29
|
+
out: `${dotSeedDir}/seed/db`,
|
|
30
|
+
dbCredentials: {
|
|
31
|
+
url: `${dotSeedDir}/seed/db/seed_db.sqlite3`,
|
|
32
|
+
},
|
|
33
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Endpoints } from './index'
|
|
2
|
+
import { SqliteRemoteResult } from 'drizzle-orm/sqlite-proxy'
|
|
3
|
+
|
|
4
|
+
export type SeedInitBrowserProps = {
|
|
5
|
+
endpoints: Endpoints
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface SeedInitBrowser {
|
|
9
|
+
(props: SeedInitBrowserProps): Promise<void>
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type DbQueryResult = SqliteRemoteResult<SqliteWasmResult>
|
|
13
|
+
|
|
14
|
+
export type ResultObject = {
|
|
15
|
+
[key: string]: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type SqliteWasmResult = {
|
|
19
|
+
type: string | null
|
|
20
|
+
row: string[] | null
|
|
21
|
+
rowNumber: number | null
|
|
22
|
+
columnNames: string[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type SqliteWasmCallback = (result: SqliteWasmResult) => void
|
|
26
|
+
|
|
27
|
+
export type ReturnObj = {
|
|
28
|
+
database: string
|
|
29
|
+
[key: string]: string | number | null | undefined | string[]
|
|
30
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { DbServiceContext, FromCallbackInput } from '@/types'
|
|
3
|
+
import {
|
|
4
|
+
BROWSER_FS_TOP_DIR,
|
|
5
|
+
DB_CHECK_STATUS_EXISTS,
|
|
6
|
+
DB_CHECK_STATUS_UPDATE_PATHS,
|
|
7
|
+
} from '@/browser/services/internal/constants'
|
|
8
|
+
import debug from 'debug'
|
|
9
|
+
|
|
10
|
+
const logger = debug('app:services:db:actors:checkStatus')
|
|
11
|
+
|
|
12
|
+
export const checkStatus = fromCallback<
|
|
13
|
+
EventObject,
|
|
14
|
+
FromCallbackInput<DbServiceContext>
|
|
15
|
+
>(({ sendBack, input: { context } }) => {
|
|
16
|
+
const { dbName } = context
|
|
17
|
+
|
|
18
|
+
logger('[db/actors] checkStatus context', context)
|
|
19
|
+
const pathToDir = `${BROWSER_FS_TOP_DIR}`
|
|
20
|
+
const pathToDbDir = `${pathToDir}/db`
|
|
21
|
+
const pathToDb = `${pathToDbDir}/${dbName}.sqlite3`
|
|
22
|
+
|
|
23
|
+
sendBack({
|
|
24
|
+
type: DB_CHECK_STATUS_UPDATE_PATHS,
|
|
25
|
+
pathToDb,
|
|
26
|
+
pathToDir,
|
|
27
|
+
pathToDbDir,
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const _checkStatus = async (): Promise<void> => {
|
|
31
|
+
// logger('[db/actors] _checkStatus pathToDb', pathToDb)
|
|
32
|
+
// const exists = await fs.promises.exists(pathToJournal)
|
|
33
|
+
// if (exists) {
|
|
34
|
+
// sendBack({
|
|
35
|
+
// type: DB_CHECK_STATUS_EXISTS,
|
|
36
|
+
// })
|
|
37
|
+
// return
|
|
38
|
+
// }
|
|
39
|
+
//
|
|
40
|
+
// return new Promise((resolve) => {
|
|
41
|
+
// sendBack({ type: DB_CHECK_STATUS_DOES_NOT_EXIST })
|
|
42
|
+
//
|
|
43
|
+
// })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
_checkStatus().then(() => {
|
|
47
|
+
sendBack({ type: DB_CHECK_STATUS_EXISTS })
|
|
48
|
+
return
|
|
49
|
+
})
|
|
50
|
+
})
|