@seedprotocol/sdk 0.1.8 → 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-Dg73YrcZ.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-CxqrGPwe.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-Dg73YrcZ.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-BeywvMUe.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,7 +1708,7 @@ const hydrateFromDb = fromCallback(({ sendBack, input: { context } }) => {
1650
1708
  renderValue: refResolvedDisplayValue,
1651
1709
  });
1652
1710
  if (propertyNameFromDb === 'storageTransactionId') {
1653
- const { Item } = yield import('./index-BeywvMUe.js');
1711
+ const { Item } = yield import('./index-BwUxXJ6P.js');
1654
1712
  const item = yield Item.find({
1655
1713
  seedLocalId,
1656
1714
  modelName: itemModelName,
@@ -1834,6 +1892,9 @@ class ItemProperty {
1834
1892
  }
1835
1893
  if (skipTransform && !this._alias) ;
1836
1894
  this._subject.next(renderValue);
1895
+ // eventEmitter.emit(
1896
+ // `item.${itemModelName},${seedUid || seedLocalId}.update`,
1897
+ // )
1837
1898
  // this._lastRenderedValue = renderValue
1838
1899
  });
1839
1900
  this._service.start();
@@ -2123,7 +2184,7 @@ const addModelsToDb = fromCallback(({ sendBack, input: { context } }) => {
2123
2184
  if (!models$1) {
2124
2185
  return;
2125
2186
  }
2126
- const { models: SeedModels } = yield import('./seed.schema.config-CxqrGPwe.js');
2187
+ const { models: SeedModels } = yield import('./seed.schema.config-D7dRgRFf.js');
2127
2188
  const allModels = Object.assign(Object.assign({}, SeedModels), models$1);
2128
2189
  let hasModelsInDb = false;
2129
2190
  const schemaDefsByModelName = new Map();
@@ -4243,11 +4304,11 @@ const globalMachine = setup({
4243
4304
  (_a) => __awaiter(void 0, [_a], void 0, function* ({ context }) {
4244
4305
  let SeedClass;
4245
4306
  if (context.environment === 'node') {
4246
- const { SeedNode } = yield import('./seed-CsQB9h6Q.js');
4307
+ const { SeedNode } = yield import('./seed-KyXHcDGt.js');
4247
4308
  SeedClass = SeedNode;
4248
4309
  }
4249
4310
  else {
4250
- const { SeedBrowser } = yield import('./seed-Cqa2atpR.js');
4311
+ const { SeedBrowser } = yield import('./seed-DDd7TM0Y.js');
4251
4312
  SeedClass = SeedBrowser;
4252
4313
  }
4253
4314
  return SeedClass;
@@ -5692,9 +5753,12 @@ const useItemProperty = (propertyName, seedLocalId) => {
5692
5753
  const useItem = ({ modelName, seedLocalId, seedUid }) => {
5693
5754
  const [item, setItem] = useImmer(undefined);
5694
5755
  const [isReadingFromDb, setIsReadingFromDb] = useState(false);
5695
- const [isInitialized, setIsInitialized] = useState(false);
5756
+ const [itemSubscription, setItemSubscription] = useState();
5696
5757
  const { status, internalStatus } = useGlobalServiceStatus();
5697
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
+ }, []);
5698
5762
  const readFromDb = useCallback(() => __awaiter(void 0, void 0, void 0, function* () {
5699
5763
  if (internalStatus !== 'ready' ||
5700
5764
  (!seedUid && !seedLocalId) ||
@@ -5710,14 +5774,29 @@ const useItem = ({ modelName, seedLocalId, seedUid }) => {
5710
5774
  logger$2('[useItem] [getItemFromDb] no item found', modelName, seedLocalId);
5711
5775
  return;
5712
5776
  }
5713
- setItem(() => foundItem);
5714
- setIsInitialized(true);
5777
+ updateItem(foundItem);
5715
5778
  }), [internalStatus, isReadingFromDb]);
5716
5779
  useEffect(() => {
5717
5780
  if (internalStatus === 'ready') {
5718
5781
  readFromDb();
5719
5782
  }
5720
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]);
5721
5800
  useEffect(() => {
5722
5801
  const seedId = seedUid || seedLocalId;
5723
5802
  eventEmitter.addListener(`item.${modelName}.${seedId}.update`, readFromDb);
@@ -5727,7 +5806,6 @@ const useItem = ({ modelName, seedLocalId, seedUid }) => {
5727
5806
  }, []);
5728
5807
  return {
5729
5808
  item,
5730
- isInitialized,
5731
5809
  isReadingFromDb,
5732
5810
  context,
5733
5811
  };
@@ -6821,7 +6899,7 @@ const client = {
6821
6899
  console.error('fs listeners not ready during init');
6822
6900
  }
6823
6901
  globalService.send({ type: 'init', endpoints, models, addresses });
6824
- import('./seed.schema.config-CxqrGPwe.js').then(({ models }) => {
6902
+ import('./seed.schema.config-D7dRgRFf.js').then(({ models }) => {
6825
6903
  for (const [key, value] of Object.entries(models)) {
6826
6904
  modelStore.set(key, value);
6827
6905
  }
@@ -6879,4 +6957,4 @@ const client = {
6879
6957
  };
6880
6958
 
6881
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 };
6882
- //# sourceMappingURL=index-Dg73YrcZ.js.map
6960
+ //# sourceMappingURL=index-C6vnbWlE.js.map