@seedprotocol/sdk 0.4.4 → 0.4.6
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/README.md +38 -348
- package/dist/{ArweaveClient-DkNLWiSZ.js → ArweaveClient-Caio37h1.js} +8 -8
- package/dist/ArweaveClient-Caio37h1.js.map +1 -0
- package/dist/{ArweaveClient-CczHvccG.js → ArweaveClient-DLKAaS3K.js} +8 -8
- package/dist/ArweaveClient-DLKAaS3K.js.map +1 -0
- package/dist/{Db-DJvjXVlp.js → Db-C9fppVRl.js} +9 -9
- package/dist/Db-C9fppVRl.js.map +1 -0
- package/dist/{Db-BzQR2KX9.js → Db-DJAG-5Di.js} +13 -11
- package/dist/Db-DJAG-5Di.js.map +1 -0
- package/dist/{EasClient-CHQYmjj2.js → EasClient-DQGAIj-b.js} +8 -8
- package/dist/EasClient-DQGAIj-b.js.map +1 -0
- package/dist/{EasClient-CodrIgW-.js → EasClient-T7Nx9185.js} +8 -8
- package/dist/EasClient-T7Nx9185.js.map +1 -0
- package/dist/{FileManager-BbvkZJCH.js → FileManager-BohtoKYo.js} +10 -9
- package/dist/FileManager-BohtoKYo.js.map +1 -0
- package/dist/{FileManager-BIVsDnMu.js → FileManager-d64CkqXy.js} +8 -8
- package/dist/FileManager-d64CkqXy.js.map +1 -0
- package/dist/Item/Item.d.ts.map +1 -1
- package/dist/Item/service/actors/fetchDataFromEas.d.ts.map +1 -1
- package/dist/Item/service/actors/loadOrCreateItem.d.ts.map +1 -1
- package/dist/Item/service/actors/runPublish.d.ts.map +1 -1
- package/dist/ItemProperty/ItemProperty.d.ts.map +1 -1
- package/dist/ItemProperty/service/actors/hydrateFromDb.d.ts.map +1 -1
- package/dist/ItemProperty/service/actors/resolveRelatedValue.d.ts.map +1 -1
- package/dist/ItemProperty/service/actors/saveValueToDb/analyzeInput.d.ts.map +1 -1
- package/dist/ItemProperty/service/actors/saveValueToDb/saveRelation.d.ts.map +1 -1
- package/dist/Model/Model.d.ts.map +1 -1
- package/dist/Model/service/actors/createModelProperties.d.ts.map +1 -1
- package/dist/Model/service/actors/loadOrCreateModel.d.ts.map +1 -1
- package/dist/Model/service/actors/validateModel.d.ts.map +1 -1
- package/dist/ModelProperty/ModelProperty.d.ts.map +1 -1
- package/dist/ModelProperty/service/actors/compareAndMarkDraft.d.ts.map +1 -1
- package/dist/ModelProperty/service/actors/saveToSchema.d.ts.map +1 -1
- package/dist/ModelProperty/service/actors/validateProperty.d.ts.map +1 -1
- package/dist/{ModelProperty-gjcA83Mq.js → ModelProperty-Cpd9xiIM.js} +67 -768
- package/dist/ModelProperty-Cpd9xiIM.js.map +1 -0
- package/dist/{PathResolver-kGzba-zC.js → PathResolver-CLZUWoX2.js} +8 -8
- package/dist/PathResolver-CLZUWoX2.js.map +1 -0
- package/dist/{PathResolver-9zAzPhee.js → PathResolver-D6M0rzKV.js} +8 -8
- package/dist/PathResolver-D6M0rzKV.js.map +1 -0
- package/dist/{QueryClient-C7ZkTT0z.js → QueryClient-BUoz2Vb0.js} +8 -8
- package/dist/QueryClient-BUoz2Vb0.js.map +1 -0
- package/dist/{QueryClient-CEtsek7Q.js → QueryClient-COQ_Rs4-.js} +8 -8
- package/dist/QueryClient-COQ_Rs4-.js.map +1 -0
- package/dist/Schema/Schema.d.ts.map +1 -1
- package/dist/Schema/service/actors/createModelInstances.d.ts.map +1 -1
- package/dist/Schema/service/actors/loadOrCreateSchema.d.ts.map +1 -1
- package/dist/Schema/service/actors/verifyModelInstancesInCache.d.ts.map +1 -1
- package/dist/Schema/service/actors/writePropertiesToDb.d.ts.map +1 -1
- package/dist/Schema/service/addModelsMachine.d.ts.map +1 -1
- package/dist/{Schema-ClTcpgbt.js → Schema-BZKBy5Tx.js} +799 -62
- package/dist/Schema-BZKBy5Tx.js.map +1 -0
- package/dist/{SchemaValidationService-BfoEiHNr.js → SchemaValidationService-F8No7BHp.js} +7 -7
- package/dist/SchemaValidationService-F8No7BHp.js.map +1 -0
- package/dist/browser/helpers/FileManager.d.ts.map +1 -1
- package/dist/browser/react/itemProperty.d.ts.map +1 -1
- package/dist/cjs/{ModelProperty-BRKRREYP.js → ModelProperty-B4pV91tc.js} +78 -438
- package/dist/cjs/ModelProperty-B4pV91tc.js.map +1 -0
- package/dist/cjs/{Schema-CV9hMg8m.js → Schema-TErl3J36.js} +437 -93
- package/dist/cjs/Schema-TErl3J36.js.map +1 -0
- package/dist/cjs/{SchemaValidationService-DUtQUerg.js → SchemaValidationService-p5BbJV9T.js} +3 -3
- package/dist/cjs/{SchemaValidationService-DUtQUerg.js.map → SchemaValidationService-p5BbJV9T.js.map} +1 -1
- package/dist/cjs/{getItem-BThT-UOt.js → getItem-BrnLfC12.js} +3 -3
- package/dist/cjs/{getItem-BThT-UOt.js.map → getItem-BrnLfC12.js.map} +1 -1
- package/dist/cjs/{getPublishPayload-CKZ7rVbU.js → getPublishPayload-BcjCN_bD.js} +8 -6
- package/dist/cjs/getPublishPayload-BcjCN_bD.js.map +1 -0
- package/dist/cjs/{getPublishUploads-CuGIvV-Q.js → getPublishUploads-5_oXzwol.js} +4 -4
- package/dist/cjs/{getPublishUploads-CuGIvV-Q.js.map → getPublishUploads-5_oXzwol.js.map} +1 -1
- package/dist/cjs/{getSegmentedItemProperties-BCJXJlTR.js → getSegmentedItemProperties-WFdizUDv.js} +2 -2
- package/dist/cjs/{getSegmentedItemProperties-BCJXJlTR.js.map → getSegmentedItemProperties-WFdizUDv.js.map} +1 -1
- package/dist/cjs/{index-CbTBDqTJ.js → index-BrmvhXsG.js} +3 -3
- package/dist/cjs/index-BrmvhXsG.js.map +1 -0
- package/dist/cjs/{index-C3j3m34a.js → index-CXI4VF1H.js} +12351 -12189
- package/dist/cjs/index-CXI4VF1H.js.map +1 -0
- package/dist/client/actors/platformClassesInit.d.ts.map +1 -1
- package/dist/client/actors/saveAppState.d.ts.map +1 -1
- package/dist/db/read/getModelSchemas.d.ts.map +1 -1
- package/dist/db/read/getPublishPayload.d.ts.map +1 -1
- package/dist/db/write/createNewItem.d.ts.map +1 -1
- package/dist/eas.d.ts.map +1 -1
- package/dist/events/files/download.d.ts.map +1 -1
- package/dist/events/item/syncDbWithEas.d.ts.map +1 -1
- package/dist/{getItem-BWPo8wd0.js → getItem-DaQVdeae.js} +8 -8
- package/dist/getItem-DaQVdeae.js.map +1 -0
- package/dist/{getPublishPayload-Caw2laZM.js → getPublishPayload-CWiLI1yd.js} +12 -10
- package/dist/getPublishPayload-CWiLI1yd.js.map +1 -0
- package/dist/{getPublishUploads-MlyBuoXh.js → getPublishUploads-DhvcKD_9.js} +9 -9
- package/dist/getPublishUploads-DhvcKD_9.js.map +1 -0
- package/dist/{getSegmentedItemProperties-COLLdDo4.js → getSegmentedItemProperties-Cp4kcWIF.js} +2 -2
- package/dist/getSegmentedItemProperties-Cp4kcWIF.js.map +1 -0
- package/dist/helpers/db.d.ts.map +1 -1
- package/dist/helpers/property.d.ts.map +1 -1
- package/dist/helpers/schema.d.ts.map +1 -1
- package/dist/helpers/updateSchema.d.ts.map +1 -1
- package/dist/imports/json.d.ts.map +1 -1
- package/dist/{index-D2OGsG4v.js → index-DBy53qbb.js} +2 -2
- package/dist/index-DBy53qbb.js.map +1 -0
- package/dist/{ClientManager-DoHUt8tU.js → index-Dk73a7JE.js} +11781 -11613
- package/dist/index-Dk73a7JE.js.map +1 -0
- package/dist/{index-B9i_mXXp.js → index-LfXKx_PE.js} +9 -9
- package/dist/index-LfXKx_PE.js.map +1 -0
- package/dist/main.cjs +2 -2
- package/dist/main.js +16 -13
- package/dist/main.js.map +1 -1
- package/dist/node/db/Db.d.ts.map +1 -1
- package/dist/node.js +15 -15
- package/dist/node.js.map +1 -1
- package/dist/{property-fK7tSUDX.js → property-1o2MvYTm.js} +10 -4
- package/dist/property-1o2MvYTm.js.map +1 -0
- package/dist/{queries-DGOk16cd.js → queries-DA4G0iEN.js} +2 -2
- package/dist/queries-DA4G0iEN.js.map +1 -0
- package/dist/services/publish/actors/preparePublishRequestData.d.ts.map +1 -1
- package/dist/services/publish/actors/upload.d.ts.map +1 -1
- package/dist/services/write/actors/validateEntity.d.ts.map +1 -1
- package/dist/services/write/actors/writeToDatabase.d.ts.map +1 -1
- package/dist/vite.js.map +1 -1
- package/package.json +8 -4
- package/dist/ArweaveClient-CczHvccG.js.map +0 -1
- package/dist/ArweaveClient-DkNLWiSZ.js.map +0 -1
- package/dist/ClientManager-DoHUt8tU.js.map +0 -1
- package/dist/Db-BzQR2KX9.js.map +0 -1
- package/dist/Db-DJvjXVlp.js.map +0 -1
- package/dist/EasClient-CHQYmjj2.js.map +0 -1
- package/dist/EasClient-CodrIgW-.js.map +0 -1
- package/dist/FileManager-BIVsDnMu.js.map +0 -1
- package/dist/FileManager-BbvkZJCH.js.map +0 -1
- package/dist/ModelProperty-gjcA83Mq.js.map +0 -1
- package/dist/PathResolver-9zAzPhee.js.map +0 -1
- package/dist/PathResolver-kGzba-zC.js.map +0 -1
- package/dist/QueryClient-C7ZkTT0z.js.map +0 -1
- package/dist/QueryClient-CEtsek7Q.js.map +0 -1
- package/dist/Schema-ClTcpgbt.js.map +0 -1
- package/dist/SchemaValidationService-BfoEiHNr.js.map +0 -1
- package/dist/cjs/ModelProperty-BRKRREYP.js.map +0 -1
- package/dist/cjs/Schema-CV9hMg8m.js.map +0 -1
- package/dist/cjs/getPublishPayload-CKZ7rVbU.js.map +0 -1
- package/dist/cjs/index-C3j3m34a.js.map +0 -1
- package/dist/cjs/index-CbTBDqTJ.js.map +0 -1
- package/dist/getItem-BWPo8wd0.js.map +0 -1
- package/dist/getPublishPayload-Caw2laZM.js.map +0 -1
- package/dist/getPublishUploads-MlyBuoXh.js.map +0 -1
- package/dist/getSegmentedItemProperties-COLLdDo4.js.map +0 -1
- package/dist/index-B9i_mXXp.js.map +0 -1
- package/dist/index-D2OGsG4v.js.map +0 -1
- package/dist/property-fK7tSUDX.js.map +0 -1
- package/dist/queries-DGOk16cd.js.map +0 -1
|
@@ -1,38 +1,389 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var xstate = require('xstate');
|
|
4
|
-
var main_cjs = require('./index-
|
|
4
|
+
var main_cjs = require('./index-CXI4VF1H.js');
|
|
5
5
|
var drizzleOrm = require('drizzle-orm');
|
|
6
6
|
var debug = require('debug');
|
|
7
|
-
var SchemaValidationService = require('./SchemaValidationService-
|
|
8
|
-
var ModelProperty = require('./ModelProperty-BRKRREYP.js');
|
|
9
|
-
require('immer');
|
|
10
|
-
require('reflect-metadata');
|
|
11
|
-
require('drizzle-orm/sqlite-core');
|
|
12
|
-
require('nanoid');
|
|
13
|
-
require('nanoid-dictionary');
|
|
7
|
+
var SchemaValidationService = require('./SchemaValidationService-p5BbJV9T.js');
|
|
14
8
|
require('pluralize');
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
|
|
10
|
+
const logger$d = debug('seedSdk:helpers:updateSchema');
|
|
11
|
+
/**
|
|
12
|
+
* Get the file path for a schema file
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Sanitize a schema name to be filesystem-safe
|
|
16
|
+
* Replaces all special characters (except alphanumeric, hyphens, underscores) with underscores
|
|
17
|
+
* Converts spaces to underscores
|
|
18
|
+
* Removes leading/trailing underscores
|
|
19
|
+
*
|
|
20
|
+
* @param name - Schema name to sanitize
|
|
21
|
+
* @returns Sanitized name safe for use in filenames
|
|
22
|
+
*/
|
|
23
|
+
const sanitizeSchemaName$1 = (name) => {
|
|
24
|
+
return name
|
|
25
|
+
.replace(/[^a-zA-Z0-9\s_-]/g, '_') // Replace special chars (except spaces, hyphens, underscores) with underscore
|
|
26
|
+
.replace(/\s+/g, '_') // Convert spaces to underscores
|
|
27
|
+
.replace(/^_+|_+$/g, '') // Remove leading/trailing underscores
|
|
28
|
+
.replace(/_+/g, '_'); // Collapse multiple underscores to single
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Get the full file path for a schema
|
|
32
|
+
* Format: {schemaFileId}_{schemaName}_v{version}.json
|
|
33
|
+
*
|
|
34
|
+
* The ID-first format ensures all files for a schema group together when sorted alphabetically.
|
|
35
|
+
*
|
|
36
|
+
* @param name - Schema name
|
|
37
|
+
* @param version - Schema version
|
|
38
|
+
* @param schemaFileId - Schema file ID (required)
|
|
39
|
+
*/
|
|
40
|
+
const getSchemaFilePath$2 = (name, version, schemaFileId) => {
|
|
41
|
+
const path = main_cjs.BaseFileManager.getPathModule();
|
|
42
|
+
const workingDir = main_cjs.BaseFileManager.getWorkingDir();
|
|
43
|
+
const sanitizedName = sanitizeSchemaName$1(name);
|
|
44
|
+
const filename = schemaFileId
|
|
45
|
+
? `${schemaFileId}_${sanitizedName}_v${version}.json`
|
|
46
|
+
: `${sanitizedName}_v${version}.json`;
|
|
47
|
+
return path.join(workingDir, filename);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Get schemaFileId from database for a schema
|
|
51
|
+
* @param schemaName - Schema name
|
|
52
|
+
* @returns Schema file ID
|
|
53
|
+
* @throws Error if schema not found or missing schemaFileId
|
|
54
|
+
*/
|
|
55
|
+
async function getSchemaFileId(schemaName) {
|
|
56
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
57
|
+
if (!db)
|
|
58
|
+
throw new Error('Database not available');
|
|
59
|
+
const dbSchema = await db
|
|
60
|
+
.select()
|
|
61
|
+
.from(main_cjs.schemas)
|
|
62
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, schemaName))
|
|
63
|
+
.orderBy(drizzleOrm.desc(main_cjs.schemas.version))
|
|
64
|
+
.limit(1);
|
|
65
|
+
if (dbSchema.length === 0 || !dbSchema[0].schemaFileId) {
|
|
66
|
+
throw new Error(`Schema ${schemaName} not found in database or missing schemaFileId`);
|
|
67
|
+
}
|
|
68
|
+
return dbSchema[0].schemaFileId;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Write the full schema to a new version file (e.g. when new models were added).
|
|
72
|
+
* Used when _editedProperties contains 'schema:models' and there are no property-level updates.
|
|
73
|
+
* @param schemaName - Schema name
|
|
74
|
+
* @param schema - Full schema object (e.g. from _buildModelsFromInstances)
|
|
75
|
+
* @returns The file path of the new schema version
|
|
76
|
+
*/
|
|
77
|
+
async function writeFullSchemaNewVersion(schemaName, schema) {
|
|
78
|
+
const latestVersion = await main_cjs.getLatestSchemaVersion(schemaName);
|
|
79
|
+
const newVersion = latestVersion + 1;
|
|
80
|
+
const schemaWithNewVersion = {
|
|
81
|
+
...schema,
|
|
82
|
+
version: newVersion,
|
|
83
|
+
metadata: {
|
|
84
|
+
...schema.metadata,
|
|
85
|
+
updatedAt: new Date().toISOString(),
|
|
86
|
+
},
|
|
87
|
+
migrations: [
|
|
88
|
+
...(schema.migrations || []),
|
|
89
|
+
{
|
|
90
|
+
version: newVersion,
|
|
91
|
+
timestamp: new Date().toISOString(),
|
|
92
|
+
description: 'New schema version (e.g. new models added)',
|
|
93
|
+
changes: [{ type: 'full_schema_write' }],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
const newFilePath = getSchemaFilePath$2(schemaName, newVersion, schema.id ?? (await getSchemaFileId(schemaName)));
|
|
98
|
+
const newContent = JSON.stringify(schemaWithNewVersion, null, 2);
|
|
99
|
+
await main_cjs.BaseFileManager.saveFile(newFilePath, newContent);
|
|
100
|
+
await main_cjs.BaseFileManager.waitForFileWithContent(newFilePath);
|
|
101
|
+
logger$d(`Created new schema version ${newVersion} for ${schemaName} at ${newFilePath}`);
|
|
102
|
+
return newFilePath;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get model name from modelId
|
|
106
|
+
* @param modelId - The model ID to look up
|
|
107
|
+
* @returns The model name, or undefined if not found
|
|
108
|
+
*/
|
|
109
|
+
async function getModelNameFromId(modelId) {
|
|
110
|
+
if (!modelId) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
114
|
+
if (!db) {
|
|
115
|
+
throw new Error('Database not found');
|
|
116
|
+
}
|
|
117
|
+
const modelRecords = await db
|
|
118
|
+
.select()
|
|
119
|
+
.from(main_cjs.models)
|
|
120
|
+
.where(drizzleOrm.eq(main_cjs.models.id, modelId))
|
|
121
|
+
.limit(1);
|
|
122
|
+
if (modelRecords.length === 0) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
return modelRecords[0].name;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Convert a TProperty/ModelPropertyMachineContext to SchemaPropertyUpdate format
|
|
129
|
+
* This function converts the internal property representation to the schema file format
|
|
130
|
+
* @param property - The TProperty instance to convert
|
|
131
|
+
* @param modelName - The name of the model this property belongs to
|
|
132
|
+
* @param propertyName - The name of the property
|
|
133
|
+
* @returns A SchemaPropertyUpdate object ready to be passed to updateModelProperties
|
|
134
|
+
*/
|
|
135
|
+
async function convertPropertyToSchemaUpdate(property, modelName, propertyName) {
|
|
136
|
+
const updates = {};
|
|
137
|
+
// Convert dataType to type
|
|
138
|
+
if (property.dataType) {
|
|
139
|
+
updates.type = property.dataType;
|
|
140
|
+
}
|
|
141
|
+
// Handle Relation type
|
|
142
|
+
if (property.dataType === main_cjs.ModelPropertyDataTypes.Relation) {
|
|
143
|
+
if (property.ref) {
|
|
144
|
+
updates.model = property.ref;
|
|
145
|
+
}
|
|
146
|
+
else if (property.refModelId) {
|
|
147
|
+
// If ref is not set but refModelId is, get the model name from the database
|
|
148
|
+
const refModelName = await getModelNameFromId(property.refModelId);
|
|
149
|
+
if (refModelName) {
|
|
150
|
+
updates.model = refModelName;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// Handle List type
|
|
155
|
+
if (property.dataType === main_cjs.ModelPropertyDataTypes.List) {
|
|
156
|
+
if (property.refValueType) {
|
|
157
|
+
updates.items = {
|
|
158
|
+
type: property.refValueType,
|
|
159
|
+
};
|
|
160
|
+
if (property.ref) {
|
|
161
|
+
updates.items.model = property.ref;
|
|
162
|
+
}
|
|
163
|
+
else if (property.refModelId) {
|
|
164
|
+
// If ref is not set but refModelId is, get the model name from the database
|
|
165
|
+
const refModelName = await getModelNameFromId(property.refModelId);
|
|
166
|
+
if (refModelName) {
|
|
167
|
+
updates.items.model = refModelName;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// Handle storage configuration (for Text properties with storage)
|
|
173
|
+
if (property.storageType || property.localStorageDir || property.filenameSuffix) {
|
|
174
|
+
const storageType = property.storageType || 'ItemStorage'; // Default to ItemStorage if not specified
|
|
175
|
+
updates.storage = {
|
|
176
|
+
type: storageType,
|
|
177
|
+
};
|
|
178
|
+
if (property.localStorageDir) {
|
|
179
|
+
updates.storage.path = property.localStorageDir;
|
|
180
|
+
}
|
|
181
|
+
if (property.filenameSuffix) {
|
|
182
|
+
updates.storage.extension = property.filenameSuffix;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
modelName,
|
|
187
|
+
propertyName,
|
|
188
|
+
updates,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Update model properties in a schema and create a new version
|
|
193
|
+
* @param schemaName - The name of the schema to update
|
|
194
|
+
* @param propertyUpdates - Array of property updates to apply
|
|
195
|
+
* @param modelUpdates - Optional array of model renames
|
|
196
|
+
* @returns The file path of the new schema version
|
|
197
|
+
* @throws Error if schema not found or updates are invalid
|
|
198
|
+
*/
|
|
199
|
+
async function updateModelProperties(schemaName, propertyUpdates, modelUpdates) {
|
|
200
|
+
// Get the latest version of the schema
|
|
201
|
+
const latestVersion = await main_cjs.getLatestSchemaVersion(schemaName);
|
|
202
|
+
if (latestVersion === 0) {
|
|
203
|
+
throw new Error(`Schema ${schemaName} not found`);
|
|
204
|
+
}
|
|
205
|
+
// Get schemaFileId from database
|
|
206
|
+
const schemaFileId = await getSchemaFileId(schemaName);
|
|
207
|
+
// Load the latest schema file
|
|
208
|
+
const latestFilePath = getSchemaFilePath$2(schemaName, latestVersion, schemaFileId);
|
|
209
|
+
const content = await main_cjs.BaseFileManager.readFileAsString(latestFilePath);
|
|
210
|
+
const schemaFile = JSON.parse(content);
|
|
211
|
+
if (!schemaFile.$schema) {
|
|
212
|
+
throw new Error(`Schema file ${latestFilePath} is not a complete schema file`);
|
|
213
|
+
}
|
|
214
|
+
// Create a copy of the schema for the new version
|
|
215
|
+
// Preserve schema ID and all model/property IDs
|
|
216
|
+
const newVersion = latestVersion + 1;
|
|
217
|
+
const updatedSchema = {
|
|
218
|
+
...schemaFile,
|
|
219
|
+
version: newVersion,
|
|
220
|
+
// Preserve schema ID from previous version
|
|
221
|
+
id: schemaFile.id,
|
|
222
|
+
metadata: {
|
|
223
|
+
...schemaFile.metadata,
|
|
224
|
+
updatedAt: new Date().toISOString(),
|
|
225
|
+
},
|
|
226
|
+
// Deep copy models to preserve IDs
|
|
227
|
+
models: Object.fromEntries(Object.entries(schemaFile.models).map(([modelName, model]) => [
|
|
228
|
+
modelName,
|
|
229
|
+
{
|
|
230
|
+
...model,
|
|
231
|
+
// Preserve model ID
|
|
232
|
+
id: model.id,
|
|
233
|
+
// Deep copy properties to preserve IDs
|
|
234
|
+
properties: Object.fromEntries(Object.entries(model.properties).map(([propName, prop]) => [
|
|
235
|
+
propName,
|
|
236
|
+
{
|
|
237
|
+
...prop,
|
|
238
|
+
// Preserve property ID
|
|
239
|
+
id: prop.id,
|
|
240
|
+
},
|
|
241
|
+
])),
|
|
242
|
+
},
|
|
243
|
+
])),
|
|
244
|
+
migrations: [
|
|
245
|
+
...schemaFile.migrations,
|
|
246
|
+
{
|
|
247
|
+
version: newVersion,
|
|
248
|
+
timestamp: new Date().toISOString(),
|
|
249
|
+
description: `Updated model properties: ${propertyUpdates.map(u => `${u.modelName}.${u.propertyName}`).join(', ')}`,
|
|
250
|
+
changes: propertyUpdates.map(update => ({
|
|
251
|
+
type: 'property_update',
|
|
252
|
+
modelName: update.modelName,
|
|
253
|
+
propertyName: update.propertyName,
|
|
254
|
+
updates: update.updates,
|
|
255
|
+
})),
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
};
|
|
259
|
+
// Apply property updates
|
|
260
|
+
for (const update of propertyUpdates) {
|
|
261
|
+
const model = updatedSchema.models[update.modelName];
|
|
262
|
+
if (!model) {
|
|
263
|
+
throw new Error(`Model ${update.modelName} not found in schema ${schemaName}`);
|
|
264
|
+
}
|
|
265
|
+
if (!model.properties[update.propertyName]) {
|
|
266
|
+
throw new Error(`Property ${update.propertyName} not found in model ${update.modelName} of schema ${schemaName}`);
|
|
267
|
+
}
|
|
268
|
+
// Update the property with new values
|
|
269
|
+
const property = model.properties[update.propertyName];
|
|
270
|
+
Object.assign(property, update.updates);
|
|
271
|
+
}
|
|
272
|
+
// Write the new schema version to file using ID-based naming (preferred)
|
|
273
|
+
const newFilePath = getSchemaFilePath$2(schemaName, newVersion, updatedSchema.id ?? undefined);
|
|
274
|
+
const newContent = JSON.stringify(updatedSchema, null, 2);
|
|
275
|
+
await main_cjs.BaseFileManager.saveFile(newFilePath, newContent);
|
|
276
|
+
// Wait for the file to be available with content (important for browser/OPFS where writes may not be immediately readable)
|
|
277
|
+
await main_cjs.BaseFileManager.waitForFileWithContent(newFilePath);
|
|
278
|
+
logger$d(`Created new schema version ${newVersion} for ${schemaName} at ${newFilePath}`);
|
|
279
|
+
// Load the new schema file to process models and add them to the database
|
|
280
|
+
// Extract model renames from migrations to pass to database update
|
|
281
|
+
const modelRenames = new Map();
|
|
282
|
+
const latestMigration = updatedSchema.migrations[updatedSchema.migrations.length - 1];
|
|
283
|
+
for (const change of latestMigration.changes) {
|
|
284
|
+
if (change.type === 'model_rename') {
|
|
285
|
+
modelRenames.set(change.oldName, change.newName);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// Load schema with model renames handled
|
|
289
|
+
await loadSchemaWithRenames(newFilePath, modelRenames);
|
|
290
|
+
return newFilePath;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Load a schema file and handle model renames in the database
|
|
294
|
+
* This is a helper function that processes model renames before loading
|
|
295
|
+
*/
|
|
296
|
+
async function loadSchemaWithRenames(schemaFilePath, modelRenames) {
|
|
297
|
+
const content = await main_cjs.BaseFileManager.readFileAsString(schemaFilePath);
|
|
298
|
+
const schemaFile = JSON.parse(content);
|
|
299
|
+
if (!schemaFile.$schema) {
|
|
300
|
+
throw new Error(`File ${schemaFilePath} is not a complete schema file (missing $schema field).`);
|
|
301
|
+
}
|
|
302
|
+
const schemaName = schemaFile.metadata?.name;
|
|
303
|
+
const version = schemaFile.version;
|
|
304
|
+
if (!schemaName) {
|
|
305
|
+
throw new Error('Schema name is required in metadata.name');
|
|
306
|
+
}
|
|
307
|
+
// Convert to JsonImportSchema format for processing
|
|
308
|
+
// Remove id fields for JsonImportSchema format (they're not part of the import format)
|
|
309
|
+
const importData = {
|
|
310
|
+
name: schemaName,
|
|
311
|
+
models: Object.fromEntries(Object.entries(schemaFile.models).map(([modelName, model]) => [
|
|
312
|
+
modelName,
|
|
313
|
+
{
|
|
314
|
+
description: model.description,
|
|
315
|
+
properties: Object.fromEntries(Object.entries(model.properties).map(([propName, prop]) => [
|
|
316
|
+
propName,
|
|
317
|
+
{
|
|
318
|
+
type: prop.type || 'string', // Ensure type is present
|
|
319
|
+
...prop,
|
|
320
|
+
// Remove id field for import format (not part of JsonImportSchema)
|
|
321
|
+
},
|
|
322
|
+
])),
|
|
323
|
+
indexes: model.indexes,
|
|
324
|
+
},
|
|
325
|
+
])),
|
|
326
|
+
};
|
|
327
|
+
// Generate schema ID if missing
|
|
328
|
+
if (!schemaFile.id) {
|
|
329
|
+
schemaFile.id = main_cjs.generateId();
|
|
330
|
+
logger$d('Generated schema ID for schema:', schemaFile.id);
|
|
331
|
+
}
|
|
332
|
+
// Extract schemaFileIds from JSON file and generate missing ones BEFORE creating models
|
|
333
|
+
// This ensures Model instances are created with correct IDs
|
|
334
|
+
const modelFileIds = new Map();
|
|
335
|
+
const propertyFileIds = new Map();
|
|
336
|
+
for (const [modelName, model] of Object.entries(schemaFile.models)) {
|
|
337
|
+
// Generate model ID if missing
|
|
338
|
+
if (!model.id) {
|
|
339
|
+
model.id = main_cjs.generateId();
|
|
340
|
+
logger$d(`Generated model ID for ${modelName}:`, model.id);
|
|
341
|
+
}
|
|
342
|
+
modelFileIds.set(modelName, model.id);
|
|
343
|
+
const propIds = new Map();
|
|
344
|
+
for (const [propName, prop] of Object.entries(model.properties)) {
|
|
345
|
+
// Generate property ID if missing
|
|
346
|
+
if (!prop.id) {
|
|
347
|
+
prop.id = main_cjs.generateId();
|
|
348
|
+
logger$d(`Generated property ID for ${modelName}.${propName}:`, prop.id);
|
|
349
|
+
}
|
|
350
|
+
propIds.set(propName, prop.id);
|
|
351
|
+
}
|
|
352
|
+
if (propIds.size > 0) {
|
|
353
|
+
propertyFileIds.set(modelName, propIds);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
// Convert JSON models to Model classes, passing modelFileIds and propertyFileIds so Model instances use correct IDs
|
|
357
|
+
const modelDefinitions = await main_cjs.createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
358
|
+
// Convert schema file metadata to schema input for database
|
|
359
|
+
const schemaInput = {
|
|
360
|
+
name: schemaName,
|
|
361
|
+
version,
|
|
362
|
+
schemaFileId: schemaFile.id || null,
|
|
363
|
+
schemaData: null,
|
|
364
|
+
isDraft: false,
|
|
365
|
+
isEdited: false,
|
|
366
|
+
createdAt: new Date(schemaFile.metadata.createdAt).getTime(),
|
|
367
|
+
updatedAt: new Date(schemaFile.metadata.updatedAt).getTime(),
|
|
368
|
+
};
|
|
369
|
+
// Use dynamic import to break circular dependency
|
|
370
|
+
const dbMod = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.db; });
|
|
371
|
+
const { addSchemaToDb, addModelsToDb } = dbMod;
|
|
372
|
+
// Add schema to database with schemaFileId
|
|
373
|
+
const schemaRecord = await addSchemaToDb(schemaInput, schemaFile.id);
|
|
374
|
+
// Add models to database with model renames handled and schemaFileIds
|
|
375
|
+
await addModelsToDb(modelDefinitions, schemaRecord, modelRenames, {
|
|
376
|
+
schemaFileId: schemaFile.id,
|
|
377
|
+
modelFileIds,
|
|
378
|
+
propertyFileIds,
|
|
379
|
+
});
|
|
380
|
+
// Models are now Model instances, no registration needed
|
|
381
|
+
// They should be created via Model.create() and are accessible via Model static methods
|
|
382
|
+
for (const [modelName] of Object.entries(modelDefinitions)) {
|
|
383
|
+
logger$d('loadSchemaWithRenames - model available:', modelName);
|
|
384
|
+
}
|
|
385
|
+
return schemaFilePath;
|
|
386
|
+
}
|
|
36
387
|
|
|
37
388
|
const logger$c = debug('seedSdk:schema:actors:loadOrCreateSchema');
|
|
38
389
|
/**
|
|
@@ -76,7 +427,8 @@ const createModelInstances$2 = async (modelIds) => {
|
|
|
76
427
|
return;
|
|
77
428
|
}
|
|
78
429
|
try {
|
|
79
|
-
const
|
|
430
|
+
const modelMod = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.Model$1; });
|
|
431
|
+
const { Model } = modelMod;
|
|
80
432
|
// Create instances for all model IDs in parallel
|
|
81
433
|
// Model.createById() will check cache first, then query DB and create if needed
|
|
82
434
|
const createPromises = modelIds.map(async (modelFileId) => {
|
|
@@ -189,7 +541,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
189
541
|
logger$c(`Loading internal Seed Protocol schema from SDK`);
|
|
190
542
|
console.log('[loadOrCreateSchema] Loading internal Seed Protocol schema from SDK');
|
|
191
543
|
try {
|
|
192
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
544
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
193
545
|
const schemaFile = internalSchema.default;
|
|
194
546
|
console.log('[loadOrCreateSchema] Loaded schema file, models count:', Object.keys(schemaFile.models || {}).length);
|
|
195
547
|
// Check if it exists in database, if not, add it
|
|
@@ -689,7 +1041,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
689
1041
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
690
1042
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
691
1043
|
try {
|
|
692
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1044
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
693
1045
|
const internalSchemaFile = internalSchema.default;
|
|
694
1046
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
695
1047
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -884,7 +1236,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
884
1236
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
885
1237
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
886
1238
|
try {
|
|
887
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1239
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
888
1240
|
const internalSchemaFile = internalSchema.default;
|
|
889
1241
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
890
1242
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -956,7 +1308,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
956
1308
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
957
1309
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
958
1310
|
try {
|
|
959
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1311
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
960
1312
|
const internalSchemaFile = internalSchema.default;
|
|
961
1313
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
962
1314
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -1025,7 +1377,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
1025
1377
|
// If schemaData is missing, try to load from internal schema file for seed-protocol
|
|
1026
1378
|
if (!dbSchema.schemaData && schemaName === 'Seed Protocol') {
|
|
1027
1379
|
try {
|
|
1028
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1380
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
1029
1381
|
const schemaFile = internalSchema.default;
|
|
1030
1382
|
logger$c(`Found seed-protocol schema in internal file (schemaData missing, using internal schema)`);
|
|
1031
1383
|
// Update database with schemaData for future loads
|
|
@@ -1134,7 +1486,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
1134
1486
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
1135
1487
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
1136
1488
|
try {
|
|
1137
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1489
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
1138
1490
|
const internalSchemaFile = internalSchema.default;
|
|
1139
1491
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
1140
1492
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -1450,20 +1802,16 @@ const addModelsMachine = xstate.setup({
|
|
|
1450
1802
|
}),
|
|
1451
1803
|
createModelInstances: xstate.fromCallback(({ sendBack, input }) => {
|
|
1452
1804
|
const _createInstances = async () => {
|
|
1453
|
-
const { Schema } = await Promise.resolve().then(function () { return Schema$1; });
|
|
1454
|
-
const { BaseDb } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.BaseDb$1; });
|
|
1455
|
-
const { models: modelsTable } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.ModelSchema; });
|
|
1456
|
-
const { eq } = await import('drizzle-orm');
|
|
1457
|
-
const { generateId } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.index; });
|
|
1458
1805
|
const debug = (await import('debug')).default;
|
|
1459
1806
|
const logger = debug('seedSdk:schema:addModels:createInstances');
|
|
1807
|
+
const schemaMod = await Promise.resolve().then(function () { return Schema$1; });
|
|
1808
|
+
const { Schema: SchemaClass, schemaInstanceState } = schemaMod;
|
|
1460
1809
|
const schemaName = input.schemaContext.metadata?.name || input.schemaContext.schemaName;
|
|
1461
|
-
const schemaInstance =
|
|
1810
|
+
const schemaInstance = SchemaClass.create(schemaName, {
|
|
1462
1811
|
waitForReady: false,
|
|
1463
1812
|
});
|
|
1464
1813
|
const modelInstances = new Map();
|
|
1465
1814
|
// Get instance state to store model instances
|
|
1466
|
-
const { schemaInstanceState } = await Promise.resolve().then(function () { return Schema$1; });
|
|
1467
1815
|
const instanceState = schemaInstanceState.get(schemaInstance);
|
|
1468
1816
|
if (!instanceState) {
|
|
1469
1817
|
throw new Error('Schema instance state not found');
|
|
@@ -1474,12 +1822,12 @@ const addModelsMachine = xstate.setup({
|
|
|
1474
1822
|
// Look up modelFileId from database BEFORE creating the Model instance
|
|
1475
1823
|
let modelFileId = undefined;
|
|
1476
1824
|
try {
|
|
1477
|
-
const db = BaseDb.getAppDb();
|
|
1825
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
1478
1826
|
if (db) {
|
|
1479
1827
|
const dbModels = await db
|
|
1480
1828
|
.select()
|
|
1481
|
-
.from(
|
|
1482
|
-
.where(eq(
|
|
1829
|
+
.from(main_cjs.models)
|
|
1830
|
+
.where(drizzleOrm.eq(main_cjs.models.name, modelName))
|
|
1483
1831
|
.limit(1);
|
|
1484
1832
|
if (dbModels.length > 0 && dbModels[0].schemaFileId) {
|
|
1485
1833
|
modelFileId = dbModels[0].schemaFileId;
|
|
@@ -1492,7 +1840,7 @@ const addModelsMachine = xstate.setup({
|
|
|
1492
1840
|
}
|
|
1493
1841
|
// If not found in database, generate a new modelFileId for new models
|
|
1494
1842
|
if (!modelFileId) {
|
|
1495
|
-
modelFileId = generateId();
|
|
1843
|
+
modelFileId = main_cjs.generateId();
|
|
1496
1844
|
logger(`Generated new modelFileId "${modelFileId}" for new model "${modelName}"`);
|
|
1497
1845
|
}
|
|
1498
1846
|
// Create new Model instance with modelFileId
|
|
@@ -1681,9 +2029,6 @@ const addModelsMachine = xstate.setup({
|
|
|
1681
2029
|
const _persist = async () => {
|
|
1682
2030
|
const debug = (await import('debug')).default;
|
|
1683
2031
|
const logger = debug('seedSdk:schema:addModels:persist');
|
|
1684
|
-
const { BaseDb } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.BaseDb$1; });
|
|
1685
|
-
const { schemas: schemasTable } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.SchemaSchema; });
|
|
1686
|
-
const { eq } = await import('drizzle-orm');
|
|
1687
2032
|
// Only process in browser environment where store is available
|
|
1688
2033
|
if (typeof window === 'undefined') {
|
|
1689
2034
|
logger('Not in browser environment, skipping store update');
|
|
@@ -1695,7 +2040,7 @@ const addModelsMachine = xstate.setup({
|
|
|
1695
2040
|
}
|
|
1696
2041
|
const schemaName = input.schemaContext.metadata?.name || input.schemaContext.schemaName;
|
|
1697
2042
|
const schemaFileId = input.schemaContext.id; // id is the schemaFileId (string) in SchemaMachineContext
|
|
1698
|
-
const db = BaseDb.getAppDb();
|
|
2043
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
1699
2044
|
if (!db) {
|
|
1700
2045
|
logger('Database not found, skipping model store update');
|
|
1701
2046
|
sendBack({
|
|
@@ -1719,8 +2064,8 @@ const addModelsMachine = xstate.setup({
|
|
|
1719
2064
|
logger(`Looking up schema by schemaFileId: ${schemaFileId} (attempt ${attempt + 1})`);
|
|
1720
2065
|
const schemasById = await db
|
|
1721
2066
|
.select()
|
|
1722
|
-
.from(
|
|
1723
|
-
.where(eq(
|
|
2067
|
+
.from(main_cjs.schemas)
|
|
2068
|
+
.where(drizzleOrm.eq(main_cjs.schemas.schemaFileId, schemaFileId))
|
|
1724
2069
|
.limit(1);
|
|
1725
2070
|
if (schemasById.length > 0) {
|
|
1726
2071
|
schemaRecord = schemasById[0];
|
|
@@ -1736,12 +2081,11 @@ const addModelsMachine = xstate.setup({
|
|
|
1736
2081
|
// FALLBACK: If not found by schemaFileId, try by name (prefer draft records)
|
|
1737
2082
|
if (!schemaRecord) {
|
|
1738
2083
|
logger(`Looking up schema by name: "${schemaName}" (attempt ${attempt + 1})`);
|
|
1739
|
-
const { desc } = await import('drizzle-orm');
|
|
1740
2084
|
const schemasByName = await db
|
|
1741
2085
|
.select()
|
|
1742
|
-
.from(
|
|
1743
|
-
.where(eq(
|
|
1744
|
-
.orderBy(desc(
|
|
2086
|
+
.from(main_cjs.schemas)
|
|
2087
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, schemaName))
|
|
2088
|
+
.orderBy(drizzleOrm.desc(main_cjs.schemas.isDraft), drizzleOrm.desc(main_cjs.schemas.version))
|
|
1745
2089
|
.limit(10); // Get multiple to find drafts
|
|
1746
2090
|
if (schemasByName.length > 0) {
|
|
1747
2091
|
// Prefer draft records
|
|
@@ -2040,7 +2384,7 @@ const createModelInstances$1 = async (modelIds) => {
|
|
|
2040
2384
|
return;
|
|
2041
2385
|
}
|
|
2042
2386
|
try {
|
|
2043
|
-
const mod = await Promise.resolve().then(function () { return require('./index-
|
|
2387
|
+
const mod = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.Model$1; });
|
|
2044
2388
|
const Model = mod?.Model ?? mod?.default;
|
|
2045
2389
|
if (!Model) {
|
|
2046
2390
|
logger$b('Model not available from dynamic import');
|
|
@@ -2085,7 +2429,7 @@ const checkExistingSchema = xstate.fromCallback(({ sendBack, input: { context }
|
|
|
2085
2429
|
if (isInternal && schemaName === main_cjs.SEED_PROTOCOL_SCHEMA_NAME) {
|
|
2086
2430
|
// For Seed Protocol, check if it exists in database
|
|
2087
2431
|
try {
|
|
2088
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
2432
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
2089
2433
|
const schemaFile = internalSchema.default;
|
|
2090
2434
|
if (db && schemaFile.id) {
|
|
2091
2435
|
const existing = await db
|
|
@@ -2443,7 +2787,7 @@ const writeSchemaToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2443
2787
|
if (isInternal && schemaName === main_cjs.SEED_PROTOCOL_SCHEMA_NAME) {
|
|
2444
2788
|
// For Seed Protocol, load from internal file
|
|
2445
2789
|
logger$a(`Loading internal Seed Protocol schema from SDK`);
|
|
2446
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
2790
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
2447
2791
|
finalSchema = internalSchema.default;
|
|
2448
2792
|
const db = main_cjs.BaseDb.getAppDb();
|
|
2449
2793
|
if (db && finalSchema.id) {
|
|
@@ -2911,7 +3255,8 @@ const createModelInstances = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2911
3255
|
return;
|
|
2912
3256
|
}
|
|
2913
3257
|
try {
|
|
2914
|
-
const
|
|
3258
|
+
const modelMod = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.Model$1; });
|
|
3259
|
+
const { Model } = modelMod;
|
|
2915
3260
|
// Create instances for all model IDs in parallel
|
|
2916
3261
|
// Model.createById() will check cache first, then query DB and create if needed
|
|
2917
3262
|
const createPromises = modelIds.map(async (modelFileId) => {
|
|
@@ -2996,7 +3341,8 @@ const verifyModelInstancesInCache = xstate.fromCallback(({ sendBack, input }) =>
|
|
|
2996
3341
|
}
|
|
2997
3342
|
try {
|
|
2998
3343
|
const result = await verifyWithRetry$2(async () => {
|
|
2999
|
-
const
|
|
3344
|
+
const modelMod = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.Model$1; });
|
|
3345
|
+
const { Model } = modelMod;
|
|
3000
3346
|
// Check each model ID in the cache
|
|
3001
3347
|
const verifiedInstances = [];
|
|
3002
3348
|
const missingIds = [];
|
|
@@ -3052,10 +3398,11 @@ const writePropertiesToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
3052
3398
|
const _write = async () => {
|
|
3053
3399
|
const { modelIds } = input;
|
|
3054
3400
|
try {
|
|
3055
|
-
const
|
|
3056
|
-
const { models: modelsTable, properties: propertiesTable } =
|
|
3057
|
-
const
|
|
3058
|
-
const
|
|
3401
|
+
const modelSchemaMod = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.ModelSchema; });
|
|
3402
|
+
const { models: modelsTable, properties: propertiesTable } = modelSchemaMod;
|
|
3403
|
+
const drizzleMod = await import('drizzle-orm');
|
|
3404
|
+
const { eq } = drizzleMod;
|
|
3405
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
3059
3406
|
if (!db) {
|
|
3060
3407
|
throw new Error('Database not available');
|
|
3061
3408
|
}
|
|
@@ -3239,9 +3586,9 @@ const createPropertyInstances = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
3239
3586
|
return;
|
|
3240
3587
|
}
|
|
3241
3588
|
try {
|
|
3242
|
-
const modProp = await Promise.resolve().then(function () { return require('./ModelProperty-
|
|
3589
|
+
const modProp = await Promise.resolve().then(function () { return require('./ModelProperty-B4pV91tc.js'); });
|
|
3243
3590
|
const ModelProperty = modProp?.ModelProperty ?? modProp?.default;
|
|
3244
|
-
const modModel = await Promise.resolve().then(function () { return require('./index-
|
|
3591
|
+
const modModel = await Promise.resolve().then(function () { return require('./index-CXI4VF1H.js'); }).then(function (n) { return n.Model$1; });
|
|
3245
3592
|
const Model = modModel?.Model ?? modModel?.default;
|
|
3246
3593
|
if (!ModelProperty || !Model) {
|
|
3247
3594
|
logger$2('ModelProperty or Model not available from dynamic import');
|
|
@@ -3346,7 +3693,7 @@ const verifyPropertyInstancesInCache = xstate.fromCallback(({ sendBack, input })
|
|
|
3346
3693
|
}
|
|
3347
3694
|
try {
|
|
3348
3695
|
const result = await verifyWithRetry(async () => {
|
|
3349
|
-
const mod = await Promise.resolve().then(function () { return require('./ModelProperty-
|
|
3696
|
+
const mod = await Promise.resolve().then(function () { return require('./ModelProperty-B4pV91tc.js'); });
|
|
3350
3697
|
const ModelProperty = mod?.ModelProperty ?? mod?.default;
|
|
3351
3698
|
if (!ModelProperty) {
|
|
3352
3699
|
throw new Error('ModelProperty not available from dynamic import');
|
|
@@ -4926,7 +5273,6 @@ class Schema {
|
|
|
4926
5273
|
throw new main_cjs.ConflictError(errorMessage, conflictCheck);
|
|
4927
5274
|
}
|
|
4928
5275
|
const context = this._getSnapshotContext();
|
|
4929
|
-
const { addSchemaToDb } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.db; });
|
|
4930
5276
|
if (!context._isDraft || !context._editedProperties || context._editedProperties.size === 0) {
|
|
4931
5277
|
logger('No changes to save');
|
|
4932
5278
|
return '';
|
|
@@ -4960,7 +5306,7 @@ class Schema {
|
|
|
4960
5306
|
migrations: context.migrations || [],
|
|
4961
5307
|
};
|
|
4962
5308
|
// Update draft in database with current state
|
|
4963
|
-
await addSchemaToDb({
|
|
5309
|
+
await main_cjs.addSchemaToDb({
|
|
4964
5310
|
name: this.schemaName,
|
|
4965
5311
|
version: currentSchema.version,
|
|
4966
5312
|
createdAt: new Date(currentSchema.metadata.createdAt).getTime(),
|
|
@@ -4973,6 +5319,8 @@ class Schema {
|
|
|
4973
5319
|
.limit(1);
|
|
4974
5320
|
// Collect all edited properties and convert them to SchemaPropertyUpdate format
|
|
4975
5321
|
const propertyUpdates = [];
|
|
5322
|
+
const modelPropertyMod = await Promise.resolve().then(function () { return require('./ModelProperty-B4pV91tc.js'); });
|
|
5323
|
+
const { ModelProperty } = modelPropertyMod;
|
|
4976
5324
|
for (const propertyKey of context._editedProperties) {
|
|
4977
5325
|
// Skip schema-level changes (like schema name changes)
|
|
4978
5326
|
if (propertyKey === 'schema:name') {
|
|
@@ -4986,7 +5334,7 @@ class Schema {
|
|
|
4986
5334
|
}
|
|
4987
5335
|
// Get ModelProperty instance from cache
|
|
4988
5336
|
const cacheKey = `${modelName}:${propertyName}`;
|
|
4989
|
-
const ModelPropertyClass = ModelProperty
|
|
5337
|
+
const ModelPropertyClass = ModelProperty;
|
|
4990
5338
|
const cachedInstance = ModelPropertyClass.instanceCache.get(cacheKey);
|
|
4991
5339
|
if (!cachedInstance) {
|
|
4992
5340
|
logger(`ModelProperty instance not found for ${cacheKey}`);
|
|
@@ -4995,15 +5343,14 @@ class Schema {
|
|
|
4995
5343
|
const modelProperty = cachedInstance.instance;
|
|
4996
5344
|
const propertyContext = modelProperty.getService().getSnapshot().context;
|
|
4997
5345
|
// Convert to SchemaPropertyUpdate
|
|
4998
|
-
const propertyUpdate = await
|
|
5346
|
+
const propertyUpdate = await convertPropertyToSchemaUpdate(propertyContext, modelName, propertyName);
|
|
4999
5347
|
propertyUpdates.push(propertyUpdate);
|
|
5000
5348
|
}
|
|
5001
5349
|
if (propertyUpdates.length === 0) {
|
|
5002
5350
|
// When only new models were added, _editedProperties contains 'schema:models' and we write the full schema
|
|
5003
5351
|
if (context._editedProperties.has('schema:models')) {
|
|
5004
|
-
const newFilePath = await
|
|
5005
|
-
const
|
|
5006
|
-
const fileContent = await BaseFileManager.readFileAsString(newFilePath);
|
|
5352
|
+
const newFilePath = await writeFullSchemaNewVersion(this.schemaName, currentSchema);
|
|
5353
|
+
const fileContent = await main_cjs.BaseFileManager.readFileAsString(newFilePath);
|
|
5007
5354
|
const publishedSchema = JSON.parse(fileContent);
|
|
5008
5355
|
if (dbSchema.length > 0) {
|
|
5009
5356
|
await db
|
|
@@ -5029,11 +5376,10 @@ class Schema {
|
|
|
5029
5376
|
return '';
|
|
5030
5377
|
}
|
|
5031
5378
|
// STEP 2: Save to new schema version (writes file)
|
|
5032
|
-
const newFilePath = await
|
|
5379
|
+
const newFilePath = await updateModelProperties(this.schemaName, propertyUpdates);
|
|
5033
5380
|
// STEP 3: After file is written, update database to mark as published (isDraft = false)
|
|
5034
5381
|
// Load the file to get the final schema with IDs
|
|
5035
|
-
const
|
|
5036
|
-
const fileContent = await BaseFileManager.readFileAsString(newFilePath);
|
|
5382
|
+
const fileContent = await main_cjs.BaseFileManager.readFileAsString(newFilePath);
|
|
5037
5383
|
const publishedSchema = JSON.parse(fileContent);
|
|
5038
5384
|
// Update database record: set isDraft = false and update schemaFileId
|
|
5039
5385
|
if (dbSchema.length > 0) {
|
|
@@ -5051,7 +5397,7 @@ class Schema {
|
|
|
5051
5397
|
}
|
|
5052
5398
|
else {
|
|
5053
5399
|
// Create new record if it doesn't exist (shouldn't happen, but safety)
|
|
5054
|
-
await addSchemaToDb({
|
|
5400
|
+
await main_cjs.addSchemaToDb({
|
|
5055
5401
|
name: this.schemaName,
|
|
5056
5402
|
version: publishedSchema.version,
|
|
5057
5403
|
createdAt: new Date(publishedSchema.metadata.createdAt).getTime(),
|
|
@@ -5069,7 +5415,7 @@ class Schema {
|
|
|
5069
5415
|
for (const propertyKey of context._editedProperties) {
|
|
5070
5416
|
const [modelName, propertyName] = propertyKey.split(':');
|
|
5071
5417
|
const cacheKey = `${modelName}:${propertyName}`;
|
|
5072
|
-
const ModelPropertyClass = ModelProperty
|
|
5418
|
+
const ModelPropertyClass = ModelProperty;
|
|
5073
5419
|
const cachedInstance = ModelPropertyClass.instanceCache.get(cacheKey);
|
|
5074
5420
|
if (cachedInstance) {
|
|
5075
5421
|
const modelProperty = cachedInstance.instance;
|
|
@@ -5227,14 +5573,11 @@ class Schema {
|
|
|
5227
5573
|
saveDraftLogger(`Client check: cacheIsStale=${cacheIsStale}, shouldRecheck=${shouldRecheck}, cachedValue=${cachedClientInitialized}, timeSinceCheck=${now - clientCheckTime}ms`);
|
|
5228
5574
|
if (shouldRecheck) {
|
|
5229
5575
|
try {
|
|
5230
|
-
|
|
5231
|
-
const { getClient } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.ClientManager$1; });
|
|
5232
|
-
const { ClientManagerState } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.constants; });
|
|
5233
|
-
const client = getClient();
|
|
5576
|
+
const client = main_cjs.getClient();
|
|
5234
5577
|
const clientSnapshot = client.getService().getSnapshot();
|
|
5235
5578
|
// Check if state is IDLE (primary check) - isInitialized is set in entry action so should be true
|
|
5236
5579
|
// But we check it as a secondary safeguard
|
|
5237
|
-
const isIdle = clientSnapshot.value === ClientManagerState.IDLE;
|
|
5580
|
+
const isIdle = clientSnapshot.value === main_cjs.ClientManagerState.IDLE;
|
|
5238
5581
|
const isInitialized = clientSnapshot.context.isInitialized;
|
|
5239
5582
|
// If state is IDLE, trust it even if isInitialized isn't set yet (entry action should set it)
|
|
5240
5583
|
// This aligns with useIsClientReady which only checks the state value
|
|
@@ -5300,8 +5643,6 @@ class Schema {
|
|
|
5300
5643
|
_editedProperties: new Set(),
|
|
5301
5644
|
};
|
|
5302
5645
|
}
|
|
5303
|
-
const { addSchemaToDb } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.db; });
|
|
5304
|
-
const { generateId } = await Promise.resolve().then(function () { return require('./index-C3j3m34a.js'); }).then(function (n) { return n.index; });
|
|
5305
5646
|
const db = main_cjs.BaseDb.getAppDb();
|
|
5306
5647
|
if (!db) {
|
|
5307
5648
|
throw new Error('Database not found');
|
|
@@ -5376,7 +5717,7 @@ class Schema {
|
|
|
5376
5717
|
}
|
|
5377
5718
|
// Build current schema state from context
|
|
5378
5719
|
// Use existing schemaFileId if we found one, otherwise use the one from context, or generate new
|
|
5379
|
-
const schemaFileId = existingSchemaId || context.id || generateId(); // id is now the schemaFileId (string)
|
|
5720
|
+
const schemaFileId = existingSchemaId || context.id || main_cjs.generateId(); // id is now the schemaFileId (string)
|
|
5380
5721
|
// Build metadata - if name changed, use newName, otherwise use context metadata
|
|
5381
5722
|
// Always ensure metadata.name matches finalNewName to prevent inconsistencies
|
|
5382
5723
|
const currentMetadata = context.metadata ? {
|
|
@@ -5554,7 +5895,7 @@ class Schema {
|
|
|
5554
5895
|
throw new Error(`Failed to update schema name: existing record not found or update failed`);
|
|
5555
5896
|
}
|
|
5556
5897
|
// Otherwise, use addSchemaToDb which will handle create/update logic (for normal saves, not name changes)
|
|
5557
|
-
await addSchemaToDb({
|
|
5898
|
+
await main_cjs.addSchemaToDb({
|
|
5558
5899
|
name: finalNewName,
|
|
5559
5900
|
version: currentSchema.version,
|
|
5560
5901
|
createdAt: new Date(currentSchema.metadata.createdAt).getTime(),
|
|
@@ -5670,7 +6011,8 @@ class Schema {
|
|
|
5670
6011
|
.filter((id) => id != null);
|
|
5671
6012
|
if (schemaIds.length === 0)
|
|
5672
6013
|
return;
|
|
5673
|
-
const
|
|
6014
|
+
const drizzleMod = await import('drizzle-orm');
|
|
6015
|
+
const { inArray } = drizzleMod;
|
|
5674
6016
|
const joinRows = await db
|
|
5675
6017
|
.select({ modelId: main_cjs.modelSchemas.modelId })
|
|
5676
6018
|
.from(main_cjs.modelSchemas)
|
|
@@ -5869,5 +6211,7 @@ var Schema$1 = /*#__PURE__*/Object.freeze({
|
|
|
5869
6211
|
});
|
|
5870
6212
|
|
|
5871
6213
|
exports.Schema = Schema;
|
|
5872
|
-
exports.
|
|
5873
|
-
|
|
6214
|
+
exports.Schema$1 = Schema$1;
|
|
6215
|
+
exports.convertPropertyToSchemaUpdate = convertPropertyToSchemaUpdate;
|
|
6216
|
+
exports.updateModelProperties = updateModelProperties;
|
|
6217
|
+
//# sourceMappingURL=Schema-TErl3J36.js.map
|