@seedprotocol/sdk 0.1.46 → 0.1.48

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 (147) hide show
  1. package/dist/bin.js.map +1 -1
  2. package/dist/constants-BLctWkrn.js.map +1 -1
  3. package/dist/{index-BEzB8REh.js → index-DMIKRod-.js} +3684 -3660
  4. package/dist/index-DMIKRod-.js.map +1 -0
  5. package/dist/{index-ChGsdGPJ.js → index-wKss7188.js} +10 -10
  6. package/dist/index-wKss7188.js.map +1 -0
  7. package/dist/main.js +7 -7
  8. package/dist/{seed.schema.config-jKpK-lR6.js → seed.schema.config-C0M8Rcti.js} +7 -7
  9. package/dist/seed.schema.config-C0M8Rcti.js.map +1 -0
  10. package/dist/src/AppStateSchema.ts +10 -0
  11. package/dist/src/Attestation.ts +21 -0
  12. package/dist/src/ConfigSchema.ts +15 -0
  13. package/dist/src/ItemProperty.ts +383 -0
  14. package/dist/src/MetadataSchema.ts +28 -0
  15. package/dist/src/ModelSchema.ts +46 -0
  16. package/dist/src/ModelUidSchema.ts +16 -0
  17. package/dist/src/PropertyUidSchema.ts +16 -0
  18. package/dist/src/Schema.ts +17 -0
  19. package/dist/src/SeedSchema.ts +29 -0
  20. package/dist/src/VersionSchema.ts +16 -0
  21. package/dist/src/actors.ts +10 -0
  22. package/dist/src/addModelsToDb.ts +150 -0
  23. package/dist/src/allItems.ts +23 -0
  24. package/dist/src/arweave.ts +37 -0
  25. package/dist/src/browser.app.db.config.ts +27 -0
  26. package/dist/src/browser.seed.db.config.ts +33 -0
  27. package/dist/src/browser.ts +30 -0
  28. package/dist/src/checkStatus.ts +50 -0
  29. package/dist/src/client.ts +85 -0
  30. package/dist/src/configureFs.ts +81 -0
  31. package/dist/src/connectToDb.ts +74 -0
  32. package/dist/src/connectionManager.ts +67 -0
  33. package/dist/src/constants.ts +118 -0
  34. package/dist/src/create.ts +39 -0
  35. package/dist/src/createItem.ts +15 -0
  36. package/dist/src/createItemMachine.ts +37 -0
  37. package/dist/src/createPublishAttempt.ts +16 -0
  38. package/dist/src/createSeeds.ts +24 -0
  39. package/dist/src/createVersion.ts +33 -0
  40. package/dist/src/db.ts +247 -0
  41. package/dist/src/dbMachine.ts +181 -0
  42. package/dist/src/deleteItem.ts +19 -0
  43. package/dist/src/download.ts +289 -0
  44. package/dist/src/drizzle.ts +82 -0
  45. package/dist/src/environment.ts +15 -0
  46. package/dist/src/eventBus.ts +5 -0
  47. package/dist/src/events.ts +14 -0
  48. package/dist/src/fetchDataFromEas.ts +90 -0
  49. package/dist/src/fetchDbData.ts +16 -0
  50. package/dist/src/fetchRelatedItems.ts +179 -0
  51. package/dist/src/fetchSeeds.ts +44 -0
  52. package/dist/src/fetchVersions.ts +41 -0
  53. package/dist/src/files.ts +16 -0
  54. package/dist/src/fragment-masking.ts +87 -0
  55. package/dist/src/fsProxy.ts +36 -0
  56. package/dist/src/getItem.ts +189 -0
  57. package/dist/src/getItemProperties.ts +162 -0
  58. package/dist/src/getItems.ts +75 -0
  59. package/dist/src/getMetadata.ts +40 -0
  60. package/dist/src/getModelSchemas.ts +88 -0
  61. package/dist/src/getSchemaForModel.ts +42 -0
  62. package/dist/src/getSeedData.ts +34 -0
  63. package/dist/src/getVersionData.ts +58 -0
  64. package/dist/src/getVersionsForVersionUids.ts +39 -0
  65. package/dist/src/globalMachine.ts +259 -0
  66. package/dist/src/gql.ts +113 -0
  67. package/dist/src/graphql.ts +3201 -0
  68. package/dist/src/helpers.ts +150 -0
  69. package/dist/src/hydrateExistingItem.ts +137 -0
  70. package/dist/src/hydrateFromDb.ts +254 -0
  71. package/dist/src/hydrateNewItem.ts +34 -0
  72. package/dist/src/index.d.ts +5 -0
  73. package/dist/src/index.ts +21 -0
  74. package/dist/src/init.ts +61 -0
  75. package/dist/src/initialize.ts +127 -0
  76. package/dist/src/internalMachine.ts +220 -0
  77. package/dist/src/item.ts +324 -0
  78. package/dist/src/itemMachineAll.ts +158 -0
  79. package/dist/src/itemMachineSingle.ts +175 -0
  80. package/dist/src/loadAppDb.ts +47 -0
  81. package/dist/src/logger.ts +33 -0
  82. package/dist/src/machine.ts +55 -0
  83. package/dist/src/machines.ts +58 -0
  84. package/dist/src/migrate.ts +288 -0
  85. package/dist/src/model.ts +71 -0
  86. package/dist/src/modelClass.ts +19 -0
  87. package/dist/src/node.app.db.config.ts +40 -0
  88. package/dist/src/prepareDb.ts +34 -0
  89. package/dist/src/preparePublishRequestData.ts +81 -0
  90. package/dist/src/processItems.ts +71 -0
  91. package/dist/src/property.ts +161 -0
  92. package/dist/src/propertyMachine.ts +154 -0
  93. package/dist/src/publish.ts +31 -0
  94. package/dist/src/publishMachine.ts +77 -0
  95. package/dist/src/queries.ts +13 -0
  96. package/dist/src/read.ts +174 -0
  97. package/dist/src/recoverDeletedItem.ts +14 -0
  98. package/dist/src/request.ts +54 -0
  99. package/dist/src/requestAll.ts +157 -0
  100. package/dist/src/resolveRelatedValue.ts +348 -0
  101. package/dist/src/resolveRemoteStorage.ts +87 -0
  102. package/dist/src/save.ts +183 -0
  103. package/dist/src/saveConfig.ts +79 -0
  104. package/dist/src/saveDataToDb.ts +145 -0
  105. package/dist/src/saveMetadata.ts +18 -0
  106. package/dist/src/saveValueToDb.ts +94 -0
  107. package/dist/src/seed.schema.config.ts +25 -0
  108. package/dist/src/seed.ts +37 -0
  109. package/dist/src/seedData.ts +0 -0
  110. package/dist/src/seedProtocol.ts +17 -0
  111. package/dist/src/services.ts +359 -0
  112. package/dist/src/sqlWasmClient.ts +88 -0
  113. package/dist/src/syncDbWithEas.ts +686 -0
  114. package/dist/src/trash.ts +29 -0
  115. package/dist/src/ts-to-proto.ts +101 -0
  116. package/dist/src/types.ts +12 -0
  117. package/dist/src/upload.ts +86 -0
  118. package/dist/src/validate.ts +42 -0
  119. package/dist/src/validateInput.ts +33 -0
  120. package/dist/src/validateItemData.ts +20 -0
  121. package/dist/src/waitForDb.ts +23 -0
  122. package/dist/src/wasm.d.ts +8300 -0
  123. package/dist/src/write.ts +366 -0
  124. package/dist/types/src/browser/db/read/getModelSchemas.d.ts.map +1 -1
  125. package/dist/types/src/browser/events/item/create.d.ts.map +1 -1
  126. package/dist/types/src/browser/events/item/publish.d.ts.map +1 -1
  127. package/dist/types/src/browser/events/item/requestAll.d.ts.map +1 -1
  128. package/dist/types/src/browser/events/item/syncDbWithEas.d.ts.map +1 -1
  129. package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts +3 -1
  130. package/dist/types/src/browser/item/single/actors/hydrateExistingItem.d.ts.map +1 -1
  131. package/dist/types/src/browser/item/single/actors/saveDataToDb.d.ts +3 -3
  132. package/dist/types/src/browser/item/single/itemMachineSingle.d.ts +3 -3
  133. package/dist/types/src/browser/property/ItemProperty.d.ts +7 -7
  134. package/dist/types/src/browser/property/ItemProperty.d.ts.map +1 -1
  135. package/dist/types/src/browser/property/actors/hydrateFromDb.d.ts.map +1 -1
  136. package/dist/types/src/browser/property/actors/initialize.d.ts.map +1 -1
  137. package/dist/types/src/browser/property/actors/resolveRemoteStorage.d.ts +3 -3
  138. package/dist/types/src/browser/react/trash.d.ts +1 -1
  139. package/dist/types/src/browser/react/trash.d.ts.map +1 -1
  140. package/dist/types/src/browser/services/db/dbMachine.d.ts +6 -6
  141. package/dist/types/src/browser/services/publish/publishMachine.d.ts +17 -17
  142. package/dist/types/src/types/machines.d.ts +4 -0
  143. package/dist/types/src/types/machines.d.ts.map +1 -1
  144. package/package.json +1 -1
  145. package/dist/index-BEzB8REh.js.map +0 -1
  146. package/dist/index-ChGsdGPJ.js.map +0 -1
  147. package/dist/seed.schema.config-jKpK-lR6.js.map +0 -1
