@seedprotocol/sdk 0.1.7 → 0.1.10

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { n as __awaiter, D as DB_NAME_SDK_CONFIG, o as models, S as SCHEMA_NJK, p as SCHEMA_TS, q as SDK_DB_CONFIG, r as SEED_DB_CONFIG, A as APP_DB_CONFIG, s as addModelsToInternalDb } from './index-B8GMq1lE.js';
2
+ import { n as __awaiter, D as DB_NAME_SDK_CONFIG, o as models, S as SCHEMA_NJK, p as SCHEMA_TS, q as SDK_DB_CONFIG, r as SEED_DB_CONFIG, A as APP_DB_CONFIG, s as addModelsToInternalDb } from './index-C6vnbWlE.js';
3
3
  import path from 'path';
4
4
  import fs from 'fs';
5
5
  import { exec as exec$1 } from 'child_process';
@@ -17,7 +17,7 @@ import { camelCase, snakeCase } from 'lodash-es';
17
17
  import * as nunjucks from 'nunjucks';
18
18
  import Database from 'better-sqlite3';
19
19
  import { drizzle } from 'drizzle-orm/better-sqlite3';
20
- import './seed.schema.config-Bq0uQNxG.js';
20
+ import './seed.schema.config-D7dRgRFf.js';
21
21
  import { defineConfig } from 'drizzle-kit';
22
22
  import dotenv from 'dotenv';
23
23
  import 'reflect-metadata';
@@ -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-B8GMq1lE.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-C6vnbWlE.js';
2
2
  import 'path';
3
3
  import 'reflect-metadata';
4
4
  import '@sinclair/typebox';
@@ -26,4 +26,4 @@ import 'arweave';
26
26
  import '@zenfs/dom';
27
27
  import 'use-immer';
28
28
  import '@xstate/react';
29
- //# sourceMappingURL=index-Bc3TXOPI.js.map
29
+ //# sourceMappingURL=index-BwUxXJ6P.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-BwUxXJ6P.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -951,7 +951,11 @@ const getPropertyData = (propertyName, seedLocalId) => __awaiter(void 0, void 0,
951
951
  updatedAt: row[12],
952
952
  };
953
953
  });
