@seedprotocol/sdk 0.1.75 → 0.1.77

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.
Files changed (39) hide show
  1. package/dist/bin.js +1 -1
  2. package/dist/{constants-B9hSFQXL.js → constants-rmQ8zg8_.js} +2 -2
  3. package/dist/{constants-B9hSFQXL.js.map → constants-rmQ8zg8_.js.map} +1 -1
  4. package/dist/index-BH0P1ozi.js +7568 -0
  5. package/dist/index-BH0P1ozi.js.map +1 -0
  6. package/dist/index-BpkGOxI0.js +32 -0
  7. package/dist/index-BpkGOxI0.js.map +1 -0
  8. package/dist/main.js +29 -7542
  9. package/dist/main.js.map +1 -1
  10. package/dist/{seed.schema.config-BPJfo_5h.js → seed.schema.config-CR4yAjam.js} +3 -3
  11. package/dist/{seed.schema.config-BPJfo_5h.js.map → seed.schema.config-CR4yAjam.js.map} +1 -1
  12. package/dist/src/ItemProperty.ts +21 -25
  13. package/dist/src/getItem.ts +12 -32
  14. package/dist/src/getItemProperties.ts +17 -113
  15. package/dist/src/hydrateFromDb.ts +58 -64
  16. package/dist/src/initialize.ts +33 -119
  17. package/dist/src/item.ts +1 -1
  18. package/dist/src/itemMachineSingle.ts +12 -29
  19. package/dist/src/queries.ts +27 -6
  20. package/dist/src/reload.ts +33 -0
  21. package/dist/src/resolveRelatedValue.ts +25 -4
  22. package/dist/src/resolveRemoteStorage.ts +3 -2
  23. package/dist/types/src/browser/db/read/getItem.d.ts.map +1 -1
  24. package/dist/types/src/browser/db/read/getItemProperties.d.ts.map +1 -1
  25. package/dist/types/src/browser/item/Item.d.ts.map +1 -1
  26. package/dist/types/src/browser/item/single/actors/reload.d.ts +5 -0
  27. package/dist/types/src/browser/item/single/actors/reload.d.ts.map +1 -0
  28. package/dist/types/src/browser/item/single/actors/saveDataToDb.d.ts +1 -55
  29. package/dist/types/src/browser/item/single/actors/saveDataToDb.d.ts.map +1 -1
  30. package/dist/types/src/browser/item/single/itemMachineSingle.d.ts +1 -56
  31. package/dist/types/src/browser/item/single/itemMachineSingle.d.ts.map +1 -1
  32. package/dist/types/src/browser/property/ItemProperty.d.ts +3 -111
  33. package/dist/types/src/browser/property/ItemProperty.d.ts.map +1 -1
  34. package/dist/types/src/browser/property/actors/hydrateFromDb.d.ts.map +1 -1
  35. package/dist/types/src/browser/property/actors/resolveRelatedValue.d.ts +4 -1
  36. package/dist/types/src/browser/property/actors/resolveRelatedValue.d.ts.map +1 -1
  37. package/dist/types/src/browser/property/actors/resolveRemoteStorage.d.ts +3 -55
  38. package/dist/types/src/browser/property/actors/resolveRemoteStorage.d.ts.map +1 -1
  39. package/package.json +2 -2
@@ -1,6 +1,6 @@
1
- import { c as __decorate, e as __metadata } from './constants-B9hSFQXL.js';
1
+ import { _ as __decorate, a as __metadata } from './constants-rmQ8zg8_.js';
2
2
  import 'drizzle-orm';
3
- import { Text, Model } from './main.js';
3
+ import { T as Text, M as Model } from './index-BH0P1ozi.js';
4
4
  import 'react';
5
5
  import 'reflect-metadata';
6
6
  import 'xstate';
@@ -76,4 +76,4 @@ const models = {
76
76
  };
77
77
 
78
78
  export { models };