@@ -1,4 +1,4 @@
1
- export { f as GET_ALL_PROPERTIES_FOR_ALL_VERSIONS, e as GET_PROPERTIES, G as GET_SCHEMAS, a as GET_SEEDS, b as GET_SEED_IDS, c as GET_STORAGE_TRANSACTION_ID, d as GET_VERSIONS, I as Item, g as itemMachineAll, i as itemMachineSingle } from './index-BEzB8REh.js';
1
+ export { f as GET_ALL_PROPERTIES_FOR_ALL_VERSIONS, e as GET_PROPERTIES, G as GET_SCHEMAS, a as GET_SEEDS, b as GET_SEED_IDS, c as GET_STORAGE_TRANSACTION_ID, d as GET_VERSIONS, I as Item, g as itemMachineAll, i as itemMachineSingle } from './index-DMIKRod-.js';
2
2
  import './constants-BLctWkrn.js';
3
3
  import 'path';
4
4
  import 'reflect-metadata';
@@ -13,18 +13,18 @@ import 'debug';
13
13
  import 'lodash-es';
14
14
  import 'drizzle-orm/sqlite-core';
15
15
  import 'eventemitter3';
16
- import 'react';
17
- import 'rxjs';
18
- import 'immer';
19
- import 'pluralize';
20
16
  import '@tanstack/react-query';
