@seedprotocol/sdk 0.1.6 → 0.1.8

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-CNBlL-jO.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-Dg73YrcZ.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-CFp_quWt.js';
20
+ import './seed.schema.config-CxqrGPwe.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-CNBlL-jO.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-Dg73YrcZ.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-o-DJt8pS.js.map
29
+ //# sourceMappingURL=index-BeywvMUe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-BeywvMUe.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1650,17 +1650,24 @@ const hydrateFromDb = fromCallback(({ sendBack, input: { context } }) => {
1650
1650
  renderValue: refResolvedDisplayValue,
1651
1651
  });
1652
1652
  if (propertyNameFromDb === 'storageTransactionId') {
1653
- const { Item } = yield import('./index-o-DJt8pS.js');
1653
+ const { Item } = yield import('./index-BeywvMUe.js');
1654
1654
  const item = yield Item.find({
1655
1655
  seedLocalId,
1656
1656
  modelName: itemModelName,
1657
1657
  });
1658
1658
  if (item) {
1659
1659
  for (const [propertyName, property] of Object.entries(item.properties)) {
1660
- if (propertyName === 'html') {
1661
- const { fs } = yield import('@zenfs/core');
1660
+ if (propertyName === 'html' &&
1661
+ propertyValueFromDb &&
1662
+ propertyValueFromDb.length === 43) {
1663
+ const htmlFilePath = `/files/html/${propertyValueFromDb}.html`;
1664
+ const exists = yield fs.promises.exists(htmlFilePath);
1665
+ if (!exists) {
1666
+ logger$c(`HTML file does not exist for Property.${localId} at ${htmlFilePath}`);
1667
+ return;
1668
+ }
1662
1669
  const renderValue = yield fs.promises
1663
- .readFile(`/files/html/${propertyValueFromDb}.html`, 'utf8')
1670
+ .readFile(htmlFilePath, 'utf8')
1664
1671
  .catch((error) => {
1665
1672
  logger$c('Error reading html file', error);
1666
1673
  });
@@ -2116,7 +2123,7 @@ const addModelsToDb = fromCallback(({ sendBack, input: { context } }) => {
2116
2123
  if (!models$1) {
2117
2124
  return;
2118
2125
  }
2119
- const { models: SeedModels } = yield import('./seed.schema.config-CFp_quWt.js');
2126
+ const { models: SeedModels } = yield import('./seed.schema.config-CxqrGPwe.js');
2120
2127
  const allModels = Object.assign(Object.assign({}, SeedModels), models$1);
2121
2128
  let hasModelsInDb = false;
2122
2129
  const schemaDefsByModelName = new Map();
@@ -4236,11 +4243,11 @@ const globalMachine = setup({
4236
4243
  (_a) => __awaiter(void 0, [_a], void 0, function* ({ context }) {
4237
4244
  let SeedClass;
4238
4245
  if (context.environment === 'node') {
4239
- const { SeedNode } = yield import('./seed-DykcKEyf.js');
4246
+ const { SeedNode } = yield import('./seed-CsQB9h6Q.js');
4240
4247
  SeedClass = SeedNode;
4241
4248
  }
4242
4249
  else {
4243
- const { SeedBrowser } = yield import('./seed-Drx1JpCZ.js');
4250
+ const { SeedBrowser } = yield import('./seed-Cqa2atpR.js');
4244
4251
  SeedClass = SeedBrowser;
4245
4252
  }
4246
4253
  return SeedClass;
@@ -5214,6 +5221,19 @@ const itemMachineSingle = setup({
5214
5221
  });
5215
5222
 
5216
5223
  const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0, void 0, function* () {
5224
+ if (seedLocalId && !seedUid) {
5225
+ const seedQueryStatement = `SELECT uid
5226
+ FROM seeds
5227
+ WHERE local_id = '${seedLocalId}';`;
5228
+ const { rows } = yield runQueryForStatement(seedQueryStatement);
5229
+ if (rows && rows.length > 0) {
5230
+ seedUid = rows[0][0];
5231
+ }
5232
+ }
5233
+ const localIdWhere = seedLocalId
5234
+ ? `s.local_id = '${seedLocalId}'`
5235
+ : 's.local_id IS NULL';
5236
+ const uidWhere = seedUid ? `s.uid = '${seedUid}'` : 's.uid IS NULL';
5217
5237
  const queryStatement = `WITH LatestMetadata as (SELECT m.property_name,
5218
5238
  m.property_value,
5219
5239
  m.version_local_id,
@@ -5224,6 +5244,7 @@ const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0,
5224
5244
  FROM metadata m
5225
5245
  JOIN seeds s ON s.local_id = m.seed_local_id
5226
5246
  GROUP BY m.property_name),
5247
+
5227
5248
  LatestVersion as (SELECT v.local_id,
5228
5249
  MAX(v.attestation_created_at) as attestation_created_at,
5229
5250
  v.uid,
@@ -5233,6 +5254,7 @@ const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0,
5233
5254
  JOIN seeds s ON s.local_id = v.seed_local_id
5234
5255
  GROUP BY s.local_id)
5235
5256
 
5257
+
5236
5258
  SELECT s.local_id,
5237
5259
  s.uid,
5238
5260
  s.schema_uid,
@@ -5255,8 +5277,8 @@ const getPropertiesForSeed = (seedLocalId, seedUid) => __awaiter(void 0, void 0,
5255
5277
  JOIN LatestVersion lv ON lv.seed_local_id = m.seed_local_id
5256
5278
  JOIN metadata m ON m.property_name = lm.property_name OR lm.property_value = s.uid
5257
5279
  JOIN versions v ON s.local_id = v.seed_local_id
5258
- WHERE s.local_id = '${seedLocalId}'
5259
- OR s.uid = '${seedUid}'
5280
+ WHERE ${localIdWhere}
5281
+ OR ${uidWhere}
5260
5282
  GROUP BY m.property_name;
5261
5283
  `;
5262
5284
  const { rows } = yield runQueryForStatement(queryStatement);
@@ -5438,6 +5460,7 @@ class Item {
5438
5460
  propertiesObj[transformedKey] = propertyInstance;
5439
5461
  }
5440
5462
  this._propertiesSubject.next(propertiesObj);
5463
+ eventEmitter.emit(`item.${modelName}.${seedUid || seedLocalId}.update`);
5441
5464
  });
5442
5465
  this._service.start();
5443
5466
  const definedKeys = ['ModelClass'];
@@ -5696,9 +5719,10 @@ const useItem = ({ modelName, seedLocalId, seedUid }) => {
5696
5719
  }
5697
5720
  }, [internalStatus, status]);
5698
5721
  useEffect(() => {
5699
- eventEmitter.addListener(`item.${modelName}.${seedLocalId}.update`, readFromDb);
5722
+ const seedId = seedUid || seedLocalId;
5723
+ eventEmitter.addListener(`item.${modelName}.${seedId}.update`, readFromDb);
5700
5724
  return () => {
5701
- eventEmitter.removeListener(`item.${modelName}.${seedLocalId}.update`, readFromDb);
5725
+ eventEmitter.removeListener(`item.${modelName}.${seedId}.update`, readFromDb);
5702
5726
  };
5703
5727
  }, []);
5704
5728
  return {
@@ -5745,7 +5769,11 @@ const useItems = ({ modelName, options }) => {
5745
5769
  };
5746
5770
  }, []);
5747
5771
  return {
5748
- items: orderBy(items, ['createdAt'], ['desc']),
5772
+ items: orderBy(items, [
5773
+ (item) => item.lastVersionPublishedAt ||
5774
+ item.attestationCreatedAt ||
5775
+ item.createdAt,
5776
+ ], ['desc']),
5749
5777
  isReadingDb,
5750
5778
  isInitialized,
5751
5779
  refresh,
@@ -5950,6 +5978,12 @@ const getVersionData = (_a) => __awaiter(void 0, [_a], void 0, function* ({ seed
5950
5978
  });
5951
5979
 
5952
5980
  const logger$1 = debug('app:write');
5981
+ const sendItemUpdateEvent = ({ modelName, seedLocalId, seedUid }) => {
5982
+ if (!modelName || (!seedLocalId && !seedUid)) {
5983
+ return;
5984
+ }
5985
+ eventEmitter.emit(`item.${modelName}.${seedUid || seedLocalId}.update`);
5986
+ };
5953
5987
  const createSeed = (_a) => __awaiter(void 0, [_a], void 0, function* ({ type }) {
5954
5988
  const appDb = getAppDb();
5955
5989
  const newSeedLocalId = generateId();
@@ -6053,7 +6087,7 @@ const updateItemPropertyValue = (_a) => __awaiter(void 0, [_a], void 0, function
6053
6087
  const propertyNameFromDb = mostRecentRecord[2];
6054
6088
  const propertyValueFromDb = mostRecentRecord[3];
6055
6089
  const modelType = mostRecentRecord[4];
6056
- mostRecentRecord[5];
6090
+ const seedUid = mostRecentRecord[5];
6057
6091
  const seedLocalIdFromDb = mostRecentRecord[6];
6058
6092
  const versionLocalId = mostRecentRecord[7];
6059
6093
  mostRecentRecord[8];
@@ -6073,7 +6107,7 @@ const updateItemPropertyValue = (_a) => __awaiter(void 0, [_a], void 0, function
6073
6107
  updated_at = ${Date.now()}
6074
6108
  WHERE local_id = '${localId}';`;
6075
6109
  yield runQueryForStatement(updatePropertyStatement);
6076
- eventEmitter.emit(`item.${modelName}.${seedLocalId}.update`);
6110
+ sendItemUpdateEvent({ modelName, seedLocalId, seedUid });
6077
6111
  return;
6078
6112
  }
6079
6113
  const seedDataFromDb = yield getSeedData({ seedLocalId });
@@ -6109,7 +6143,7 @@ const updateItemPropertyValue = (_a) => __awaiter(void 0, [_a], void 0, function
6109
6143
  ${refResolvedDisplayValue ? `'${refResolvedDisplayValue}'` : 'NULL'},
6110
6144
  ${Date.now()});`;
6111
6145
  yield runQueryForStatement(newPropertyStatement);
6112
- eventEmitter.emit(`item.${modelName}.${seedLocalId}.update`);
6146
+ sendItemUpdateEvent({ modelName, seedLocalId, seedUid });
6113
6147
  return;
6114
6148
  }
6115
6149
  // Here there are no records for this property on this seed so we should create one
@@ -6153,137 +6187,8 @@ const updateItemPropertyValue = (_a) => __awaiter(void 0, [_a], void 0, function
6153
6187
  ${refResolvedDisplayValue ? `'${refResolvedDisplayValue}'` : 'NULL'},
6154
6188
  ${Date.now()});`;
6155
6189
  yield runQueryForStatement(newPropertyStatement);
6156
- eventEmitter.emit(`item.${modelName}.${seedLocalId}.update`);
6190
+ sendItemUpdateEvent({ modelName, seedLocalId, seedUid });
6157
6191
  });
6158
- // if (propertyLocalId) {
6159
- // const existingItemPropertyStatement = `SELECT local_id,
6160
- // uid,
6161
- // property_name,
6162
- // property_value,
6163
- // model_type,
6164
- // seed_uid,
6165
- // seed_local_id,
6166
- // version_local_id,
6167
- // version_uid,
6168
- // schema_uid
6169
- // FROM metadata
6170
- // WHERE local_id = '${propertyLocalId}';`
6171
- //
6172
- // const { rows } = await runQueryForStatement(existingItemPropertyStatement)
6173
- //
6174
- // if (!rows || rows.length === 0) {
6175
- // logger(
6176
- // `[db/write] [updateItemPropertyValue] no property found for local_id: ${propertyLocalId}`,
6177
- // )
6178
- // return
6179
- // }
6180
- //
6181
- // const propertyValues = rows[0]
6182
- // const propertyUid = propertyValues[1]
6183
- //
6184
- // if (!propertyUid) {
6185
- // const updatePropertyStatement = `UPDATE metadata
6186
- // SET property_value = '${safeNewValue}',
6187
- // ref_seed_type = ${refSeedType ? `'${refSeedType}'` : 'NULL'},
6188
- // ref_resolved_value = ${refResolvedValue ? `'${refResolvedValue}'` : 'NULL'},
6189
- // ref_resolved_display_value = ${refResolvedDisplayValue ? `'${refResolvedDisplayValue}'` : 'NULL'},
6190
- // updated_at = ${Date.now()}
6191
- // WHERE local_id = '${propertyLocalId}';`
6192
- //
6193
- // await runQueryForStatement(updatePropertyStatement)
6194
- //
6195
- // return
6196
- // }
6197
- // const newLocalId = generateId()
6198
- // const propertyNameFromDb = propertyValues[2]
6199
- // const modelType = propertyValues[4]
6200
- // const seedUid = propertyValues[5]
6201
- // const seedLocaIdFromDb = propertyValues[6]
6202
- // const versionLocalId = propertyValues[7]
6203
- // const versionUid = propertyValues[8]
6204
- // const schemaUid = propertyValues[9]
6205
- // const easDataType = propertyValues[10]
6206
- //
6207
- // const newPropertyStatement = `INSERT INTO metadata (local_id,
6208
- // property_name,
6209
- // property_value,
6210
- // model_type,
6211
- // seed_uid,
6212
- // seed_local_id,
6213
- // version_local_id,
6214
- // version_uid,
6215
- // schema_uid,
6216
- // eas_data_type,
6217
- // created_at)
6218
- // VALUES ('${newLocalId}',
6219
- // '${propertyNameFromDb}',
6220
- // '${safeNewValue}',
6221
- // '${modelType}',
6222
- // '${seedUid}',
6223
- // '${seedLocaIdFromDb}',
6224
- // '${versionLocalId}',
6225
- // '${versionUid}',
6226
- // '${schemaUid}',
6227
- // '${easDataType}',
6228
- // ${Date.now()});`
6229
- //
6230
- // await runQueryForStatement(newPropertyStatement)
6231
- //
6232
- // return
6233
- // }
6234
- //
6235
- // if (seedLocalId && propertyName) {
6236
- // const existingItemPropertyStatement = `SELECT local_id, uid
6237
- // FROM metadata
6238
- // WHERE seed_local_id = '${seedLocalId}'
6239
- // AND property_name = '${propertyName}'
6240
- // ORDER BY COALESCE(attestation_created_at, created_at) DESC;`
6241
- //
6242
- // const { rows } = await runQueryForStatement(existingItemPropertyStatement)
6243
- //
6244
- // if (!rows || rows.length === 0) {
6245
- // // This means that there's no existing record for this property_name on the seed
6246
- // const createMetadataStatement = `INSERT INTO metadata (local_id,
6247
- // seed_local_id,
6248
- // property_name,
6249
- // property_value,
6250
- // model_type,
6251
- // ref_seed_type,
6252
- // ref_resolved_value,
6253
- // ref_resolved_display_value,
6254
- // version_local_id,
6255
- // schema_uid,
6256
- // created_at,
6257
- // updated_at)
6258
- // VALUES ('${generateId()}',
6259
- // '${seedLocalId}',
6260
- // '${propertyName}',
6261
- // '${safeNewValue}',
6262
- // '${modelName}',
6263
- // ${refSeedType ? `'${refSeedType}'` : 'NULL'},
6264
- // ${refResolvedValue ? `'${refResolvedValue}'` : 'NULL'},
6265
- // ${refResolvedDisplayValue ? `'${refResolvedDisplayValue}'` : 'NULL'},
6266
- // '${versionLocalId}',
6267
- // '${schemaUid}',
6268
- // ${Date.now()},
6269
- // ${Date.now()});`
6270
- //
6271
- // await runQueryForStatement(createMetadataStatement)
6272
- //
6273
- // return
6274
- // }
6275
- //
6276
- // const updatePropertyStatement = `UPDATE metadata
6277
- // SET property_value = '${newValue}',
6278
- // ref_seed_type = ${refSeedType ? `'${refSeedType}'` : 'NULL'},
6279
- // ref_resolved_value = ${refResolvedValue ? `'${refResolvedValue}'` : 'NULL'},
6280
- // ref_resolved_display_value = ${refResolvedDisplayValue ? `'${refResolvedDisplayValue}'` : 'NULL'}
6281
- // WHERE seed_local_id = '${seedLocalId}'
6282
- // AND property_name = '${propertyName}';`
6283
- //
6284
- // await runQueryForStatement(updatePropertyStatement)
6285
- //
6286
- // return
6287
6192
 
6288
6193
  const saveValueToDb = fromCallback(({ sendBack, input: { context, event } }) => {
6289
6194
  const { localId, propertyName: propertyNameRaw, seedLocalId, propertyValue: existingValue, propertyRecordSchema, itemModelName, schemaUid, } = context;
@@ -6916,7 +6821,7 @@ const client = {
6916
6821
  console.error('fs listeners not ready during init');
6917
6822
  }
6918
6823
  globalService.send({ type: 'init', endpoints, models, addresses });
6919
- import('./seed.schema.config-CFp_quWt.js').then(({ models }) => {
6824
+ import('./seed.schema.config-CxqrGPwe.js').then(({ models }) => {
6920
6825
  for (const [key, value] of Object.entries(models)) {
6921
6826
  modelStore.set(key, value);
6922
6827
  }
@@ -6974,4 +6879,4 @@ const client = {
6974
6879
  };
6975
6880
 
6976
6881
  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 };
6977
- //# sourceMappingURL=index-CNBlL-jO.js.map
6882
+ //# sourceMappingURL=index-Dg73YrcZ.js.map