79
- //# sourceMappingURL=seed.schema.config-BPJfo_5h.js.map
79
+ //# sourceMappingURL=seed.schema.config-CR4yAjam.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"seed.schema.config-BPJfo_5h.js","sources":["../../src/shared/configs/seed.schema.config.ts"],"sourcesContent":["import { Model, Text } from '@/browser'\n\n@Model\nclass Seed {\n @Text() uid!: string\n @Text() type!: string\n}\n\n@Model\nclass Version {\n @Text() seedUid!: string\n @Text() note!: string\n}\n\n@Model\nclass Metadata {\n @Text() key!: string\n @Text() value!: string\n}\n\nexport const models = {\n Seed,\n Version,\n Metadata,\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAM,IAAI,GAAV,MAAM,IAAI,CAAA;CAGT;AAFS,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAa,CAAA,EAAA,IAAA,CAAA,SAAA,EAAA,KAAA,EAAA,KAAA,CAAA,CAAA;AACZ,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAc,CAAA,EAAA,IAAA,CAAA,SAAA,EAAA,MAAA,EAAA,KAAA,CAAA,CAAA;AAFjB,IAAI,GAAA,UAAA,CAAA;IADT;AACK,CAAA,EAAA,IAAI,CAGT;AAGD,IAAM,OAAO,GAAb,MAAM,OAAO,CAAA;CAGZ;AAFS,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAiB,CAAA,EAAA,OAAA,CAAA,SAAA,EAAA,SAAA,EAAA,KAAA,CAAA,CAAA;AAChB,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAc,CAAA,EAAA,OAAA,CAAA,SAAA,EAAA,MAAA,EAAA,KAAA,CAAA,CAAA;AAFjB,OAAO,GAAA,UAAA,CAAA;IADZ;AACK,CAAA,EAAA,OAAO,CAGZ;AAGD,IAAM,QAAQ,GAAd,MAAM,QAAQ,CAAA;CAGb;AAFS,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAa,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,KAAA,EAAA,KAAA,CAAA,CAAA;AACZ,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAe,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,OAAA,EAAA,KAAA,CAAA,CAAA;AAFlB,QAAQ,GAAA,UAAA,CAAA;IADb;AACK,CAAA,EAAA,QAAQ,CAGb;AAEY,MAAA,MAAM,GAAG;IACpB,IAAI;IACJ,OAAO;IACP,QAAQ;;;;;"}
1
+ {"version":3,"file":"seed.schema.config-CR4yAjam.js","sources":["../../src/shared/configs/seed.schema.config.ts"],"sourcesContent":["import { Model, Text } from '@/browser'\n\n@Model\nclass Seed {\n @Text() uid!: string\n @Text() type!: string\n}\n\n@Model\nclass Version {\n @Text() seedUid!: string\n @Text() note!: string\n}\n\n@Model\nclass Metadata {\n @Text() key!: string\n @Text() value!: string\n}\n\nexport const models = {\n Seed,\n Version,\n Metadata,\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,IAAM,IAAI,GAAV,MAAM,IAAI,CAAA;CAGT;AAFS,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAa,CAAA,EAAA,IAAA,CAAA,SAAA,EAAA,KAAA,EAAA,KAAA,CAAA,CAAA;AACZ,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAc,CAAA,EAAA,IAAA,CAAA,SAAA,EAAA,MAAA,EAAA,KAAA,CAAA,CAAA;AAFjB,IAAI,GAAA,UAAA,CAAA;IADT;AACK,CAAA,EAAA,IAAI,CAGT;AAGD,IAAM,OAAO,GAAb,MAAM,OAAO,CAAA;CAGZ;AAFS,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAiB,CAAA,EAAA,OAAA,CAAA,SAAA,EAAA,SAAA,EAAA,KAAA,CAAA,CAAA;AAChB,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAc,CAAA,EAAA,OAAA,CAAA,SAAA,EAAA,MAAA,EAAA,KAAA,CAAA,CAAA;AAFjB,OAAO,GAAA,UAAA,CAAA;IADZ;AACK,CAAA,EAAA,OAAO,CAGZ;AAGD,IAAM,QAAQ,GAAd,MAAM,QAAQ,CAAA;CAGb;AAFS,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAa,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,KAAA,EAAA,KAAA,CAAA,CAAA;AACZ,UAAA,CAAA;AAAP,IAAA,IAAI,EAAE;;AAAe,CAAA,EAAA,QAAA,CAAA,SAAA,EAAA,OAAA,EAAA,KAAA,CAAA,CAAA;AAFlB,QAAQ,GAAA,UAAA,CAAA;IADb;AACK,CAAA,EAAA,QAAQ,CAGb;AAEY,MAAA,MAAM,GAAG;IACpB,IAAI;IACJ,OAAO;IACP,QAAQ;;;;;"}
@@ -18,7 +18,6 @@ import { getModel } from '@/browser/stores/modelClass'
18
18
  import { getCorrectId } from '@/browser/helpers'
19
19
  import { Static } from '@sinclair/typebox'
20
20
  import { TProperty } from '@/browser/property'
21
- import { fs } from '@zenfs/core'
22
21
 
23
22
  const logger = debug('app:property:class')
24
23
 
@@ -151,14 +150,6 @@ export class ItemProperty<PropertyType> {
151
150
  const { context } = snapshot
152
151
  const { propertyRecordSchema } = context
153
152
 
154
- // if (this.uid && !cacheHasProperty(this.uid)) {
155
- // addPropertyToCache(this)
156
- // }
157
- //
158
- // if (!this.uid && this.localId && !cacheHasProperty(this.localId,)) {
159
- // addPropertyToCache(this)
160
- // }
161
-
162
153
  if (context.seedLocalId) {
163
154
  const cacheKey = ItemProperty.cacheKey(
164
155
  context.seedLocalId,
@@ -179,19 +170,26 @@ export class ItemProperty<PropertyType> {
179
170
 
180
171
  let renderValue
181
172
 
182
- if (
183
- propertyRecordSchema &&
184
- propertyRecordSchema.storageType &&
185
- propertyRecordSchema.storageType === 'ItemStorage' &&
186
- context.resolvedValue &&
187
- context.localStorageDir
188
- ) {
189
- const filePath = `/files/${context.localStorageDir}/${context.resolvedValue}`
190
- const exists = await fs.promises.exists(filePath)
191
- if (exists) {
192
- renderValue = await fs.promises.readFile(filePath, 'utf-8')
193
- }
194
- }
173
+ // if (
174
+ // propertyRecordSchema &&
175
+ // propertyRecordSchema.storageType &&
176
+ // propertyRecordSchema.storageType === 'ItemStorage' &&
177
+ // context.resolvedValue &&
178
+ // context.localStorageDir
179
+ // ) {
180
+ // const filePath = `/files/${context.localStorageDir}/${context.resolvedValue}`
181
+ // try {
182
+ // const exists = await fs.promises.exists(filePath)
183
+ // if (exists) {
184
+ // renderValue = await fs.promises.readFile(filePath, 'utf-8')
185
+ // }
186
+ // } catch (e) {
187
+ // logger(
188
+ // `[ItemProperty] [${context.seedLocalId}] [${context.propertyName}] [storageType] error`,
189
+ // e,
190
+ // )
191
+ // }
192
+ // }
195
193
 
196
194
  if (!renderValue) {
197
195
  renderValue = context.renderValue || context.propertyValue
@@ -296,13 +294,11 @@ export class ItemProperty<PropertyType> {
296
294
  const cacheKey = ItemProperty.cacheKey(cacheKeyId!, propertyName)
297
295
  if (this.instanceCache.has(cacheKey)) {
298
296
  const { instance, refCount } = this.instanceCache.get(cacheKey)!
297
+ instance.getService().send({ type: 'reload' })
299
298
  this.instanceCache.set(cacheKey, {
300
299
  instance,
301
300
  refCount: refCount + 1,
302
301
  })
303
- if (instance.status === 'idle') {
304
- instance.getService().send({ type: 'refresh' })
305
- }
306
302
  return instance
307
303
  }
308
304
  const propertyData = await getPropertyData(
@@ -83,46 +83,26 @@ export const getItemDataFromDb: GetItemDataFromDb = async ({
83
83
 
84
84
  let itemData = itemDataRows[0]
85
85
 
86
- const propertiesData = await getItemProperties({ seedLocalId, seedUid })
87
-
88
- // const initObj: ItemData = {
89
- // seedLocalId,
90
- // seedUid,
91
- // modelName,
92
- // }
86
+ const propertiesData = await getItemProperties({
87
+ seedLocalId,
88
+ seedUid: itemData.seedUid || undefined,
89
+ })
93
90
 
94
91
  if (!propertiesData || propertiesData.length === 0) {
95
92
  return itemData
96
93
  }
97
94
 
98
- const firstPropertyData = propertiesData[0]
99
-
100
95
  for (const propertyData of propertiesData) {
101
96
  const propertyName = propertyData.propertyName
102
97
 
103
- const propertyValue = propertyData.propertyValue
104
-
105
- // TODO: Find a better place for the property data below
106
- // Since initObj is used to initialize an Item, the following values
107
- // just overwrite each other for each property since they are Property
108
- // specific.
109
-
110
- // const refSeedType = propertyDbValues[11]
111
- // if (refSeedType) {
112
- // initObj.refSeedType = refSeedType
113
- // }
114
- // const refValueType = propertyDbValues[12]
115
- // if (refValueType) {
116
- // initObj.refValueType = refValueType
117
- // }
118
- //
119
- // if (
120
- // refSeedType &&
121
- // refValueType === 'list' &&
122
- // propertyName.endsWith('Ids')
123
- // ) {
124
- // logger('[db/queries] [getItemDataFromDb] propertyName', propertyName)
125
- // }
98
+ let propertyValue = propertyData.propertyValue
99
+
100
+ if (propertyName.endsWith('Id') || propertyName.endsWith('Ids')) {
101
+ if (propertyData.refSeedType) {
102
+ const propertyNameVariant = propertyName.replace(/Ids?$/, '')
103
+ itemData[propertyNameVariant] = propertyValue
104
+ }
105
+ }
126
106
 
127
107
  itemData[propertyName] = propertyValue
128
108
  }
@@ -1,15 +1,7 @@
1
1
  import { PropertyData } from '@/types'
2
2
  import { getAppDb } from '@/browser/db/sqlWasmClient'
3
3
  import { metadata, seeds, versions } from '@/shared/seedSchema'
4
- import {
5
- and,
6
- eq,
7
- getTableColumns,
8
- isNotNull,
9
- isNull,
10
- or,
11
- SQL,
12
- } from 'drizzle-orm'
4
+ import { and, eq, getTableColumns, isNotNull, isNull, SQL } from 'drizzle-orm'
13
5
 
14
6
  type GetPropertiesForSeedProps = {
15
7
  seedLocalId?: string
@@ -30,16 +22,24 @@ export const getItemProperties: GetItemProperties = async ({
30
22
 
31
23
  const whereClauses: SQL[] = [isNotNull(metadata.propertyName)]
32
24
 
33
- const uidWhereClause: SQL = seedUid
34
- ? eq(seeds.uid, seedUid)
35
- : isNull(seeds.uid)
36
- const localWhereClause: SQL = seedLocalId
37
- ? eq(seeds.localId, seedLocalId)
38
- : isNull(seeds.localId)
25
+ if (seedUid) {
26
+ whereClauses.push(eq(seeds.uid, seedUid))
27
+ }
28
+
29
+ if (seedLocalId) {
30
+ whereClauses.push(eq(seeds.localId, seedLocalId))
31
+ }
39
32
 
40
- whereClauses.push(or(localWhereClause, uidWhereClause) as SQL)
33
+ // const uidWhereClause: SQL = seedUid
34
+ // ? eq(seeds.uid, seedUid)
35
+ // : isNull(seeds.uid)
36
+ // const localWhereClause: SQL = seedLocalId
37
+ // ? eq(seeds.localId, seedLocalId)
38
+ // : isNull(seeds.localId)
39
+
40
+ // whereClauses.push(or(localWhereClause, uidWhereClause) as SQL)
41
41
  whereClauses.push(isNotNull(metadata.propertyName))
42
- whereClauses.push(isNotNull(metadata.easDataType))
42
+ // whereClauses.push(isNotNull(metadata.easDataType))
43
43
 
44
44
  if (typeof edited !== 'undefined') {
45
45
  if (edited) {
@@ -50,21 +50,6 @@ export const getItemProperties: GetItemProperties = async ({
50
50
  }
51
51
  }
52
52
 
53
- // if (!seedLocalId || !seedUid) {
54
- // const seedRows = await appDb
55
- // .select({
56
- // localId: seeds.localId,
57
- // uid: seeds.uid,
58
- // })
59
- // .from(seeds)
60
- // .where(or(localWhereClause, uidWhereClause))
61
- //
62
- // if (seedRows && seedRows.length > 0) {
63
- // seedUid = seedRows[0].uid as string
64
- // seedLocalId = seedRows[0].localId as string
65
- // }
66
- // }
67
-
68
53
  const metadataColumns = getTableColumns(metadata)
69
54
 
70
55
  const propertiesData = await appDb
@@ -79,84 +64,3 @@ export const getItemProperties: GetItemProperties = async ({
79
64
 
80
65
  return propertiesData
81
66
  }
82
-
83
- // const localIdWhere = seedLocalId
84
- // ? `s.local_id = '${seedLocalId}'`
85
- // : 's.local_id IS NULL'
86
- // const uidWhere = seedUid ? `s.uid = '${seedUid}'` : 's.uid IS NULL'
87
- //
88
- // const queryStatement = `WITH LatestMetadata as (SELECT m.property_name,
89
- // m.property_value,
90
- // m.version_local_id,
91
- // MAX(m.attestation_created_at),
92
- // m.uid,
93
- // m.seed_local_id,
94
- // seed_uid
95
- // FROM metadata m
96
- // JOIN seeds s ON s.local_id = m.seed_local_id
97
- // GROUP BY m.property_name),
98
- //
99
- // LatestVersion as (SELECT v.local_id,
100
- // MAX(v.attestation_created_at) as attestation_created_at,
101
- // v.uid,
102
- // v.seed_local_id,
103
- // v.seed_uid
104
- // FROM versions v
105
- // JOIN seeds s ON s.local_id = v.seed_local_id
106
- // GROUP BY s.local_id)
107
- //
108
- //
109
- // SELECT s.local_id,
110
- // s.uid,
111
- // s.schema_uid,
112
- // m.property_name,
113
- // m.property_value,
114
- // COUNT(v.local_id) as versions_count,
115
- // m.model_type,
116
- // lv.attestation_created_at,
117
- // m.local_id,
118
- // m.uid,
119
- // MAX(m.attestation_created_at),
120
- // m.ref_seed_type,
121
- // m.ref_value_type,
122
- // m.seed_local_id,
123
- // m.seed_uid,
124
- // m.created_at,
125
- // m.updated_at,
126
- // m.version_uid
127
- // FROM seeds s
128
- // JOIN LatestMetadata lm ON s.local_id = m.seed_local_id
129
- // JOIN LatestVersion lv ON lv.seed_local_id = m.seed_local_id
130
- // JOIN metadata m ON m.property_name = lm.property_name OR lm.property_value = s.uid
131
- // JOIN versions v ON s.local_id = v.seed_local_id
132
- // WHERE ${localIdWhere}
133
- // OR ${uidWhere}
134
- // GROUP BY m.property_name;
135
- // `
136
- //
137
- // const { rows } = await runQueryForStatement(queryStatement)
138
- //
139
- // const propertiesDataOld: PropertyData[] = []
140
- //
141
- // for (const row of rows) {
142
- // propertiesDataOld.push({
143
- // localId: row[0],
144
- // uid: row[1],
145
- // schemaUid: row[2],
146
- // propertyName: row[3],
147
- // propertyValue: row[4],
148
- // versionsCount: row[5],
149
- // itemModelName: row[6],
150
- // attestationCreatedAt: row[7],
151
- // metadataLocalId: row[8],
152
- // metadataUid: row[9],
153
- // metadataAttestationCreatedAt: row[10],
154
- // refSeedType: row[11],
155
- // refValueType: row[12],
156
- // seedLocalId: row[13],
157
- // seedUid: row[14],
158
- // createdAt: row[15],
159
- // updatedAt: row[16],
160
- // versionUid: row[17],
161
- // })
162
- // }
@@ -46,32 +46,14 @@ export const hydrateFromDb = fromCallback<
46
46
  const _hydrateFromDb = async () => {
47
47
  const appDb = getAppDb()
48
48
 
49
- const whereClauses = []
50
-
51
- let hydrateQuery
52
- // let safeValue = propertyValue
53
- // let propertyValueQueryString = `property_value `
54
- // let propertyNameQueryString = `property_name = '${propertyName}'`
55
- let propertyNameQuery = eq(metadata.propertyName, propertyName)
56
-
57
- // if (safeValue && typeof propertyValue === 'string') {
58
- // safeValue = escapeSqliteString(propertyValue)
59
- // propertyValueQueryString += `= '${safeValue}'`
60
- // }
61
-
62
- // if (!safeValue) {
63
- // propertyValueQueryString += 'IS NULL'
64
- // }
65
-
66
- // if (typeof propertyValue === 'number') {
67
- // propertyValueQueryString += `= ${propertyValue}`
68
- // }
69
-
70
- if (
49
+ const isRelation =
71
50
  propertyRecordSchema &&
72
51
  propertyRecordSchema.ref &&
73
52
  propertyRecordSchema.dataType === 'Relation'
74
- ) {
53
+
54
+ const whereClauses = []
55
+
56
+ if (isRelation) {
75
57
  let missingPropertyNameVariant
76
58
  if (propertyName.endsWith('Id')) {
77
59
  missingPropertyNameVariant = propertyName.slice(0, -2)
@@ -86,44 +68,22 @@ export const hydrateFromDb = fromCallback<
86
68
  eq(metadata.propertyName, missingPropertyNameVariant),
87
69
  ),
88
70
  )
89
- // propertyNameQuery = or(
90
- // eq(metadata.propertyName, propertyName),
91
- // eq(metadata.propertyName, missingPropertyNameVariant),
92
- // )
93
71
  }
94
72
  if (!missingPropertyNameVariant) {
95
73
  whereClauses.push(eq(metadata.propertyName, propertyName))
96
74
  }
75
+ } else {
76
+ whereClauses.push(eq(metadata.propertyName, propertyName))
97
77
  }
98
78
 
99
- // if (seedUid && !seedLocalId) {
100
- // hydrateQuery = selectFromStatement.where(
101
- // and(eq(metadata.seedUid, seedUid), propertyNameQuery),
102
- // )
103
- // }
104
-
105
79
  if (seedUid) {
106
- // hydrateQuery = selectFromStatement.where(
107
- // and(
108
- // eq(metadata.seedLocalId, seedLocalId),
109
- // eq(metadata.seedUid, seedUid),
110
- // propertyNameQuery,
111
- // ),
112
- // )
113
80
  whereClauses.push(eq(metadata.seedUid, seedUid))
114
81
  }
115
82
 
116
83
  if (seedLocalId) {
117
- // hydrateQuery = selectFromStatement.where(
118
- // and(eq(metadata.seedLocalId, seedLocalId), propertyNameQuery),
119
- // )
120
84
  whereClauses.push(eq(metadata.seedLocalId, seedLocalId))
121
85
  }
122
86
 
123
- if (!hydrateQuery) {
124
- return
125
- }
126
-
127
87
  const rows = await appDb
128
88
  .select()
129
89
  .from(metadata)
@@ -147,11 +107,10 @@ export const hydrateFromDb = fromCallback<
147
107
  versionLocalId: versionLocalIdFromDb,
148
108
  versionUid: versionUidFromDb,
149
109
  refValueType,
150
- refResolvedValue,
151
110
  localStorageDir,
152
111
  } = firstRow
153
112
 
154
- let { refResolvedDisplayValue } = firstRow
113
+ let { refResolvedDisplayValue, refResolvedValue } = firstRow
155
114
 
156
115
  let propertyValueProcessed: string | string[] | undefined | null =
157
116
  propertyValueFromDb
@@ -193,26 +152,31 @@ export const hydrateFromDb = fromCallback<
193
152
  }
194
153
 
195
154
  if (
196
- refResolvedValue &&
197
- refResolvedDisplayValue &&
198
- refResolvedDisplayValue.includes('http')
155
+ propertyRecordSchema &&
156
+ propertyRecordSchema.refValueType === 'ImageSrc'
199
157
  ) {
200
- let urlNeedsToBeRefreshed = false
201
-
202
- try {
203
- const response = await fetch(refResolvedDisplayValue, {
204
- method: 'HEAD',
205
- })
158
+ let shouldReadFromFile = true
159
+
160
+ if (
161
+ refResolvedValue &&
162
+ refResolvedDisplayValue &&
163
+ refResolvedDisplayValue.includes('http')
164
+ ) {
165
+ try {
166
+ const response = await fetch(refResolvedDisplayValue, {
167
+ method: 'HEAD',
168
+ })
206
169
 
207
- // Check if the status is in the 200-299 range
208
- if (!response.ok) {
209
- urlNeedsToBeRefreshed = true
170
+ // Check if the status is in the 200-299 range
171
+ if (response.ok) {
172
+ shouldReadFromFile = false
173
+ }
174
+ } catch (error) {
175
+ shouldReadFromFile = true
210
176
  }
211
- } catch (error) {
212
- urlNeedsToBeRefreshed = true
213
177
  }
214
178
 
215
- if (urlNeedsToBeRefreshed) {
179
+ if (shouldReadFromFile) {
216
180
  let dir = localStorageDir
217
181
  if (
218
182
  !dir &&
@@ -221,6 +185,36 @@ export const hydrateFromDb = fromCallback<
221
185
  ) {
222
186
  dir = 'images'
223
187
  }
188
+
189
+ if (
190
+ !refResolvedValue &&
191
+ propertyValueFromDb &&
192
+ propertyValueFromDb.length === 66
193
+ ) {
194
+ const storageTransactionQuery = await appDb
195
+ .select({
196
+ propertyValue: metadata.propertyValue,
197
+ })
198
+ .from(metadata)
199
+ .where(
200
+ and(
201
+ eq(metadata.seedUid, propertyValueFromDb),
202
+ eq(metadata.propertyName, 'storageTransactionId'),
203
+ ),
204
+ )
205
+
206
+ if (storageTransactionQuery && storageTransactionQuery.length > 0) {
207
+ const row = storageTransactionQuery[0]
208
+ refResolvedValue = row.propertyValue
209
+ console.log('storageTransactionId', refResolvedValue)
210
+ await updateMetadata({
211
+ localId,
212
+ refResolvedValue,
213
+ localStorageDir: '/images',
214
+ })
215
+ }
216
+ }
217
+
224
218
  const filePath = `/files/${dir}/${refResolvedValue}`
225
219
  const fileExists = await fs.promises.exists(filePath)
226
220
  if (fileExists) {
@@ -1,127 +1,41 @@
1
- import { EventObject, fromCallback, Subscription } from 'xstate'
2
- import { isNode, isReactNative } from '@/shared'
3
- import {
4
- GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES,
5
- GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY,
6
- GLOBAL_INITIALIZING_SEND_CONFIG,
7
- } from '@/browser/services/internal/constants'
8
- import debug from 'debug'
9
- import { FromCallbackInput, GlobalMachineContext } from '@/types'
10
- import { getAppDb } from '@/browser/db/sqlWasmClient'
11
- import { appState } from '@/shared/seedSchema'
12
- import { like } from 'drizzle-orm'
13
-
14
- const logger = debug('app:services:global:actors:initialize')
15
-
16
- export const initialize = fromCallback<
17
- EventObject,
18
- FromCallbackInput<GlobalMachineContext>
19
- >(({ sendBack, input: { event, context } }) => {
20
- const { internalService, models, endpoints } = context
21
-
22
- const { addresses } = event
23
-
24
- let environment = 'browser'
25
-
26
- if (isNode()) {
27
- environment = 'node'
28
- }
29
-
30
- if (isReactNative()) {
31
- environment = 'react-native'
32
- }
33
-
34
- let internalSubscription: Subscription | undefined
35
- let easSubscription: Subscription | undefined
36
-
37
- if (environment === 'browser' && models) {
38
- const _initFileSystem = async (): Promise<void> => {
39
- return
40
- // return new Promise((resolve) => {
41
- // })
42
- }
43
-
44
- const _initInternal = async (): Promise<void> => {
45
- return new Promise((resolve) => {
46
- internalSubscription = internalService.subscribe((snapshot) => {
47
- logger('[sdk] [internal] snapshot', snapshot)
48
- if (snapshot.value === 'ready') {
49
- resolve()
50
- }
1
+ import { EventObject, fromCallback } from 'xstate'
2
+ import { itemMachineSingle } from '@/browser/item/single/itemMachineSingle'
3
+ import pluralize from 'pluralize'
4
+ import { getExistingItem } from '@/browser/db/read'
5
+
6
+ export const initialize = fromCallback<EventObject, typeof itemMachineSingle>(
7
+ ({ sendBack, input: { context } }) => {
8
+ const { seedLocalId, seedUid, ModelClass } = context
9
+
10
+ const modelName = ModelClass.originalConstructor.name
11
+ const modelNamePlural = pluralize(modelName)
12
+ const modelTableName = modelNamePlural.toLowerCase()
13
+
14
+ const _intialize = async (): Promise<void> => {
15
+ const existingItem = await getExistingItem({ seedUid, seedLocalId })
16
+
17
+ if (existingItem) {
18
+ sendBack({
19
+ type: 'hasExistingItem',
20
+ modelName,
21
+ modelTableName,
22
+ modelNamePlural,
23
+ existingItem,
51
24
  })
52
- internalService.send({ type: 'init', endpoints, addresses })
53
- })
54
- }
55
-
56
- const _initAllItemsServices = async (): Promise<void> => {
57
- const appDb = getAppDb()
58
-
59
- const rows = await appDb
60
- .select()
61
- .from(appState)
62
- .where(like(appState.key, 'snapshot__%'))
63
-
64
- const payloadObj = {
65
- create: {},
66
- restore: {},
25
+ return
67
26
  }
68
27
 
69
- const modelNamesRestored: string[] = []
70
-
71
- if (rows && rows.length > 0) {
72
- for (const row of rows) {
73
- const modelName = row.key.replace('snapshot__', '')
74
- payloadObj.restore[modelName] = JSON.parse(row.value)
75
- modelNamesRestored.push(modelName)
76
- }
77
- }
78
- for (const [modelName, ModelClass] of Object.entries(models)) {
79
- if (!modelNamesRestored.includes(modelName)) {
80
- payloadObj.create[modelName] = ModelClass
81
- }
82
- }
83
28
  sendBack({
84
- type: GLOBAL_INITIALIZING_CREATE_ALL_ITEMS_SERVICES,
85
- ...payloadObj,
29
+ type: 'isNewItem',
30
+ modelName,
31
+ modelTableName,
32
+ modelNamePlural,
86
33
  })
87
34
  }
88
35
 
89
- const _initEas = async (): Promise<void> => {
90
- // const { easService } = await import('@/browser/eas')
91
- // easService.send({ type: 'init', endpoints, models })
92
- // return new Promise((resolve) => {
93
- // easSubscription = easService.subscribe((snapshot) => {
94
- // if (snapshot.value === 'ready') {
95
- // resolve()
96
- // }
97
- // })
98
- // easService.send({ type: 'init', endpoints, models })
99
- // })
100
- }
101
-
102
- _initFileSystem().then(() => {
103
- logger('[global/actors] File system initialized')
36
+ _intialize().then(() => {
37
+ sendBack({ type: 'initializeSuccess' })
38
+ return
104
39
  })
105
-
106
- _initInternal()
107
- .then(() => {
108
- return _initAllItemsServices()
109
- })
110
- .then(() => {
111
- logger('[global/actors] Internal initialized')
112
- sendBack({ type: GLOBAL_INITIALIZING_INTERNAL_SERVICE_READY })
113
- internalSubscription?.unsubscribe()
114
- })
115
-
116
- // _initEas().then(() => {
117
- // logger('EAS initialized')
118
- // })
119
- }
120
-
121
- sendBack({ type: GLOBAL_INITIALIZING_SEND_CONFIG, environment })
122
-
123
- return () => {
124
- internalSubscription?.unsubscribe()
125
- easSubscription?.unsubscribe()
126
- }
127
- })
40
+ },
41
+ )
package/dist/src/item.ts CHANGED
@@ -251,7 +251,7 @@ export const useItems: UseItems = ({ modelName, deleted }) => {
251
251
  item.createdAt,
252
252
  ],
253
253
  ['desc'],
254
- ).slice(0, 10),
254
+ ),
255
255
  isReadingDb,
256
256
  }
257
257
  }