@seedprotocol/sdk 0.4.4 → 0.4.5
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-CgWK-JgT.js} +8 -8
- package/dist/{ArweaveClient-DkNLWiSZ.js.map → ArweaveClient-CgWK-JgT.js.map} +1 -1
- package/dist/{ArweaveClient-CczHvccG.js → ArweaveClient-WcG8CZAE.js} +8 -8
- package/dist/{ArweaveClient-CczHvccG.js.map → ArweaveClient-WcG8CZAE.js.map} +1 -1
- package/dist/{Db-DJvjXVlp.js → Db-DjFdIdR9.js} +9 -9
- package/dist/{Db-DJvjXVlp.js.map → Db-DjFdIdR9.js.map} +1 -1
- package/dist/{Db-BzQR2KX9.js → Db-DjofXdeU.js} +9 -9
- package/dist/{Db-BzQR2KX9.js.map → Db-DjofXdeU.js.map} +1 -1
- package/dist/{EasClient-CHQYmjj2.js → EasClient-Aojewp6P.js} +8 -8
- package/dist/{EasClient-CodrIgW-.js.map → EasClient-Aojewp6P.js.map} +1 -1
- package/dist/{EasClient-CodrIgW-.js → EasClient-BVFXp2O6.js} +8 -8
- package/dist/{EasClient-CHQYmjj2.js.map → EasClient-BVFXp2O6.js.map} +1 -1
- package/dist/{FileManager-BbvkZJCH.js → FileManager-C9zr4AJe.js} +8 -8
- package/dist/{FileManager-BbvkZJCH.js.map → FileManager-C9zr4AJe.js.map} +1 -1
- package/dist/{FileManager-BIVsDnMu.js → FileManager-CxGJLw5C.js} +8 -8
- package/dist/{FileManager-BIVsDnMu.js.map → FileManager-CxGJLw5C.js.map} +1 -1
- package/dist/Item/Item.d.ts.map +1 -1
- package/dist/ItemProperty/ItemProperty.d.ts.map +1 -1
- package/dist/Model/Model.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-gjcA83Mq.js → ModelProperty-CGdkocQ8.js} +31 -766
- package/dist/ModelProperty-CGdkocQ8.js.map +1 -0
- package/dist/{PathResolver-kGzba-zC.js → PathResolver-CX6GHoTS.js} +8 -8
- package/dist/{PathResolver-kGzba-zC.js.map → PathResolver-CX6GHoTS.js.map} +1 -1
- package/dist/{PathResolver-9zAzPhee.js → PathResolver-z_WX47_o.js} +8 -8
- package/dist/{PathResolver-9zAzPhee.js.map → PathResolver-z_WX47_o.js.map} +1 -1
- package/dist/{QueryClient-C7ZkTT0z.js → QueryClient-ByKPdRmE.js} +8 -8
- package/dist/{QueryClient-C7ZkTT0z.js.map → QueryClient-ByKPdRmE.js.map} +1 -1
- package/dist/{QueryClient-CEtsek7Q.js → QueryClient-Cb1iJO-x.js} +8 -8
- package/dist/{QueryClient-CEtsek7Q.js.map → QueryClient-Cb1iJO-x.js.map} +1 -1
- package/dist/Schema/Schema.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-D1eqDHyt.js} +758 -60
- package/dist/Schema-D1eqDHyt.js.map +1 -0
- package/dist/{SchemaValidationService-BfoEiHNr.js → SchemaValidationService-DyttFaV_.js} +7 -7
- package/dist/{SchemaValidationService-BfoEiHNr.js.map → SchemaValidationService-DyttFaV_.js.map} +1 -1
- package/dist/cjs/{ModelProperty-BRKRREYP.js → ModelProperty-BeJvgKMw.js} +62 -436
- package/dist/cjs/ModelProperty-BeJvgKMw.js.map +1 -0
- package/dist/cjs/{Schema-CV9hMg8m.js → Schema-CVs9J6eP.js} +426 -91
- package/dist/cjs/Schema-CVs9J6eP.js.map +1 -0
- package/dist/cjs/{SchemaValidationService-DUtQUerg.js → SchemaValidationService-CDKcVRFQ.js} +3 -3
- package/dist/cjs/{SchemaValidationService-DUtQUerg.js.map → SchemaValidationService-CDKcVRFQ.js.map} +1 -1
- package/dist/cjs/{getItem-BThT-UOt.js → getItem-B5RYPvrG.js} +3 -3
- package/dist/cjs/{getItem-BThT-UOt.js.map → getItem-B5RYPvrG.js.map} +1 -1
- package/dist/cjs/{getPublishPayload-CKZ7rVbU.js → getPublishPayload-BD1qRob1.js} +6 -6
- package/dist/cjs/getPublishPayload-BD1qRob1.js.map +1 -0
- package/dist/cjs/{getPublishUploads-CuGIvV-Q.js → getPublishUploads-CnC9aYxs.js} +4 -4
- package/dist/cjs/{getPublishUploads-CuGIvV-Q.js.map → getPublishUploads-CnC9aYxs.js.map} +1 -1
- package/dist/cjs/{getSegmentedItemProperties-BCJXJlTR.js → getSegmentedItemProperties-B_njnntx.js} +2 -2
- package/dist/cjs/{getSegmentedItemProperties-BCJXJlTR.js.map → getSegmentedItemProperties-B_njnntx.js.map} +1 -1
- package/dist/cjs/{index-C3j3m34a.js → index-BeKPbbk0.js} +11466 -11546
- package/dist/cjs/index-BeKPbbk0.js.map +1 -0
- package/dist/cjs/{index-CbTBDqTJ.js → index-Dnywap_P.js} +3 -3
- package/dist/cjs/index-Dnywap_P.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/eas.d.ts.map +1 -1
- package/dist/{getItem-BWPo8wd0.js → getItem-BB5HBCbK.js} +8 -8
- package/dist/{getItem-BWPo8wd0.js.map → getItem-BB5HBCbK.js.map} +1 -1
- package/dist/{getPublishPayload-Caw2laZM.js → getPublishPayload-uLm0AqN_.js} +10 -10
- package/dist/getPublishPayload-uLm0AqN_.js.map +1 -0
- package/dist/{getPublishUploads-MlyBuoXh.js → getPublishUploads-Dc-HqhO8.js} +9 -9
- package/dist/{getPublishUploads-MlyBuoXh.js.map → getPublishUploads-Dc-HqhO8.js.map} +1 -1
- package/dist/{getSegmentedItemProperties-COLLdDo4.js → getSegmentedItemProperties-BrIqFNfD.js} +2 -2
- package/dist/{getSegmentedItemProperties-COLLdDo4.js.map → getSegmentedItemProperties-BrIqFNfD.js.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-2FcQHgKp.js} +2 -2
- package/dist/index-2FcQHgKp.js.map +1 -0
- package/dist/{ClientManager-DoHUt8tU.js → index-DPll6EAp.js} +11460 -11538
- package/dist/index-DPll6EAp.js.map +1 -0
- package/dist/{index-B9i_mXXp.js → index-LEY0Og1p.js} +9 -9
- package/dist/index-LEY0Og1p.js.map +1 -0
- package/dist/main.cjs +2 -2
- package/dist/main.js +13 -13
- package/dist/main.js.map +1 -1
- package/dist/node.js +15 -15
- package/dist/{property-fK7tSUDX.js → property-B15X7jLX.js} +4 -4
- package/dist/property-B15X7jLX.js.map +1 -0
- package/dist/{queries-DGOk16cd.js → queries-BPDSpiEX.js} +2 -2
- package/dist/{queries-DGOk16cd.js.map → queries-BPDSpiEX.js.map} +1 -1
- package/dist/services/write/actors/writeToDatabase.d.ts.map +1 -1
- package/package.json +6 -2
- package/dist/ClientManager-DoHUt8tU.js.map +0 -1
- package/dist/ModelProperty-gjcA83Mq.js.map +0 -1
- package/dist/Schema-ClTcpgbt.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/getPublishPayload-Caw2laZM.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
|
@@ -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-D1eqDHyt.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-DPll6EAp.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-DyttFaV_.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,11 @@ 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 { listLatestSchemaFiles } = await import('./
|
|
29
|
+
const { listLatestSchemaFiles } = await import('./index-DPll6EAp.js').then(function (n) { return n.aU; });
|
|
747
30
|
const latestSchemas = await listLatestSchemaFiles();
|
|
748
31
|
for (const schema of latestSchemas) {
|
|
749
32
|
try {
|
|
750
|
-
const { BaseFileManager } = await import('./
|
|
33
|
+
const { BaseFileManager } = await import('./index-DPll6EAp.js').then(function (n) { return n.aS; });
|
|
751
34
|
const content = await BaseFileManager.readFileAsString(schema.filePath);
|
|
752
35
|
const schemaFile = JSON.parse(content);
|
|
753
36
|
if (schemaFile.models && schemaFile.models[modelName]) {
|
|
@@ -764,7 +47,7 @@ async function getSchemaNameFromModel(modelName) {
|
|
|
764
47
|
const saveToSchema = fromCallback(({ sendBack, input: { context } }) => {
|
|
765
48
|
const _saveToSchema = async () => {
|
|
766
49
|
// Use dynamic import to break circular dependency
|
|
767
|
-
const { SchemaValidationService } = await import('./SchemaValidationService-
|
|
50
|
+
const { SchemaValidationService } = await import('./SchemaValidationService-DyttFaV_.js');
|
|
768
51
|
const validationService = new SchemaValidationService();
|
|
769
52
|
// Validate property structure before saving
|
|
770
53
|
const validationResult = validationService.validatePropertyStructure(context);
|
|
@@ -792,8 +75,7 @@ const saveToSchema = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
792
75
|
await updateModelProperties(schemaName, [propertyUpdate]);
|
|
793
76
|
// Clear isEdited flag in database after saving to schema file
|
|
794
77
|
try {
|
|
795
|
-
const {
|
|
796
|
-
const { properties: propertiesTable, models: modelsTable } = await import('./index-D2OGsG4v.js').then(function (n) { return n.s; });
|
|
78
|
+
const { properties: propertiesTable, models: modelsTable } = await import('./index-2FcQHgKp.js').then(function (n) { return n.s; });
|
|
797
79
|
const { eq, and } = await import('drizzle-orm');
|
|
798
80
|
const db = BaseDb.getAppDb();
|
|
799
81
|
if (db && context.modelName && context.name) {
|
|
@@ -837,12 +119,6 @@ const saveToSchema = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
837
119
|
};
|
|
838
120
|
});
|
|
839
121
|
|
|
840
|
-
var saveToSchema$1 = /*#__PURE__*/Object.freeze({
|
|
841
|
-
__proto__: null,
|
|
842
|
-
getSchemaNameFromModel: getSchemaNameFromModel,
|
|
843
|
-
saveToSchema: saveToSchema
|
|
844
|
-
});
|
|
845
|
-
|
|
846
122
|
const logger$2 = debug('seedSdk:modelProperty:actors:compareAndMarkDraft');
|
|
847
123
|
const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
848
124
|
const _compareAndMarkDraft = async () => {
|
|
@@ -855,7 +131,7 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
855
131
|
const schemaFileIdForResolve = fullContext._propertyFileId || (typeof fullContext.id === 'string' ? fullContext.id : undefined);
|
|
856
132
|
if (schemaFileIdForResolve && (fullContext.modelName === undefined || fullContext.dataType === undefined)) {
|
|
857
133
|
try {
|
|
858
|
-
const { getPropertyModelNameAndDataType, getModelNameByModelId } = await import('./
|
|
134
|
+
const { getPropertyModelNameAndDataType, getModelNameByModelId } = await import('./index-DPll6EAp.js').then(function (n) { return n.aT; });
|
|
859
135
|
let fromDb;
|
|
860
136
|
for (let attempt = 0; attempt < 6; attempt++) {
|
|
861
137
|
fromDb = await getPropertyModelNameAndDataType(schemaFileIdForResolve);
|
|
@@ -899,7 +175,7 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
899
175
|
if (fullContext.modelName && fullContext.name && schemaFileId) {
|
|
900
176
|
logger$2(`[compareAndMarkDraft] _originalValues not set, but saving to database anyway for property ${context.modelName}:${context.name} (schemaFileId: ${schemaFileId})`);
|
|
901
177
|
try {
|
|
902
|
-
const { savePropertyToDb } = await import('./
|
|
178
|
+
const { savePropertyToDb } = await import('./index-DPll6EAp.js').then(function (n) { return n.aT; });
|
|
903
179
|
// Ensure _propertyFileId is set for savePropertyToDb to find the property
|
|
904
180
|
const contextWithFileId = {
|
|
905
181
|
...fullContext,
|
|
@@ -958,7 +234,7 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
958
234
|
logger$2(`Property ${fullContext.modelName}:${fullContext.name} has changes, marking as edited`);
|
|
959
235
|
logger$2(`[compareAndMarkDraft] Context when saving: id=${fullContext.id}, _propertyFileId=${fullContext._propertyFileId}, name=${fullContext.name}, _originalValues.name=${fullContext._originalValues?.name}`);
|
|
960
236
|
// Use dynamic import to break circular dependency
|
|
961
|
-
const { savePropertyToDb } = await import('./
|
|
237
|
+
const { savePropertyToDb } = await import('./index-DPll6EAp.js').then(function (n) { return n.aT; });
|
|
962
238
|
// Save to database (but not JSON file) - always save to DB when there are changes
|
|
963
239
|
try {
|
|
964
240
|
await savePropertyToDb(fullContext);
|
|
@@ -971,7 +247,7 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
971
247
|
// Mark schema as draft if schema name is available
|
|
972
248
|
if (fullContext._schemaName) {
|
|
973
249
|
// Get the Schema instance and mark it as draft
|
|
974
|
-
const { Schema } = await import('./Schema-
|
|
250
|
+
const { Schema } = await import('./Schema-D1eqDHyt.js').then(function (n) { return n.b; });
|
|
975
251
|
const schema = Schema.create(fullContext._schemaName, {
|
|
976
252
|
waitForReady: false,
|
|
977
253
|
});
|
|
@@ -987,8 +263,7 @@ const compareAndMarkDraft = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
987
263
|
logger$2(`Property ${fullContext.modelName}:${fullContext.name} has no changes`);
|
|
988
264
|
// Clear isEdited flag in database
|
|
989
265
|
try {
|
|
990
|
-
const {
|
|
991
|
-
const { properties: propertiesTable, models: modelsTable } = await import('./index-D2OGsG4v.js').then(function (n) { return n.s; });
|
|
266
|
+
const { properties: propertiesTable, models: modelsTable } = await import('./index-2FcQHgKp.js').then(function (n) { return n.s; });
|
|
992
267
|
const { eq, and } = await import('drizzle-orm');
|
|
993
268
|
const db = BaseDb.getAppDb();
|
|
994
269
|
if (db && fullContext.modelName && fullContext.name) {
|
|
@@ -1050,7 +325,7 @@ const validateProperty = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
1050
325
|
// Brief wait so trackPendingWrite from ModelProperty.create() has time to run (it's in setTimeout(0))
|
|
1051
326
|
await new Promise((r) => setTimeout(r, 60));
|
|
1052
327
|
try {
|
|
1053
|
-
const { getPropertyModelNameAndDataType, getModelNameByModelId } = await import('./
|
|
328
|
+
const { getPropertyModelNameAndDataType, getModelNameByModelId } = await import('./index-DPll6EAp.js').then(function (n) { return n.aT; });
|
|
1054
329
|
// Try pending write first (property row may not exist yet)
|
|
1055
330
|
if (fullContext.modelName === undefined && schemaFileId) {
|
|
1056
331
|
const mod = await Promise.resolve().then(function () { return ModelProperty$1; });
|
|
@@ -1108,9 +383,9 @@ const validateProperty = fromCallback(({ sendBack, input: { context } }) => {
|
|
|
1108
383
|
_originalValues: fullContext._originalValues
|
|
1109
384
|
});
|
|
1110
385
|
// Use dynamic imports to break circular dependencies
|
|
1111
|
-
const { SchemaValidationService } = await import('./SchemaValidationService-
|
|
386
|
+
const { SchemaValidationService } = await import('./SchemaValidationService-DyttFaV_.js');
|
|
1112
387
|
const validationService = new SchemaValidationService();
|
|
1113
|
-
const { Schema } = await import('./Schema-
|
|
388
|
+
const { Schema } = await import('./Schema-D1eqDHyt.js').then(function (n) { return n.b; });
|
|
1114
389
|
// Validate property structure
|
|
1115
390
|
console.log('[validateProperty] Validating property structure...');
|
|
1116
391
|
const structureResult = validationService.validatePropertyStructure(fullContext);
|
|
@@ -1708,7 +983,6 @@ class ModelProperty {
|
|
|
1708
983
|
return fallbackIsEdited;
|
|
1709
984
|
}
|
|
1710
985
|
try {
|
|
1711
|
-
const { eq, and } = await import('drizzle-orm');
|
|
1712
986
|
const db = BaseDb.getAppDb();
|
|
1713
987
|
if (!db) {
|
|
1714
988
|
return fallbackIsEdited;
|
|
@@ -1747,7 +1021,6 @@ class ModelProperty {
|
|
|
1747
1021
|
return undefined;
|
|
1748
1022
|
}
|
|
1749
1023
|
try {
|
|
1750
|
-
const { eq } = await import('drizzle-orm');
|
|
1751
1024
|
const db = BaseDb.getAppDb();
|
|
1752
1025
|
if (!db) {
|
|
1753
1026
|
return undefined;
|
|
@@ -1776,8 +1049,6 @@ class ModelProperty {
|
|
|
1776
1049
|
return undefined;
|
|
1777
1050
|
}
|
|
1778
1051
|
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
1052
|
const model = await Model.getByNameAsync(property.modelName);
|
|
1782
1053
|
if (!model || !model.properties || model.properties.length === 0) {
|
|
1783
1054
|
return undefined;
|
|
@@ -1826,7 +1097,6 @@ class ModelProperty {
|
|
|
1826
1097
|
// Try to get schema name from database first (more reliable)
|
|
1827
1098
|
if (this.modelId) {
|
|
1828
1099
|
try {
|
|
1829
|
-
const { eq } = await import('drizzle-orm');
|
|
1830
1100
|
const db = BaseDb.getAppDb();
|
|
1831
1101
|
if (db) {
|
|
1832
1102
|
const modelSchemaRecords = await db
|
|
@@ -1849,7 +1119,6 @@ class ModelProperty {
|
|
|
1849
1119
|
}
|
|
1850
1120
|
// Fall back to schema file lookup if database didn't work
|
|
1851
1121
|
if (!schemaName) {
|
|
1852
|
-
const { getSchemaNameFromModel } = await Promise.resolve().then(function () { return saveToSchema$1; });
|
|
1853
1122
|
schemaName = await getSchemaNameFromModel(modelName);
|
|
1854
1123
|
}
|
|
1855
1124
|
if (schemaName) {
|
|
@@ -2030,7 +1299,6 @@ class ModelProperty {
|
|
|
2030
1299
|
else if (typeof propertyWithId.modelId === 'string') {
|
|
2031
1300
|
// modelId is a string (modelFileId), need to convert to database ID
|
|
2032
1301
|
try {
|
|
2033
|
-
const { getModelIdByFileId } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aW; });
|
|
2034
1302
|
resolvedModelId = await getModelIdByFileId(propertyWithId.modelId);
|
|
2035
1303
|
logger(`Converted modelFileId "${propertyWithId.modelId}" to database modelId: ${resolvedModelId}`);
|
|
2036
1304
|
}
|
|
@@ -2043,7 +1311,6 @@ class ModelProperty {
|
|
|
2043
1311
|
// If we still don't have a modelId, try to resolve it from modelName
|
|
2044
1312
|
if (!resolvedModelId && propertyWithId.modelName) {
|
|
2045
1313
|
try {
|
|
2046
|
-
const { getModelId } = await import('./ClientManager-DoHUt8tU.js').then(function (n) { return n.aW; });
|
|
2047
1314
|
// Get schemaName from context if available
|
|
2048
1315
|
const schemaName = snapshot.context._schemaName;
|
|
2049
1316
|
resolvedModelId = await getModelId(propertyWithId.modelName, schemaName);
|
|
@@ -2152,7 +1419,6 @@ class ModelProperty {
|
|
|
2152
1419
|
return cachedInstance;
|
|
2153
1420
|
}
|
|
2154
1421
|
// Query database to get property data from ID
|
|
2155
|
-
const { eq } = await import('drizzle-orm');
|
|
2156
1422
|
const db = BaseDb.getAppDb();
|
|
2157
1423
|
console.log('db', !!db);
|
|
2158
1424
|
if (!db) {
|
|
@@ -2455,7 +1721,6 @@ class ModelProperty {
|
|
|
2455
1721
|
}
|
|
2456
1722
|
}
|
|
2457
1723
|
if (schemaName) {
|
|
2458
|
-
const { Schema } = await import('./Schema-ClTcpgbt.js');
|
|
2459
1724
|
const schema = Schema.create(schemaName, { waitForReady: false });
|
|
2460
1725
|
const snapshot = schema.getService().getSnapshot();
|
|
2461
1726
|
const schemaContext = snapshot.context;
|
|
@@ -2481,5 +1746,5 @@ var ModelProperty$1 = /*#__PURE__*/Object.freeze({
|
|
|
2481
1746
|
ModelProperty: ModelProperty
|
|
2482
1747
|
});
|
|
2483
1748
|
|
|
2484
|
-
export { ModelProperty
|
|
2485
|
-
//# sourceMappingURL=ModelProperty-
|
|
1749
|
+
export { ModelProperty };
|
|
1750
|
+
//# sourceMappingURL=ModelProperty-CGdkocQ8.js.map
|