21
17
  import 'graphql-request';
22
18
  import '@tanstack/query-sync-storage-persister';
23
19
  import '@tanstack/react-query-persist-client';
24
- import 'use-immer';
25
- import '@xstate/react';
20
+ import 'rxjs';
21
+ import 'immer';
22
+ import 'pluralize';
26
23
  import '@statelyai/inspect';
27
- import 'arweave';
28
- import '@zenfs/dom';
29
24
  import 'drizzle-orm/sqlite-proxy';
30
- //# sourceMappingURL=index-ChGsdGPJ.js.map
25
+ import '@zenfs/dom';
26
+ import 'arweave';
27
+ import 'react';
28
+ import 'use-immer';
29
+ import '@xstate/react';
30
+ //# sourceMappingURL=index-wKss7188.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-wKss7188.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/main.js CHANGED
@@ -1,4 +1,4 @@
1
- export { h as ImageSrc, I as Item, j as ItemProperty, J as Json, L as List, M as Model, P as Property, R as Relation, T as Text, s as client, t as getCorrectId, r as getGlobalService, m as useCreateItem, o as useDeleteItem, p as useGlobalServiceStatus, k as useItem, l as useItemProperties, n as useItemProperty, u as useItems, q as useServices, w as withSeed } from './index-BEzB8REh.js';
1
+ export { h as ImageSrc, I as Item, j as ItemProperty, J as Json, L as List, M as Model, P as Property, R as Relation, T as Text, s as client, t as getCorrectId, r as getGlobalService, m as useCreateItem, o as useDeleteItem, p as useGlobalServiceStatus, k as useItem, l as useItemProperties, n as useItemProperty, u as useItems, q as useServices, w as withSeed } from './index-DMIKRod-.js';
2
2
  import 'immer';
3
3
  import 'reflect-metadata';
4
4
  import './constants-BLctWkrn.js';
@@ -15,16 +15,16 @@ import 'debug';
15
15
  import 'lodash-es';
16
16
  import 'drizzle-orm/sqlite-core';
17
17
  import 'eventemitter3';
18
- import 'react';
19
- import 'rxjs';
20
- import 'pluralize';
21
18
  import '@tanstack/react-query';
22
19
  import 'graphql-request';
23
20
  import '@tanstack/query-sync-storage-persister';
24
21
  import '@tanstack/react-query-persist-client';
25
- import 'use-immer';
26
- import '@xstate/react';
22
+ import 'rxjs';
23
+ import 'pluralize';
27
24
  import '@statelyai/inspect';
