@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,33 @@
|
|
|
1
|
+
import { dayjs } from '@/shared/helpers/index'
|
|
2
|
+
|
|
3
|
+
type Log = (message: string, data?: unknown) => void
|
|
4
|
+
|
|
5
|
+
type LoggerType = (tag: string) => {
|
|
6
|
+
log: Log
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const tagsToLog = [
|
|
10
|
+
// 'InternalMachine',
|
|
11
|
+
'internal/actors',
|
|
12
|
+
'db/actors',
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
const Logger: LoggerType = (tag: string) => {
|
|
16
|
+
const _tag = tag || 'Logger'
|
|
17
|
+
|
|
18
|
+
const log: Log = (message, data = undefined): void => {
|
|
19
|
+
if (process.env.IS_SEED_DEV && tagsToLog.includes(_tag)) {
|
|
20
|
+
const timestamp = dayjs().format('YYYY-MM-DD HH:mm:ss')
|
|
21
|
+
console.log(
|
|
22
|
+
`[${timestamp}] [${_tag}] ${dayjs().format('YYYY-MM-DD HH:mm:ss')} ${message}`,
|
|
23
|
+
data || '',
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
log,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default Logger
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ActorRefFrom, assign, setup } from 'xstate'
|
|
2
|
+
import { downloadMachine } from '@/browser/schema/file/download'
|
|
3
|
+
import { uploadMachine } from '@/browser/schema/file/upload'
|
|
4
|
+
import { MachineIds } from '@/browser/services/internal/constants'
|
|
5
|
+
|
|
6
|
+
type FileSystemMachineContext = {
|
|
7
|
+
addresses?: string[]
|
|
8
|
+
downloads: ActorRefFrom<typeof downloadMachine>[]
|
|
9
|
+
uploads: ActorRefFrom<typeof uploadMachine>[]
|
|
10
|
+
files?: any[]
|
|
11
|
+
filesMetadata?: any[]
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const fileSystemMachine = setup({
|
|
15
|
+
types: {
|
|
16
|
+
context: {} as FileSystemMachineContext,
|
|
17
|
+
},
|
|
18
|
+
actors: {},
|
|
19
|
+
}).createMachine({
|
|
20
|
+
id: MachineIds.FILE_SYSTEM,
|
|
21
|
+
initial: 'idle',
|
|
22
|
+
context: ({ input }) =>
|
|
23
|
+
({
|
|
24
|
+
...input,
|
|
25
|
+
fetchRequests: [],
|
|
26
|
+
downloads: [],
|
|
27
|
+
uploads: [],
|
|
28
|
+
files: [],
|
|
29
|
+
}) as FileSystemMachineContext,
|
|
30
|
+
on: {
|
|
31
|
+
updateFilesMetadata: {
|
|
32
|
+
actions: assign({
|
|
33
|
+
filesMetadata: ({ event }) => event.filesMetadata,
|
|
34
|
+
}),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
states: {
|
|
38
|
+
idle: {
|
|
39
|
+
on: {
|
|
40
|
+
download: {
|
|
41
|
+
target: 'createDownload',
|
|
42
|
+
},
|
|
43
|
+
upload: {
|
|
44
|
+
target: 'createUpload',
|
|
45
|
+
},
|
|
46
|
+
fetch: {
|
|
47
|
+
target: 'createFetchFiles',
|
|
48
|
+
actions: assign({
|
|
49
|
+
addresses: ({ event }) => event.addresses,
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Endpoints, Environment, ModelClassType } from './index'
|
|
2
|
+
import { ActorRefFrom } from 'xstate'
|
|
3
|
+
import { fileSystemMachine } from '@/browser/schema/file/machine'
|
|
4
|
+
import { PublishRequestData } from './seedProtocol'
|
|
5
|
+
|
|
6
|
+
export type DbServiceContext = {
|
|
7
|
+
dbName: string
|
|
8
|
+
dbId?: string
|
|
9
|
+
dirName: string
|
|
10
|
+
drizzleDb?: any
|
|
11
|
+
pathToDb?: string
|
|
12
|
+
pathToDir?: string
|
|
13
|
+
pathToDbDir?: string
|
|
14
|
+
hasFiles?: boolean
|
|
15
|
+
error?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type InternalMachineContext = {
|
|
19
|
+
error?: string
|
|
20
|
+
endpoints: Endpoints
|
|
21
|
+
addresses: string[]
|
|
22
|
+
environment: string
|
|
23
|
+
hasFiles: boolean
|
|
24
|
+
seedDbService: any
|
|
25
|
+
appDbService: any
|
|
26
|
+
sdkDbService: any
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type GlobalMachineContext = {
|
|
30
|
+
isInitialized?: boolean
|
|
31
|
+
addedModelRecordsToDb?: boolean
|
|
32
|
+
environment?: Environment
|
|
33
|
+
endpoints?: Endpoints
|
|
34
|
+
addresses?: string[]
|
|
35
|
+
models?: { [key: string]: ModelClassType }
|
|
36
|
+
internalService?: ActorRefFrom<any>
|
|
37
|
+
fileSystemService?: ActorRefFrom<typeof fileSystemMachine>
|
|
38
|
+
publishItemService?: ActorRefFrom<any>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type PublishMachineContext = PublishRequestData & {
|
|
42
|
+
status: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type GetSchemaForModelEvent = {
|
|
46
|
+
type: 'getSchemaForModel'
|
|
47
|
+
modelName: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type HydrateExistingItemEvent = {
|
|
51
|
+
type: 'hydrateExistingItem'
|
|
52
|
+
existingItem: any
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type FromCallbackInput<T, P = undefined> = {
|
|
56
|
+
context: T
|
|
57
|
+
event?: P
|
|
58
|
+
}
|
|
@@ -0,0 +1,288 @@
|
|
|
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 '@/browser/services/internal/constants'
|
|
7
|
+
import { getSqliteWasmClient, setAppDb } from '@/browser/db/sqlWasmClient'
|
|
8
|
+
import { drizzle } from 'drizzle-orm/sqlite-proxy'
|
|
9
|
+
import { sql } from 'drizzle-orm'
|
|
10
|
+
import { fs } from '@zenfs/core'
|
|
11
|
+
import debug from 'debug'
|
|
12
|
+
|
|
13
|
+
const logger = debug('app:services:db:actors:migrate')
|
|
14
|
+
|
|
15
|
+
export const dbExec = async (dbId, params, sql, dbName, retries = 2) => {
|
|
16
|
+
const rowsToReturnRaw: SqliteWasmResult[] = []
|
|
17
|
+
const rowsValues: string[][] = []
|
|
18
|
+
|
|
19
|
+
const sqliteWasmClient = await getSqliteWasmClient()
|
|
20
|
+
|
|
21
|
+
// For a single exec command, the callback potentially gets called several times -- once for each row.
|
|
22
|
+
// So we need to collect all rows into a final array to return (execResult).
|
|
23
|
+
const rowsToReturn = await new Promise((resolve, reject) => {
|
|
24
|
+
sqliteWasmClient('exec', {
|
|
25
|
+
dbId,
|
|
26
|
+
sql,
|
|
27
|
+
bind: params,
|
|
28
|
+
callback: (result) => {
|
|
29
|
+
// Checks if this is the final callback of the query
|
|
30
|
+
if (!result || !result.row || !result.rowNumber) {
|
|
31
|
+
const returnResult = []
|
|
32
|
+
// Before returning the array, we process it to match the expected output format
|
|
33
|
+
// const rowsToReturnProcessed = rowsToReturnRaw.reduce((acc, curr) => {
|
|
34
|
+
// if (
|
|
35
|
+
// Array.isArray(curr.row) &&
|
|
36
|
+
// curr.row?.length > 0 &&
|
|
37
|
+
// curr.columnNames.length > 0
|
|
38
|
+
// ) {
|
|
39
|
+
// const returnObj: ReturnObj = {
|
|
40
|
+
// database: dbName,
|
|
41
|
+
// }
|
|
42
|
+
//
|
|
43
|
+
// const values = []
|
|
44
|
+
//
|
|
45
|
+
// curr.columnNames.forEach((colName, index: number) => {
|
|
46
|
+
// if (curr.row && curr.row[index]) {
|
|
47
|
+
// returnObj[colName] = curr.row[index]
|
|
48
|
+
// values.push(curr.row[index])
|
|
49
|
+
// }
|
|
50
|
+
// })
|
|
51
|
+
// // rowsValueStrings.push(`(${values.join(', ')})`)
|
|
52
|
+
// acc.push(returnObj)
|
|
53
|
+
// }
|
|
54
|
+
// return acc
|
|
55
|
+
// }, [] as string[])
|
|
56
|
+
for (const currRow of rowsToReturnRaw) {
|
|
57
|
+
// const values: string[] = []
|
|
58
|
+
// currRow.columnNames.forEach((colName, index: number) => {
|
|
59
|
+
// if (currRow.row) {
|
|
60
|
+
// values.push(currRow.row[index])
|
|
61
|
+
// }
|
|
62
|
+
// })
|
|
63
|
+
// logger(`[db/actors] [dbExec] currRow`, currRow)
|
|
64
|
+
returnResult.push(currRow.row)
|
|
65
|
+
}
|
|
66
|
+
resolve(returnResult)
|
|
67
|
+
} else {
|
|
68
|
+
// If not the final response, add this row to the return array
|
|
69
|
+
rowsToReturnRaw.push(result)
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
}).catch(async (error) => {
|
|
73
|
+
reject(error)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// logger(`[db/actors] [dbExec] rowsToReturn`, rowsToReturn)
|
|
78
|
+
// logger(`[db/actors] [dbExec] rowsValues`, rowsValues)
|
|
79
|
+
|
|
80
|
+
return rowsToReturn || []
|
|
81
|
+
}
|
|
82
|
+
export const migrate = fromCallback<
|
|
83
|
+
EventObject,
|
|
84
|
+
FromCallbackInput<DbServiceContext>
|
|
85
|
+
>(({ sendBack, input: { context } }) => {
|
|
86
|
+
const { pathToDbDir, dbId, dbName } = context
|
|
87
|
+
|
|
88
|
+
logger('[db/actors] migrate context', context)
|
|
89
|
+
|
|
90
|
+
const schemaGlobString = `${BROWSER_FS_TOP_DIR}/schema/*`
|
|
91
|
+
|
|
92
|
+
let journalExists = false
|
|
93
|
+
|
|
94
|
+
// const _initFs = async (): Promise<void> => {
|
|
95
|
+
// const handle = await navigator.storage.getDirectory()
|
|
96
|
+
// // await configure({ backend: WebAccess, handle })
|
|
97
|
+
// await configureSingle({
|
|
98
|
+
// backend: WebAccess,
|
|
99
|
+
// handle,
|
|
100
|
+
// })
|
|
101
|
+
// }
|
|
102
|
+
//
|
|
103
|
+
// _initFs()
|
|
104
|
+
|
|
105
|
+
const _checkForFiles = async (): Promise<void> => {
|
|
106
|
+
const { fs } = await import('@zenfs/core')
|
|
107
|
+
|
|
108
|
+
journalExists = await fs.promises.exists(
|
|
109
|
+
`/${pathToDbDir}/meta/_journal.json`,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
if (!journalExists) {
|
|
113
|
+
window.location.reload()
|
|
114
|
+
// setTimeout(() => {
|
|
115
|
+
// _checkForFiles().then(() => {
|
|
116
|
+
// return
|
|
117
|
+
// })
|
|
118
|
+
// }, 500)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const _migrate = async (): Promise<void> => {
|
|
123
|
+
const drizzleDb = drizzle(
|
|
124
|
+
async (sql, params, method) => {
|
|
125
|
+
try {
|
|
126
|
+
// logger(
|
|
127
|
+
// `executing sql on ${dbName} with id: ${dbId} and method: ${method}`,
|
|
128
|
+
// sql,
|
|
129
|
+
// )
|
|
130
|
+
|
|
131
|
+
const finalResult = await dbExec(dbId, params, sql, dbName)
|
|
132
|
+
|
|
133
|
+
// logger(`finalResult with method: ${method}`, finalResult)
|
|
134
|
+
// Drizzle always waits for {rows: string[][]} or {rows: string[]} for the return value.
|
|
135
|
+
|
|
136
|
+
// When the method is get, you should return a value as {rows: string[]}.
|
|
137
|
+
// Otherwise, you should return {rows: string[][]}.
|
|
138
|
+
|
|
139
|
+
return { rows: finalResult }
|
|
140
|
+
} catch (e: any) {
|
|
141
|
+
console.error('Error from sqlite proxy server: ', JSON.stringify(e))
|
|
142
|
+
return { rows: [] }
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
schema: schemaGlobString,
|
|
147
|
+
// logger: true,
|
|
148
|
+
},
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
const { migrate: drizzleMigrate } = await import(
|
|
152
|
+
'drizzle-orm/sqlite-proxy/migrator'
|
|
153
|
+
)
|
|
154
|
+
const { readMigrationFiles } = await import('drizzle-orm/migrator')
|
|
155
|
+
|
|
156
|
+
try {
|
|
157
|
+
const migrations = readMigrationFiles({
|
|
158
|
+
migrationsFolder: pathToDbDir,
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
if (migrations.length > 0) {
|
|
162
|
+
const incomingMigrationHashes = migrations.map(
|
|
163
|
+
(migration) => migration.hash,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
let existingMigrationHashes
|
|
167
|
+
let rows = []
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
const query = await drizzleDb.run(
|
|
171
|
+
sql.raw(
|
|
172
|
+
`SELECT hash, created_at
|
|
173
|
+
FROM main.__drizzle_migrations;`,
|
|
174
|
+
),
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
rows = query.rows
|
|
178
|
+
} catch (e) {
|
|
179
|
+
rows = []
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (rows && rows.length > 0) {
|
|
183
|
+
existingMigrationHashes = rows.map((row) => row[0])
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (existingMigrationHashes) {
|
|
187
|
+
let shouldRebuildDb = false
|
|
188
|
+
for (const existingHash of existingMigrationHashes) {
|
|
189
|
+
if (!incomingMigrationHashes.includes(existingHash)) {
|
|
190
|
+
shouldRebuildDb = true
|
|
191
|
+
break
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (shouldRebuildDb) {
|
|
195
|
+
await fs.promises.unlink(`${pathToDbDir}/${dbName}.sqlite3`)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
await drizzleMigrate(
|
|
201
|
+
drizzleDb,
|
|
202
|
+
async (queriesToRun) => {
|
|
203
|
+
// logger('queriesToRun', queriesToRun)
|
|
204
|
+
for (const query of queriesToRun) {
|
|
205
|
+
// logger('query', query)
|
|
206
|
+
await drizzleDb.run(sql.raw(query))
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
migrationsFolder: pathToDbDir,
|
|
211
|
+
},
|
|
212
|
+
)
|
|
213
|
+
} catch (error) {
|
|
214
|
+
console.error('Error migrating database: ', error)
|
|
215
|
+
window.location.reload()
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
setAppDb(drizzleDb)
|
|
219
|
+
// const createTempTableQuery = await appDb.run(
|
|
220
|
+
// sql.raw(
|
|
221
|
+
// `CREATE TEMP TABLE IF NOT EXISTS temp_last_inserted_id (id INTEGER, table TEXT);`,
|
|
222
|
+
// ),
|
|
223
|
+
// )
|
|
224
|
+
//
|
|
225
|
+
// logger(
|
|
226
|
+
// '[db/actors] [migrate] createTempTableQuery',
|
|
227
|
+
// createTempTableQuery,
|
|
228
|
+
// )
|
|
229
|
+
|
|
230
|
+
// const triggersQuery = await appDb.run(
|
|
231
|
+
// sql.raw(
|
|
232
|
+
// `SELECT name
|
|
233
|
+
// FROM main.sqlite_master
|
|
234
|
+
// WHERE type = 'trigger';`,
|
|
235
|
+
// ),
|
|
236
|
+
// )
|
|
237
|
+
//
|
|
238
|
+
// logger('[db/actors] [migrate] triggersQuery', triggersQuery)
|
|
239
|
+
//
|
|
240
|
+
// const triggers = triggersQuery.rows.map((row) => row[0])
|
|
241
|
+
//
|
|
242
|
+
// const tablesQuery = await appDb.run(
|
|
243
|
+
// sql.raw(
|
|
244
|
+
// `SELECT name
|
|
245
|
+
// FROM main.sqlite_master
|
|
246
|
+
// WHERE type = 'table';`,
|
|
247
|
+
// ),
|
|
248
|
+
// )
|
|
249
|
+
//
|
|
250
|
+
// logger('[db/actors] [migrate] tablesQuery', tablesQuery)
|
|
251
|
+
//
|
|
252
|
+
// const tableNames = tablesQuery.rows.map((row) => row[0])
|
|
253
|
+
// logger('[db/actors] [migrate] tableNames', tableNames)
|
|
254
|
+
// for (const tableName of tableNames) {
|
|
255
|
+
// const triggerName = `after_insert_${tableName}`
|
|
256
|
+
// if (triggers.includes(triggerName)) {
|
|
257
|
+
// continue
|
|
258
|
+
// }
|
|
259
|
+
// const createTriggerQuery = await appDb.run(
|
|
260
|
+
// sql.raw(
|
|
261
|
+
// `CREATE TRIGGER after_insert_${tableName}
|
|
262
|
+
// AFTER INSERT ON ${tableName}
|
|
263
|
+
// BEGIN
|
|
264
|
+
// DELETE FROM temp_last_inserted_id;
|
|
265
|
+
// INSERT INTO temp_last_inserted_id (id) VALUES (new.id);
|
|
266
|
+
// END;`,
|
|
267
|
+
// ),
|
|
268
|
+
// )
|
|
269
|
+
//
|
|
270
|
+
// logger(
|
|
271
|
+
// '[db/actors] [migrate] createTriggerQuery',
|
|
272
|
+
// createTriggerQuery,
|
|
273
|
+
// )
|
|
274
|
+
// }
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
_checkForFiles()
|
|
278
|
+
.then(() => {
|
|
279
|
+
if (journalExists) {
|
|
280
|
+
return _migrate()
|
|
281
|
+
}
|
|
282
|
+
})
|
|
283
|
+
.then(() => {
|
|
284
|
+
sendBack({ type: DB_MIGRATING_SUCCESS, dbName })
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
return () => {}
|
|
288
|
+
})
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { PropertyStates, PropertyValue } from './property'
|
|
2
|
+
import { Actor } from 'xstate'
|
|
3
|
+
import { Static } from '@sinclair/typebox'
|
|
4
|
+
import { IModelClass, Item, TModelSchema } from '@/browser/schema'
|
|
5
|
+
|
|
6
|
+
export type ModelDefinitions = {
|
|
7
|
+
[modelName: string]: ModelClassType
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type ModelStatus = (propName: string) => keyof PropertyStates
|
|
11
|
+
|
|
12
|
+
export type ExcludedKeys = 'states' | 'status'
|
|
13
|
+
|
|
14
|
+
type ExcludeKeys<T, K> = {
|
|
15
|
+
[P in keyof T as Exclude<P, K>]: T[P]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type ModelClassType = {
|
|
19
|
+
originalConstructor: () => void
|
|
20
|
+
schema: ModelSchema
|
|
21
|
+
schemaUid?: string
|
|
22
|
+
create: <T>(values: ModelValues<T>) => Promise<Item<T>>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type ModelValues<T> = Item<T> & {
|
|
26
|
+
schema: ModelSchema
|
|
27
|
+
ModelClass?: ModelClassType
|
|
28
|
+
[key: string & keyof T]: PropertyValue
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type StatesMap<T> = Map<string, Actor<T>>
|
|
32
|
+
|
|
33
|
+
// export type ModelSchema = {
|
|
34
|
+
// [key: string]: TObject
|
|
35
|
+
// }
|
|
36
|
+
|
|
37
|
+
export type ModelSchema = Partial<Static<typeof TModelSchema>>
|
|
38
|
+
|
|
39
|
+
export type ModelConstructor = <
|
|
40
|
+
T extends { new (...args: any[]): IModelClass },
|
|
41
|
+
>(
|
|
42
|
+
constructor: T,
|
|
43
|
+
) => T & IModelClass
|
|
44
|
+
|
|
45
|
+
// export type ModelConstructor = Static<TModelConstructor>
|
|
46
|
+
|
|
47
|
+
export interface ModelProperty {
|
|
48
|
+
propertyLocalId?: string
|
|
49
|
+
name: string
|
|
50
|
+
dataType:
|
|
51
|
+
| 'string'
|
|
52
|
+
| 'bytes32'
|
|
53
|
+
| 'uint8'
|
|
54
|
+
| 'uint256'
|
|
55
|
+
| 'bool'
|
|
56
|
+
| 'address'
|
|
57
|
+
| 'bytes'
|
|
58
|
+
| 'int8'
|
|
59
|
+
| 'int256'
|
|
60
|
+
| 'int'
|
|
61
|
+
| 'bytes32[]'
|
|
62
|
+
modelSchemaUids: string[]
|
|
63
|
+
modelLocalId?: string
|
|
64
|
+
schemaName?: string
|
|
65
|
+
schemaUid: string
|
|
66
|
+
schemaDefinition: string
|
|
67
|
+
relatedModelSchemaUid?: string
|
|
68
|
+
relatedModelLocalId?: string
|
|
69
|
+
createdAt?: number
|
|
70
|
+
updatedAt?: number
|
|
71
|
+
}
|
|
@@ -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,40 @@
|
|
|
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
|
+
'project',
|
|
23
|
+
'.seed',
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let schemaDir = `${sdkRoot}/dist/shared/seedSchema/*.ts`
|
|
28
|
+
|
|
29
|
+
if (process.env.IS_SEED_DEV) {
|
|
30
|
+
schemaDir = `${sdkRoot}/shared/seedSchema/*.ts`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default defineConfig({
|
|
34
|
+
schema: schemaDir,
|
|
35
|
+
dialect: 'sqlite',
|
|
36
|
+
out: `${dotSeedDir}/db`,
|
|
37
|
+
dbCredentials: {
|
|
38
|
+
url: `${dotSeedDir}/db/app_db.sqlite3`,
|
|
39
|
+
},
|
|
40
|
+
}) as DrizzleConfig
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { SqliteConnectionManager } from '@/browser/services/db'
|
|
3
|
+
import { getSqliteWasmClient, setManager } from '@/browser/db/sqlWasmClient'
|
|
4
|
+
|
|
5
|
+
export const prepareDb = fromCallback<EventObject>(({ sendBack }) => {
|
|
6
|
+
let sqliteWasmClient
|
|
7
|
+
const _prepareDb = async (): Promise<void> => {
|
|
8
|
+
if (typeof window === 'undefined') {
|
|
9
|
+
return
|
|
10
|
+
}
|
|
11
|
+
sqliteWasmClient = await getSqliteWasmClient()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const interval = setInterval(() => {
|
|
15
|
+
// TODO: Add a timeout
|
|
16
|
+
// TODO: Add a cancel token to the promise so we can prevent more loops starting while we're checking the successful outcome
|
|
17
|
+
if (sqliteWasmClient) {
|
|
18
|
+
clearInterval(interval)
|
|
19
|
+
const manager = new SqliteConnectionManager(sqliteWasmClient)
|
|
20
|
+
setManager(manager)
|
|
21
|
+
sendBack({ type: 'prepareDbSuccess', manager })
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
_prepareDb().then(() => {
|
|
25
|
+
return
|
|
26
|
+
})
|
|
27
|
+
}, 200)
|
|
28
|
+
|
|
29
|
+
return () => {
|
|
30
|
+
if (interval) {
|
|
31
|
+
clearInterval(interval)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
})
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { EventObject, fromCallback } from 'xstate'
|
|
2
|
+
import { FromCallbackInput, PublishMachineContext } from '@/types'
|
|
3
|
+
import { Item } from '@/browser/item'
|
|
4
|
+
import { models as modelsTable, modelUids } from '@/shared/seedSchema'
|
|
5
|
+
import { getAppDb } from '@/browser'
|
|
6
|
+
import { eq } from 'drizzle-orm'
|
|
7
|
+
import { getModelSchemas } from '@/browser/db/read/getModelSchemas'
|
|
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 Item.find({ seedLocalId: localId })
|
|
17
|
+
|
|
18
|
+
if (!item) {
|
|
19
|
+
return false
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const appDb = 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
|
+
})
|