@seedprotocol/sdk 0.1.65 → 0.1.67

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/src/save.ts DELETED
@@ -1,183 +0,0 @@
1
- import { Attestation } from '@/browser/gql/graphql'
2
- import { sql } from 'drizzle-orm'
3
- import { generateId } from '@/shared/helpers'
4
- import { escapeSqliteString } from '@/shared/helpers/db'
5
- import { camelCase } from 'lodash-es'
6
-
7
- import { getVersionsForVersionUids } from '@/browser/db/read/getVersionsForVersionUids'
8
- import debug from 'debug'
9
- import { getAppDb } from '@/browser/db/sqlWasmClient'
10
-
11
- const logger = debug('app:property:save')
12
-
13
- const relationValuesToExclude = [
14
- '0x0000000000000000000000000000000000000000000000000000000000000020',
15
- ]
16
-
17
- const seedUidToLocalId = new Map<string, string>()
18
- const seedUidToModelType = new Map<string, string>()
19
-
20
- const versionUidToLocalId = new Map<string, string>()
21
- const versionUidToSeedUid = new Map<string, string>()
22
-
23
- const propertyUidToLocalId = new Map<string, string>()
24
-
25
- export const savePropertiesToDb = async (properties: Attestation[]) => {
26
- const appDb = getAppDb()
27
-
28
- const propertyUids = properties.map((property) => property.id)
29
- const versionUids = properties.map((property) => property.refUID)
30
-
31
- const existingRecordsQuery = await appDb.run(
32
- sql.raw(
33
- `SELECT uid, local_id
34
- FROM metadata
35
- WHERE uid IN ('${propertyUids.join("','")}');`,
36
- ),
37
- )
38
-
39
- const existingRecordsUids = new Set<string>()
40
-
41
- if (
42
- existingRecordsQuery &&
43
- existingRecordsQuery.rows &&
44
- existingRecordsQuery.rows.length > 0
45
- ) {
46
- for (const row of existingRecordsQuery.rows) {
47
- existingRecordsUids.add(row[0])
48
- propertyUidToLocalId.set(row[0], row[1])
49
- }
50
- }
51
-
52
- const newProperties = properties.filter(
53
- (property) => !existingRecordsUids.has(property.id),
54
- )
55
-
56
- if (newProperties.length === 0) {
57
- return { propertyUids }
58
- }
59
-
60
- let insertPropertiesQuery = `INSERT INTO metadata (local_id, uid, schema_uid, property_name, property_value,
61
- eas_data_type, version_uid, version_local_id, seed_uid,
62
- seed_local_id, model_type, created_at, attestation_created_at,
63
- attestation_raw)
64
- VALUES `
65
-
66
- for (let i = 0; i < newProperties.length; i++) {
67
- const property = newProperties[i]
68
- const propertyLocalId = generateId()
69
- const metadata = JSON.parse(property.decodedDataJson)[0].value
70
-
71
- let propertyNameSnake = metadata.name
72
-
73
- if (!propertyNameSnake) {
74
- propertyNameSnake = metadata.name
75
- }
76
-
77
- if (!propertyNameSnake) {
78
- logger(
79
- '[item/events] [syncDbWithEas] no propertyName found for property: ',
80
- property,
81
- )
82
- return
83
- }
84
-
85
- let isRelation = false
86
- let refValueType = 'single'
87
- let refResolvedValue
88
- let refResolvedDisplayValue
89
-
90
- if (
91
- (propertyNameSnake.endsWith('_id') ||
92
- propertyNameSnake.endsWith('_ids')) &&
93
- propertyNameSnake !== 'storage_transaction_id' &&
94
- propertyNameSnake !== 'storage_provider_transaction_id'
95
- ) {
96
- isRelation = true
97
- let isList = false
98
-
99
- if (Array.isArray(metadata.value)) {
100
- isList = true
101
- refValueType = 'list'
102
-
103
- // const relatedValuesQuery = await appDb.run(
104
- // sql.raw(
105
- // `SELECT s.uid, MAX(v.attestation_created_at) as last_published_at
106
- // FROM seeds s
107
- // JOIN versions v ON s.local_id = v.seed_local_id
108
- // JOIN metadata m ON v.local_id = m.version_local_id
109
- // WHERE s.uid IN ('${metadata.value.join("','")}')
110
- // AND m.property_name = 'storage_transation_id';`,
111
- // ),
112
- // )
113
- //
114
- // if (
115
- // relatedValuesQuery &&
116
- // relatedValuesQuery.rows &&
117
- // relatedValuesQuery.rows.length > 0
118
- // ) {
119
- // for (const row of relatedValuesQuery.rows) {
120
- // // relatedSeedUids.add(row[0])
121
- // }
122
- // }
123
- }
124
-
125
- if (!isList) {
126
- if (relationValuesToExclude.includes(metadata.value)) {
127
- continue
128
- }
129
- }
130
- }
131
-
132
- const versionsData = await getVersionsForVersionUids(versionUids)
133
-
134
- for (const version of versionsData) {
135
- const { seedUid, seedLocalId, uid, localId } = version
136
- versionUidToLocalId.set(uid, localId)
137
- versionUidToSeedUid.set(uid, seedUid)
138
- seedUidToLocalId.set(seedUid, seedLocalId)
139
- }
140
-
141
- let propertyValue = metadata.value
142
-
143
- if (typeof propertyValue !== 'string') {
144
- propertyValue = JSON.stringify(propertyValue)
145
- }
146
-
147
- const propertyName = camelCase(propertyNameSnake)
148
- propertyValue = escapeSqliteString(propertyValue)
149
- const easDataType = metadata.type
150
- const versionUid = property.refUID
151
- const versionLocalId = versionUidToLocalId.get(versionUid)
152
- const attestationCreatedAt = property.timeCreated * 1000
153
- const attestationRaw = escapeSqliteString(JSON.stringify(property))
154
- const seedUid = versionUidToSeedUid.get(versionUid)
155
- const seedLocalId = seedUidToLocalId.get(seedUid!)
156
- const modelType = seedUidToModelType.get(seedUid!)
157
-
158
- const valuesString = `('${propertyLocalId}', '${property.id}', '${property.schemaId}', '${propertyName}', '${propertyValue}', '${easDataType}', '${versionUid}', '${versionLocalId}', '${seedUid}', '${seedLocalId}', '${modelType}',
159
- ${Date.now()}, ${attestationCreatedAt}, '${attestationRaw}')`
160
-
161
- if (i < newProperties.length - 1) {
162
- insertPropertiesQuery += valuesString + ', '
163
- }
164
-
165
- if (i === newProperties.length - 1) {
166
- insertPropertiesQuery += valuesString + ';'
167
- }
168
-
169
- propertyUidToLocalId.set(property.id, propertyLocalId)
170
- }
171
-
172
- if (insertPropertiesQuery.endsWith('VALUES ')) {
173
- return { propertyUids }
174
- }
175
-
176
- if (insertPropertiesQuery.endsWith(', ')) {
177
- insertPropertiesQuery = insertPropertiesQuery.slice(0, -2) + ';'
178
- }
179
-
180
- await appDb.run(sql.raw(insertPropertiesQuery))
181
-
182
- return { propertyUids }
183
- }
@@ -1,107 +0,0 @@
1
- import { EventObject, fromCallback } from 'xstate'
2
- import { FromCallbackInput } from '@/types/machines'
3
- import {
4
- ItemPropertyValueType,
5
- PropertyMachineContext,
6
- SaveValueToDbEvent,
7
- } from '@/types/property'
8
-
9
- export const saveListRelation = fromCallback<
10
- EventObject,
11
- FromCallbackInput<PropertyMachineContext, SaveValueToDbEvent>
12
- >(({ sendBack, input: { context, event } }) => {
13
- const {
14
- localId,
15
- propertyName: propertyNameRaw,
16
- versionLocalId,
17
- seedUid,
18
- seedLocalId,
19
- propertyValue: existingValue,
20
- propertyRecordSchema,
21
- } = context
22
-
23
- if (!propertyRecordSchema) {
24
- throw new Error('Missing propertyRecordSchema')
25
- }
26
-
27
- let newValue: ItemPropertyValueType
28
-
29
- if (event) {
30
- newValue = event.newValue
31
- }
32
-
33
- const _saveListRelation = async (): Promise<boolean> => {
34
- let refResolvedDisplayValue
35
- let refSeedType
36
- let propertyName = propertyNameRaw
37
- let versionLocalIdToSave = versionLocalId
38
-
39
- const refResolvedValue = newValue
40
-
41
- if (!propertyName.endsWith('Ids')) {
42
- propertyName = `${propertyName}Ids`
43
- }
44
-
45
- let newValueType
46
-
47
- if (typeof newValue === 'string') {
48
- newValue = newValue.split(',')
49
- }
50
-
51
- if (!Array.isArray(newValue)) {
52
- sendBack({
53
- type: 'saveListRelationFailure',
54
- })
55
- return false
56
- }
57
-
58
- // Should have array of ids here
59
-
60
- return true
61
-
62
- // let fileType
63
- //
64
- // const dirs = await fs.promises.readdir('/files')
65
- //
66
- // for (const dir of dirs) {
67
- // const files = await fs.promises.readdir(`/files/${dir}`)
68
- // if (newValue && files.includes(newValue as string)) {
69
- // fileType = dir
70
- // break
71
- // }
72
- // }
73
- //
74
- // if (newValue && fileType === 'images') {
75
- // const filePath = `/files/images/${newValue}`
76
- // refResolvedDisplayValue = await getContentUrlFromPath(filePath)
77
- // refSeedType = 'image'
78
- // newValue = await createSeed({
79
- // type: refSeedType,
80
- // })
81
- // await createVersion({
82
- // seedLocalId,
83
- // seedUid,
84
- // seedType: refSeedType,
85
- // })
86
- // }
87
- //
88
- // await updateItemPropertyValue({
89
- // propertyLocalId: localId,
90
- // propertyName,
91
- // newValue,
92
- // seedLocalId,
93
- // refSeedType,
94
- // refResolvedValue,
95
- // refResolvedDisplayValue,
96
- // versionLocalId,
97
- // modelName: itemModelName,
98
- // schemaUid,
99
- // })
100
- }
101
-
102
- _saveListRelation().then((success) => {
103
- if (success) {
104
- sendBack({ type: 'saveRelationSuccess' })
105
- }
106
- })
107
- })
@@ -1,5 +0,0 @@
1
- import { Attestation } from '@/browser/gql/graphql';
2
- export declare const savePropertiesToDb: (properties: Attestation[]) => Promise<{
3
- propertyUids: string[];
4
- } | undefined>;
5
- //# sourceMappingURL=save.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"save.d.ts","sourceRoot":"","sources":["../../../../../src/browser/db/save.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAwBnD,eAAO,MAAM,kBAAkB,eAAsB,WAAW,EAAE;;cA8JjE,CAAA"}
@@ -1,5 +0,0 @@
1
- import { EventObject } from 'xstate';
2
- import { FromCallbackInput } from '@/types/machines';
3
- import { PropertyMachineContext, SaveValueToDbEvent } from '@/types/property';
4
- export declare const saveListRelation: import("xstate").CallbackActorLogic<EventObject, FromCallbackInput<PropertyMachineContext, SaveValueToDbEvent>, EventObject>;
5
- //# sourceMappingURL=saveListRelation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"saveListRelation.d.ts","sourceRoot":"","sources":["../../../../../../../src/browser/property/actors/saveValueToDb/saveListRelation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAgB,MAAM,QAAQ,CAAA;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAEL,sBAAsB,EACtB,kBAAkB,EACnB,MAAM,kBAAkB,CAAA;AAEzB,eAAO,MAAM,gBAAgB,8HAkG3B,CAAA"}