28
- import '@zenfs/dom';
29
25
  import 'drizzle-orm/sqlite-proxy';
26
+ import '@zenfs/dom';
27
+ import 'react';
28
+ import 'use-immer';
29
+ import '@xstate/react';
30
30
  //# sourceMappingURL=main.js.map
@@ -1,6 +1,6 @@
1
1
  import { _ as __decorate, a as __metadata } from './constants-BLctWkrn.js';
2
2
  import 'drizzle-orm';
3
- import { T as Text, M as Model } from './index-BEzB8REh.js';
3
+ import { T as Text, M as Model } from './index-DMIKRod-.js';
4
4
  import 'react';
5
5
  import 'reflect-metadata';
6
6
  import 'xstate';
@@ -16,17 +16,17 @@ import 'debug';
16
16
  import 'lodash-es';
17
17
  import 'drizzle-orm/sqlite-core';
18
18
  import 'eventemitter3';
19
- import 'rxjs';
20
- import 'immer';
21
- import 'pluralize';
22
19
  import '@tanstack/react-query';
23
20
  import 'graphql-request';
24
21
  import '@tanstack/query-sync-storage-persister';
25
22
  import '@tanstack/react-query-persist-client';
26
- import 'use-immer';
27
- import '@xstate/react';
23
+ import 'rxjs';
24
+ import 'immer';
25
+ import 'pluralize';
28
26
  import '@statelyai/inspect';
29
27
  import 'drizzle-orm/sqlite-proxy';
28
+ import 'use-immer';
29
+ import '@xstate/react';
30
30
 
31
31
  let Seed = class Seed {
32
32
  };
@@ -74,4 +74,4 @@ const models = {
74
74
  };
75
75
 
76
76
  export { models };
