@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,739 +1,22 @@
|
|
|
1
1
|
import { fromCallback, setup, assign, createActor } from 'xstate';
|
|
2
|
-
import {
|
|
3
|
-
import '
|
|
4
|
-
import { eq, and } from 'drizzle-orm';
|
|
2
|
+
import { c as convertPropertyToSchemaUpdate, u as updateModelProperties, S as Schema } from './Schema-BZKBy5Tx.js';
|
|
3
|
+
import { p as BaseDb, ad as writeProcessMachine, q as models, r as properties, M as Model, o as modelPropertiesToObject, W as schemas, G as modelSchemas, ae as generateId, af as waitForEntityIdle, ag as createReactiveProxy, ah as findEntity, x as BaseFileManager, ai as forceRemoveFromCaches, aj as runDestroyLifecycle, ak as getModelIdByFileId, al as getModelId } from './index-Dk73a7JE.js';
|
|
5
4
|
import debug from 'debug';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return name
|
|
22
|
-
.replace(/[^a-zA-Z0-9\s_-]/g, '_') // Replace special chars (except spaces, hyphens, underscores) with underscore
|
|
23
|
-
.replace(/\s+/g, '_') // Convert spaces to underscores
|
|
24
|
-
.replace(/^_+|_+$/g, '') // Remove leading/trailing underscores
|
|
25
|
-
.replace(/_+/g, '_'); // Collapse multiple underscores to single
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Get the full file path for a schema
|
|
29
|
-
* Format: {schemaFileId}_{schemaName}_v{version}.json
|
|
30
|
-
*
|
|
31
|
-
* The ID-first format ensures all files for a schema group together when sorted alphabetically.
|
|
32
|
-
*
|
|
33
|
-
* @param name - Schema name
|
|
34
|
-
* @param version - Schema version
|
|
35
|
-
* @param schemaFileId - Schema file ID (required)
|
|
36
|
-
*/
|
|
37
|
-
const getSchemaFilePath = (name, version, schemaFileId) => {
|
|
38
|
-
const path = BaseFileManager.getPathModule();
|
|
39
|
-
const workingDir = BaseFileManager.getWorkingDir();
|
|
40
|
-
const sanitizedName = sanitizeSchemaName(name);
|
|
41
|
-
const filename = schemaFileId
|
|
42
|
-
? `${schemaFileId}_${sanitizedName}_v${version}.json`
|
|
43
|
-
: `${sanitizedName}_v${version}.json`;
|
|
44
|
-
return path.join(workingDir, filename);
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Get schemaFileId from database for a schema
|
|
48
|
-
* @param schemaName - Schema name
|
|
49
|
-
* @returns Schema file ID
|
|
50
|
-
* @throws Error if schema not found or missing schemaFileId
|
|
51
|
-
*/
|
|
52
|
-
async function getSchemaFileId(schemaName) {
|
|
53
|
-
const { BaseDb } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aP; });
|
|
54
|
-
const db = BaseDb.getAppDb();
|
|
55
|
-
const { schemas } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aU; });
|
|
56
|
-
const { eq, desc } = await import('drizzle-orm');
|
|
57
|
-
const dbSchema = await db
|
|
58
|
-
.select()
|
|
59
|
-
.from(schemas)
|
|
60
|
-
.where(eq(schemas.name, schemaName))
|
|
61
|
-
.orderBy(desc(schemas.version))
|
|
62
|
-
.limit(1);
|
|
63
|
-
if (dbSchema.length === 0 || !dbSchema[0].schemaFileId) {
|
|
64
|
-
throw new Error(`Schema ${schemaName} not found in database or missing schemaFileId`);
|
|
65
|
-
}
|
|
66
|
-
return dbSchema[0].schemaFileId;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Write the full schema to a new version file (e.g. when new models were added).
|
|
70
|
-
* Used when _editedProperties contains 'schema:models' and there are no property-level updates.
|
|
71
|
-
* @param schemaName - Schema name
|
|
72
|
-
* @param schema - Full schema object (e.g. from _buildModelsFromInstances)
|
|
73
|
-
* @returns The file path of the new schema version
|
|
74
|
-
*/
|
|
75
|
-
async function writeFullSchemaNewVersion(schemaName, schema) {
|
|
76
|
-
const latestVersion = await getLatestSchemaVersion(schemaName);
|
|
77
|
-
const newVersion = latestVersion + 1;
|
|
78
|
-
const schemaWithNewVersion = {
|
|
79
|
-
...schema,
|
|
80
|
-
version: newVersion,
|
|
81
|
-
metadata: {
|
|
82
|
-
...schema.metadata,
|
|
83
|
-
updatedAt: new Date().toISOString(),
|
|
84
|
-
},
|
|
85
|
-
migrations: [
|
|
86
|
-
...(schema.migrations || []),
|
|
87
|
-
{
|
|
88
|
-
version: newVersion,
|
|
89
|
-
timestamp: new Date().toISOString(),
|
|
90
|
-
description: 'New schema version (e.g. new models added)',
|
|
91
|
-
changes: [{ type: 'full_schema_write' }],
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
};
|
|
95
|
-
const newFilePath = getSchemaFilePath(schemaName, newVersion, schema.id ?? (await getSchemaFileId(schemaName)));
|
|
96
|
-
const newContent = JSON.stringify(schemaWithNewVersion, null, 2);
|
|
97
|
-
await BaseFileManager.saveFile(newFilePath, newContent);
|
|
98
|
-
await BaseFileManager.waitForFileWithContent(newFilePath);
|
|
99
|
-
logger$4(`Created new schema version ${newVersion} for ${schemaName} at ${newFilePath}`);
|
|
100
|
-
return newFilePath;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Get model name from modelId
|
|
104
|
-
* @param modelId - The model ID to look up
|
|
105
|
-
* @returns The model name, or undefined if not found
|
|
106
|
-
*/
|
|
107
|
-
async function getModelNameFromId(modelId) {
|
|
108
|
-
if (!modelId) {
|
|
109
|
-
return undefined;
|
|
110
|
-
}
|
|
111
|
-
const db = BaseDb.getAppDb();
|
|
112
|
-
if (!db) {
|
|
113
|
-
throw new Error('Database not found');
|
|
114
|
-
}
|
|
115
|
-
const modelRecords = await db
|
|
116
|
-
.select()
|
|
117
|
-
.from(models)
|
|
118
|
-
.where(eq(models.id, modelId))
|
|
119
|
-
.limit(1);
|
|
120
|
-
if (modelRecords.length === 0) {
|
|
121
|
-
return undefined;
|
|
122
|
-
}
|
|
123
|
-
return modelRecords[0].name;
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Convert a TProperty/ModelPropertyMachineContext to SchemaPropertyUpdate format
|
|
127
|
-
* This function converts the internal property representation to the schema file format
|
|
128
|
-
* @param property - The TProperty instance to convert
|
|
129
|
-
* @param modelName - The name of the model this property belongs to
|
|
130
|
-
* @param propertyName - The name of the property
|
|
131
|
-
* @returns A SchemaPropertyUpdate object ready to be passed to updateModelProperties
|
|
132
|
-
*/
|
|
133
|
-
async function convertPropertyToSchemaUpdate(property, modelName, propertyName) {
|
|
134
|
-
const updates = {};
|
|
135
|
-
// Convert dataType to type
|
|
136
|
-
if (property.dataType) {
|
|
137
|
-
updates.type = property.dataType;
|
|
138
|
-
}
|
|
139
|
-
// Handle Relation type
|
|
140
|
-
if (property.dataType === ModelPropertyDataTypes.Relation) {
|
|
141
|
-
if (property.ref) {
|
|
142
|
-
updates.model = property.ref;
|
|
143
|
-
}
|
|
144
|
-
else if (property.refModelId) {
|
|
145
|
-
// If ref is not set but refModelId is, get the model name from the database
|
|
146
|
-
const refModelName = await getModelNameFromId(property.refModelId);
|
|
147
|
-
if (refModelName) {
|
|
148
|
-
updates.model = refModelName;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
// Handle List type
|
|
153
|
-
if (property.dataType === ModelPropertyDataTypes.List) {
|
|
154
|
-
if (property.refValueType) {
|
|
155
|
-
updates.items = {
|
|
156
|
-
type: property.refValueType,
|
|
157
|
-
};
|
|
158
|
-
if (property.ref) {
|
|
159
|
-
updates.items.model = property.ref;
|
|
160
|
-
}
|
|
161
|
-
else if (property.refModelId) {
|
|
162
|
-
// If ref is not set but refModelId is, get the model name from the database
|
|
163
|
-
const refModelName = await getModelNameFromId(property.refModelId);
|
|
164
|
-
if (refModelName) {
|
|
165
|
-
updates.items.model = refModelName;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
// Handle storage configuration (for Text properties with storage)
|
|
171
|
-
if (property.storageType || property.localStorageDir || property.filenameSuffix) {
|
|
172
|
-
const storageType = property.storageType || 'ItemStorage'; // Default to ItemStorage if not specified
|
|
173
|
-
updates.storage = {
|
|
174
|
-
type: storageType,
|
|
175
|
-
};
|
|
176
|
-
if (property.localStorageDir) {
|
|
177
|
-
updates.storage.path = property.localStorageDir;
|
|
178
|
-
}
|
|
179
|
-
if (property.filenameSuffix) {
|
|
180
|
-
updates.storage.extension = property.filenameSuffix;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
return {
|
|
184
|
-
modelName,
|
|
185
|
-
propertyName,
|
|
186
|
-
updates,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Update model properties in a schema and create a new version
|
|
191
|
-
* @param schemaName - The name of the schema to update
|
|
192
|
-
* @param propertyUpdates - Array of property updates to apply
|
|
193
|
-
* @param modelUpdates - Optional array of model renames
|
|
194
|
-
* @returns The file path of the new schema version
|
|
195
|
-
* @throws Error if schema not found or updates are invalid
|
|
196
|
-
*/
|
|
197
|
-
async function updateModelProperties(schemaName, propertyUpdates, modelUpdates) {
|
|
198
|
-
// Get the latest version of the schema
|
|
199
|
-
const latestVersion = await getLatestSchemaVersion(schemaName);
|
|
200
|
-
if (latestVersion === 0) {
|
|
201
|
-
throw new Error(`Schema ${schemaName} not found`);
|
|
202
|
-
}
|
|
203
|
-
// Get schemaFileId from database
|
|
204
|
-
const schemaFileId = await getSchemaFileId(schemaName);
|
|
205
|
-
// Load the latest schema file
|
|
206
|
-
const latestFilePath = getSchemaFilePath(schemaName, latestVersion, schemaFileId);
|
|
207
|
-
const content = await BaseFileManager.readFileAsString(latestFilePath);
|
|
208
|
-
const schemaFile = JSON.parse(content);
|
|
209
|
-
if (!schemaFile.$schema) {
|
|
210
|
-
throw new Error(`Schema file ${latestFilePath} is not a complete schema file`);
|
|
211
|
-
}
|
|
212
|
-
// Create a copy of the schema for the new version
|
|
213
|
-
// Preserve schema ID and all model/property IDs
|
|
214
|
-
const newVersion = latestVersion + 1;
|
|
215
|
-
const updatedSchema = {
|
|
216
|
-
...schemaFile,
|
|
217
|
-
version: newVersion,
|
|
218
|
-
// Preserve schema ID from previous version
|
|
219
|
-
id: schemaFile.id,
|
|
220
|
-
metadata: {
|
|
221
|
-
...schemaFile.metadata,
|
|
222
|
-
updatedAt: new Date().toISOString(),
|
|
223
|
-
},
|
|
224
|
-
// Deep copy models to preserve IDs
|
|
225
|
-
models: Object.fromEntries(Object.entries(schemaFile.models).map(([modelName, model]) => [
|
|
226
|
-
modelName,
|
|
227
|
-
{
|
|
228
|
-
...model,
|
|
229
|
-
// Preserve model ID
|
|
230
|
-
id: model.id,
|
|
231
|
-
// Deep copy properties to preserve IDs
|
|
232
|
-
properties: Object.fromEntries(Object.entries(model.properties).map(([propName, prop]) => [
|
|
233
|
-
propName,
|
|
234
|
-
{
|
|
235
|
-
...prop,
|
|
236
|
-
// Preserve property ID
|
|
237
|
-
id: prop.id,
|
|
238
|
-
},
|
|
239
|
-
])),
|
|
240
|
-
},
|
|
241
|
-
])),
|
|
242
|
-
migrations: [
|
|
243
|
-
...schemaFile.migrations,
|
|
244
|
-
{
|
|
245
|
-
version: newVersion,
|
|
246
|
-
timestamp: new Date().toISOString(),
|
|
247
|
-
description: `Updated model properties: ${propertyUpdates.map(u => `${u.modelName}.${u.propertyName}`).join(', ')}`,
|
|
248
|
-
changes: propertyUpdates.map(update => ({
|
|
249
|
-
type: 'property_update',
|
|
250
|
-
modelName: update.modelName,
|
|
251
|
-
propertyName: update.propertyName,
|
|
252
|
-
updates: update.updates,
|
|
253
|
-
})),
|
|
254
|
-
},
|
|
255
|
-
],
|
|
256
|
-
};
|
|
257
|
-
// Apply model renames first (if any)
|
|
258
|
-
if (modelUpdates && modelUpdates.length > 0) {
|
|
259
|
-
for (const modelUpdate of modelUpdates) {
|
|
260
|
-
if (updatedSchema.models[modelUpdate.oldName]) {
|
|
261
|
-
// Rename the model in the models object
|
|
262
|
-
updatedSchema.models[modelUpdate.newName] = updatedSchema.models[modelUpdate.oldName];
|
|
263
|
-
delete updatedSchema.models[modelUpdate.oldName];
|
|
264
|
-
// Update any property references to this model
|
|
265
|
-
for (const modelName in updatedSchema.models) {
|
|
266
|
-
const model = updatedSchema.models[modelName];
|
|
267
|
-
for (const propertyName in model.properties) {
|
|
268
|
-
const property = model.properties[propertyName];
|
|
269
|
-
if (property.model === modelUpdate.oldName) {
|
|
270
|
-
property.model = modelUpdate.newName;
|
|
271
|
-
}
|
|
272
|
-
if (property.items?.model === modelUpdate.oldName) {
|
|
273
|
-
property.items.model = modelUpdate.newName;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
// Add to migration changes
|
|
278
|
-
updatedSchema.migrations[updatedSchema.migrations.length - 1].changes.push({
|
|
279
|
-
type: 'model_rename',
|
|
280
|
-
oldName: modelUpdate.oldName,
|
|
281
|
-
newName: modelUpdate.newName,
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
// Apply property updates
|
|
287
|
-
for (const update of propertyUpdates) {
|
|
288
|
-
const model = updatedSchema.models[update.modelName];
|
|
289
|
-
if (!model) {
|
|
290
|
-
throw new Error(`Model ${update.modelName} not found in schema ${schemaName}`);
|
|
291
|
-
}
|
|
292
|
-
if (!model.properties[update.propertyName]) {
|
|
293
|
-
throw new Error(`Property ${update.propertyName} not found in model ${update.modelName} of schema ${schemaName}`);
|
|
294
|
-
}
|
|
295
|
-
// Update the property with new values
|
|
296
|
-
const property = model.properties[update.propertyName];
|
|
297
|
-
Object.assign(property, update.updates);
|
|
298
|
-
}
|
|
299
|
-
// Write the new schema version to file using ID-based naming (preferred)
|
|
300
|
-
const newFilePath = getSchemaFilePath(schemaName, newVersion, updatedSchema.id ?? undefined);
|
|
301
|
-
const newContent = JSON.stringify(updatedSchema, null, 2);
|
|
302
|
-
await BaseFileManager.saveFile(newFilePath, newContent);
|
|
303
|
-
// Wait for the file to be available with content (important for browser/OPFS where writes may not be immediately readable)
|
|
304
|
-
await BaseFileManager.waitForFileWithContent(newFilePath);
|
|
305
|
-
logger$4(`Created new schema version ${newVersion} for ${schemaName} at ${newFilePath}`);
|
|
306
|
-
// Load the new schema file to process models and add them to the database
|
|
307
|
-
// Extract model renames from migrations to pass to database update
|
|
308
|
-
const modelRenames = new Map();
|
|
309
|
-
const latestMigration = updatedSchema.migrations[updatedSchema.migrations.length - 1];
|
|
310
|
-
for (const change of latestMigration.changes) {
|
|
311
|
-
if (change.type === 'model_rename') {
|
|
312
|
-
modelRenames.set(change.oldName, change.newName);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
// Load schema with model renames handled
|
|
316
|
-
await loadSchemaWithRenames(newFilePath, modelRenames);
|
|
317
|
-
return newFilePath;
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Load a schema file and handle model renames in the database
|
|
321
|
-
* This is a helper function that processes model renames before loading
|
|
322
|
-
*/
|
|
323
|
-
async function loadSchemaWithRenames(schemaFilePath, modelRenames) {
|
|
324
|
-
const content = await BaseFileManager.readFileAsString(schemaFilePath);
|
|
325
|
-
const schemaFile = JSON.parse(content);
|
|
326
|
-
if (!schemaFile.$schema) {
|
|
327
|
-
throw new Error(`File ${schemaFilePath} is not a complete schema file (missing $schema field).`);
|
|
328
|
-
}
|
|
329
|
-
const schemaName = schemaFile.metadata?.name;
|
|
330
|
-
const version = schemaFile.version;
|
|
331
|
-
if (!schemaName) {
|
|
332
|
-
throw new Error('Schema name is required in metadata.name');
|
|
333
|
-
}
|
|
334
|
-
// Convert to JsonImportSchema format for processing
|
|
335
|
-
// Remove id fields for JsonImportSchema format (they're not part of the import format)
|
|
336
|
-
const importData = {
|
|
337
|
-
name: schemaName,
|
|
338
|
-
models: Object.fromEntries(Object.entries(schemaFile.models).map(([modelName, model]) => [
|
|
339
|
-
modelName,
|
|
340
|
-
{
|
|
341
|
-
description: model.description,
|
|
342
|
-
properties: Object.fromEntries(Object.entries(model.properties).map(([propName, prop]) => [
|
|
343
|
-
propName,
|
|
344
|
-
{
|
|
345
|
-
type: prop.type || 'string', // Ensure type is present
|
|
346
|
-
...prop,
|
|
347
|
-
// Remove id field for import format (not part of JsonImportSchema)
|
|
348
|
-
},
|
|
349
|
-
])),
|
|
350
|
-
indexes: model.indexes,
|
|
351
|
-
},
|
|
352
|
-
])),
|
|
353
|
-
};
|
|
354
|
-
// Generate schema ID if missing
|
|
355
|
-
if (!schemaFile.id) {
|
|
356
|
-
const { generateId } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aS; });
|
|
357
|
-
schemaFile.id = generateId();
|
|
358
|
-
logger$4('Generated schema ID for schema:', schemaFile.id);
|
|
359
|
-
}
|
|
360
|
-
// Extract schemaFileIds from JSON file and generate missing ones BEFORE creating models
|
|
361
|
-
// This ensures Model instances are created with correct IDs
|
|
362
|
-
const modelFileIds = new Map();
|
|
363
|
-
const propertyFileIds = new Map();
|
|
364
|
-
const { generateId } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aS; });
|
|
365
|
-
for (const [modelName, model] of Object.entries(schemaFile.models)) {
|
|
366
|
-
// Generate model ID if missing
|
|
367
|
-
if (!model.id) {
|
|
368
|
-
model.id = generateId();
|
|
369
|
-
logger$4(`Generated model ID for ${modelName}:`, model.id);
|
|
370
|
-
}
|
|
371
|
-
modelFileIds.set(modelName, model.id);
|
|
372
|
-
const propIds = new Map();
|
|
373
|
-
for (const [propName, prop] of Object.entries(model.properties)) {
|
|
374
|
-
// Generate property ID if missing
|
|
375
|
-
if (!prop.id) {
|
|
376
|
-
prop.id = generateId();
|
|
377
|
-
logger$4(`Generated property ID for ${modelName}.${propName}:`, prop.id);
|
|
378
|
-
}
|
|
379
|
-
propIds.set(propName, prop.id);
|
|
380
|
-
}
|
|
381
|
-
if (propIds.size > 0) {
|
|
382
|
-
propertyFileIds.set(modelName, propIds);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
// Convert JSON models to Model classes, passing modelFileIds and propertyFileIds so Model instances use correct IDs
|
|
386
|
-
const modelDefinitions = await createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
387
|
-
// Convert schema file metadata to schema input for database
|
|
388
|
-
const schemaInput = {
|
|
389
|
-
name: schemaName,
|
|
390
|
-
version,
|
|
391
|
-
schemaFileId: schemaFile.id || null,
|
|
392
|
-
schemaData: null,
|
|
393
|
-
isDraft: false,
|
|
394
|
-
isEdited: false,
|
|
395
|
-
createdAt: new Date(schemaFile.metadata.createdAt).getTime(),
|
|
396
|
-
updatedAt: new Date(schemaFile.metadata.updatedAt).getTime(),
|
|
397
|
-
};
|
|
398
|
-
// Use dynamic import to break circular dependency
|
|
399
|
-
const { addSchemaToDb, addModelsToDb } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aW; });
|
|
400
|
-
// Add schema to database with schemaFileId
|
|
401
|
-
const schemaRecord = await addSchemaToDb(schemaInput, schemaFile.id);
|
|
402
|
-
// Add models to database with model renames handled and schemaFileIds
|
|
403
|
-
await addModelsToDb(modelDefinitions, schemaRecord, modelRenames, {
|
|
404
|
-
schemaFileId: schemaFile.id,
|
|
405
|
-
modelFileIds,
|
|
406
|
-
propertyFileIds,
|
|
407
|
-
});
|
|
408
|
-
// Models are now Model instances, no registration needed
|
|
409
|
-
// They should be created via Model.create() and are accessible via Model static methods
|
|
410
|
-
for (const [modelName] of Object.entries(modelDefinitions)) {
|
|
411
|
-
logger$4('loadSchemaWithRenames - model available:', modelName);
|
|
412
|
-
}
|
|
413
|
-
return schemaFilePath;
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
* Rename a property in a model
|
|
417
|
-
* This is a convenience function that updates the property name
|
|
418
|
-
* Note: This creates a new property and you may want to handle the old property separately
|
|
419
|
-
* @param schemaName - The name of the schema
|
|
420
|
-
* @param modelName - The name of the model
|
|
421
|
-
* @param oldPropertyName - The current property name
|
|
422
|
-
* @param newPropertyName - The new property name
|
|
423
|
-
* @returns The file path of the new schema version
|
|
424
|
-
*/
|
|
425
|
-
async function renameModelProperty(schemaName, modelName, oldPropertyName, newPropertyName) {
|
|
426
|
-
// Get the latest version
|
|
427
|
-
const latestVersion = await getLatestSchemaVersion(schemaName);
|
|
428
|
-
if (latestVersion === 0) {
|
|
429
|
-
throw new Error(`Schema ${schemaName} not found`);
|
|
430
|
-
}
|
|
431
|
-
// Get schemaFileId from database
|
|
432
|
-
const schemaFileId = await getSchemaFileId(schemaName);
|
|
433
|
-
// Load the latest schema file
|
|
434
|
-
const latestFilePath = getSchemaFilePath(schemaName, latestVersion, schemaFileId);
|
|
435
|
-
const content = await BaseFileManager.readFileAsString(latestFilePath);
|
|
436
|
-
const schemaFile = JSON.parse(content);
|
|
437
|
-
if (!schemaFile.$schema) {
|
|
438
|
-
throw new Error(`Schema file ${latestFilePath} is not a complete schema file`);
|
|
439
|
-
}
|
|
440
|
-
const model = schemaFile.models[modelName];
|
|
441
|
-
if (!model) {
|
|
442
|
-
throw new Error(`Model ${modelName} not found in schema ${schemaName}`);
|
|
443
|
-
}
|
|
444
|
-
if (!model.properties[oldPropertyName]) {
|
|
445
|
-
throw new Error(`Property ${oldPropertyName} not found in model ${modelName} of schema ${schemaName}`);
|
|
446
|
-
}
|
|
447
|
-
if (model.properties[newPropertyName]) {
|
|
448
|
-
throw new Error(`Property ${newPropertyName} already exists in model ${modelName} of schema ${schemaName}`);
|
|
449
|
-
}
|
|
450
|
-
// Create new version with renamed property
|
|
451
|
-
// Preserve all IDs - when renaming, we keep the same property ID
|
|
452
|
-
const newVersion = latestVersion + 1;
|
|
453
|
-
const oldProperty = model.properties[oldPropertyName];
|
|
454
|
-
const updatedSchema = {
|
|
455
|
-
...schemaFile,
|
|
456
|
-
version: newVersion,
|
|
457
|
-
id: schemaFile.id, // Preserve schema ID
|
|
458
|
-
metadata: {
|
|
459
|
-
...schemaFile.metadata,
|
|
460
|
-
updatedAt: new Date().toISOString(),
|
|
461
|
-
},
|
|
462
|
-
models: {
|
|
463
|
-
...schemaFile.models,
|
|
464
|
-
[modelName]: {
|
|
465
|
-
...model,
|
|
466
|
-
id: model.id, // Preserve model ID
|
|
467
|
-
properties: {
|
|
468
|
-
...Object.fromEntries(Object.entries(model.properties)
|
|
469
|
-
.filter(([name]) => name !== oldPropertyName)
|
|
470
|
-
.map(([name, prop]) => [name, { ...prop, id: prop.id }])),
|
|
471
|
-
[newPropertyName]: {
|
|
472
|
-
...oldProperty,
|
|
473
|
-
id: oldProperty.id, // Preserve property ID when renaming
|
|
474
|
-
},
|
|
475
|
-
},
|
|
476
|
-
},
|
|
477
|
-
},
|
|
478
|
-
migrations: [
|
|
479
|
-
...schemaFile.migrations,
|
|
480
|
-
{
|
|
481
|
-
version: newVersion,
|
|
482
|
-
timestamp: new Date().toISOString(),
|
|
483
|
-
description: `Renamed property ${modelName}.${oldPropertyName} to ${newPropertyName}`,
|
|
484
|
-
changes: [
|
|
485
|
-
{
|
|
486
|
-
type: 'property_rename',
|
|
487
|
-
modelName,
|
|
488
|
-
oldPropertyName,
|
|
489
|
-
newPropertyName,
|
|
490
|
-
},
|
|
491
|
-
],
|
|
492
|
-
},
|
|
493
|
-
],
|
|
494
|
-
};
|
|
495
|
-
// Remove the old property name
|
|
496
|
-
delete updatedSchema.models[modelName].properties[oldPropertyName];
|
|
497
|
-
// Write the new schema version using ID-based naming (preferred)
|
|
498
|
-
const newFilePath = getSchemaFilePath(schemaName, newVersion, updatedSchema.id ?? undefined);
|
|
499
|
-
const newContent = JSON.stringify(updatedSchema, null, 2);
|
|
500
|
-
// Ensure the directory exists before saving
|
|
501
|
-
const path = BaseFileManager.getPathModule();
|
|
502
|
-
const dir = path.dirname(newFilePath);
|
|
503
|
-
await BaseFileManager.createDirIfNotExists(dir);
|
|
504
|
-
await BaseFileManager.saveFile(newFilePath, newContent);
|
|
505
|
-
// Wait for the file to be available with content (important for browser/OPFS where writes may not be immediately readable)
|
|
506
|
-
await BaseFileManager.waitForFileWithContent(newFilePath);
|
|
507
|
-
logger$4(`Renamed property ${oldPropertyName} to ${newPropertyName} in schema ${schemaName} v${newVersion}`);
|
|
508
|
-
// Load the new schema file
|
|
509
|
-
await loadSchemaFromFile(newFilePath);
|
|
510
|
-
return newFilePath;
|
|
511
|
-
}
|
|
512
|
-
/**
|
|
513
|
-
* Delete a model from a schema
|
|
514
|
-
* @param schemaName - The name of the schema
|
|
515
|
-
* @param modelName - The name of the model to delete
|
|
516
|
-
* @param options - Optional deletion options
|
|
517
|
-
* @returns The file path of the new schema version
|
|
518
|
-
* @throws Error if schema or model not found
|
|
519
|
-
*/
|
|
520
|
-
async function deleteModelFromSchema(schemaName, modelName, options = {}) {
|
|
521
|
-
const { removeReferencingProperties = false } = options;
|
|
522
|
-
// Get the latest version
|
|
523
|
-
const latestVersion = await getLatestSchemaVersion(schemaName);
|
|
524
|
-
if (latestVersion === 0) {
|
|
525
|
-
throw new Error(`Schema ${schemaName} not found`);
|
|
526
|
-
}
|
|
527
|
-
// Get schemaFileId from database
|
|
528
|
-
const schemaFileId = await getSchemaFileId(schemaName);
|
|
529
|
-
// Load the latest schema file
|
|
530
|
-
const latestFilePath = getSchemaFilePath(schemaName, latestVersion, schemaFileId);
|
|
531
|
-
const content = await BaseFileManager.readFileAsString(latestFilePath);
|
|
532
|
-
const schemaFile = JSON.parse(content);
|
|
533
|
-
if (!schemaFile.$schema) {
|
|
534
|
-
throw new Error(`Schema file ${latestFilePath} is not a complete schema file`);
|
|
535
|
-
}
|
|
536
|
-
if (!schemaFile.models[modelName]) {
|
|
537
|
-
throw new Error(`Model ${modelName} not found in schema ${schemaName}`);
|
|
538
|
-
}
|
|
539
|
-
// Create new version without the model
|
|
540
|
-
// Preserve IDs for remaining models and properties
|
|
541
|
-
const newVersion = latestVersion + 1;
|
|
542
|
-
const updatedSchema = {
|
|
543
|
-
...schemaFile,
|
|
544
|
-
version: newVersion,
|
|
545
|
-
id: schemaFile.id, // Preserve schema ID
|
|
546
|
-
metadata: {
|
|
547
|
-
...schemaFile.metadata,
|
|
548
|
-
updatedAt: new Date().toISOString(),
|
|
549
|
-
},
|
|
550
|
-
// Preserve IDs for remaining models and their properties
|
|
551
|
-
models: Object.fromEntries(Object.entries(schemaFile.models)
|
|
552
|
-
.filter(([name]) => name !== modelName)
|
|
553
|
-
.map(([name, model]) => [
|
|
554
|
-
name,
|
|
555
|
-
{
|
|
556
|
-
...model,
|
|
557
|
-
id: model.id, // Preserve model ID
|
|
558
|
-
properties: Object.fromEntries(Object.entries(model.properties).map(([propName, prop]) => [
|
|
559
|
-
propName,
|
|
560
|
-
{ ...prop, id: prop.id }, // Preserve property ID
|
|
561
|
-
])),
|
|
562
|
-
},
|
|
563
|
-
])),
|
|
564
|
-
migrations: [
|
|
565
|
-
...schemaFile.migrations,
|
|
566
|
-
{
|
|
567
|
-
version: newVersion,
|
|
568
|
-
timestamp: new Date().toISOString(),
|
|
569
|
-
description: `Deleted model ${modelName} from schema`,
|
|
570
|
-
changes: [
|
|
571
|
-
{
|
|
572
|
-
type: 'model_delete',
|
|
573
|
-
modelName,
|
|
574
|
-
removeReferencingProperties,
|
|
575
|
-
},
|
|
576
|
-
],
|
|
577
|
-
},
|
|
578
|
-
],
|
|
579
|
-
};
|
|
580
|
-
// Remove the model
|
|
581
|
-
delete updatedSchema.models[modelName];
|
|
582
|
-
// Handle properties that reference this model
|
|
583
|
-
const propertiesToRemove = [];
|
|
584
|
-
for (const [otherModelName, model] of Object.entries(updatedSchema.models)) {
|
|
585
|
-
for (const [propertyName, property] of Object.entries(model.properties)) {
|
|
586
|
-
// Check if property references the deleted model
|
|
587
|
-
if (property.model === modelName || property.items?.model === modelName) {
|
|
588
|
-
if (removeReferencingProperties) {
|
|
589
|
-
// Mark for removal
|
|
590
|
-
propertiesToRemove.push({ modelName: otherModelName, propertyName });
|
|
591
|
-
}
|
|
592
|
-
else {
|
|
593
|
-
// Remove the reference (set to null or remove model field)
|
|
594
|
-
if (property.model === modelName) {
|
|
595
|
-
delete property.model;
|
|
596
|
-
}
|
|
597
|
-
if (property.items?.model === modelName) {
|
|
598
|
-
delete property.items.model;
|
|
599
|
-
// If items only had model, we might want to remove items entirely
|
|
600
|
-
// But for now, just remove the model reference
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
// Remove properties if requested
|
|
607
|
-
if (removeReferencingProperties) {
|
|
608
|
-
for (const { modelName: mName, propertyName } of propertiesToRemove) {
|
|
609
|
-
delete updatedSchema.models[mName].properties[propertyName];
|
|
610
|
-
updatedSchema.migrations[updatedSchema.migrations.length - 1].changes.push({
|
|
611
|
-
type: 'property_delete',
|
|
612
|
-
modelName: mName,
|
|
613
|
-
propertyName,
|
|
614
|
-
reason: `Referenced deleted model ${modelName}`,
|
|
615
|
-
});
|
|
616
|
-
}
|
|
617
|
-
}
|
|
618
|
-
else {
|
|
619
|
-
// Update migration to note which properties were updated
|
|
620
|
-
const updatedProperties = [];
|
|
621
|
-
for (const [otherModelName, model] of Object.entries(updatedSchema.models)) {
|
|
622
|
-
for (const [propertyName, property] of Object.entries(model.properties)) {
|
|
623
|
-
if (property.model === modelName || property.items?.model === modelName) {
|
|
624
|
-
updatedProperties.push({ modelName: otherModelName, propertyName });
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
}
|
|
628
|
-
if (updatedProperties.length > 0) {
|
|
629
|
-
updatedSchema.migrations[updatedSchema.migrations.length - 1].changes.push({
|
|
630
|
-
type: 'properties_updated',
|
|
631
|
-
properties: updatedProperties,
|
|
632
|
-
reason: `Removed references to deleted model ${modelName}`,
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
// Write the new schema version using ID-based naming (preferred)
|
|
637
|
-
const newFilePath = getSchemaFilePath(schemaName, newVersion, updatedSchema.id ?? undefined);
|
|
638
|
-
const newContent = JSON.stringify(updatedSchema, null, 2);
|
|
639
|
-
// Ensure the directory exists before saving
|
|
640
|
-
const path = BaseFileManager.getPathModule();
|
|
641
|
-
const dir = path.dirname(newFilePath);
|
|
642
|
-
await BaseFileManager.createDirIfNotExists(dir);
|
|
643
|
-
await BaseFileManager.saveFile(newFilePath, newContent);
|
|
644
|
-
// Wait for the file to be available with content (important for browser/OPFS where writes may not be immediately readable)
|
|
645
|
-
await BaseFileManager.waitForFileWithContent(newFilePath);
|
|
646
|
-
logger$4(`Deleted model ${modelName} from schema ${schemaName} v${newVersion}`);
|
|
647
|
-
// Load the new schema file
|
|
648
|
-
await loadSchemaFromFile(newFilePath);
|
|
649
|
-
return newFilePath;
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* Delete a property from a model in a schema
|
|
653
|
-
* @param schemaName - The name of the schema
|
|
654
|
-
* @param modelName - The name of the model
|
|
655
|
-
* @param propertyName - The name of the property to delete
|
|
656
|
-
* @param options - Optional deletion options
|
|
657
|
-
* @returns The file path of the new schema version
|
|
658
|
-
* @throws Error if schema, model, or property not found
|
|
659
|
-
*/
|
|
660
|
-
async function deletePropertyFromModel(schemaName, modelName, propertyName, options = {}) {
|
|
661
|
-
// Get the latest version
|
|
662
|
-
const latestVersion = await getLatestSchemaVersion(schemaName);
|
|
663
|
-
if (latestVersion === 0) {
|
|
664
|
-
throw new Error(`Schema ${schemaName} not found`);
|
|
665
|
-
}
|
|
666
|
-
// Get schemaFileId from database
|
|
667
|
-
const schemaFileId = await getSchemaFileId(schemaName);
|
|
668
|
-
// Load the latest schema file
|
|
669
|
-
const latestFilePath = getSchemaFilePath(schemaName, latestVersion, schemaFileId);
|
|
670
|
-
const content = await BaseFileManager.readFileAsString(latestFilePath);
|
|
671
|
-
const schemaFile = JSON.parse(content);
|
|
672
|
-
if (!schemaFile.$schema) {
|
|
673
|
-
throw new Error(`Schema file ${latestFilePath} is not a complete schema file`);
|
|
674
|
-
}
|
|
675
|
-
const model = schemaFile.models[modelName];
|
|
676
|
-
if (!model) {
|
|
677
|
-
throw new Error(`Model ${modelName} not found in schema ${schemaName}`);
|
|
678
|
-
}
|
|
679
|
-
if (!model.properties[propertyName]) {
|
|
680
|
-
throw new Error(`Property ${propertyName} not found in model ${modelName} of schema ${schemaName}`);
|
|
681
|
-
}
|
|
682
|
-
// Create new version without the property
|
|
683
|
-
// Preserve IDs for schema, models, and remaining properties
|
|
684
|
-
const newVersion = latestVersion + 1;
|
|
685
|
-
const updatedSchema = {
|
|
686
|
-
...schemaFile,
|
|
687
|
-
version: newVersion,
|
|
688
|
-
id: schemaFile.id, // Preserve schema ID
|
|
689
|
-
metadata: {
|
|
690
|
-
...schemaFile.metadata,
|
|
691
|
-
updatedAt: new Date().toISOString(),
|
|
692
|
-
},
|
|
693
|
-
models: Object.fromEntries(Object.entries(schemaFile.models).map(([mName, m]) => [
|
|
694
|
-
mName,
|
|
695
|
-
{
|
|
696
|
-
...m,
|
|
697
|
-
id: m.id, // Preserve model ID
|
|
698
|
-
properties: Object.fromEntries(Object.entries(m.properties)
|
|
699
|
-
.filter(([propName]) => !(mName === modelName && propName === propertyName))
|
|
700
|
-
.map(([propName, prop]) => [
|
|
701
|
-
propName,
|
|
702
|
-
{ ...prop, id: prop.id }, // Preserve property ID
|
|
703
|
-
])),
|
|
704
|
-
},
|
|
705
|
-
])),
|
|
706
|
-
migrations: [
|
|
707
|
-
...schemaFile.migrations,
|
|
708
|
-
{
|
|
709
|
-
version: newVersion,
|
|
710
|
-
timestamp: new Date().toISOString(),
|
|
711
|
-
description: `Deleted property ${modelName}.${propertyName} from schema`,
|
|
712
|
-
changes: [
|
|
713
|
-
{
|
|
714
|
-
type: 'property_delete',
|
|
715
|
-
modelName,
|
|
716
|
-
propertyName,
|
|
717
|
-
},
|
|
718
|
-
],
|
|
719
|
-
},
|
|
720
|
-
],
|
|
721
|
-
};
|
|
722
|
-
// Write the new schema version using ID-based naming (preferred)
|
|
723
|
-
const newFilePath = getSchemaFilePath(schemaName, newVersion, updatedSchema.id ?? undefined);
|
|
724
|
-
const newContent = JSON.stringify(updatedSchema, null, 2);
|
|
725
|
-
// Ensure the directory exists before saving
|
|
726
|
-
const path = BaseFileManager.getPathModule();
|
|
727
|
-
const dir = path.dirname(newFilePath);
|
|
728
|
-
await BaseFileManager.createDirIfNotExists(dir);
|
|
729
|
-
await BaseFileManager.saveFile(newFilePath, newContent);
|
|
730
|
-
// Wait for the file to be available with content (important for browser/OPFS where writes may not be immediately readable)
|
|
731
|
-
await BaseFileManager.waitForFileWithContent(newFilePath);
|
|
732
|
-
logger$4(`Deleted property ${propertyName} from model ${modelName} in schema ${schemaName} v${newVersion}`);
|
|
733
|
-
// Load the new schema file
|
|
734
|
-
await loadSchemaFromFile(newFilePath);
|
|
735
|
-
return newFilePath;
|
|
736
|
-
}
|
|
5
|
+
import { eq, and } from 'drizzle-orm';
|
|
6
|
+
import './SchemaValidationService-F8No7BHp.js';
|
|
7
|
+
import '@sinclair/typebox/value';
|
|
8
|
+
import 'reflect-metadata';
|
|
9
|
+
import 'pluralize';
|
|
10
|
+
import '@sinclair/typebox';
|
|
11
|
+
import 'drizzle-orm/sqlite-core';
|
|
12
|
+
import 'nanoid';
|
|
13
|
+
import 'nanoid-dictionary';
|
|
14
|
+
import 'ethers';
|
|
15
|
+
import 'rxjs';
|
|
16
|
+
import 'lodash-es';
|
|
17
|
+
import 'drizzle-orm/casing';
|
|
18
|
+
import 'eventemitter3';
|
|
19
|
+
import 'arweave';
|
|
737
20
|
|
|
738
21
|
const logger$3 = debug('seedSdk:modelProperty:actors:saveToSchema');
|
|
739
22
|
/**
|
|
@@ -743,11 +26,17 @@ const logger$3 = debug('seedSdk:modelProperty:actors:saveToSchema');
|
|
|
743
26
|
*/
|
|
744
27
|
async function getSchemaNameFromModel(modelName) {
|
|
745
28
|
// Get the latest schema files and find which one contains this model
|
|
746
|
-
const
|
|
29
|
+
const _mod_0 = await import('./index-Dk73a7JE.js');
|
|
30
|
+
const _ns_0 = _mod_0.aU;
|
|
31
|
+
const schemaHelpersMod = _ns_0;
|
|
32
|
+
const { listLatestSchemaFiles } = schemaHelpersMod;
|
|
747
33
|
const latestSchemas = await listLatestSchemaFiles();
|
|
748
34
|
for (const schema of latestSchemas) {
|
|
749
35
|
try {
|
|
750
|
-
const
|
|
36
|
+
const _mod_1 = await import('./index-Dk73a7JE.js');
|
|
37
|
+
const _ns_1 = _mod_1.aS;
|
|
38
|
+
const fileManagerMod = _ns_1;
|
|
39
|
+
const { BaseFileManager } = fileManagerMod;
|
|
751
40
|
const content = await BaseFileManager.readFileAsString(schema.filePath);
|
|
752
41
|
const schemaFile = JSON.parse(content);
|
|
753
42
|
if (schemaFile.models && schemaFile.models[modelName]) {
|
|
@@ -764,7 +53,8 @@ async function getSchemaNameFromModel(modelName) {
|
|
|
764
53
|
const saveToSchema = fromCallback(({ sendBack, input: { context } }) => {
|
|
765
54
|
const _saveToSchema = async () => {
|
|
766
55
|
// Use dynamic import to break circular dependency
|
|
767
|
-
const
|
|
56
|
+
const validationServiceMod = await import('./SchemaValidationService-F8No7BHp.js');
|
|
57
|
+
const { SchemaValidationService } = validationServiceMod;
|
|
768
58
|
const validationService = new SchemaValidationService();
|
|
769
59
|
// Validate property structure before saving
|
|
770
60
|
const validationResult = validationService.validatePropertyStructure(context);
|
|
@@ -792,9 +82,12 @@ const saveToSchema = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
792
82
|
await updateModelProperties(schemaName, [propertyUpdate]);
|
|
793
83
|
// Clear isEdited flag in database after saving to schema file
|
|
794
84
|
try {
|
|
795
|
-
const
|
|
796
|
-
const
|
|
797
|
-
const
|
|
85
|
+
const _mod_2 = await import('./index-DBy53qbb.js');
|
|
86
|
+
const _ns_2 = _mod_2.s;
|
|
87
|
+
const seedSchemaMod = _ns_2;
|
|
88
|
+
const { properties: propertiesTable, models: modelsTable } = seedSchemaMod;
|
|
89
|
+
const drizzleMod = await import('drizzle-orm');
|
|
90
|
+
const { eq, and } = drizzleMod;
|
|
798
91
|
const db = BaseDb.getAppDb();
|
|
799
92
|
if (db && context.modelName && context.name) {
|
|
800
93
|
// Find model by name
|
|
@@ -837,12 +130,6 @@ const saveToSchema = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
837
130
|
};
|
|
838
131
|
});
|
|
839
132
|
|
|
840
|
-
var saveToSchema$1 = /*#__PURE__*/Object.freeze({
|
|
841
|
-
__proto__: null,
|
|
842
|
-
getSchemaNameFromModel: getSchemaNameFromModel,
|
|
843
|
-
saveToSchema: saveToSchema
|
|
844
|
-
});
|
|
845
|
-
|
|
846
133
|
const logger$2 = debug('seedSdk:modelProperty:actors:compareAndMarkDraft');
|
|
847
134
|
const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
848
135
|
const _compareAndMarkDraft = async () => {
|
|
@@ -855,7 +142,10 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
855
142
|
const schemaFileIdForResolve = fullContext._propertyFileId || (typeof fullContext.id === 'string' ? fullContext.id : undefined);
|
|
856
143
|
if (schemaFileIdForResolve && (fullContext.modelName === undefined || fullContext.dataType === undefined)) {
|
|
857
144
|
try {
|
|
858
|
-
const
|
|
145
|
+
const _mod_3 = await import('./index-Dk73a7JE.js');
|
|
146
|
+
const _ns_3 = _mod_3.aT;
|
|
147
|
+
const dbMod = _ns_3;
|
|
148
|
+
const { getPropertyModelNameAndDataType, getModelNameByModelId } = dbMod;
|
|
859
149
|
let fromDb;
|
|
860
150
|
for (let attempt = 0; attempt < 6; attempt++) {
|
|
861
151
|
fromDb = await getPropertyModelNameAndDataType(schemaFileIdForResolve);
|
|
@@ -899,7 +189,10 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
899
189
|
if (fullContext.modelName && fullContext.name && schemaFileId) {
|
|
900
190
|
logger$2(`[compareAndMarkDraft] _originalValues not set, but saving to database anyway for property ${context.modelName}:${context.name} (schemaFileId: ${schemaFileId})`);
|
|
901
191
|
try {
|
|
902
|
-
const
|
|
192
|
+
const _mod_4 = await import('./index-Dk73a7JE.js');
|
|
193
|
+
const _ns_4 = _mod_4.aT;
|
|
194
|
+
const dbMod = _ns_4;
|
|
195
|
+
const { savePropertyToDb } = dbMod;
|
|
903
196
|
// Ensure _propertyFileId is set for savePropertyToDb to find the property
|
|
904
197
|
const contextWithFileId = {
|
|
905
198
|
...fullContext,
|
|
@@ -958,7 +251,10 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
958
251
|
logger$2(`Property ${fullContext.modelName}:${fullContext.name} has changes, marking as edited`);
|
|
959
252
|
logger$2(`[compareAndMarkDraft] Context when saving: id=${fullContext.id}, _propertyFileId=${fullContext._propertyFileId}, name=${fullContext.name}, _originalValues.name=${fullContext._originalValues?.name}`);
|
|
960
253
|
// Use dynamic import to break circular dependency
|
|
961
|
-
const
|
|
254
|
+
const _mod_5 = await import('./index-Dk73a7JE.js');
|
|
255
|
+
const _ns_5 = _mod_5.aT;
|
|
256
|
+
const dbMod = _ns_5;
|
|
257
|
+
const { savePropertyToDb } = dbMod;
|
|
962
258
|
// Save to database (but not JSON file) - always save to DB when there are changes
|
|
963
259
|
try {
|
|
964
260
|
await savePropertyToDb(fullContext);
|
|
@@ -971,7 +267,10 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
971
267
|
// Mark schema as draft if schema name is available
|
|
972
268
|
if (fullContext._schemaName) {
|
|
973
269
|
// Get the Schema instance and mark it as draft
|
|
974
|
-
const
|
|
270
|
+
const _mod_6 = await import('./Schema-BZKBy5Tx.js');
|
|
271
|
+
const _ns_6 = _mod_6.b;
|
|
272
|
+
const schemaMod = _ns_6;
|
|
273
|
+
const { Schema } = schemaMod;
|
|
975
274
|
const schema = Schema.create(fullContext._schemaName, {
|
|
976
275
|
waitForReady: false,
|
|
977
276
|
});
|
|
@@ -987,9 +286,12 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
987
286
|
logger$2(`Property ${fullContext.modelName}:${fullContext.name} has no changes`);
|
|
988
287
|
// Clear isEdited flag in database
|
|
989
288
|
try {
|
|
990
|
-
const
|
|
991
|
-
const
|
|
992
|
-
const
|
|
289
|
+
const _mod_7 = await import('./index-DBy53qbb.js');
|
|
290
|
+
const _ns_7 = _mod_7.s;
|
|
291
|
+
const seedSchemaMod = _ns_7;
|
|
292
|
+
const { properties: propertiesTable, models: modelsTable } = seedSchemaMod;
|
|
293
|
+
const drizzleMod = await import('drizzle-orm');
|
|
294
|
+
const { eq, and } = drizzleMod;
|
|
993
295
|
const db = BaseDb.getAppDb();
|
|
994
296
|
if (db && fullContext.modelName && fullContext.name) {
|
|
995
297
|
// Find model by name
|
|
@@ -1050,7 +352,10 @@ const validateProperty = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
1050
352
|
// Brief wait so trackPendingWrite from ModelProperty.create() has time to run (it's in setTimeout(0))
|
|
1051
353
|
await new Promise((r) => setTimeout(r, 60));
|
|
1052
354
|
try {
|
|
1053
|
-
const
|
|
355
|
+
const _mod_8 = await import('./index-Dk73a7JE.js');
|
|
356
|
+
const _ns_8 = _mod_8.aT;
|
|
357
|
+
const dbMod = _ns_8;
|
|
358
|
+
const { getPropertyModelNameAndDataType, getModelNameByModelId } = dbMod;
|
|
1054
359
|
// Try pending write first (property row may not exist yet)
|
|
1055
360
|
if (fullContext.modelName === undefined && schemaFileId) {
|
|
1056
361
|
const mod = await Promise.resolve().then(function () { return ModelProperty$1; });
|
|
@@ -1108,9 +413,13 @@ const validateProperty = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
1108
413
|
_originalValues: fullContext._originalValues
|
|
1109
414
|
});
|
|
1110
415
|
// Use dynamic imports to break circular dependencies
|
|
1111
|
-
const
|
|
416
|
+
const validationServiceMod = await import('./SchemaValidationService-F8No7BHp.js');
|
|
417
|
+
const { SchemaValidationService } = validationServiceMod;
|
|
1112
418
|
const validationService = new SchemaValidationService();
|
|
1113
|
-
const
|
|
419
|
+
const _mod_9 = await import('./Schema-BZKBy5Tx.js');
|
|
420
|
+
const _ns_9 = _mod_9.b;
|
|
421
|
+
const schemaMod = _ns_9;
|
|
422
|
+
const { Schema } = schemaMod;
|
|
1114
423
|
// Validate property structure
|
|
1115
424
|
console.log('[validateProperty] Validating property structure...');
|
|
1116
425
|
const structureResult = validationService.validatePropertyStructure(fullContext);
|
|
@@ -1708,7 +1017,6 @@ class ModelProperty {
|
|
|
1708
1017
|
return fallbackIsEdited;
|
|
1709
1018
|
}
|
|
1710
1019
|
try {
|
|
1711
|
-
const { eq, and } = await import('drizzle-orm');
|
|
1712
1020
|
const db = BaseDb.getAppDb();
|
|
1713
1021
|
if (!db) {
|
|
1714
1022
|
return fallbackIsEdited;
|
|
@@ -1747,7 +1055,6 @@ class ModelProperty {
|
|
|
1747
1055
|
return undefined;
|
|
1748
1056
|
}
|
|
1749
1057
|
try {
|
|
1750
|
-
const { eq } = await import('drizzle-orm');
|
|
1751
1058
|
const db = BaseDb.getAppDb();
|
|
1752
1059
|
if (!db) {
|
|
1753
1060
|
return undefined;
|
|
@@ -1776,8 +1083,6 @@ class ModelProperty {
|
|
|
1776
1083
|
return undefined;
|
|
1777
1084
|
}
|
|
1778
1085
|
try {
|
|
1779
|
-
const { Model } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aX; });
|
|
1780
|
-
const { modelPropertiesToObject } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aV; });
|
|
1781
1086
|
const model = await Model.getByNameAsync(property.modelName);
|
|
1782
1087
|
if (!model || !model.properties || model.properties.length === 0) {
|
|
1783
1088
|
return undefined;
|
|
@@ -1826,7 +1131,6 @@ class ModelProperty {
|
|
|
1826
1131
|
// Try to get schema name from database first (more reliable)
|
|
1827
1132
|
if (this.modelId) {
|
|
1828
1133
|
try {
|
|
1829
|
-
const { eq } = await import('drizzle-orm');
|
|
1830
1134
|
const db = BaseDb.getAppDb();
|
|
1831
1135
|
if (db) {
|
|
1832
1136
|
const modelSchemaRecords = await db
|
|
@@ -1849,7 +1153,6 @@ class ModelProperty {
|
|
|
1849
1153
|
}
|
|
1850
1154
|
// Fall back to schema file lookup if database didn't work
|
|
1851
1155
|
if (!schemaName) {
|
|
1852
|
-
const { getSchemaNameFromModel } = await Promise.resolve().then(function () { return saveToSchema$1; });
|
|
1853
1156
|
schemaName = await getSchemaNameFromModel(modelName);
|
|
1854
1157
|
}
|
|
1855
1158
|
if (schemaName) {
|
|
@@ -2030,7 +1333,6 @@ class ModelProperty {
|
|
|
2030
1333
|
else if (typeof propertyWithId.modelId === 'string') {
|
|
2031
1334
|
// modelId is a string (modelFileId), need to convert to database ID
|
|
2032
1335
|
try {
|
|
2033
|
-
const { getModelIdByFileId } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aW; });
|
|
2034
1336
|
resolvedModelId = await getModelIdByFileId(propertyWithId.modelId);
|
|
2035
1337
|
logger(`Converted modelFileId "${propertyWithId.modelId}" to database modelId: ${resolvedModelId}`);
|
|
2036
1338
|
}
|
|
@@ -2043,7 +1345,6 @@ class ModelProperty {
|
|
|
2043
1345
|
// If we still don't have a modelId, try to resolve it from modelName
|
|
2044
1346
|
if (!resolvedModelId && propertyWithId.modelName) {
|
|
2045
1347
|
try {
|
|
2046
|
-
const { getModelId } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aW; });
|
|
2047
1348
|
// Get schemaName from context if available
|
|
2048
1349
|
const schemaName = snapshot.context._schemaName;
|
|
2049
1350
|
resolvedModelId = await getModelId(propertyWithId.modelName, schemaName);
|
|
@@ -2152,7 +1453,6 @@ class ModelProperty {
|
|
|
2152
1453
|
return cachedInstance;
|
|
2153
1454
|
}
|
|
2154
1455
|
// Query database to get property data from ID
|
|
2155
|
-
const { eq } = await import('drizzle-orm');
|
|
2156
1456
|
const db = BaseDb.getAppDb();
|
|
2157
1457
|
console.log('db', !!db);
|
|
2158
1458
|
if (!db) {
|
|
@@ -2455,7 +1755,6 @@ class ModelProperty {
|
|
|
2455
1755
|
}
|
|
2456
1756
|
}
|
|
2457
1757
|
if (schemaName) {
|
|
2458
|
-
const { Schema } = await import('./Schema-ClTcpgbt.js');
|
|
2459
1758
|
const schema = Schema.create(schemaName, { waitForReady: false });
|
|
2460
1759
|
const snapshot = schema.getService().getSnapshot();
|
|
2461
1760
|
const schemaContext = snapshot.context;
|
|
@@ -2481,5 +1780,5 @@ var ModelProperty$1 = /*#__PURE__*/Object.freeze({
|
|
|
2481
1780
|
ModelProperty: ModelProperty
|
|
2482
1781
|
});
|
|
2483
1782
|
|
|
2484
|
-
export { ModelProperty
|
|
2485
|
-
//# sourceMappingURL=ModelProperty-
|
|
1783
|
+
export { ModelProperty };
|
|
1784
|
+
//# sourceMappingURL=ModelProperty-Cpd9xiIM.js.map
|