@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
package/dist/src/db.ts
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import {
|
|
2
|
+
appState,
|
|
3
|
+
models as modelsTable,
|
|
4
|
+
NewModelRecord,
|
|
5
|
+
NewPropertyRecord,
|
|
6
|
+
properties,
|
|
7
|
+
} from 'src/shared/seedSchema'
|
|
8
|
+
import { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy'
|
|
9
|
+
import { DbQueryResult, ModelDefinitions, ResultObject } from '@/types'
|
|
10
|
+
import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'
|
|
11
|
+
import { SQLiteTableWithColumns } from 'drizzle-orm/sqlite-core'
|
|
12
|
+
import { and, eq, isNull, SQL } from 'drizzle-orm'
|
|
13
|
+
|
|
14
|
+
import { getAppDb } from '@/browser/db/sqlWasmClient'
|
|
15
|
+
|
|
16
|
+
export const escapeSqliteString = (value: string): string => {
|
|
17
|
+
if (typeof value !== 'string') {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`Value must be a string, instead got: ${JSON.stringify(value)}`,
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
return value.replace(/'/g, "''")
|
|
23
|
+
}
|
|
24
|
+
export const getObjectForRow = (row: any): ResultObject => {
|
|
25
|
+
const obj: ResultObject = {}
|
|
26
|
+
|
|
27
|
+
row.columnNames.forEach((colName, index) => {
|
|
28
|
+
const value = row.row[index]
|
|
29
|
+
if (typeof value !== 'string') {
|
|
30
|
+
obj[colName] = row.row[index]
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Try to parse the value as JSON
|
|
35
|
+
try {
|
|
36
|
+
obj[colName] = JSON.parse(value)
|
|
37
|
+
} catch (e) {
|
|
38
|
+
// If it fails, just set the value as a string
|
|
39
|
+
obj[colName] = value
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
return obj
|
|
44
|
+
}
|
|
45
|
+
export const getSqlResultObject = (
|
|
46
|
+
queryResult: DbQueryResult,
|
|
47
|
+
): ResultObject | ResultObject[] | undefined => {
|
|
48
|
+
if (!queryResult || !queryResult.rows || queryResult.rows.length === 0) {
|
|
49
|
+
return
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let obj: ResultObject | ResultObject[] | undefined
|
|
53
|
+
|
|
54
|
+
if (queryResult.rows.length === 1) {
|
|
55
|
+
obj = getObjectForRow(queryResult.rows[0])
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (queryResult.rows.length > 1) {
|
|
59
|
+
obj = queryResult.rows.reduce((acc, row) => {
|
|
60
|
+
const rowObj = getObjectForRow(row)
|
|
61
|
+
|
|
62
|
+
acc.push(rowObj)
|
|
63
|
+
return acc
|
|
64
|
+
}, [] as ResultObject[])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return obj
|
|
68
|
+
}
|
|
69
|
+
export const createOrUpdate = async <T>(
|
|
70
|
+
db: BetterSQLite3Database | SqliteRemoteDatabase,
|
|
71
|
+
table: SQLiteTableWithColumns<any>,
|
|
72
|
+
values: Partial<Record<keyof T, T[keyof T]>>,
|
|
73
|
+
) => {
|
|
74
|
+
const startTime = Date.now()
|
|
75
|
+
|
|
76
|
+
const valueFilters: SQL[] = []
|
|
77
|
+
|
|
78
|
+
const propertiesToExcludeFromDb = ['ref']
|
|
79
|
+
|
|
80
|
+
const safeValues = Object.keys(values).reduce(
|
|
81
|
+
(acc, key) => {
|
|
82
|
+
if (!propertiesToExcludeFromDb.includes(key)) {
|
|
83
|
+
acc[key] = values[key as string & keyof T]
|
|
84
|
+
}
|
|
85
|
+
return acc
|
|
86
|
+
},
|
|
87
|
+
{} as Record<string, unknown>,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
for (const [key, value] of Object.entries(safeValues)) {
|
|
91
|
+
let finalValue = value
|
|
92
|
+
if (key === 'TObject') {
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
if (typeof value === 'object') {
|
|
96
|
+
finalValue = JSON.stringify(value)
|
|
97
|
+
}
|
|
98
|
+
const column = table[key]
|
|
99
|
+
if (!column) {
|
|
100
|
+
throw new Error(`Column not found for ${key}`)
|
|
101
|
+
}
|
|
102
|
+
if (typeof finalValue === 'undefined') {
|
|
103
|
+
finalValue = null
|
|
104
|
+
}
|
|
105
|
+
if (finalValue === null) {
|
|
106
|
+
valueFilters.push(isNull(table[key]))
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
valueFilters.push(eq(table[key], finalValue))
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const doneWithFilters = Date.now()
|
|
113
|
+
|
|
114
|
+
// console.log('valueFilters:', valueFilters)
|
|
115
|
+
|
|
116
|
+
// for ( const filter of valueFilters ) {
|
|
117
|
+
// console.log('filter:', Object.keys(filter))
|
|
118
|
+
// }
|
|
119
|
+
|
|
120
|
+
// Build a query to find the record based on properties
|
|
121
|
+
const existingRecords = await db
|
|
122
|
+
.select()
|
|
123
|
+
.from(table)
|
|
124
|
+
.where(and(...valueFilters))
|
|
125
|
+
|
|
126
|
+
const doneWithExistingRecords = Date.now()
|
|
127
|
+
|
|
128
|
+
if (existingRecords.length > 1) {
|
|
129
|
+
throw new Error('Multiple records found')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (existingRecords.length > 0) {
|
|
133
|
+
// If record exists, update it
|
|
134
|
+
await db
|
|
135
|
+
.update(table)
|
|
136
|
+
.set(safeValues)
|
|
137
|
+
.where(and(...valueFilters))
|
|
138
|
+
console.log('updatedRecord:', {
|
|
139
|
+
...existingRecords[0],
|
|
140
|
+
...safeValues,
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const doneWithUpdate = Date.now()
|
|
144
|
+
|
|
145
|
+
console.log(
|
|
146
|
+
'[helpers/db] [createOrUpdate] filters duration:',
|
|
147
|
+
doneWithFilters - startTime,
|
|
148
|
+
)
|
|
149
|
+
console.log(
|
|
150
|
+
'[helpers/db] [createOrUpdate] existingRecords duration:',
|
|
151
|
+
doneWithExistingRecords - doneWithFilters,
|
|
152
|
+
)
|
|
153
|
+
console.log(
|
|
154
|
+
'[helpers/db] [createOrUpdate] update duration:',
|
|
155
|
+
doneWithUpdate - doneWithExistingRecords,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
return existingRecords[0] as T
|
|
159
|
+
} else {
|
|
160
|
+
// If no record exists, create a new one
|
|
161
|
+
const newRecord = await db.insert(table).values(safeValues).returning()
|
|
162
|
+
console.log('newRecord:', newRecord)
|
|
163
|
+
return newRecord[0] as T
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
export const addModelsToInternalDb = async (
|
|
167
|
+
db: BetterSQLite3Database<any> | SqliteRemoteDatabase<any>,
|
|
168
|
+
models: ModelDefinitions,
|
|
169
|
+
) => {
|
|
170
|
+
for (const [modelName, modelClass] of Object.entries(models)) {
|
|
171
|
+
console.log(
|
|
172
|
+
'[helpers/db] [addModelsToInternalDb] starting modelName:',
|
|
173
|
+
modelName,
|
|
174
|
+
)
|
|
175
|
+
const modelRecord = await createOrUpdate<NewModelRecord>(db, modelsTable, {
|
|
176
|
+
name: modelName,
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
for (let [propertyName, propertyValues] of Object.entries(
|
|
180
|
+
modelClass.schema,
|
|
181
|
+
)) {
|
|
182
|
+
if (!propertyValues) {
|
|
183
|
+
throw new Error(`Property values not found for ${propertyName}`)
|
|
184
|
+
}
|
|
185
|
+
propertyValues.name = propertyName
|
|
186
|
+
propertyValues.modelId = modelRecord.id!
|
|
187
|
+
for (let [key, value] of Object.entries(propertyValues)) {
|
|
188
|
+
if (key === 'ref') {
|
|
189
|
+
const refModel = await createOrUpdate<NewModelRecord>(
|
|
190
|
+
db,
|
|
191
|
+
modelsTable,
|
|
192
|
+
{
|
|
193
|
+
name: value,
|
|
194
|
+
},
|
|
195
|
+
)
|
|
196
|
+
// delete propertyValues.ref
|
|
197
|
+
propertyValues.refModelId = refModel.id
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const propertyRecord = await createOrUpdate<NewPropertyRecord>(
|
|
202
|
+
db,
|
|
203
|
+
properties,
|
|
204
|
+
propertyValues,
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
console.log('propertyRecord:', propertyRecord)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
console.log(
|
|
211
|
+
'[helpers/db] [addModelsToInternalDb] done modelName:',
|
|
212
|
+
modelName,
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export const getAddressesFromDb = async (): Promise<string[]> => {
|
|
217
|
+
const appDb = getAppDb()
|
|
218
|
+
|
|
219
|
+
if (!appDb) {
|
|
220
|
+
return new Promise((resolve) => {
|
|
221
|
+
setTimeout(async () => {
|
|
222
|
+
const addresses = await getAddressesFromDb()
|
|
223
|
+
resolve(addresses)
|
|
224
|
+
}, 500)
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const appStatesRecords = await appDb!
|
|
229
|
+
.select()
|
|
230
|
+
.from(appState)
|
|
231
|
+
.where(eq(appState.key, 'addresses'))
|
|
232
|
+
.limit(1)
|
|
233
|
+
|
|
234
|
+
if (!appStatesRecords || appStatesRecords.length === 0) {
|
|
235
|
+
throw new Error('No appStatesRecords for addresses found')
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const addressRecord = appStatesRecords[0]
|
|
239
|
+
|
|
240
|
+
const addressArrayString = addressRecord.value
|
|
241
|
+
|
|
242
|
+
if (!addressArrayString) {
|
|
243
|
+
throw new Error('No addresses found')
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return JSON.parse(addressArrayString)
|
|
247
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { assign, emit, setup } from 'xstate'
|
|
2
|
+
import {
|
|
3
|
+
DB_CHECK_STATUS_EXISTS,
|
|
4
|
+
DB_CHECK_STATUS_UPDATE_PATHS,
|
|
5
|
+
DB_CREATING_SUCCESS,
|
|
6
|
+
DB_MIGRATING_SUCCESS,
|
|
7
|
+
DB_MIGRATING_WAIT,
|
|
8
|
+
DB_VALIDATING_SUCCESS,
|
|
9
|
+
DB_VALIDATING_WAIT,
|
|
10
|
+
DB_WAITING_FOR_FILES_RECEIVED,
|
|
11
|
+
DbState,
|
|
12
|
+
MachineIds,
|
|
13
|
+
} from '../internal/constants'
|
|
14
|
+
import debug from 'debug'
|
|
15
|
+
import { DbServiceContext } from '@/types'
|
|
16
|
+
import { checkStatus } from '@/browser/services/db/actors/checkStatus'
|
|
17
|
+
import { connectToDb } from '@/browser/services/db/actors/connectToDb'
|
|
18
|
+
import { validate } from '@/browser/services/db/actors/validate'
|
|
19
|
+
import { migrate } from '@/browser/services/db/actors/migrate'
|
|
20
|
+
|
|
21
|
+
const logger = debug('app:services:db:machine')
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
CHECKING_STATUS,
|
|
25
|
+
VALIDATING,
|
|
26
|
+
WAITING_FOR_FILES,
|
|
27
|
+
CONNECTING_TO_DB,
|
|
28
|
+
MIGRATING,
|
|
29
|
+
} = DbState
|
|
30
|
+
|
|
31
|
+
const dbMachine = setup({
|
|
32
|
+
types: {
|
|
33
|
+
context: {} as Partial<DbServiceContext>,
|
|
34
|
+
input: {} as Partial<DbServiceContext> | undefined,
|
|
35
|
+
},
|
|
36
|
+
actors: {
|
|
37
|
+
checkStatus,
|
|
38
|
+
validate,
|
|
39
|
+
connectToDb,
|
|
40
|
+
migrate,
|
|
41
|
+
},
|
|
42
|
+
}).createMachine({
|
|
43
|
+
id: MachineIds.DB,
|
|
44
|
+
initial: CHECKING_STATUS,
|
|
45
|
+
context: ({ input }) => input as DbServiceContext,
|
|
46
|
+
on: {
|
|
47
|
+
[DB_WAITING_FOR_FILES_RECEIVED]: {
|
|
48
|
+
actions: assign({
|
|
49
|
+
hasFiles: ({ event }) => {
|
|
50
|
+
logger('[db/machine] DB_WAITING_FOR_FILES_RECEIVED event:', event)
|
|
51
|
+
return true
|
|
52
|
+
},
|
|
53
|
+
}),
|
|
54
|
+
},
|
|
55
|
+
updateHasFiles: {
|
|
56
|
+
target: `.${CHECKING_STATUS}`,
|
|
57
|
+
actions: assign({
|
|
58
|
+
hasFiles: ({ context, event }) => {
|
|
59
|
+
logger('[db/machine] updateHasFiles event:', event)
|
|
60
|
+
logger('[db/machine] updateHasFiles context:', context)
|
|
61
|
+
return event.hasFiles
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
// always: {
|
|
67
|
+
// target: `.${CHECKING_STATUS}`,
|
|
68
|
+
// guard: ({ context, event }) => context.hasFiles && event.type === 'updateHasFiles',
|
|
69
|
+
// },
|
|
70
|
+
states: {
|
|
71
|
+
idle: {
|
|
72
|
+
on: {
|
|
73
|
+
start: CHECKING_STATUS,
|
|
74
|
+
},
|
|
75
|
+
meta: {
|
|
76
|
+
displayText: 'DB starting ...',
|
|
77
|
+
percentComplete: 0,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
[CHECKING_STATUS]: {
|
|
81
|
+
on: {
|
|
82
|
+
[DB_CHECK_STATUS_UPDATE_PATHS]: {
|
|
83
|
+
actions: assign({
|
|
84
|
+
pathToDb: ({ event }) => event.pathToDb,
|
|
85
|
+
pathToDir: ({ event }) => event.pathToDir,
|
|
86
|
+
pathToDbDir: ({ event }) => event.pathToDbDir,
|
|
87
|
+
}),
|
|
88
|
+
},
|
|
89
|
+
[DB_CHECK_STATUS_EXISTS]: CONNECTING_TO_DB,
|
|
90
|
+
},
|
|
91
|
+
invoke: {
|
|
92
|
+
src: 'checkStatus',
|
|
93
|
+
input: ({ context, event }) => ({ context, event }),
|
|
94
|
+
},
|
|
95
|
+
meta: {
|
|
96
|
+
displayText: 'Checking DB status',
|
|
97
|
+
percentComplete: 60,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
[CONNECTING_TO_DB]: {
|
|
101
|
+
on: {
|
|
102
|
+
[DB_CREATING_SUCCESS]: {
|
|
103
|
+
target: VALIDATING,
|
|
104
|
+
actions: assign({
|
|
105
|
+
dbId: ({ event }) => event.dbId,
|
|
106
|
+
}),
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
invoke: {
|
|
110
|
+
src: 'connectToDb',
|
|
111
|
+
input: ({ context }) => ({ context }),
|
|
112
|
+
},
|
|
113
|
+
meta: {
|
|
114
|
+
displayText: 'Connecting to local DB',
|
|
115
|
+
percentComplete: 70,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
[VALIDATING]: {
|
|
119
|
+
on: {
|
|
120
|
+
[DB_VALIDATING_SUCCESS]: {
|
|
121
|
+
target: MIGRATING,
|
|
122
|
+
// guard: ({ context }) => context.hasFiles,
|
|
123
|
+
},
|
|
124
|
+
[DB_VALIDATING_WAIT]: {
|
|
125
|
+
target: WAITING_FOR_FILES,
|
|
126
|
+
// guard: ({ context }) => !context.hasFiles,
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
invoke: {
|
|
130
|
+
src: 'validate',
|
|
131
|
+
input: ({ context }) => ({ context }),
|
|
132
|
+
},
|
|
133
|
+
meta: {
|
|
134
|
+
displayText: 'Validating DB',
|
|
135
|
+
percentComplete: 80,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
// Here we're waiting for migration and schema files to be downloaded
|
|
139
|
+
[WAITING_FOR_FILES]: {
|
|
140
|
+
on: {
|
|
141
|
+
[DB_WAITING_FOR_FILES_RECEIVED]: {
|
|
142
|
+
target: MIGRATING,
|
|
143
|
+
actions: assign({
|
|
144
|
+
hasFiles: true,
|
|
145
|
+
}),
|
|
146
|
+
},
|
|
147
|
+
[DB_MIGRATING_SUCCESS]: 'ready',
|
|
148
|
+
},
|
|
149
|
+
entry: ({ context }) => {
|
|
150
|
+
if (context.hasFiles) {
|
|
151
|
+
emit({ type: DB_WAITING_FOR_FILES_RECEIVED })
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
[MIGRATING]: {
|
|
156
|
+
on: {
|
|
157
|
+
[DB_MIGRATING_WAIT]: WAITING_FOR_FILES,
|
|
158
|
+
[DB_MIGRATING_SUCCESS]: {
|
|
159
|
+
target: 'ready',
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
invoke: {
|
|
163
|
+
src: 'migrate',
|
|
164
|
+
input: ({ context }) => ({ context }),
|
|
165
|
+
},
|
|
166
|
+
meta: {
|
|
167
|
+
displayText: 'Migrating DB',
|
|
168
|
+
percentComplete: 90,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
ready: {
|
|
172
|
+
target: 'idle',
|
|
173
|
+
meta: {
|
|
174
|
+
displayText: 'Wrapping up the db ...',
|
|
175
|
+
percentComplete: 100,
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
export { dbMachine }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getAppDb } from '../sqlWasmClient'
|
|
2
|
+
import { seeds } from '@/shared/seedSchema'
|
|
3
|
+
import { eq, or } from 'drizzle-orm'
|
|
4
|
+
|
|
5
|
+
type DeleteItemProps = {
|
|
6
|
+
seedLocalId?: string
|
|
7
|
+
seedUid?: string
|
|
8
|
+
}
|
|
9
|
+
type DeleteItem = (props: DeleteItemProps) => Promise<void>
|
|
10
|
+
export const deleteItem: DeleteItem = async ({ seedLocalId, seedUid }) => {
|
|
11
|
+
const appDb = getAppDb()
|
|
12
|
+
|
|
13
|
+
await appDb
|
|
14
|
+
.update(seeds)
|
|
15
|
+
.set({
|
|
16
|
+
_markedForDeletion: 1,
|
|
17
|
+
})
|
|
18
|
+
.where(or(eq(seeds.localId, seedLocalId), eq(seeds.uid, seedUid)))
|
|
19
|
+
}
|