77
- //# sourceMappingURL=seed.schema.config-jKpK-lR6.js.map
77
+ //# sourceMappingURL=seed.schema.config-C0M8Rcti.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"seed.schema.config-C0M8Rcti.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;;;;;"}
@@ -0,0 +1,10 @@
1
+ import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
2
+
3
+ export const appState = sqliteTable('appState', {
4
+ key: text('key').unique(),
5
+ value: text('value'),
6
+ createdAt: int('created_at'),
7
+ updatedAt: int('updated_at'),
8
+ })
9
+
10
+ export type appStateType = appState.$inferSelect
@@ -0,0 +1,21 @@
1
+ import { graphql, } from '../gql'
2
+
3
+ export const ATTESTATION_FIELDS = graphql(/* GraphQL */ `
4
+ fragment attestationFields on Attestation {
5
+ id
6
+ decodedDataJson
7
+ attester
8
+ schema {
9
+ schemaNames {
10
+ name
11
+ }
12
+ }
13
+ refUID
14
+ revoked
15
+ schemaId
16
+ txid
17
+ timeCreated
18
+ time
19
+ isOffchain
20
+ }
21
+ `,)
@@ -0,0 +1,15 @@
1
+ import { blob, int, sqliteTable, text, check, } from 'drizzle-orm/sqlite-core'
2
+ import { sql } from 'drizzle-orm'
3
+
4
+ export const config = sqliteTable('config', {
5
+ id: int('id').primaryKey({ autoIncrement: true }),
6
+ key: text('key').notNull(),
7
+ text: text('text'),
8
+ json: text('json', {mode: 'json'}),
9
+ blob: blob('blob', {mode: 'buffer'}),
10
+ }, () => {
11
+ return {
12
+ // Add a custom check constraint
13
+ atLeastOneNotNull: check('hasValue', sql`key IS NOT NULL OR text IS NOT NULL OR json IS NOT NULL OR blob IS NOT NULL`),
14
+ }
15
+ })
@@ -0,0 +1,383 @@
1
+ import { ItemPropertyProps, PropertyMachineContext } from '@/types'
2
+ import { ActorRefFrom, createActor, Subscription, waitFor } from 'xstate'
3
+ import { BehaviorSubject, Subscriber } from 'rxjs'
4
+ import { propertyMachine } from './propertyMachine'
5
+ import { immerable } from 'immer'
6
+ import pluralize from 'pluralize'
7
+ import { eventEmitter } from '@/eventBus'
8
+ import { getPropertyData } from '@/browser/db/read'
9
+ import { internalPropertyNames } from '@/shared/helpers/constants'
10
+ import debug from 'debug'
11
+ import { getModel } from '@/browser/stores/modelClass'
12
+ import { getCorrectId } from '@/browser/helpers'
13
+ import { Static } from '@sinclair/typebox'
14
+ import { TProperty } from '@/browser/property'
15
+
16
+ const logger = debug('app:property:class')
17
+
18
+ type ItemPropertyFindProps = {
19
+ propertyName: string
20
+ propertyLocalId?: string
21
+ seedLocalId?: string
22
+ seedUid?: string
23
+ }
24
+
25
+ const namesThatEndWithId: string[] = []
26
+
27
+ export class ItemProperty<PropertyType> {
28
+ private static instanceCache: Map<
29
+ string,
30
+ { instance: ItemProperty<any>; refCount: number }
31
+ > = new Map()
32
+ private readonly _service: ActorRefFrom<typeof propertyMachine>
33
+ private _subject: BehaviorSubject<any>
34
+ private readonly _isRelation: boolean = false
35
+ private readonly _isList: boolean = false
36
+ private readonly _alias: string | undefined
37
+ private _subscription: Subscription
38
+ private _dataType: string | undefined
39
+ private _schemaUid: string | undefined
40
+ private _updateResponseEvent: string | undefined;
41
+ [immerable] = true
42
+
43
+ // private constructor(localIdOrUid) {
44
+ // }
45
+
46
+ constructor({
47
+ propertyValue,
48
+ seedUid,
49
+ seedLocalId,
50
+ itemModelName,
51
+ propertyName,
52
+ storageTransactionId,
53
+ schemaUid,
54
+ }: ItemPropertyProps) {
55
+ const ModelClass = getModel(itemModelName)
56
+
57
+ if (!ModelClass) {
58
+ throw new Error(`Model ${itemModelName} not found`)
59
+ }
60
+
61
+ if (!propertyName) {
62
+ throw new Error(`Property name not provided`)
63
+ }
64
+
65
+ this._lastRenderedValue = propertyValue
66
+
67
+ const serviceInput: Partial<PropertyMachineContext> = {
68
+ propertyValue,
69
+ seedUid,
70
+ propertyName,
71
+ seedLocalId,
72
+ itemModelName,
73
+ storageTransactionId,
74
+ propertyRecordSchema: ModelClass.schema[propertyName],
75
+ schemaUid,
76
+ }
77
+
78
+ if (!internalPropertyNames.includes(propertyName)) {
79
+ let propertyNameWithoutId
80
+
81
+ if (
82
+ propertyName.endsWith('Id') &&
83
+ !namesThatEndWithId.includes(propertyName)
84
+ ) {
85
+ propertyNameWithoutId = propertyName.slice(0, -2)
86
+ }
87
+
88
+ const propertyRecordSchema =
89
+ ModelClass.schema[propertyNameWithoutId || propertyName]
90
+ if (propertyRecordSchema) {
91
+ this._dataType = propertyRecordSchema.dataType
92
+
93
+ serviceInput.propertyRecordSchema = propertyRecordSchema
94
+
95
+ if (propertyRecordSchema.dataType === 'Relation') {
96
+ this._isRelation = true
97
+ }
98
+
99
+ if (
100
+ propertyRecordSchema.dataType === 'List' &&
101
+ propertyRecordSchema.ref
102
+ ) {
103
+ this._isList = true
104
+ this._isRelation = true
105
+
106
+ if (propertyValue) {
107
+ try {
108
+ serviceInput.propertyValue = JSON.parse(propertyValue)
109
+ } catch (e) {
110
+ logger('List property value is not JSON', e)
111
+ }
112
+ }
113
+
114
+ const propertyNameSingular = pluralize(propertyName!, 1)
115
+
116
+ this._alias = propertyName
117
+
118
+ serviceInput.propertyName = `${propertyNameSingular}${propertyRecordSchema.ref}Ids`
119
+ }
120
+
121
+ if (!this._alias && propertyNameWithoutId) {
122
+ this._alias = propertyNameWithoutId
123
+ }
124
+ }
125
+ }
126
+
127
+ serviceInput.isRelation = this._isRelation
128
+
129
+ this._subject = new BehaviorSubject(propertyValue)
130
+ this._service = createActor(propertyMachine, {
131
+ input: serviceInput,
132
+ })
133
+ this._subscription = this._service.subscribe((snapshot) => {
134
+ const { context } = snapshot
135
+
136
+ // if (this.uid && !cacheHasProperty(this.uid)) {
137
+ // addPropertyToCache(this)
138
+ // }
139
+ //
140
+ // if (!this.uid && this.localId && !cacheHasProperty(this.localId,)) {
141
+ // addPropertyToCache(this)
142
+ // }
143
+
144
+ if (context.seedLocalId) {
145
+ const cacheKey = ItemProperty.cacheKey(
146
+ context.seedLocalId,
147
+ context.propertyName,
148
+ )
149
+ if (!ItemProperty.instanceCache.has(cacheKey)) {
150
+ ItemProperty.instanceCache.set(cacheKey, {
151
+ instance: this,
152
+ refCount: 1,
153
+ })
154
+ }
155
+ // this._updateResponseEvent = `property.${context.seedLocalId}.${this.propertyName}.save.response`
156
+ // eventEmitter.addListener(
157
+ // this._updateResponseEvent,
158
+ // this._updateResponseListener,
159
+ // )
160
+ }
161
+
162
+ const renderValue = context.renderValue || context.propertyValue
163
+
164
+ let transformedPropertyName = propertyName
165
+
166
+ const skipTransform =
167
+ internalPropertyNames.includes(propertyName) || !!this._alias
168
+
169
+ if (!skipTransform && transformedPropertyName.endsWith('Id')) {
170
+ transformedPropertyName = transformedPropertyName.slice(0, -2)
171
+ }
172
+
173
+ if (!skipTransform && transformedPropertyName.endsWith('Ids')) {
174
+ transformedPropertyName = transformedPropertyName.slice(0, -3)
175
+ }
176
+
177
+ if (skipTransform && this._alias) {
178
+ transformedPropertyName = this._alias
179
+ }
180
+
181
+ if (skipTransform && !this._alias) {
182
+ }
183
+
184
+ this._subject.next(renderValue)
185
+ if (context.seedLocalId) {
186
+ eventEmitter.emit(`item.${itemModelName}.${context.seedLocalId}.update`)
187
+ }
188
+ if (context.seedUid) {
189
+ eventEmitter.emit(`item.${itemModelName}.${context.seedUid}.update`)
190
+ }
191
+ })
192
+
193
+ this._service.start()
194
+ }
195
+
196
+ private _updateResponseListener(event) {
197
+ logger(
198
+ `[ItemProperty] [_updateResponseListener] [${this.itemModelName}.${this.seedLocalId}] ${this.propertyName} event`,
199
+ event,
200
+ )
201
+ }
202
+
203
+ static create(props: ItemPropertyProps): ItemProperty<any> | undefined {
204
+ const { propertyName, seedLocalId, seedUid } = props
205
+ if (!propertyName || (!seedLocalId && !seedUid)) {
206
+ return
207
+ }
208
+ const cacheKey = ItemProperty.cacheKey(
209
+ (seedUid || seedLocalId) as string,
210
+ propertyName,
211
+ )
212
+ if (seedLocalId && propertyName) {
213
+ if (ItemProperty.instanceCache.has(cacheKey)) {
214
+ const { instance, refCount } = ItemProperty.instanceCache.get(cacheKey)!
215
+ ItemProperty.instanceCache.set(cacheKey, {
216
+ instance,
217
+ refCount: refCount + 1,
218
+ })
219
+ return instance
220
+ }
221
+ if (!ItemProperty.instanceCache.has(cacheKey)) {
222
+ const newInstance = new ItemProperty(props)
223
+ ItemProperty.instanceCache.set(cacheKey, {
224
+ instance: newInstance,
225
+ refCount: 1,
226
+ })
227
+ return newInstance
228
+ }
229
+ }
230
+ if (seedUid && propertyName) {
231
+ if (this.instanceCache.has(cacheKey)) {
232
+ const { instance, refCount } = this.instanceCache.get(cacheKey)!
233
+ this.instanceCache.set(cacheKey, { instance, refCount: refCount + 1 })
234
+ return instance
235
+ }
236
+ if (!this.instanceCache.has(cacheKey)) {
237
+ const newInstance = new ItemProperty(props)
238
+ this.instanceCache.set(cacheKey, { instance: newInstance, refCount: 1 })
239
+ return newInstance
240
+ }
241
+ }
242
+ return new ItemProperty(props)
243
+ }
244
+
245
+ static async find({
246
+ propertyName,
247
+ seedLocalId,
248
+ seedUid,
249
+ }: ItemPropertyFindProps): Promise<ItemProperty<any> | undefined> {
250
+ if ((!seedLocalId && !seedUid) || !propertyName) {
251
+ return
252
+ }
253
+ const cacheKeyId = seedUid || seedLocalId
254
+ const cacheKey = ItemProperty.cacheKey(cacheKeyId!, propertyName)
255
+ if (this.instanceCache.has(cacheKey)) {
256
+ const { instance, refCount } = this.instanceCache.get(cacheKey)!
257
+ this.instanceCache.set(cacheKey, {
258
+ instance,
259
+ refCount: refCount + 1,
260
+ })
261
+ if (instance.status === 'idle') {
262
+ instance.getService().send({ type: 'refresh' })
263
+ }
264
+ return instance
265
+ }
266
+ const propertyData = await getPropertyData(
267
+ propertyName,
268
+ seedLocalId,
269
+ seedUid,
270
+ )
271
+ if (!propertyData) {
272
+ return
273
+ }
274
+ return ItemProperty.create(propertyData)
275
+ }
276
+
277
+ static cacheKey(seedLocalIdOrUid: string, propertyName: string): string {
278
+ const { uid, localId } = getCorrectId(seedLocalIdOrUid)
279
+ return `Item_${uid || localId}_${propertyName}`
280
+ }
281
+
282
+ getService() {
283
+ return this._service
284
+ }
285
+
286
+ get localId() {
287
+ return this._service.getSnapshot().context.localId
288
+ }
289
+
290
+ get uid() {
291
+ return this._service.getSnapshot().context.uid
292
+ }
293
+
294
+ get seedLocalId() {
295
+ return this._service.getSnapshot().context.seedLocalId
296
+ }
297
+
298
+ get seedUid() {
299
+ return this._service.getSnapshot().context.seedUid
300
+ }
301
+
302
+ get propertyName() {
303
+ if (this._alias) {
304
+ return this._alias
305
+ }
306
+ return this._service.getSnapshot().context.propertyName
307
+ }
308
+
309
+ get itemModelName() {
310
+ return this._service.getSnapshot().context.itemModelName
311
+ }
312
+
313
+ get propertyDef(): Static<typeof TProperty> | undefined {
314
+ return this._service.getSnapshot().context.propertyRecordSchema
315
+ }
316
+
317
+ get status() {
318
+ return this._service.getSnapshot().value
319
+ }
320
+
321
+ get alias() {
322
+ return this._alias
323
+ }
324
+
325
+ get value() {
326
+ // logger(
327
+ // `[XXXXXXXXXX] [value] [get] subjectValue: ${this._subject.value} serviceValue: ${this._service.getSnapshot().context.renderValue}`,
328
+ // )
329
+ return this._subject.value
330
+ }
331
+
332
+ set value(value: any) {
333
+ if (this._subject.value === value) {
334
+ return
335
+ }
336
+ const context = this._service.getSnapshot().context
337
+ if (!context.propertyRecordSchema) {
338
+ this._subject.next(value)
339
+ }
340
+ this._service.send({
341
+ type: 'save',
342
+ newValue: value,
343
+ })
344
+ }
345
+
346
+ get published(): boolean {
347
+ return !!this._service.getSnapshot().context.uid
348
+ }
349
+
350
+ subscribe(callback: Partial<Subscriber<any>>) {
351
+ return this._subject.subscribe(callback)
352
+ }
353
+
354
+ async save(): Promise<void> {
355
+ await waitFor(
356
+ this._service,
357
+ (snapshot) => !snapshot.context.isSaving && snapshot.value === 'idle',
358
+ {
359
+ timeout: 10_000,
360
+ },
361
+ )
362
+ // return new Promise((resolve) => {
363
+ // const saveSub = this._service.subscribe((snapshot) => {
364
+ // if (!snapshot.context.isSaving) {
365
+ // saveSub.unsubscribe()
366
+ // resolve()
367
+ // }
368
+ // })
369
+ // })
370
+ }
371
+
372
+ unload() {
373
+ this._service.stop()
374
+ logger(
375
+ `[XXXXXX] [ItemProperty] [${this.seedLocalId}] [unload] removing listener`,
376
+ this._updateResponseEvent,
377
+ )
378
+ eventEmitter.removeListener(
379
+ this._updateResponseEvent,
380
+ this._updateResponseListener,
381
+ )
382
+ }
383
+ }
@@ -0,0 +1,28 @@
1
+ import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
2
+
3
+ export const metadata = sqliteTable('metadata', {
4
+ localId: text('local_id').unique(),
5
+ uid: text('uid'),
6
+ propertyName: text('property_name'),
7
+ propertyValue: text('property_value'),
8
+ schemaUid: text('schema_uid'),
9
+ modelType: text('model_type'),
10
+ seedLocalId: text('seed_local_id'),
11
+ seedUid: text('seed_uid'),
12
+ versionLocalId: text('version_local_id'),
13
+ versionUid: text('version_uid'),
14
+ easDataType: text('eas_data_type'),
15
+ refValueType: text('ref_value_type'),
16
+ refModelUid: text('ref_schema_uid'),
17
+ refSeedType: text('ref_seed_type'),
18
+ refResolvedValue: text('ref_resolved_value'),
19
+ refResolvedDisplayValue: text('ref_resolved_display_value'),
20
+ localStorageDir: text('local_storage_dir'),
21
+ attestationRaw: text('attestation_raw'),
22
+ attestationCreatedAt: int('attestation_created_at'),
23
+ contentHash: text('content_hash'),
24
+ createdAt: int('created_at'),
25
+ updatedAt: int('updated_at'),
26
+ })
27
+
28
+ export type MetadataType = metadata.$inferSelect
@@ -0,0 +1,46 @@
1
+ import { int, sqliteTable, text, unique } from 'drizzle-orm/sqlite-core'
2
+ import { relations } from 'drizzle-orm'
3
+
4
+ export const models = sqliteTable('models', {
5
+ id: int('id').primaryKey({ autoIncrement: true }),
6
+ name: text('name').notNull(),
7
+ })
8
+
9
+ export const modelsRelations = relations(models, ({ many }) => ({
10
+ properties: many(properties),
11
+ }))
12
+
13
+ export type NewModelRecord = typeof models.$inferInsert
14
+ export type ModelRecordType = typeof models.$inferSelect
15
+
16
+ export const properties = sqliteTable(
17
+ 'properties',
18
+ {
19
+ id: int('id').primaryKey({ autoIncrement: true }),
20
+ name: text('name').notNull(),
21
+ dataType: text('data_type').notNull(),
22
+ readEndpoint: text('read_endpoint'),
23
+ updateEndpoint: text('update_endpoint'),
24
+ modelId: int('model_id')
25
+ .notNull()
26
+ .references(() => models.id),
27
+ refModelId: int('ref_model_id').references(() => models.id),
28
+ refValueType: text('ref_value_type'),
29
+ },
30
+ (table) => {
31
+ return {
32
+ uniqueNameModelId: unique('unique_name_model_id').on(
33
+ table.name,
34
+ table.modelId,
35
+ ),
36
+ }
37
+ },
38
+ )
39
+
40
+ export const propertiesRelations = relations(properties, ({ one }) => ({
41
+ model: one(models),
42
+ refModel: one(models),
43
+ }))
44
+
45
+ export type NewPropertyRecord = typeof properties.$inferInsert
46
+ export type PropertyType = typeof properties.$inferSelect
@@ -0,0 +1,16 @@
1
+ import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
2
+ import { relations } from 'drizzle-orm'
3
+ import { models } from './ModelSchema'
4
+
5
+ export const modelUids = sqliteTable('model_uids', {
6
+ id: int('id').primaryKey({ autoIncrement: true }),
7
+ uid: text('uid').notNull(),
8
+ modelId: int('model_id')
9
+ .notNull()
10
+ .unique()
11
+ .references(() => models.id),
12
+ })
13
+
14
+ export const modelRelations = relations(modelUids, ({ many, one }) => ({
15
+ model: one(models),
16
+ }))
@@ -0,0 +1,16 @@
1
+ import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core'
2
+ import { relations } from 'drizzle-orm'
3
+ import { models, properties } from './ModelSchema'
4
+
5
+ export const propertyUids = sqliteTable('property_uids', {
6
+ id: int('id').primaryKey({ autoIncrement: true }),
7
+ uid: text('uid').notNull(),
8
+ propertyId: int('property_id')
9
+ .notNull()
10
+ .unique()
11
+ .references(() => models.id),
12
+ })
13
+
14
+ export const propertyUidRelations = relations(propertyUids, ({ one }) => ({
15
+ property: one(properties),
16
+ }))
@@ -0,0 +1,17 @@
1
+ import { graphql, } from '../gql'
2
+
3
+ export const SCHEMA_FIELDS = graphql(/* GraphQL */ `
4
+ fragment schemaFields on Schema {
5
+ id
6
+ resolver
7
+ revocable
8
+ schema
9
+ index
10
+ schemaNames {
11
+ name
12
+ }
13
+ time
14
+ txid
15
+ creator
16
+ }
17
+ `,)