954
+ const seedUidToStorageTransactionId = new Map();
954
955
  const getStorageTransactionIdForSeedUid = (seedUid) => __awaiter(void 0, void 0, void 0, function* () {
956
+ if (seedUidToStorageTransactionId.has(seedUid)) {
957
+ return seedUidToStorageTransactionId.get(seedUid);
958
+ }
955
959
  const storageTransactionIdStatement = `SELECT property_value,
956
960
  MAX(attestation_created_at),
957
961
  ref_resolved_display_value,
@@ -968,6 +972,7 @@ const getStorageTransactionIdForSeedUid = (seedUid) => __awaiter(void 0, void 0,
968
972
  if (!rows || rows.length === 0 || !rows[0][0]) {
969
973
  return;
970
974
  }
975
+ seedUidToStorageTransactionId.set(seedUid, rows[0][0]);
971
976
  return rows[0][0];
972
977
  });
973
978
  const getRelationValueData = (propertyValue) => __awaiter(void 0, void 0, void 0, function* () {
@@ -1029,16 +1034,46 @@ const getExistingItem = (_a) => __awaiter(void 0, [_a], void 0, function* ({ see
1029
1034
  return existingItem;
1030
1035
  });
1031
1036
 
1037
+ const storageTransactionIdToContentUrl = new Map();
1038
+ const refResolvedValueToContentUrl = new Map();
1039
+ const seedUidToContentUrl = new Map();
1032
1040
  const resolveRelatedValue = fromCallback(({ sendBack, input: { context } }) => {
1033
1041
  const { isRelation, propertyValue, propertyName, seedUid, propertyRecordSchema, seedLocalId, schemaUid, } = context;
1034
- getAppDb();
1035
1042
  const _resolveRelatedValue = () => __awaiter(void 0, void 0, void 0, function* () {
1036
1043
  if (!propertyValue || !isRelation) {
1037
1044
  return;
1038
1045
  }
1046
+ if (seedUidToContentUrl.has(propertyValue)) {
1047
+ const contentUrl = seedUidToContentUrl.get(propertyValue);
1048
+ sendBack({
1049
+ type: 'updateRenderValue',
1050
+ renderValue: contentUrl,
1051
+ });
1052
+ sendBack({
1053
+ type: 'resolvingRelatedValueSuccess',
1054
+ resolvedDisplayValue: contentUrl,
1055
+ });
1056
+ return true;
1057
+ }
1039
1058
  const storageTransactionId = yield getStorageTransactionIdForSeedUid(propertyValue);
1040
1059
  if (storageTransactionId) {
1060
+ if (storageTransactionIdToContentUrl.has(storageTransactionId)) {
1061
+ const contentUrl = storageTransactionIdToContentUrl.get(storageTransactionId);
1062
+ sendBack({
1063
+ type: 'updateRenderValue',
1064
+ renderValue: contentUrl,
1065
+ });
1066
+ sendBack({
1067
+ type: 'resolvingRelatedValueSuccess',
1068
+ resolvedDisplayValue: contentUrl,
1069
+ resolvedValue: storageTransactionId,
1070
+ });
1071
+ return true;
1072
+ }
1041
1073
  const contentUrl = yield convertTxIdToImageSrc(storageTransactionId);
1074
+ if (contentUrl) {
1075
+ seedUidToContentUrl.set(propertyValue, contentUrl);
1076
+ }
1042
1077
  sendBack({
1043
1078
  type: 'updateRenderValue',
1044
1079
  renderValue: contentUrl,
@@ -1061,11 +1096,24 @@ const resolveRelatedValue = fromCallback(({ sendBack, input: { context } }) => {
1061
1096
  propertyRecordSchema.dataType === 'Relation' &&
1062
1097
  propertyValueFromDb.length === 10 &&
1063
1098
  propertyRecordSchema.ref) {
1099
+ if (refResolvedValueToContentUrl.has(refResolvedValue)) {
1100
+ const contentUrl = refResolvedValueToContentUrl.get(refResolvedValue);
1101
+ sendBack({
1102
+ type: 'updateRenderValue',
1103
+ renderValue: contentUrl,
1104
+ });
1105
+ sendBack({
1106
+ type: 'resolvingRelatedValueSuccess',
1107
+ resolvedDisplayValue: contentUrl,
1108
+ });
1109
+ return true;
1110
+ }
1064
1111
  const fileExists = yield fs.promises.exists('/files/images/' + refResolvedValue);
1065
1112
  if (fileExists) {
1066
1113
  const fileContents = yield fs.promises.readFile('/files/images/' + refResolvedValue);
1067
1114
  const fileHandler = new File([fileContents], refResolvedValue);
1068
1115
  const contentUrl = URL.createObjectURL(fileHandler);
1116
+ refResolvedValueToContentUrl.set(refResolvedValue, contentUrl);
1069
1117
  sendBack({
1070
1118
  type: 'updateRenderValue',
1071
1119
  renderValue: contentUrl,
@@ -1080,11 +1128,21 @@ const resolveRelatedValue = fromCallback(({ sendBack, input: { context } }) => {
1080
1128
  if (typeof propertyValueFromDb === 'string') {
1081
1129
  // Check files for a filename that matches the propertyValue
1082
1130
  if (propertyRecordSchema.dataType === 'ImageSrc') {
1083
- const imageFileExists = yield fs.promises.exists(`/images/${propertyValue}`);
1084
- const fileContents = yield fs.promises.readFile(`/images/${propertyValue}`);
1085
- const fileHandler = new File([fileContents], propertyValue);
1086
- const contentUrl = URL.createObjectURL(fileHandler);
1087
- if (imageFileExists) {
1131
+ let contentUrl;
1132
+ if (storageTransactionIdToContentUrl.has(propertyValueFromDb)) {
1133
+ contentUrl =
1134
+ storageTransactionIdToContentUrl.get(propertyValueFromDb);
1135
+ }
1136
+ if (!contentUrl) {
1137
+ const imageFileExists = yield fs.promises.exists(`/images/${propertyValue}`);
1138
+ if (imageFileExists) {
1139
+ const fileContents = yield fs.promises.readFile(`/images/${propertyValue}`);
1140
+ const fileHandler = new File([fileContents], propertyValue);
1141
+ contentUrl = URL.createObjectURL(fileHandler);
1142
+ storageTransactionIdToContentUrl.set(propertyValueFromDb, contentUrl);
1143
+ }
1144
+ }
1145
+ if (contentUrl) {
1088
1146
  sendBack({
1089
1147
  type: 'updateRenderValue',
1090
1148
  renderValue: contentUrl,
@@ -1650,17 +1708,24 @@ const hydrateFromDb = fromCallback(({ sendBack, input: { context } }) => {
1650
1708
  renderValue: refResolvedDisplayValue,
1651
1709
  });
1652
1710
  if (propertyNameFromDb === 'storageTransactionId') {
1653
- const { Item } = yield import('./index-Bc3TXOPI.js');
1711
+ const { Item } = yield import('./index-BwUxXJ6P.js');
1654
1712
  const item = yield Item.find({
1655
1713
  seedLocalId,
1656
1714
  modelName: itemModelName,
1657
1715
  });
1658
1716
  if (item) {
1659
1717
  for (const [propertyName, property] of Object.entries(item.properties)) {
1660
- if (propertyName === 'html') {
1661
- const { fs } = yield import('@zenfs/core');
1718
+ if (propertyName === 'html' &&
1719
+ propertyValueFromDb &&
1720
+ propertyValueFromDb.length === 43) {
1721
+ const htmlFilePath = `/files/html/${propertyValueFromDb}.html`;
1722
+ const exists = yield fs.promises.exists(htmlFilePath);
1723
+ if (!exists) {
1724
+ logger$c(`HTML file does not exist for Property.${localId} at ${htmlFilePath}`);
1725
+ return;
1726
+ }
1662
1727
  const renderValue = yield fs.promises
1663
- .readFile(`/files/html/${propertyValueFromDb}.html`, 'utf8')
1728
+ .readFile(htmlFilePath, 'utf8')
1664
1729
  .catch((error) => {
1665
1730
  logger$c('Error reading html file', error);
1666
1731
  });
@@ -1827,6 +1892,9 @@ class ItemProperty {
1827
1892
  }
1828
1893
  if (skipTransform && !this._alias) ;
1829
1894
  this._subject.next(renderValue);
1895
+ // eventEmitter.emit(
1896
+ // `item.${itemModelName},${seedUid || seedLocalId}.update`,
1897
+ // )
1830
1898
  // this._lastRenderedValue = renderValue
1831
1899
  });
1832
1900
  this._service.start();
@@ -2116,7 +2184,7 @@ const addModelsToDb = fromCallback(({ sendBack, input: { context } }) => {
2116
2184
  if (!models$1) {
2117
2185
  return;
2118
2186
  }
2119
- const { models: SeedModels } = yield import('./seed.schema.config-Bq0uQNxG.js');
2187
+ const { models: SeedModels } = yield import('./seed.schema.config-D7dRgRFf.js');
2120
2188
  const allModels = Object.assign(Object.assign({}, SeedModels), models$1);
2121
2189
  let hasModelsInDb = false;
2122
2190
  const schemaDefsByModelName = new Map();
@@ -4236,11 +4304,11 @@ const globalMachine = setup({
4236
4304
  (_a) => __awaiter(void 0, [_a], void 0, function* ({ context }) {
4237
4305
  let SeedClass;
4238
4306
  if (context.environment === 'node') {
4239
- const { SeedNode } = yield import('./seed-67gCBvdP.js');
4307
+ const { SeedNode } = yield import('./seed-KyXHcDGt.js');
4240
4308
  SeedClass = SeedNode;
4241
4309
  }
4242
4310
  else {
4243
- const { SeedBrowser } = yield import('./seed-C7Q0_oW_.js');
4311
+ const { SeedBrowser } = yield import('./seed-DDd7TM0Y.js');
4244
4312
  SeedClass = SeedBrowser;
4245
4313
  }
4246
4314
  return SeedClass;
@@ -5214,6 +5282,19 @@ const itemMachineSingle = setup({
5214
5282
  });
5215
5283
 
5216
5284
  const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0, void 0, function* () {
5285
+ if (seedLocalId && !seedUid) {
5286
+ const seedQueryStatement = `SELECT uid
5287
+ FROM seeds
5288
+ WHERE local_id = '${seedLocalId}';`;
5289
+ const { rows } = yield runQueryForStatement(seedQueryStatement);
5290
+ if (rows && rows.length > 0) {
5291
+ seedUid = rows[0][0];
5292
+ }
5293
+ }
5294
+ const localIdWhere = seedLocalId
5295
+ ? `s.local_id = '${seedLocalId}'`
5296
+ : 's.local_id IS NULL';
5297
+ const uidWhere = seedUid ? `s.uid = '${seedUid}'` : 's.uid IS NULL';
5217
5298
  const queryStatement = `WITH LatestMetadata as (SELECT m.property_name,
5218
5299
  m.property_value,
5219
5300
  m.version_local_id,
@@ -5224,6 +5305,7 @@ const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0,
5224
5305
  FROM metadata m
5225
5306
  JOIN seeds s ON s.local_id = m.seed_local_id
5226
5307
  GROUP BY m.property_name),
5308
+
5227
5309
  LatestVersion as (SELECT v.local_id,
5228
5310
  MAX(v.attestation_created_at) as attestation_created_at,
5229
5311
  v.uid,
@@ -5233,6 +5315,7 @@ const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0,
5233
5315
  JOIN seeds s ON s.local_id = v.seed_local_id
5234
5316
  GROUP BY s.local_id)
5235
5317
 
5318
+
5236
5319
  SELECT s.local_id,
5237
5320
  s.uid,
5238
5321
  s.schema_uid,
@@ -5255,8 +5338,8 @@ const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0,
5255
5338
  JOIN LatestVersion lv ON lv.seed_local_id = m.seed_local_id
5256
5339
  JOIN metadata m ON m.property_name = lm.property_name OR lm.property_value = s.uid
5257
5340
  JOIN versions v ON s.local_id = v.seed_local_id
5258
- WHERE s.local_id = '${seedLocalId}'
5259
- OR s.uid = '${seedUid}'
5341
+ WHERE ${localIdWhere}
5342
+ OR ${uidWhere}
5260
5343
  GROUP BY m.property_name;
5261
5344
  `;
5262
5345
  const { rows } = yield runQueryForStatement(queryStatement);
@@ -5438,6 +5521,7 @@ class Item {
5438
5521
  propertiesObj[transformedKey] = propertyInstance;
5439
5522
  }
5440
5523
  this._propertiesSubject.next(propertiesObj);
5524
+ eventEmitter.emit(`item.${modelName}.${seedUid || seedLocalId}.update`);
5441
5525
  });
5442
5526
  this._service.start();
5443
5527
  const definedKeys = ['ModelClass'];
@@ -5669,9 +5753,12 @@ const useItemProperty = (propertyName, seedLocalId) => {
5669
5753
  const useItem = ({ modelName, seedLocalId, seedUid }) => {
5670
5754
  const [item, setItem] = useImmer(undefined);
5671
5755
  const [isReadingFromDb, setIsReadingFromDb] = useState(false);
5672
- const [isInitialized, setIsInitialized] = useState(false);
5756
+ const [itemSubscription, setItemSubscription] = useState();
5673
5757
  const { status, internalStatus } = useGlobalServiceStatus();
5674
5758
  const context = useSelector(item === null || item === void 0 ? void 0 : item.getService(), (snapshot) => snapshot === null || snapshot === void 0 ? void 0 : snapshot.context);
5759
+ const updateItem = useCallback((newItem) => {
5760
+ setItem(() => newItem);
5761
+ }, []);
5675
5762
  const readFromDb = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
5676
5763
  if (internalStatus !== 'ready' ||
5677
5764
  (!seedUid && !seedLocalId) ||
@@ -5687,14 +5774,29 @@ const useItem = ({ modelName, seedLocalId, seedUid }) => {
5687
5774
  logger$2('[useItem] [getItemFromDb] no item found', modelName, seedLocalId);
5688
5775
  return;
5689
5776
  }
5690
- setItem(() => foundItem);
5691
- setIsInitialized(true);
5777
+ updateItem(foundItem);
5692
5778
  }), [internalStatus, isReadingFromDb]);
5693
5779
  useEffect(() => {
5694
5780
  if (internalStatus === 'ready') {
5695
5781
  readFromDb();
5696
5782
  }
5697
5783
  }, [internalStatus, status]);
5784
+ useEffect(() => {
5785
+ if (item && !itemSubscription) {
5786
+ const subscription = item.subscribe((_) => __awaiter(void 0, void 0, void 0, function* () {
5787
+ const newItem = yield Item.find({ modelName, seedLocalId, seedUid });
5788
+ if (!newItem) {
5789
+ logger$2('[useItem] [itemSubscription] no item found', modelName, seedLocalId);
5790
+ return;
5791
+ }
5792
+ setItem(() => newItem);
5793
+ }));
5794
+ setItemSubscription(subscription);
5795
+ }
5796
+ return () => {
5797
+ itemSubscription === null || itemSubscription === void 0 ? void 0 : itemSubscription.unsubscribe();
5798
+ };
5799
+ }, [item, itemSubscription]);
5698
5800
  useEffect(() => {
5699
5801
  const seedId = seedUid || seedLocalId;
5700
5802
  eventEmitter.addListener(`item.${modelName}.${seedId}.update`, readFromDb);
@@ -5704,7 +5806,6 @@ const useItem = ({ modelName, seedLocalId, seedUid }) => {
5704
5806
  }, []);
5705
5807
  return {
5706
5808
  item,
5707
- isInitialized,
5708
5809
  isReadingFromDb,
5709
5810
  context,
5710
5811
  };
@@ -5746,7 +5847,11 @@ const useItems = ({ modelName, options }) => {
5746
5847
  };
5747
5848
  }, []);
5748
5849
  return {
5749
- items: orderBy(items, ['createdAt'], ['desc']),
5850
+ items: orderBy(items, [
5851
+ (item) => item.lastVersionPublishedAt ||
5852
+ item.attestationCreatedAt ||
5853
+ item.createdAt,
5854
+ ], ['desc']),
5750
5855
  isReadingDb,
5751
5856
  isInitialized,
5752
5857
  refresh,
@@ -6794,7 +6899,7 @@ const client = {
6794
6899
  console.error('fs listeners not ready during init');
6795
6900
  }
6796
6901
  globalService.send({ type: 'init', endpoints, models, addresses });
6797
- import('./seed.schema.config-Bq0uQNxG.js').then(({ models }) => {
6902
+ import('./seed.schema.config-D7dRgRFf.js').then(({ models }) => {
6798
6903
  for (const [key, value] of Object.entries(models)) {
6799
6904
  modelStore.set(key, value);
6800
6905
  }
@@ -6852,4 +6957,4 @@ const client = {
6852
6957
  };
6853
6958
 
6854
6959
  export { APP_DB_CONFIG as A, useCreateItem as B, CHILD_SNAPSHOT as C, DB_NAME_SDK_CONFIG as D, useItemProperty as E, useDeleteItem as F, GET_SCHEMAS as G, getGlobalService as H, Item as I, Json as J, getCorrectId as K, List as L, Model as M, Property as P, Relation as R, SCHEMA_NJK as S, Text as T, __decorate as _, GET_SEEDS as a, GET_SEED_IDS as b, GET_STORAGE_TRANSACTION_ID as c, GET_VERSIONS as d, GET_PROPERTIES as e, GET_ALL_PROPERTIES_FOR_ALL_VERSIONS as f, itemMachineAll as g, __metadata as h, itemMachineSingle as i, internalMachine as j, isNode as k, isBrowser as l, isReactNative as m, __awaiter as n, models as o, SCHEMA_TS as p, SDK_DB_CONFIG as q, SEED_DB_CONFIG as r, addModelsToInternalDb as s, client as t, ImageSrc as u, ItemProperty as v, withSeed as w, useItems as x, useItem as y, useItemProperties as z };
6855
- //# sourceMappingURL=index-B8GMq1lE.js.map
6960
+ //# sourceMappingURL=index-C6vnbWlE.js.map