@seedprotocol/sdk 0.4.3 → 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-CleX_4Gw.js → ArweaveClient-CgWK-JgT.js} +8 -8
- package/dist/{ArweaveClient-CleX_4Gw.js.map → ArweaveClient-CgWK-JgT.js.map} +1 -1
- package/dist/{ArweaveClient-BvJ1FhQ5.js → ArweaveClient-WcG8CZAE.js} +8 -8
- package/dist/{ArweaveClient-BvJ1FhQ5.js.map → ArweaveClient-WcG8CZAE.js.map} +1 -1
- package/dist/{Db-DX08SxS9.js → Db-DjFdIdR9.js} +9 -16
- package/dist/{Db-DX08SxS9.js.map → Db-DjFdIdR9.js.map} +1 -1
- package/dist/{Db-BPnO1-_p.js → Db-DjofXdeU.js} +9 -9
- package/dist/{Db-BPnO1-_p.js.map → Db-DjofXdeU.js.map} +1 -1
- package/dist/{EasClient-BwhUcPjY.js → EasClient-Aojewp6P.js} +8 -8
- package/dist/{EasClient-CJSs38Db.js.map → EasClient-Aojewp6P.js.map} +1 -1
- package/dist/{EasClient-CJSs38Db.js → EasClient-BVFXp2O6.js} +8 -8
- package/dist/{EasClient-BwhUcPjY.js.map → EasClient-BVFXp2O6.js.map} +1 -1
- package/dist/{FileManager-B1tdLMsX.js → FileManager-C9zr4AJe.js} +8 -8
- package/dist/{FileManager-B1tdLMsX.js.map → FileManager-C9zr4AJe.js.map} +1 -1
- package/dist/{FileManager-Ct91ZhOE.js → FileManager-CxGJLw5C.js} +8 -8
- package/dist/{FileManager-Ct91ZhOE.js.map → FileManager-CxGJLw5C.js.map} +1 -1
- package/dist/Item/Item.d.ts +28 -7
- package/dist/Item/Item.d.ts.map +1 -1
- package/dist/Item/service/actors/runPublish.d.ts +5 -0
- package/dist/Item/service/actors/runPublish.d.ts.map +1 -0
- package/dist/Item/service/itemMachineSingle.d.ts +10 -5
- package/dist/Item/service/itemMachineSingle.d.ts.map +1 -1
- package/dist/ItemProperty/ItemProperty.d.ts +30 -5
- package/dist/ItemProperty/ItemProperty.d.ts.map +1 -1
- package/dist/ItemProperty/service/actors/loadOrCreateProperty.d.ts.map +1 -1
- package/dist/ItemProperty/service/propertyMachine.d.ts +10 -10
- package/dist/ItemProperty/service/propertyMachine.d.ts.map +1 -1
- package/dist/Model/Model.d.ts +27 -20
- package/dist/Model/Model.d.ts.map +1 -1
- package/dist/Model/index.d.ts +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/Model/service/modelMachine.d.ts +18 -3
- package/dist/Model/service/modelMachine.d.ts.map +1 -1
- package/dist/ModelProperty/ModelProperty.d.ts +25 -2
- 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/service/modelPropertyMachine.d.ts +17 -3
- package/dist/ModelProperty/service/modelPropertyMachine.d.ts.map +1 -1
- package/dist/{ModelProperty-Cr3BmgkC.js → ModelProperty-CGdkocQ8.js} +349 -817
- package/dist/ModelProperty-CGdkocQ8.js.map +1 -0
- package/dist/{PathResolver-DJdxE_OK.js → PathResolver-CX6GHoTS.js} +8 -8
- package/dist/{PathResolver-DJdxE_OK.js.map → PathResolver-CX6GHoTS.js.map} +1 -1
- package/dist/{PathResolver-BErmcZqP.js → PathResolver-z_WX47_o.js} +8 -8
- package/dist/{PathResolver-BErmcZqP.js.map → PathResolver-z_WX47_o.js.map} +1 -1
- package/dist/{QueryClient-DIu9c-w6.js → QueryClient-ByKPdRmE.js} +8 -8
- package/dist/{QueryClient-DIu9c-w6.js.map → QueryClient-ByKPdRmE.js.map} +1 -1
- package/dist/{QueryClient-D2mv63gP.js → QueryClient-Cb1iJO-x.js} +8 -8
- package/dist/{QueryClient-D2mv63gP.js.map → QueryClient-Cb1iJO-x.js.map} +1 -1
- package/dist/Schema/Schema.d.ts +24 -3
- package/dist/Schema/Schema.d.ts.map +1 -1
- package/dist/Schema/service/actors/checkExistingSchema.d.ts.map +1 -1
- package/dist/Schema/service/actors/createPropertyInstances.d.ts.map +1 -1
- package/dist/Schema/service/actors/loadOrCreateSchema.d.ts.map +1 -1
- package/dist/Schema/service/actors/verifyPropertyInstancesInCache.d.ts.map +1 -1
- package/dist/Schema/service/actors/writeModelsToDb.d.ts.map +1 -1
- package/dist/Schema/service/actors/writePropertiesToDb.d.ts.map +1 -1
- package/dist/Schema/service/actors/writeSchemaToDb.d.ts.map +1 -1
- package/dist/Schema/service/addModelsMachine.d.ts.map +1 -1
- package/dist/Schema/service/schemaMachine.d.ts +17 -3
- package/dist/Schema/service/schemaMachine.d.ts.map +1 -1
- package/dist/{Schema-DeKabJ0T.js → Schema-D1eqDHyt.js} +995 -186
- package/dist/Schema-D1eqDHyt.js.map +1 -0
- package/dist/{SchemaValidationService-cTlURuDt.js → SchemaValidationService-DyttFaV_.js} +7 -7
- package/dist/{SchemaValidationService-cTlURuDt.js.map → SchemaValidationService-DyttFaV_.js.map} +1 -1
- package/dist/browser/db/Db.d.ts.map +1 -1
- package/dist/browser/react/SeedProvider.d.ts +30 -0
- package/dist/browser/react/SeedProvider.d.ts.map +1 -0
- package/dist/browser/react/index.d.ts +4 -1
- package/dist/browser/react/index.d.ts.map +1 -1
- package/dist/browser/react/item.d.ts +10 -6
- package/dist/browser/react/item.d.ts.map +1 -1
- package/dist/browser/react/itemProperty.d.ts +37 -1
- package/dist/browser/react/itemProperty.d.ts.map +1 -1
- package/dist/browser/react/liveQuery.d.ts.map +1 -1
- package/dist/browser/react/model.d.ts +21 -7
- package/dist/browser/react/model.d.ts.map +1 -1
- package/dist/browser/react/modelProperty.d.ts +23 -0
- package/dist/browser/react/modelProperty.d.ts.map +1 -1
- package/dist/browser/react/queryClient.d.ts +28 -0
- package/dist/browser/react/queryClient.d.ts.map +1 -0
- package/dist/browser/react/schema.d.ts +8 -0
- package/dist/browser/react/schema.d.ts.map +1 -1
- package/dist/browser/react/trash.d.ts +5 -2
- package/dist/browser/react/trash.d.ts.map +1 -1
- package/dist/cjs/{ModelProperty-MkN5Rmx7.js → ModelProperty-BeJvgKMw.js} +377 -477
- package/dist/cjs/ModelProperty-BeJvgKMw.js.map +1 -0
- package/dist/cjs/{Schema-B5cr_JVK.js → Schema-CVs9J6eP.js} +709 -263
- package/dist/cjs/Schema-CVs9J6eP.js.map +1 -0
- package/dist/cjs/{SchemaValidationService-BgIzc3-r.js → SchemaValidationService-CDKcVRFQ.js} +4 -4
- package/dist/cjs/{SchemaValidationService-BgIzc3-r.js.map → SchemaValidationService-CDKcVRFQ.js.map} +1 -1
- package/dist/cjs/{getItem-CVJJPky2.js → getItem-B5RYPvrG.js} +4 -4
- package/dist/cjs/{getItem-CVJJPky2.js.map → getItem-B5RYPvrG.js.map} +1 -1
- package/dist/cjs/{getPublishPayload-DbOc3WA-.js → getPublishPayload-BD1qRob1.js} +26 -11
- package/dist/cjs/getPublishPayload-BD1qRob1.js.map +1 -0
- package/dist/cjs/{getPublishUploads-NzioLz-3.js → getPublishUploads-CnC9aYxs.js} +5 -5
- package/dist/cjs/getPublishUploads-CnC9aYxs.js.map +1 -0
- package/dist/cjs/{getSegmentedItemProperties-BsaklLwI.js → getSegmentedItemProperties-B_njnntx.js} +2 -2
- package/dist/cjs/{getSegmentedItemProperties-BsaklLwI.js.map → getSegmentedItemProperties-B_njnntx.js.map} +1 -1
- package/dist/cjs/{index-BmIVfqGN.js → index-BeKPbbk0.js} +12715 -12384
- package/dist/cjs/index-BeKPbbk0.js.map +1 -0
- package/dist/cjs/{index-C_0angRB.js → index-Dnywap_P.js} +4 -4
- package/dist/cjs/index-Dnywap_P.js.map +1 -0
- package/dist/client/actors/platformClassesInit.d.ts.map +1 -1
- package/dist/client/actors/processSchemaFiles.d.ts.map +1 -1
- package/dist/client/actors/saveAppState.d.ts.map +1 -1
- package/dist/db/read/getItemData.d.ts.map +1 -1
- package/dist/db/read/getItems.d.ts.map +1 -1
- package/dist/db/read/getModelPropertiesData.d.ts +19 -0
- package/dist/db/read/getModelPropertiesData.d.ts.map +1 -0
- package/dist/db/read/getModelsData.d.ts +15 -0
- package/dist/db/read/getModelsData.d.ts.map +1 -0
- package/dist/db/read/getPublishPayload.d.ts.map +1 -1
- package/dist/db/read/getPublishUploads.d.ts +1 -7
- package/dist/db/read/getPublishUploads.d.ts.map +1 -1
- package/dist/db/read/getSchemaUidForModel.d.ts.map +1 -1
- package/dist/db/write/updateSeedUid.d.ts +7 -0
- package/dist/db/write/updateSeedUid.d.ts.map +1 -0
- package/dist/eas.d.ts.map +1 -1
- package/dist/events/item/index.d.ts.map +1 -1
- package/dist/events/item/syncDbWithEas.d.ts.map +1 -1
- package/dist/{getItem-CcttmUY_.js → getItem-BB5HBCbK.js} +8 -8
- package/dist/{getItem-CcttmUY_.js.map → getItem-BB5HBCbK.js.map} +1 -1
- package/dist/{getPublishPayload-NFpqbd_H.js → getPublishPayload-uLm0AqN_.js} +29 -14
- package/dist/getPublishPayload-uLm0AqN_.js.map +1 -0
- package/dist/{getPublishUploads-Cpb9vgwE.js → getPublishUploads-Dc-HqhO8.js} +9 -9
- package/dist/getPublishUploads-Dc-HqhO8.js.map +1 -0
- package/dist/{getSegmentedItemProperties-DiyQPMgI.js → getSegmentedItemProperties-BrIqFNfD.js} +2 -2
- package/dist/{getSegmentedItemProperties-DiyQPMgI.js.map → getSegmentedItemProperties-BrIqFNfD.js.map} +1 -1
- package/dist/helpers/db.d.ts +12 -0
- package/dist/helpers/db.d.ts.map +1 -1
- package/dist/helpers/entity/entityDestroy.d.ts +41 -0
- package/dist/helpers/entity/entityDestroy.d.ts.map +1 -0
- package/dist/helpers/entity/index.d.ts +1 -0
- package/dist/helpers/entity/index.d.ts.map +1 -1
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/property/index.d.ts +12 -12
- package/dist/helpers/property/index.d.ts.map +1 -1
- package/dist/helpers/reactiveProxy.d.ts.map +1 -1
- package/dist/helpers/schema.d.ts.map +1 -1
- package/dist/helpers/updateSchema.d.ts +9 -0
- package/dist/helpers/updateSchema.d.ts.map +1 -1
- package/dist/helpers/waitForEntityIdle.d.ts +2 -2
- package/dist/helpers/waitForEntityIdle.d.ts.map +1 -1
- package/dist/imports/json.d.ts.map +1 -1
- package/dist/{index-r45w9hEq.js → index-2FcQHgKp.js} +2 -2
- package/dist/index-2FcQHgKp.js.map +1 -0
- package/dist/{json-I3vJhXo8.js → index-DPll6EAp.js} +12450 -12121
- package/dist/index-DPll6EAp.js.map +1 -0
- package/dist/{index-CRuq6HVi.js → index-LEY0Og1p.js} +9 -9
- package/dist/index-LEY0Og1p.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/interfaces/IItem.d.ts +2 -0
- package/dist/interfaces/IItem.d.ts.map +1 -1
- package/dist/interfaces/IItemProperty.d.ts +1 -0
- package/dist/interfaces/IItemProperty.d.ts.map +1 -1
- package/dist/main.cjs +3 -3
- package/dist/main.js +999 -1033
- package/dist/main.js.map +1 -1
- package/dist/node.js +16 -16
- package/dist/node.js.map +1 -1
- package/dist/{property-Dy09KTxg.js → property-B15X7jLX.js} +7 -5
- package/dist/property-B15X7jLX.js.map +1 -0
- package/dist/{queries-LZYSuhtz.js → queries-BPDSpiEX.js} +2 -2
- package/dist/{queries-LZYSuhtz.js.map → queries-BPDSpiEX.js.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/services/write/writeProcessMachine.d.ts +1 -1
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/item.d.ts +12 -0
- package/dist/types/item.d.ts.map +1 -1
- package/dist/types/property.d.ts +6 -0
- package/dist/types/property.d.ts.map +1 -1
- package/dist/types/publish.d.ts +9 -0
- package/dist/types/publish.d.ts.map +1 -0
- package/package.json +12 -4
- package/dist/ModelProperty-Cr3BmgkC.js.map +0 -1
- package/dist/Schema-DeKabJ0T.js.map +0 -1
- package/dist/cjs/ModelProperty-MkN5Rmx7.js.map +0 -1
- package/dist/cjs/Schema-B5cr_JVK.js.map +0 -1
- package/dist/cjs/getPublishPayload-DbOc3WA-.js.map +0 -1
- package/dist/cjs/getPublishUploads-NzioLz-3.js.map +0 -1
- package/dist/cjs/index-BmIVfqGN.js.map +0 -1
- package/dist/cjs/index-C_0angRB.js.map +0 -1
- package/dist/events/item/publish.d.ts +0 -7
- package/dist/events/item/publish.d.ts.map +0 -1
- package/dist/getPublishPayload-NFpqbd_H.js.map +0 -1
- package/dist/getPublishUploads-Cpb9vgwE.js.map +0 -1
- package/dist/index-CRuq6HVi.js.map +0 -1
- package/dist/index-r45w9hEq.js.map +0 -1
- package/dist/json-I3vJhXo8.js.map +0 -1
- package/dist/property-Dy09KTxg.js.map +0 -1
|
@@ -1,38 +1,388 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var xstate = require('xstate');
|
|
4
|
-
var main_cjs = require('./index-
|
|
4
|
+
var main_cjs = require('./index-BeKPbbk0.js');
|
|
5
5
|
var drizzleOrm = require('drizzle-orm');
|
|
6
6
|
var debug = require('debug');
|
|
7
|
-
var SchemaValidationService = require('./SchemaValidationService-
|
|
8
|
-
var ModelProperty = require('./ModelProperty-MkN5Rmx7.js');
|
|
9
|
-
require('immer');
|
|
10
|
-
require('reflect-metadata');
|
|
11
|
-
require('drizzle-orm/sqlite-core');
|
|
12
|
-
require('nanoid');
|
|
13
|
-
require('nanoid-dictionary');
|
|
7
|
+
var SchemaValidationService = require('./SchemaValidationService-CDKcVRFQ.js');
|
|
14
8
|
require('pluralize');
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
9
|
+
|
|
10
|
+
const logger$d = debug('seedSdk:helpers:updateSchema');
|
|
11
|
+
/**
|
|
12
|
+
* Get the file path for a schema file
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Sanitize a schema name to be filesystem-safe
|
|
16
|
+
* Replaces all special characters (except alphanumeric, hyphens, underscores) with underscores
|
|
17
|
+
* Converts spaces to underscores
|
|
18
|
+
* Removes leading/trailing underscores
|
|
19
|
+
*
|
|
20
|
+
* @param name - Schema name to sanitize
|
|
21
|
+
* @returns Sanitized name safe for use in filenames
|
|
22
|
+
*/
|
|
23
|
+
const sanitizeSchemaName$1 = (name) => {
|
|
24
|
+
return name
|
|
25
|
+
.replace(/[^a-zA-Z0-9\s_-]/g, '_') // Replace special chars (except spaces, hyphens, underscores) with underscore
|
|
26
|
+
.replace(/\s+/g, '_') // Convert spaces to underscores
|
|
27
|
+
.replace(/^_+|_+$/g, '') // Remove leading/trailing underscores
|
|
28
|
+
.replace(/_+/g, '_'); // Collapse multiple underscores to single
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Get the full file path for a schema
|
|
32
|
+
* Format: {schemaFileId}_{schemaName}_v{version}.json
|
|
33
|
+
*
|
|
34
|
+
* The ID-first format ensures all files for a schema group together when sorted alphabetically.
|
|
35
|
+
*
|
|
36
|
+
* @param name - Schema name
|
|
37
|
+
* @param version - Schema version
|
|
38
|
+
* @param schemaFileId - Schema file ID (required)
|
|
39
|
+
*/
|
|
40
|
+
const getSchemaFilePath$2 = (name, version, schemaFileId) => {
|
|
41
|
+
const path = main_cjs.BaseFileManager.getPathModule();
|
|
42
|
+
const workingDir = main_cjs.BaseFileManager.getWorkingDir();
|
|
43
|
+
const sanitizedName = sanitizeSchemaName$1(name);
|
|
44
|
+
const filename = schemaFileId
|
|
45
|
+
? `${schemaFileId}_${sanitizedName}_v${version}.json`
|
|
46
|
+
: `${sanitizedName}_v${version}.json`;
|
|
47
|
+
return path.join(workingDir, filename);
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Get schemaFileId from database for a schema
|
|
51
|
+
* @param schemaName - Schema name
|
|
52
|
+
* @returns Schema file ID
|
|
53
|
+
* @throws Error if schema not found or missing schemaFileId
|
|
54
|
+
*/
|
|
55
|
+
async function getSchemaFileId(schemaName) {
|
|
56
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
57
|
+
if (!db)
|
|
58
|
+
throw new Error('Database not available');
|
|
59
|
+
const dbSchema = await db
|
|
60
|
+
.select()
|
|
61
|
+
.from(main_cjs.schemas)
|
|
62
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, schemaName))
|
|
63
|
+
.orderBy(drizzleOrm.desc(main_cjs.schemas.version))
|
|
64
|
+
.limit(1);
|
|
65
|
+
if (dbSchema.length === 0 || !dbSchema[0].schemaFileId) {
|
|
66
|
+
throw new Error(`Schema ${schemaName} not found in database or missing schemaFileId`);
|
|
67
|
+
}
|
|
68
|
+
return dbSchema[0].schemaFileId;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Write the full schema to a new version file (e.g. when new models were added).
|
|
72
|
+
* Used when _editedProperties contains 'schema:models' and there are no property-level updates.
|
|
73
|
+
* @param schemaName - Schema name
|
|
74
|
+
* @param schema - Full schema object (e.g. from _buildModelsFromInstances)
|
|
75
|
+
* @returns The file path of the new schema version
|
|
76
|
+
*/
|
|
77
|
+
async function writeFullSchemaNewVersion(schemaName, schema) {
|
|
78
|
+
const latestVersion = await main_cjs.getLatestSchemaVersion(schemaName);
|
|
79
|
+
const newVersion = latestVersion + 1;
|
|
80
|
+
const schemaWithNewVersion = {
|
|
81
|
+
...schema,
|
|
82
|
+
version: newVersion,
|
|
83
|
+
metadata: {
|
|
84
|
+
...schema.metadata,
|
|
85
|
+
updatedAt: new Date().toISOString(),
|
|
86
|
+
},
|
|
87
|
+
migrations: [
|
|
88
|
+
...(schema.migrations || []),
|
|
89
|
+
{
|
|
90
|
+
version: newVersion,
|
|
91
|
+
timestamp: new Date().toISOString(),
|
|
92
|
+
description: 'New schema version (e.g. new models added)',
|
|
93
|
+
changes: [{ type: 'full_schema_write' }],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
const newFilePath = getSchemaFilePath$2(schemaName, newVersion, schema.id ?? (await getSchemaFileId(schemaName)));
|
|
98
|
+
const newContent = JSON.stringify(schemaWithNewVersion, null, 2);
|
|
99
|
+
await main_cjs.BaseFileManager.saveFile(newFilePath, newContent);
|
|
100
|
+
await main_cjs.BaseFileManager.waitForFileWithContent(newFilePath);
|
|
101
|
+
logger$d(`Created new schema version ${newVersion} for ${schemaName} at ${newFilePath}`);
|
|
102
|
+
return newFilePath;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Get model name from modelId
|
|
106
|
+
* @param modelId - The model ID to look up
|
|
107
|
+
* @returns The model name, or undefined if not found
|
|
108
|
+
*/
|
|
109
|
+
async function getModelNameFromId(modelId) {
|
|
110
|
+
if (!modelId) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
114
|
+
if (!db) {
|
|
115
|
+
throw new Error('Database not found');
|
|
116
|
+
}
|
|
117
|
+
const modelRecords = await db
|
|
118
|
+
.select()
|
|
119
|
+
.from(main_cjs.models)
|
|
120
|
+
.where(drizzleOrm.eq(main_cjs.models.id, modelId))
|
|
121
|
+
.limit(1);
|
|
122
|
+
if (modelRecords.length === 0) {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
return modelRecords[0].name;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Convert a TProperty/ModelPropertyMachineContext to SchemaPropertyUpdate format
|
|
129
|
+
* This function converts the internal property representation to the schema file format
|
|
130
|
+
* @param property - The TProperty instance to convert
|
|
131
|
+
* @param modelName - The name of the model this property belongs to
|
|
132
|
+
* @param propertyName - The name of the property
|
|
133
|
+
* @returns A SchemaPropertyUpdate object ready to be passed to updateModelProperties
|
|
134
|
+
*/
|
|
135
|
+
async function convertPropertyToSchemaUpdate(property, modelName, propertyName) {
|
|
136
|
+
const updates = {};
|
|
137
|
+
// Convert dataType to type
|
|
138
|
+
if (property.dataType) {
|
|
139
|
+
updates.type = property.dataType;
|
|
140
|
+
}
|
|
141
|
+
// Handle Relation type
|
|
142
|
+
if (property.dataType === main_cjs.ModelPropertyDataTypes.Relation) {
|
|
143
|
+
if (property.ref) {
|
|
144
|
+
updates.model = property.ref;
|
|
145
|
+
}
|
|
146
|
+
else if (property.refModelId) {
|
|
147
|
+
// If ref is not set but refModelId is, get the model name from the database
|
|
148
|
+
const refModelName = await getModelNameFromId(property.refModelId);
|
|
149
|
+
if (refModelName) {
|
|
150
|
+
updates.model = refModelName;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// Handle List type
|
|
155
|
+
if (property.dataType === main_cjs.ModelPropertyDataTypes.List) {
|
|
156
|
+
if (property.refValueType) {
|
|
157
|
+
updates.items = {
|
|
158
|
+
type: property.refValueType,
|
|
159
|
+
};
|
|
160
|
+
if (property.ref) {
|
|
161
|
+
updates.items.model = property.ref;
|
|
162
|
+
}
|
|
163
|
+
else if (property.refModelId) {
|
|
164
|
+
// If ref is not set but refModelId is, get the model name from the database
|
|
165
|
+
const refModelName = await getModelNameFromId(property.refModelId);
|
|
166
|
+
if (refModelName) {
|
|
167
|
+
updates.items.model = refModelName;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// Handle storage configuration (for Text properties with storage)
|
|
173
|
+
if (property.storageType || property.localStorageDir || property.filenameSuffix) {
|
|
174
|
+
const storageType = property.storageType || 'ItemStorage'; // Default to ItemStorage if not specified
|
|
175
|
+
updates.storage = {
|
|
176
|
+
type: storageType,
|
|
177
|
+
};
|
|
178
|
+
if (property.localStorageDir) {
|
|
179
|
+
updates.storage.path = property.localStorageDir;
|
|
180
|
+
}
|
|
181
|
+
if (property.filenameSuffix) {
|
|
182
|
+
updates.storage.extension = property.filenameSuffix;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
modelName,
|
|
187
|
+
propertyName,
|
|
188
|
+
updates,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Update model properties in a schema and create a new version
|
|
193
|
+
* @param schemaName - The name of the schema to update
|
|
194
|
+
* @param propertyUpdates - Array of property updates to apply
|
|
195
|
+
* @param modelUpdates - Optional array of model renames
|
|
196
|
+
* @returns The file path of the new schema version
|
|
197
|
+
* @throws Error if schema not found or updates are invalid
|
|
198
|
+
*/
|
|
199
|
+
async function updateModelProperties(schemaName, propertyUpdates, modelUpdates) {
|
|
200
|
+
// Get the latest version of the schema
|
|
201
|
+
const latestVersion = await main_cjs.getLatestSchemaVersion(schemaName);
|
|
202
|
+
if (latestVersion === 0) {
|
|
203
|
+
throw new Error(`Schema ${schemaName} not found`);
|
|
204
|
+
}
|
|
205
|
+
// Get schemaFileId from database
|
|
206
|
+
const schemaFileId = await getSchemaFileId(schemaName);
|
|
207
|
+
// Load the latest schema file
|
|
208
|
+
const latestFilePath = getSchemaFilePath$2(schemaName, latestVersion, schemaFileId);
|
|
209
|
+
const content = await main_cjs.BaseFileManager.readFileAsString(latestFilePath);
|
|
210
|
+
const schemaFile = JSON.parse(content);
|
|
211
|
+
if (!schemaFile.$schema) {
|
|
212
|
+
throw new Error(`Schema file ${latestFilePath} is not a complete schema file`);
|
|
213
|
+
}
|
|
214
|
+
// Create a copy of the schema for the new version
|
|
215
|
+
// Preserve schema ID and all model/property IDs
|
|
216
|
+
const newVersion = latestVersion + 1;
|
|
217
|
+
const updatedSchema = {
|
|
218
|
+
...schemaFile,
|
|
219
|
+
version: newVersion,
|
|
220
|
+
// Preserve schema ID from previous version
|
|
221
|
+
id: schemaFile.id,
|
|
222
|
+
metadata: {
|
|
223
|
+
...schemaFile.metadata,
|
|
224
|
+
updatedAt: new Date().toISOString(),
|
|
225
|
+
},
|
|
226
|
+
// Deep copy models to preserve IDs
|
|
227
|
+
models: Object.fromEntries(Object.entries(schemaFile.models).map(([modelName, model]) => [
|
|
228
|
+
modelName,
|
|
229
|
+
{
|
|
230
|
+
...model,
|
|
231
|
+
// Preserve model ID
|
|
232
|
+
id: model.id,
|
|
233
|
+
// Deep copy properties to preserve IDs
|
|
234
|
+
properties: Object.fromEntries(Object.entries(model.properties).map(([propName, prop]) => [
|
|
235
|
+
propName,
|
|
236
|
+
{
|
|
237
|
+
...prop,
|
|
238
|
+
// Preserve property ID
|
|
239
|
+
id: prop.id,
|
|
240
|
+
},
|
|
241
|
+
])),
|
|
242
|
+
},
|
|
243
|
+
])),
|
|
244
|
+
migrations: [
|
|
245
|
+
...schemaFile.migrations,
|
|
246
|
+
{
|
|
247
|
+
version: newVersion,
|
|
248
|
+
timestamp: new Date().toISOString(),
|
|
249
|
+
description: `Updated model properties: ${propertyUpdates.map(u => `${u.modelName}.${u.propertyName}`).join(', ')}`,
|
|
250
|
+
changes: propertyUpdates.map(update => ({
|
|
251
|
+
type: 'property_update',
|
|
252
|
+
modelName: update.modelName,
|
|
253
|
+
propertyName: update.propertyName,
|
|
254
|
+
updates: update.updates,
|
|
255
|
+
})),
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
};
|
|
259
|
+
// Apply property updates
|
|
260
|
+
for (const update of propertyUpdates) {
|
|
261
|
+
const model = updatedSchema.models[update.modelName];
|
|
262
|
+
if (!model) {
|
|
263
|
+
throw new Error(`Model ${update.modelName} not found in schema ${schemaName}`);
|
|
264
|
+
}
|
|
265
|
+
if (!model.properties[update.propertyName]) {
|
|
266
|
+
throw new Error(`Property ${update.propertyName} not found in model ${update.modelName} of schema ${schemaName}`);
|
|
267
|
+
}
|
|
268
|
+
// Update the property with new values
|
|
269
|
+
const property = model.properties[update.propertyName];
|
|
270
|
+
Object.assign(property, update.updates);
|
|
271
|
+
}
|
|
272
|
+
// Write the new schema version to file using ID-based naming (preferred)
|
|
273
|
+
const newFilePath = getSchemaFilePath$2(schemaName, newVersion, updatedSchema.id ?? undefined);
|
|
274
|
+
const newContent = JSON.stringify(updatedSchema, null, 2);
|
|
275
|
+
await main_cjs.BaseFileManager.saveFile(newFilePath, newContent);
|
|
276
|
+
// Wait for the file to be available with content (important for browser/OPFS where writes may not be immediately readable)
|
|
277
|
+
await main_cjs.BaseFileManager.waitForFileWithContent(newFilePath);
|
|
278
|
+
logger$d(`Created new schema version ${newVersion} for ${schemaName} at ${newFilePath}`);
|
|
279
|
+
// Load the new schema file to process models and add them to the database
|
|
280
|
+
// Extract model renames from migrations to pass to database update
|
|
281
|
+
const modelRenames = new Map();
|
|
282
|
+
const latestMigration = updatedSchema.migrations[updatedSchema.migrations.length - 1];
|
|
283
|
+
for (const change of latestMigration.changes) {
|
|
284
|
+
if (change.type === 'model_rename') {
|
|
285
|
+
modelRenames.set(change.oldName, change.newName);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
// Load schema with model renames handled
|
|
289
|
+
await loadSchemaWithRenames(newFilePath, modelRenames);
|
|
290
|
+
return newFilePath;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Load a schema file and handle model renames in the database
|
|
294
|
+
* This is a helper function that processes model renames before loading
|
|
295
|
+
*/
|
|
296
|
+
async function loadSchemaWithRenames(schemaFilePath, modelRenames) {
|
|
297
|
+
const content = await main_cjs.BaseFileManager.readFileAsString(schemaFilePath);
|
|
298
|
+
const schemaFile = JSON.parse(content);
|
|
299
|
+
if (!schemaFile.$schema) {
|
|
300
|
+
throw new Error(`File ${schemaFilePath} is not a complete schema file (missing $schema field).`);
|
|
301
|
+
}
|
|
302
|
+
const schemaName = schemaFile.metadata?.name;
|
|
303
|
+
const version = schemaFile.version;
|
|
304
|
+
if (!schemaName) {
|
|
305
|
+
throw new Error('Schema name is required in metadata.name');
|
|
306
|
+
}
|
|
307
|
+
// Convert to JsonImportSchema format for processing
|
|
308
|
+
// Remove id fields for JsonImportSchema format (they're not part of the import format)
|
|
309
|
+
const importData = {
|
|
310
|
+
name: schemaName,
|
|
311
|
+
models: Object.fromEntries(Object.entries(schemaFile.models).map(([modelName, model]) => [
|
|
312
|
+
modelName,
|
|
313
|
+
{
|
|
314
|
+
description: model.description,
|
|
315
|
+
properties: Object.fromEntries(Object.entries(model.properties).map(([propName, prop]) => [
|
|
316
|
+
propName,
|
|
317
|
+
{
|
|
318
|
+
type: prop.type || 'string', // Ensure type is present
|
|
319
|
+
...prop,
|
|
320
|
+
// Remove id field for import format (not part of JsonImportSchema)
|
|
321
|
+
},
|
|
322
|
+
])),
|
|
323
|
+
indexes: model.indexes,
|
|
324
|
+
},
|
|
325
|
+
])),
|
|
326
|
+
};
|
|
327
|
+
// Generate schema ID if missing
|
|
328
|
+
if (!schemaFile.id) {
|
|
329
|
+
schemaFile.id = main_cjs.generateId();
|
|
330
|
+
logger$d('Generated schema ID for schema:', schemaFile.id);
|
|
331
|
+
}
|
|
332
|
+
// Extract schemaFileIds from JSON file and generate missing ones BEFORE creating models
|
|
333
|
+
// This ensures Model instances are created with correct IDs
|
|
334
|
+
const modelFileIds = new Map();
|
|
335
|
+
const propertyFileIds = new Map();
|
|
336
|
+
for (const [modelName, model] of Object.entries(schemaFile.models)) {
|
|
337
|
+
// Generate model ID if missing
|
|
338
|
+
if (!model.id) {
|
|
339
|
+
model.id = main_cjs.generateId();
|
|
340
|
+
logger$d(`Generated model ID for ${modelName}:`, model.id);
|
|
341
|
+
}
|
|
342
|
+
modelFileIds.set(modelName, model.id);
|
|
343
|
+
const propIds = new Map();
|
|
344
|
+
for (const [propName, prop] of Object.entries(model.properties)) {
|
|
345
|
+
// Generate property ID if missing
|
|
346
|
+
if (!prop.id) {
|
|
347
|
+
prop.id = main_cjs.generateId();
|
|
348
|
+
logger$d(`Generated property ID for ${modelName}.${propName}:`, prop.id);
|
|
349
|
+
}
|
|
350
|
+
propIds.set(propName, prop.id);
|
|
351
|
+
}
|
|
352
|
+
if (propIds.size > 0) {
|
|
353
|
+
propertyFileIds.set(modelName, propIds);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
// Convert JSON models to Model classes, passing modelFileIds and propertyFileIds so Model instances use correct IDs
|
|
357
|
+
const modelDefinitions = await main_cjs.createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
358
|
+
// Convert schema file metadata to schema input for database
|
|
359
|
+
const schemaInput = {
|
|
360
|
+
name: schemaName,
|
|
361
|
+
version,
|
|
362
|
+
schemaFileId: schemaFile.id || null,
|
|
363
|
+
schemaData: null,
|
|
364
|
+
isDraft: false,
|
|
365
|
+
isEdited: false,
|
|
366
|
+
createdAt: new Date(schemaFile.metadata.createdAt).getTime(),
|
|
367
|
+
updatedAt: new Date(schemaFile.metadata.updatedAt).getTime(),
|
|
368
|
+
};
|
|
369
|
+
// Use dynamic import to break circular dependency
|
|
370
|
+
const { addSchemaToDb, addModelsToDb } = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.db; });
|
|
371
|
+
// Add schema to database with schemaFileId
|
|
372
|
+
const schemaRecord = await addSchemaToDb(schemaInput, schemaFile.id);
|
|
373
|
+
// Add models to database with model renames handled and schemaFileIds
|
|
374
|
+
await addModelsToDb(modelDefinitions, schemaRecord, modelRenames, {
|
|
375
|
+
schemaFileId: schemaFile.id,
|
|
376
|
+
modelFileIds,
|
|
377
|
+
propertyFileIds,
|
|
378
|
+
});
|
|
379
|
+
// Models are now Model instances, no registration needed
|
|
380
|
+
// They should be created via Model.create() and are accessible via Model static methods
|
|
381
|
+
for (const [modelName] of Object.entries(modelDefinitions)) {
|
|
382
|
+
logger$d('loadSchemaWithRenames - model available:', modelName);
|
|
383
|
+
}
|
|
384
|
+
return schemaFilePath;
|
|
385
|
+
}
|
|
36
386
|
|
|
37
387
|
const logger$c = debug('seedSdk:schema:actors:loadOrCreateSchema');
|
|
38
388
|
/**
|
|
@@ -48,14 +398,13 @@ const getModelIdsForSchema$1 = async (schemaId) => {
|
|
|
48
398
|
return [];
|
|
49
399
|
}
|
|
50
400
|
try {
|
|
51
|
-
const { modelSchemas, models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.schema; });
|
|
52
401
|
const modelRecords = await db
|
|
53
402
|
.select({
|
|
54
|
-
modelFileId:
|
|
403
|
+
modelFileId: main_cjs.models.schemaFileId,
|
|
55
404
|
})
|
|
56
|
-
.from(modelSchemas)
|
|
57
|
-
.innerJoin(
|
|
58
|
-
.where(drizzleOrm.eq(modelSchemas.schemaId, schemaId));
|
|
405
|
+
.from(main_cjs.modelSchemas)
|
|
406
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
407
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, schemaId));
|
|
59
408
|
const modelIds = modelRecords
|
|
60
409
|
.map((row) => row.modelFileId)
|
|
61
410
|
.filter((id) => id !== null && id !== undefined);
|
|
@@ -77,7 +426,7 @@ const createModelInstances$2 = async (modelIds) => {
|
|
|
77
426
|
return;
|
|
78
427
|
}
|
|
79
428
|
try {
|
|
80
|
-
const { Model } = await Promise.resolve().then(function () { return require('./index-
|
|
429
|
+
const { Model } = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.Model$1; });
|
|
81
430
|
// Create instances for all model IDs in parallel
|
|
82
431
|
// Model.createById() will check cache first, then query DB and create if needed
|
|
83
432
|
const createPromises = modelIds.map(async (modelFileId) => {
|
|
@@ -117,13 +466,11 @@ const verifyPropertiesPersisted = async (modelId, modelName, maxRetries = 10, re
|
|
|
117
466
|
if (!db) {
|
|
118
467
|
throw new Error('Database not available for property verification');
|
|
119
468
|
}
|
|
120
|
-
const { properties: propertiesTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchema; });
|
|
121
|
-
const { eq } = await import('drizzle-orm');
|
|
122
469
|
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
123
470
|
const props = await db
|
|
124
471
|
.select()
|
|
125
|
-
.from(
|
|
126
|
-
.where(eq(
|
|
472
|
+
.from(main_cjs.properties)
|
|
473
|
+
.where(drizzleOrm.eq(main_cjs.properties.modelId, modelId))
|
|
127
474
|
.limit(1);
|
|
128
475
|
if (props.length > 0) {
|
|
129
476
|
logger$c(`Verified properties exist for model "${modelName}" (modelId: ${modelId}) after ${attempt + 1} attempt(s)`);
|
|
@@ -185,15 +532,14 @@ const getSchemaFilePath$1 = (name, version, schemaFileId) => {
|
|
|
185
532
|
const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }) => {
|
|
186
533
|
const _loadOrCreateSchema = async () => {
|
|
187
534
|
const { schemaName } = context;
|
|
188
|
-
// Check if this is an internal SDK schema (should not create files in app directory)
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
if (isInternal && schemaName === SEED_PROTOCOL_SCHEMA_NAME) {
|
|
535
|
+
// Check if this is an internal SDK schema (should not create files in app directory) — use static import so consumer bundles resolve correctly
|
|
536
|
+
const isInternal = main_cjs.isInternalSchema(schemaName);
|
|
537
|
+
if (isInternal && schemaName === main_cjs.SEED_PROTOCOL_SCHEMA_NAME) {
|
|
192
538
|
// For Seed Protocol, always load from internal file, never create new
|
|
193
539
|
logger$c(`Loading internal Seed Protocol schema from SDK`);
|
|
194
540
|
console.log('[loadOrCreateSchema] Loading internal Seed Protocol schema from SDK');
|
|
195
541
|
try {
|
|
196
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
542
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
197
543
|
const schemaFile = internalSchema.default;
|
|
198
544
|
console.log('[loadOrCreateSchema] Loaded schema file, models count:', Object.keys(schemaFile.models || {}).length);
|
|
199
545
|
// Check if it exists in database, if not, add it
|
|
@@ -218,8 +564,6 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
218
564
|
logger$c(`Added Seed Protocol schema to database`);
|
|
219
565
|
// Also add models and properties to database
|
|
220
566
|
// Convert to JsonImportSchema format for processing
|
|
221
|
-
const { createModelsFromJson } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.json; });
|
|
222
|
-
const { addModelsToDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.db; });
|
|
223
567
|
const importData = {
|
|
224
568
|
name: schemaName,
|
|
225
569
|
models: Object.fromEntries(Object.entries(schemaFile.models || {}).map(([modelName, model]) => [
|
|
@@ -297,10 +641,10 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
297
641
|
}
|
|
298
642
|
}
|
|
299
643
|
// Convert JSON models to Model classes, passing modelFileIds and propertyFileIds so Model instances use correct IDs
|
|
300
|
-
const modelDefinitions = await createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
644
|
+
const modelDefinitions = await main_cjs.createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
301
645
|
// Add models to database and link them to the schema with schemaFileIds
|
|
302
646
|
if (Object.keys(modelDefinitions).length > 0 && schemaRecord) {
|
|
303
|
-
await addModelsToDb(modelDefinitions, schemaRecord, undefined, {
|
|
647
|
+
await main_cjs.addModelsToDb(modelDefinitions, schemaRecord, undefined, {
|
|
304
648
|
schemaFileId: schemaFile.id,
|
|
305
649
|
modelFileIds,
|
|
306
650
|
propertyFileIds,
|
|
@@ -308,16 +652,14 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
308
652
|
logger$c(`Added ${Object.keys(modelDefinitions).length} models and their properties to database for Seed Protocol schema`);
|
|
309
653
|
// Verify properties are persisted (important for browser environments)
|
|
310
654
|
// Query the database to get model IDs that were just created
|
|
311
|
-
const { modelSchemas } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchemaSchema; });
|
|
312
|
-
const { models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchema; });
|
|
313
655
|
const modelLinks = await db
|
|
314
656
|
.select({
|
|
315
|
-
modelId: modelSchemas.modelId,
|
|
316
|
-
modelName:
|
|
657
|
+
modelId: main_cjs.modelSchemas.modelId,
|
|
658
|
+
modelName: main_cjs.models.name,
|
|
317
659
|
})
|
|
318
|
-
.from(modelSchemas)
|
|
319
|
-
.innerJoin(
|
|
320
|
-
.where(drizzleOrm.eq(modelSchemas.schemaId, schemaRecord.id));
|
|
660
|
+
.from(main_cjs.modelSchemas)
|
|
661
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
662
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, schemaRecord.id));
|
|
321
663
|
// Verify properties for at least one model (Seed model if available)
|
|
322
664
|
const seedModelLink = modelLinks.find((link) => link.modelName === 'Seed');
|
|
323
665
|
if (seedModelLink && seedModelLink.modelId) {
|
|
@@ -333,18 +675,15 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
333
675
|
// Schema exists, but always ensure models/properties are in database
|
|
334
676
|
// This handles the case where schema was added but models weren't (from previous code)
|
|
335
677
|
// or where models were added but properties weren't
|
|
336
|
-
const { modelSchemas } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchemaSchema; });
|
|
337
|
-
const { models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchema; });
|
|
338
|
-
const { properties: propertiesTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchema; });
|
|
339
678
|
// Check if models are linked to the schema
|
|
340
679
|
const modelLinks = await db
|
|
341
680
|
.select({
|
|
342
|
-
modelId: modelSchemas.modelId,
|
|
343
|
-
modelName:
|
|
681
|
+
modelId: main_cjs.modelSchemas.modelId,
|
|
682
|
+
modelName: main_cjs.models.name,
|
|
344
683
|
})
|
|
345
|
-
.from(modelSchemas)
|
|
346
|
-
.innerJoin(
|
|
347
|
-
.where(drizzleOrm.eq(modelSchemas.schemaId, schemaRecord.id));
|
|
684
|
+
.from(main_cjs.modelSchemas)
|
|
685
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
686
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, schemaRecord.id));
|
|
348
687
|
// Check if we have all expected models
|
|
349
688
|
const expectedModelNames = Object.keys(schemaFile.models || {});
|
|
350
689
|
const linkedModelNames = modelLinks
|
|
@@ -359,8 +698,8 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
359
698
|
if (link.modelId) {
|
|
360
699
|
const props = await db
|
|
361
700
|
.select()
|
|
362
|
-
.from(
|
|
363
|
-
.where(drizzleOrm.eq(
|
|
701
|
+
.from(main_cjs.properties)
|
|
702
|
+
.where(drizzleOrm.eq(main_cjs.properties.modelId, link.modelId))
|
|
364
703
|
.limit(1);
|
|
365
704
|
if (props.length === 0) {
|
|
366
705
|
missingProperties = true;
|
|
@@ -374,8 +713,6 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
374
713
|
if (missingModels.length > 0 || missingProperties || modelLinks.length === 0) {
|
|
375
714
|
logger$c(`Seed Protocol schema exists but models/properties incomplete (missing models: ${missingModels.length}, missing properties: ${missingProperties}), adding them now`);
|
|
376
715
|
console.log(`[loadOrCreateSchema] Adding models/properties: missingModels=${missingModels.length}, missingProperties=${missingProperties}, modelLinks=${modelLinks.length}`);
|
|
377
|
-
const { createModelsFromJson } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.json; });
|
|
378
|
-
const { addModelsToDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.db; });
|
|
379
716
|
// Convert SchemaFileFormat to JsonImportSchema format
|
|
380
717
|
// Schema format: { dataType, ref, refValueType, storageType, localStorageDir, filenameSuffix }
|
|
381
718
|
// JSON import format: { type, model, items, storage: { type, path, extension } }
|
|
@@ -449,7 +786,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
449
786
|
}
|
|
450
787
|
}
|
|
451
788
|
// Convert JSON models to Model classes, passing modelFileIds and propertyFileIds so Model instances use correct IDs
|
|
452
|
-
const modelDefinitions = await createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
789
|
+
const modelDefinitions = await main_cjs.createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
453
790
|
console.log(`[loadOrCreateSchema] createModelsFromJson returned ${Object.keys(modelDefinitions).length} models: ${Object.keys(modelDefinitions).join(', ')}`);
|
|
454
791
|
console.log(`[loadOrCreateSchema] Expected ${expectedModelNames.length} models from schema file: ${expectedModelNames.join(', ')}`);
|
|
455
792
|
// Verify all expected models are in modelDefinitions
|
|
@@ -459,7 +796,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
459
796
|
}
|
|
460
797
|
if (Object.keys(modelDefinitions).length > 0) {
|
|
461
798
|
console.log(`[loadOrCreateSchema] Calling addModelsToDb with ${Object.keys(modelDefinitions).length} models: ${Object.keys(modelDefinitions).join(', ')}`);
|
|
462
|
-
await addModelsToDb(modelDefinitions, schemaRecord, undefined, {
|
|
799
|
+
await main_cjs.addModelsToDb(modelDefinitions, schemaRecord, undefined, {
|
|
463
800
|
schemaFileId: schemaFile.id,
|
|
464
801
|
modelFileIds,
|
|
465
802
|
propertyFileIds,
|
|
@@ -478,12 +815,12 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
478
815
|
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
479
816
|
updatedModelLinks = await db
|
|
480
817
|
.select({
|
|
481
|
-
modelId: modelSchemas.modelId,
|
|
482
|
-
modelName:
|
|
818
|
+
modelId: main_cjs.modelSchemas.modelId,
|
|
819
|
+
modelName: main_cjs.models.name,
|
|
483
820
|
})
|
|
484
|
-
.from(modelSchemas)
|
|
485
|
-
.innerJoin(
|
|
486
|
-
.where(drizzleOrm.eq(modelSchemas.schemaId, schemaRecord.id));
|
|
821
|
+
.from(main_cjs.modelSchemas)
|
|
822
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
823
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, schemaRecord.id));
|
|
487
824
|
const linkedModelNames = updatedModelLinks
|
|
488
825
|
.map((link) => link.modelName)
|
|
489
826
|
.filter((n) => n !== null);
|
|
@@ -702,7 +1039,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
702
1039
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
703
1040
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
704
1041
|
try {
|
|
705
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1042
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
706
1043
|
const internalSchemaFile = internalSchema.default;
|
|
707
1044
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
708
1045
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -897,7 +1234,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
897
1234
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
898
1235
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
899
1236
|
try {
|
|
900
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1237
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
901
1238
|
const internalSchemaFile = internalSchema.default;
|
|
902
1239
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
903
1240
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -969,7 +1306,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
969
1306
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
970
1307
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
971
1308
|
try {
|
|
972
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1309
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
973
1310
|
const internalSchemaFile = internalSchema.default;
|
|
974
1311
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
975
1312
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -1038,7 +1375,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
1038
1375
|
// If schemaData is missing, try to load from internal schema file for seed-protocol
|
|
1039
1376
|
if (!dbSchema.schemaData && schemaName === 'Seed Protocol') {
|
|
1040
1377
|
try {
|
|
1041
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1378
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
1042
1379
|
const schemaFile = internalSchema.default;
|
|
1043
1380
|
logger$c(`Found seed-protocol schema in internal file (schemaData missing, using internal schema)`);
|
|
1044
1381
|
// Update database with schemaData for future loads
|
|
@@ -1147,7 +1484,7 @@ const loadOrCreateSchema = xstate.fromCallback(({ sendBack, input: { context } }
|
|
|
1147
1484
|
// Ensure models are populated (fallback for seed-protocol if missing)
|
|
1148
1485
|
if ((!mergedModels || Object.keys(mergedModels).length === 0) && schemaName === 'Seed Protocol') {
|
|
1149
1486
|
try {
|
|
1150
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
1487
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
1151
1488
|
const internalSchemaFile = internalSchema.default;
|
|
1152
1489
|
mergedModels = { ...(internalSchemaFile.models || {}) };
|
|
1153
1490
|
logger$c(`Populated models for seed-protocol schema from internal file`);
|
|
@@ -1463,18 +1800,15 @@ const addModelsMachine = xstate.setup({
|
|
|
1463
1800
|
}),
|
|
1464
1801
|
createModelInstances: xstate.fromCallback(({ sendBack, input }) => {
|
|
1465
1802
|
const _createInstances = async () => {
|
|
1466
|
-
const { Schema } = await Promise.resolve().then(function () { return Schema$1; });
|
|
1467
|
-
const { BaseDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.BaseDb$1; });
|
|
1468
|
-
const { models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchema; });
|
|
1469
|
-
const { eq } = await import('drizzle-orm');
|
|
1470
|
-
const { generateId } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.index; });
|
|
1471
1803
|
const debug = (await import('debug')).default;
|
|
1472
1804
|
const logger = debug('seedSdk:schema:addModels:createInstances');
|
|
1805
|
+
const { Schema: SchemaClass, schemaInstanceState } = await Promise.resolve().then(function () { return Schema$1; });
|
|
1473
1806
|
const schemaName = input.schemaContext.metadata?.name || input.schemaContext.schemaName;
|
|
1474
|
-
const schemaInstance =
|
|
1807
|
+
const schemaInstance = SchemaClass.create(schemaName, {
|
|
1808
|
+
waitForReady: false,
|
|
1809
|
+
});
|
|
1475
1810
|
const modelInstances = new Map();
|
|
1476
1811
|
// Get instance state to store model instances
|
|
1477
|
-
const { schemaInstanceState } = await Promise.resolve().then(function () { return Schema$1; });
|
|
1478
1812
|
const instanceState = schemaInstanceState.get(schemaInstance);
|
|
1479
1813
|
if (!instanceState) {
|
|
1480
1814
|
throw new Error('Schema instance state not found');
|
|
@@ -1485,12 +1819,12 @@ const addModelsMachine = xstate.setup({
|
|
|
1485
1819
|
// Look up modelFileId from database BEFORE creating the Model instance
|
|
1486
1820
|
let modelFileId = undefined;
|
|
1487
1821
|
try {
|
|
1488
|
-
const db = BaseDb.getAppDb();
|
|
1822
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
1489
1823
|
if (db) {
|
|
1490
1824
|
const dbModels = await db
|
|
1491
1825
|
.select()
|
|
1492
|
-
.from(
|
|
1493
|
-
.where(eq(
|
|
1826
|
+
.from(main_cjs.models)
|
|
1827
|
+
.where(drizzleOrm.eq(main_cjs.models.name, modelName))
|
|
1494
1828
|
.limit(1);
|
|
1495
1829
|
if (dbModels.length > 0 && dbModels[0].schemaFileId) {
|
|
1496
1830
|
modelFileId = dbModels[0].schemaFileId;
|
|
@@ -1503,12 +1837,15 @@ const addModelsMachine = xstate.setup({
|
|
|
1503
1837
|
}
|
|
1504
1838
|
// If not found in database, generate a new modelFileId for new models
|
|
1505
1839
|
if (!modelFileId) {
|
|
1506
|
-
modelFileId = generateId();
|
|
1840
|
+
modelFileId = main_cjs.generateId();
|
|
1507
1841
|
logger(`Generated new modelFileId "${modelFileId}" for new model "${modelName}"`);
|
|
1508
1842
|
}
|
|
1509
1843
|
// Create new Model instance with modelFileId
|
|
1510
1844
|
// Model.create() will set _modelFileId in the context automatically
|
|
1511
|
-
const modelInstance = main_cjs.Model.create(modelName, schemaName, {
|
|
1845
|
+
const modelInstance = main_cjs.Model.create(modelName, schemaName, {
|
|
1846
|
+
modelFileId,
|
|
1847
|
+
waitForReady: false,
|
|
1848
|
+
});
|
|
1512
1849
|
const service = modelInstance.getService();
|
|
1513
1850
|
logger(`Created Model instance for "${modelName}" with modelFileId "${modelFileId}"`);
|
|
1514
1851
|
// Wait for the Model service to finish loading (loadOrCreateModel completes)
|
|
@@ -1689,11 +2026,6 @@ const addModelsMachine = xstate.setup({
|
|
|
1689
2026
|
const _persist = async () => {
|
|
1690
2027
|
const debug = (await import('debug')).default;
|
|
1691
2028
|
const logger = debug('seedSdk:schema:addModels:persist');
|
|
1692
|
-
const { createModelFromJson } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.json; });
|
|
1693
|
-
const { addModelsToDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.db; });
|
|
1694
|
-
const { BaseDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.BaseDb$1; });
|
|
1695
|
-
const { schemas: schemasTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.SchemaSchema; });
|
|
1696
|
-
const { eq } = await import('drizzle-orm');
|
|
1697
2029
|
// Only process in browser environment where store is available
|
|
1698
2030
|
if (typeof window === 'undefined') {
|
|
1699
2031
|
logger('Not in browser environment, skipping store update');
|
|
@@ -1705,7 +2037,7 @@ const addModelsMachine = xstate.setup({
|
|
|
1705
2037
|
}
|
|
1706
2038
|
const schemaName = input.schemaContext.metadata?.name || input.schemaContext.schemaName;
|
|
1707
2039
|
const schemaFileId = input.schemaContext.id; // id is the schemaFileId (string) in SchemaMachineContext
|
|
1708
|
-
const db = BaseDb.getAppDb();
|
|
2040
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
1709
2041
|
if (!db) {
|
|
1710
2042
|
logger('Database not found, skipping model store update');
|
|
1711
2043
|
sendBack({
|
|
@@ -1729,8 +2061,8 @@ const addModelsMachine = xstate.setup({
|
|
|
1729
2061
|
logger(`Looking up schema by schemaFileId: ${schemaFileId} (attempt ${attempt + 1})`);
|
|
1730
2062
|
const schemasById = await db
|
|
1731
2063
|
.select()
|
|
1732
|
-
.from(
|
|
1733
|
-
.where(eq(
|
|
2064
|
+
.from(main_cjs.schemas)
|
|
2065
|
+
.where(drizzleOrm.eq(main_cjs.schemas.schemaFileId, schemaFileId))
|
|
1734
2066
|
.limit(1);
|
|
1735
2067
|
if (schemasById.length > 0) {
|
|
1736
2068
|
schemaRecord = schemasById[0];
|
|
@@ -1746,12 +2078,11 @@ const addModelsMachine = xstate.setup({
|
|
|
1746
2078
|
// FALLBACK: If not found by schemaFileId, try by name (prefer draft records)
|
|
1747
2079
|
if (!schemaRecord) {
|
|
1748
2080
|
logger(`Looking up schema by name: "${schemaName}" (attempt ${attempt + 1})`);
|
|
1749
|
-
const { desc } = await import('drizzle-orm');
|
|
1750
2081
|
const schemasByName = await db
|
|
1751
2082
|
.select()
|
|
1752
|
-
.from(
|
|
1753
|
-
.where(eq(
|
|
1754
|
-
.orderBy(desc(
|
|
2083
|
+
.from(main_cjs.schemas)
|
|
2084
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, schemaName))
|
|
2085
|
+
.orderBy(drizzleOrm.desc(main_cjs.schemas.isDraft), drizzleOrm.desc(main_cjs.schemas.version))
|
|
1755
2086
|
.limit(10); // Get multiple to find drafts
|
|
1756
2087
|
if (schemasByName.length > 0) {
|
|
1757
2088
|
// Prefer draft records
|
|
@@ -1813,14 +2144,14 @@ const addModelsMachine = xstate.setup({
|
|
|
1813
2144
|
properties: convertedProperties,
|
|
1814
2145
|
};
|
|
1815
2146
|
// Create Model instance
|
|
1816
|
-
const ModelClass = await createModelFromJson(modelName, jsonModelDef, schemaName);
|
|
2147
|
+
const ModelClass = await main_cjs.createModelFromJson(modelName, jsonModelDef, schemaName);
|
|
1817
2148
|
modelDefinitions[modelName] = ModelClass;
|
|
1818
2149
|
// Model is now accessible via Model static methods, no registration needed
|
|
1819
2150
|
logger(`Created model "${modelName}"`);
|
|
1820
2151
|
}
|
|
1821
2152
|
// Add models to database with modelFileIds
|
|
1822
2153
|
if (Object.keys(modelDefinitions).length > 0) {
|
|
1823
|
-
await addModelsToDb(modelDefinitions, schemaRecord, undefined, {
|
|
2154
|
+
await main_cjs.addModelsToDb(modelDefinitions, schemaRecord, undefined, {
|
|
1824
2155
|
schemaFileId: input.schemaContext.id, // id is the schemaFileId (string) in SchemaMachineContext
|
|
1825
2156
|
modelFileIds: input.modelFileIds.size > 0 ? input.modelFileIds : undefined,
|
|
1826
2157
|
});
|
|
@@ -2007,14 +2338,13 @@ const getModelIdsForSchema = async (schemaId) => {
|
|
|
2007
2338
|
return [];
|
|
2008
2339
|
}
|
|
2009
2340
|
try {
|
|
2010
|
-
const { modelSchemas, models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.schema; });
|
|
2011
2341
|
const modelRecords = await db
|
|
2012
2342
|
.select({
|
|
2013
|
-
modelFileId:
|
|
2343
|
+
modelFileId: main_cjs.models.schemaFileId,
|
|
2014
2344
|
})
|
|
2015
|
-
.from(modelSchemas)
|
|
2016
|
-
.innerJoin(
|
|
2017
|
-
.where(drizzleOrm.eq(modelSchemas.schemaId, schemaId));
|
|
2345
|
+
.from(main_cjs.modelSchemas)
|
|
2346
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
2347
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, schemaId));
|
|
2018
2348
|
const modelIds = modelRecords
|
|
2019
2349
|
.map((row) => row.modelFileId)
|
|
2020
2350
|
.filter((id) => id !== null && id !== undefined);
|
|
@@ -2051,7 +2381,12 @@ const createModelInstances$1 = async (modelIds) => {
|
|
|
2051
2381
|
return;
|
|
2052
2382
|
}
|
|
2053
2383
|
try {
|
|
2054
|
-
const
|
|
2384
|
+
const mod = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.Model$1; });
|
|
2385
|
+
const Model = mod?.Model ?? mod?.default;
|
|
2386
|
+
if (!Model) {
|
|
2387
|
+
logger$b('Model not available from dynamic import');
|
|
2388
|
+
return;
|
|
2389
|
+
}
|
|
2055
2390
|
const createPromises = modelIds.map(async (modelFileId) => {
|
|
2056
2391
|
try {
|
|
2057
2392
|
const model = await Model.createById(modelFileId);
|
|
@@ -2086,13 +2421,12 @@ const checkExistingSchema = xstate.fromCallback(({ sendBack, input: { context }
|
|
|
2086
2421
|
});
|
|
2087
2422
|
return;
|
|
2088
2423
|
}
|
|
2089
|
-
// Check if this is an internal SDK schema (Seed Protocol)
|
|
2090
|
-
const
|
|
2091
|
-
|
|
2092
|
-
if (isInternal && schemaName === SEED_PROTOCOL_SCHEMA_NAME) {
|
|
2424
|
+
// Check if this is an internal SDK schema (Seed Protocol) — use static import so consumer bundles resolve correctly
|
|
2425
|
+
const isInternal = main_cjs.isInternalSchema(schemaName);
|
|
2426
|
+
if (isInternal && schemaName === main_cjs.SEED_PROTOCOL_SCHEMA_NAME) {
|
|
2093
2427
|
// For Seed Protocol, check if it exists in database
|
|
2094
2428
|
try {
|
|
2095
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
2429
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
2096
2430
|
const schemaFile = internalSchema.default;
|
|
2097
2431
|
if (db && schemaFile.id) {
|
|
2098
2432
|
const existing = await db
|
|
@@ -2443,15 +2777,14 @@ const writeSchemaToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2443
2777
|
const _write = async () => {
|
|
2444
2778
|
const { schemaName, schemaFile, existingDbSchema } = input;
|
|
2445
2779
|
try {
|
|
2446
|
-
// Check if this is an internal SDK schema (Seed Protocol)
|
|
2447
|
-
const
|
|
2448
|
-
const isInternal = isInternalSchema(schemaName);
|
|
2780
|
+
// Check if this is an internal SDK schema (Seed Protocol) — use static import so consumer bundles resolve correctly
|
|
2781
|
+
const isInternal = main_cjs.isInternalSchema(schemaName);
|
|
2449
2782
|
let finalSchema;
|
|
2450
2783
|
let schemaRecord;
|
|
2451
|
-
if (isInternal && schemaName === SEED_PROTOCOL_SCHEMA_NAME) {
|
|
2784
|
+
if (isInternal && schemaName === main_cjs.SEED_PROTOCOL_SCHEMA_NAME) {
|
|
2452
2785
|
// For Seed Protocol, load from internal file
|
|
2453
2786
|
logger$a(`Loading internal Seed Protocol schema from SDK`);
|
|
2454
|
-
const internalSchema = await Promise.resolve().then(function () { return require('./index-
|
|
2787
|
+
const internalSchema = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.SEEDPROTOCOL_Seed_Protocol_v1; });
|
|
2455
2788
|
finalSchema = internalSchema.default;
|
|
2456
2789
|
const db = main_cjs.BaseDb.getAppDb();
|
|
2457
2790
|
if (db && finalSchema.id) {
|
|
@@ -2647,23 +2980,19 @@ const writeModelsToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2647
2980
|
const { schema, schemaRecord, schemaName } = input;
|
|
2648
2981
|
try {
|
|
2649
2982
|
// Check if models already exist in database
|
|
2650
|
-
const
|
|
2651
|
-
const { modelSchemas } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchemaSchema; });
|
|
2652
|
-
const { models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchema; });
|
|
2653
|
-
const { eq } = await import('drizzle-orm');
|
|
2654
|
-
const db = BaseDb.getAppDb();
|
|
2983
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
2655
2984
|
if (!db) {
|
|
2656
2985
|
throw new Error('Database not available');
|
|
2657
2986
|
}
|
|
2658
2987
|
// Check if models are linked to the schema
|
|
2659
2988
|
const modelLinks = await db
|
|
2660
2989
|
.select({
|
|
2661
|
-
modelId: modelSchemas.modelId,
|
|
2662
|
-
modelName:
|
|
2990
|
+
modelId: main_cjs.modelSchemas.modelId,
|
|
2991
|
+
modelName: main_cjs.models.name,
|
|
2663
2992
|
})
|
|
2664
|
-
.from(modelSchemas)
|
|
2665
|
-
.innerJoin(
|
|
2666
|
-
.where(eq(modelSchemas.schemaId, schemaRecord.id));
|
|
2993
|
+
.from(main_cjs.modelSchemas)
|
|
2994
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
2995
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, schemaRecord.id));
|
|
2667
2996
|
// Check if we have all expected models
|
|
2668
2997
|
const expectedModelNames = Object.keys(schema.models || {});
|
|
2669
2998
|
const linkedModelNames = modelLinks
|
|
@@ -2678,9 +3007,9 @@ const writeModelsToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2678
3007
|
for (const link of modelLinks) {
|
|
2679
3008
|
if (link.modelId) {
|
|
2680
3009
|
const modelRecord = await db
|
|
2681
|
-
.select({ schemaFileId:
|
|
2682
|
-
.from(
|
|
2683
|
-
.where(eq(
|
|
3010
|
+
.select({ schemaFileId: main_cjs.models.schemaFileId })
|
|
3011
|
+
.from(main_cjs.models)
|
|
3012
|
+
.where(drizzleOrm.eq(main_cjs.models.id, link.modelId))
|
|
2684
3013
|
.limit(1);
|
|
2685
3014
|
if (modelRecord.length > 0 && modelRecord[0].schemaFileId) {
|
|
2686
3015
|
modelFileIds.push(modelRecord[0].schemaFileId);
|
|
@@ -2693,13 +3022,10 @@ const writeModelsToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2693
3022
|
});
|
|
2694
3023
|
return;
|
|
2695
3024
|
}
|
|
2696
|
-
//
|
|
2697
|
-
const
|
|
2698
|
-
// Check if this is Seed Protocol schema (has different format)
|
|
2699
|
-
const { isInternalSchema, SEED_PROTOCOL_SCHEMA_NAME } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.constants$1; });
|
|
2700
|
-
const isInternal = isInternalSchema(schemaName);
|
|
3025
|
+
// Check if this is Seed Protocol schema (has different format) — use static import so consumer bundles resolve correctly
|
|
3026
|
+
const isInternal = main_cjs.isInternalSchema(schemaName);
|
|
2701
3027
|
let importData;
|
|
2702
|
-
if (isInternal && schemaName === SEED_PROTOCOL_SCHEMA_NAME) {
|
|
3028
|
+
if (isInternal && schemaName === main_cjs.SEED_PROTOCOL_SCHEMA_NAME) {
|
|
2703
3029
|
// Convert Seed Protocol schema format to JSON import format
|
|
2704
3030
|
importData = {
|
|
2705
3031
|
name: schemaName,
|
|
@@ -2782,7 +3108,7 @@ const writeModelsToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2782
3108
|
}
|
|
2783
3109
|
}
|
|
2784
3110
|
// Convert JSON models to Model classes
|
|
2785
|
-
const modelDefinitions = await createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
3111
|
+
const modelDefinitions = await main_cjs.createModelsFromJson(importData, modelFileIds, propertyFileIds);
|
|
2786
3112
|
// Add models to database
|
|
2787
3113
|
if (Object.keys(modelDefinitions).length > 0) {
|
|
2788
3114
|
await main_cjs.addModelsToDb(modelDefinitions, schemaRecord, undefined, {
|
|
@@ -2926,7 +3252,7 @@ const createModelInstances = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
2926
3252
|
return;
|
|
2927
3253
|
}
|
|
2928
3254
|
try {
|
|
2929
|
-
const { Model } = await Promise.resolve().then(function () { return require('./index-
|
|
3255
|
+
const { Model } = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.Model$1; });
|
|
2930
3256
|
// Create instances for all model IDs in parallel
|
|
2931
3257
|
// Model.createById() will check cache first, then query DB and create if needed
|
|
2932
3258
|
const createPromises = modelIds.map(async (modelFileId) => {
|
|
@@ -3011,7 +3337,7 @@ const verifyModelInstancesInCache = xstate.fromCallback(({ sendBack, input }) =>
|
|
|
3011
3337
|
}
|
|
3012
3338
|
try {
|
|
3013
3339
|
const result = await verifyWithRetry$2(async () => {
|
|
3014
|
-
const { Model } = await Promise.resolve().then(function () { return require('./index-
|
|
3340
|
+
const { Model } = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.Model$1; });
|
|
3015
3341
|
// Check each model ID in the cache
|
|
3016
3342
|
const verifiedInstances = [];
|
|
3017
3343
|
const missingIds = [];
|
|
@@ -3067,10 +3393,9 @@ const writePropertiesToDb = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
3067
3393
|
const _write = async () => {
|
|
3068
3394
|
const { modelIds } = input;
|
|
3069
3395
|
try {
|
|
3070
|
-
const {
|
|
3071
|
-
const { models: modelsTable, properties: propertiesTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ModelSchema; });
|
|
3396
|
+
const { models: modelsTable, properties: propertiesTable } = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.ModelSchema; });
|
|
3072
3397
|
const { eq } = await import('drizzle-orm');
|
|
3073
|
-
const db = BaseDb.getAppDb();
|
|
3398
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
3074
3399
|
if (!db) {
|
|
3075
3400
|
throw new Error('Database not available');
|
|
3076
3401
|
}
|
|
@@ -3254,8 +3579,15 @@ const createPropertyInstances = xstate.fromCallback(({ sendBack, input }) => {
|
|
|
3254
3579
|
return;
|
|
3255
3580
|
}
|
|
3256
3581
|
try {
|
|
3257
|
-
const
|
|
3258
|
-
const
|
|
3582
|
+
const modProp = await Promise.resolve().then(function () { return require('./ModelProperty-BeJvgKMw.js'); });
|
|
3583
|
+
const ModelProperty = modProp?.ModelProperty ?? modProp?.default;
|
|
3584
|
+
const modModel = await Promise.resolve().then(function () { return require('./index-BeKPbbk0.js'); }).then(function (n) { return n.Model$1; });
|
|
3585
|
+
const Model = modModel?.Model ?? modModel?.default;
|
|
3586
|
+
if (!ModelProperty || !Model) {
|
|
3587
|
+
logger$2('ModelProperty or Model not available from dynamic import');
|
|
3588
|
+
sendBack({ type: 'instancesCreated', count: 0 });
|
|
3589
|
+
return;
|
|
3590
|
+
}
|
|
3259
3591
|
// Properties are typically loaded when Model instances are loaded
|
|
3260
3592
|
// But we can verify they exist by checking Model instances
|
|
3261
3593
|
let successCount = 0;
|
|
@@ -3354,7 +3686,11 @@ const verifyPropertyInstancesInCache = xstate.fromCallback(({ sendBack, input })
|
|
|
3354
3686
|
}
|
|
3355
3687
|
try {
|
|
3356
3688
|
const result = await verifyWithRetry(async () => {
|
|
3357
|
-
const
|
|
3689
|
+
const mod = await Promise.resolve().then(function () { return require('./ModelProperty-BeJvgKMw.js'); });
|
|
3690
|
+
const ModelProperty = mod?.ModelProperty ?? mod?.default;
|
|
3691
|
+
if (!ModelProperty) {
|
|
3692
|
+
throw new Error('ModelProperty not available from dynamic import');
|
|
3693
|
+
}
|
|
3358
3694
|
// Check each property ID in the cache
|
|
3359
3695
|
const verifiedInstances = [];
|
|
3360
3696
|
const missingIds = [];
|
|
@@ -3514,6 +3850,23 @@ const schemaMachine = xstate.setup({
|
|
|
3514
3850
|
return newContext;
|
|
3515
3851
|
}),
|
|
3516
3852
|
},
|
|
3853
|
+
destroyStarted: {
|
|
3854
|
+
actions: xstate.assign({ _destroyInProgress: true, _destroyError: null }),
|
|
3855
|
+
},
|
|
3856
|
+
destroyDone: {
|
|
3857
|
+
actions: xstate.assign({ _destroyInProgress: false }),
|
|
3858
|
+
},
|
|
3859
|
+
destroyError: {
|
|
3860
|
+
actions: xstate.assign(({ event }) => ({
|
|
3861
|
+
_destroyInProgress: false,
|
|
3862
|
+
_destroyError: event.error instanceof Error
|
|
3863
|
+
? { message: event.error.message, name: event.error.name }
|
|
3864
|
+
: { message: String(event.error) },
|
|
3865
|
+
})),
|
|
3866
|
+
},
|
|
3867
|
+
clearDestroyError: {
|
|
3868
|
+
actions: xstate.assign({ _destroyError: null }),
|
|
3869
|
+
},
|
|
3517
3870
|
},
|
|
3518
3871
|
states: {
|
|
3519
3872
|
loading: {
|
|
@@ -4107,10 +4460,12 @@ class Schema {
|
|
|
4107
4460
|
});
|
|
4108
4461
|
// Note: Property getters/setters are now handled by the Proxy in create()
|
|
4109
4462
|
}
|
|
4110
|
-
static create(schemaName) {
|
|
4463
|
+
static create(schemaName, options) {
|
|
4111
4464
|
if (!schemaName) {
|
|
4112
4465
|
throw new Error('Schema name is required');
|
|
4113
4466
|
}
|
|
4467
|
+
const waitForReady = options?.waitForReady !== false;
|
|
4468
|
+
const readyTimeout = options?.readyTimeout ?? 5000;
|
|
4114
4469
|
// First, check if we have an instance cached by name
|
|
4115
4470
|
if (this.instanceCacheByName.has(schemaName)) {
|
|
4116
4471
|
const { instance, refCount } = this.instanceCacheByName.get(schemaName);
|
|
@@ -4118,7 +4473,9 @@ class Schema {
|
|
|
4118
4473
|
instance,
|
|
4119
4474
|
refCount: refCount + 1,
|
|
4120
4475
|
});
|
|
4121
|
-
|
|
4476
|
+
if (!waitForReady)
|
|
4477
|
+
return instance;
|
|
4478
|
+
return main_cjs.waitForEntityIdle(instance, { timeout: readyTimeout }).then(() => instance);
|
|
4122
4479
|
}
|
|
4123
4480
|
// Create new instance
|
|
4124
4481
|
const newInstance = new this(schemaName);
|
|
@@ -4441,9 +4798,10 @@ class Schema {
|
|
|
4441
4798
|
instance: proxiedInstance,
|
|
4442
4799
|
refCount: 1,
|
|
4443
4800
|
});
|
|
4444
|
-
|
|
4445
|
-
|
|
4446
|
-
|
|
4801
|
+
const schema = proxiedInstance;
|
|
4802
|
+
if (!waitForReady)
|
|
4803
|
+
return schema;
|
|
4804
|
+
return main_cjs.waitForEntityIdle(schema, { timeout: readyTimeout }).then(() => schema);
|
|
4447
4805
|
}
|
|
4448
4806
|
/**
|
|
4449
4807
|
* Update the cache to use schemaFileId as the key instead of schemaName
|
|
@@ -4480,7 +4838,7 @@ class Schema {
|
|
|
4480
4838
|
});
|
|
4481
4839
|
return instance;
|
|
4482
4840
|
}
|
|
4483
|
-
return
|
|
4841
|
+
return null;
|
|
4484
4842
|
}
|
|
4485
4843
|
/**
|
|
4486
4844
|
* Clear all cached Schema instances.
|
|
@@ -4527,17 +4885,15 @@ class Schema {
|
|
|
4527
4885
|
return cachedInstance;
|
|
4528
4886
|
}
|
|
4529
4887
|
// Query database to get schema name from ID
|
|
4530
|
-
const
|
|
4531
|
-
const { schemas: schemasTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.SchemaSchema; });
|
|
4532
|
-
const db = BaseDb.getAppDb();
|
|
4888
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
4533
4889
|
if (!db) {
|
|
4534
4890
|
throw new Error('Database not available');
|
|
4535
4891
|
}
|
|
4536
4892
|
const dbSchemas = await db
|
|
4537
4893
|
.select()
|
|
4538
|
-
.from(
|
|
4539
|
-
.where(drizzleOrm.eq(
|
|
4540
|
-
.orderBy(drizzleOrm.desc(
|
|
4894
|
+
.from(main_cjs.schemas)
|
|
4895
|
+
.where(drizzleOrm.eq(main_cjs.schemas.schemaFileId, schemaFileId))
|
|
4896
|
+
.orderBy(drizzleOrm.desc(main_cjs.schemas.version))
|
|
4541
4897
|
.limit(1);
|
|
4542
4898
|
if (dbSchemas.length === 0) {
|
|
4543
4899
|
throw new Error(`Schema with ID "${schemaFileId}" not found in database`);
|
|
@@ -4584,7 +4940,7 @@ class Schema {
|
|
|
4584
4940
|
* @returns Array of Schema instances
|
|
4585
4941
|
*/
|
|
4586
4942
|
static async all(options = {}) {
|
|
4587
|
-
const { includeAllVersions = false, includeInternal = false } = options;
|
|
4943
|
+
const { includeAllVersions = false, includeInternal = false, waitForReady = false, readyTimeout = 5000, } = options;
|
|
4588
4944
|
try {
|
|
4589
4945
|
// Use loadAllSchemasFromDb as single source of truth
|
|
4590
4946
|
// This intelligently merges database and file data, including drafts
|
|
@@ -4667,13 +5023,18 @@ class Schema {
|
|
|
4667
5023
|
}
|
|
4668
5024
|
catch (error) {
|
|
4669
5025
|
// Fallback to creating by name if createById fails
|
|
4670
|
-
schemaInstances.push(this.create(schemaName));
|
|
5026
|
+
schemaInstances.push(this.create(schemaName, { waitForReady: false }));
|
|
4671
5027
|
}
|
|
4672
5028
|
}
|
|
4673
5029
|
else {
|
|
4674
|
-
schemaInstances.push(this.create(schemaName));
|
|
5030
|
+
schemaInstances.push(this.create(schemaName, { waitForReady: false }));
|
|
4675
5031
|
}
|
|
4676
5032
|
}
|
|
5033
|
+
if (waitForReady && schemaInstances.length > 0) {
|
|
5034
|
+
await Promise.all(schemaInstances.map((s) => main_cjs.waitForEntityIdle(s, {
|
|
5035
|
+
timeout: readyTimeout,
|
|
5036
|
+
})));
|
|
5037
|
+
}
|
|
4677
5038
|
return schemaInstances;
|
|
4678
5039
|
}
|
|
4679
5040
|
catch (error) {
|
|
@@ -4711,7 +5072,12 @@ class Schema {
|
|
|
4711
5072
|
// Create Schema instances for each unique schema name
|
|
4712
5073
|
const schemaInstances = [];
|
|
4713
5074
|
for (const schemaName of uniqueSchemaNames) {
|
|
4714
|
-
schemaInstances.push(this.create(schemaName));
|
|
5075
|
+
schemaInstances.push(this.create(schemaName, { waitForReady: false }));
|
|
5076
|
+
}
|
|
5077
|
+
if (waitForReady && schemaInstances.length > 0) {
|
|
5078
|
+
await Promise.all(schemaInstances.map((s) => main_cjs.waitForEntityIdle(s, {
|
|
5079
|
+
timeout: readyTimeout,
|
|
5080
|
+
})));
|
|
4715
5081
|
}
|
|
4716
5082
|
return schemaInstances;
|
|
4717
5083
|
}
|
|
@@ -4754,6 +5120,7 @@ class Schema {
|
|
|
4754
5120
|
*/
|
|
4755
5121
|
get models() {
|
|
4756
5122
|
const context = this._getSnapshotContext();
|
|
5123
|
+
const schemaName = context.schemaName;
|
|
4757
5124
|
// Get model IDs from service context (reactive state)
|
|
4758
5125
|
const liveQueryIds = context._liveQueryModelIds || [];
|
|
4759
5126
|
// Get pending model IDs (not yet in DB)
|
|
@@ -4762,15 +5129,27 @@ class Schema {
|
|
|
4762
5129
|
const pendingIds = [];
|
|
4763
5130
|
// Combine and deduplicate
|
|
4764
5131
|
const allModelIds = [...new Set([...liveQueryIds, ...pendingIds])];
|
|
4765
|
-
// Get Model instances from static cache
|
|
5132
|
+
// Get Model instances from static cache (from schema load)
|
|
5133
|
+
const seen = new Set();
|
|
4766
5134
|
const modelInstances = [];
|
|
4767
5135
|
for (const modelFileId of allModelIds) {
|
|
4768
5136
|
const model = main_cjs.Model.getById(modelFileId);
|
|
4769
|
-
if (model) {
|
|
5137
|
+
if (model && modelFileId && !seen.has(modelFileId)) {
|
|
4770
5138
|
modelInstances.push(model);
|
|
5139
|
+
seen.add(modelFileId);
|
|
5140
|
+
}
|
|
5141
|
+
}
|
|
5142
|
+
// Include models created at runtime via Model.create() that belong to this schema
|
|
5143
|
+
// (they may not be in _liveQueryModelIds until schema context is updated)
|
|
5144
|
+
if (schemaName) {
|
|
5145
|
+
const cachedForSchema = main_cjs.Model.getCachedInstancesForSchema(schemaName);
|
|
5146
|
+
for (const model of cachedForSchema) {
|
|
5147
|
+
const id = model.id;
|
|
5148
|
+
if (id && !seen.has(id)) {
|
|
5149
|
+
modelInstances.push(model);
|
|
5150
|
+
seen.add(id);
|
|
5151
|
+
}
|
|
4771
5152
|
}
|
|
4772
|
-
// Note: Cannot create models asynchronously in this synchronous getter
|
|
4773
|
-
// Models will be created elsewhere when needed
|
|
4774
5153
|
}
|
|
4775
5154
|
// Return a new array reference (snapshot at time of access)
|
|
4776
5155
|
return [...modelInstances];
|
|
@@ -4887,9 +5266,6 @@ class Schema {
|
|
|
4887
5266
|
throw new main_cjs.ConflictError(errorMessage, conflictCheck);
|
|
4888
5267
|
}
|
|
4889
5268
|
const context = this._getSnapshotContext();
|
|
4890
|
-
const { BaseDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.BaseDb$1; });
|
|
4891
|
-
const { schemas: schemasTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.SchemaSchema; });
|
|
4892
|
-
const { addSchemaToDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.db; });
|
|
4893
5269
|
if (!context._isDraft || !context._editedProperties || context._editedProperties.size === 0) {
|
|
4894
5270
|
logger('No changes to save');
|
|
4895
5271
|
return '';
|
|
@@ -4902,7 +5278,7 @@ class Schema {
|
|
|
4902
5278
|
}
|
|
4903
5279
|
logger(`Saving new version for schema ${this.schemaName} with ${context._editedProperties.size} edited properties`);
|
|
4904
5280
|
// STEP 1: Ensure draft exists in database and update it with current state
|
|
4905
|
-
const db = BaseDb.getAppDb();
|
|
5281
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
4906
5282
|
if (!db) {
|
|
4907
5283
|
throw new Error('Database not found');
|
|
4908
5284
|
}
|
|
@@ -4923,14 +5299,20 @@ class Schema {
|
|
|
4923
5299
|
migrations: context.migrations || [],
|
|
4924
5300
|
};
|
|
4925
5301
|
// Update draft in database with current state
|
|
4926
|
-
await addSchemaToDb({
|
|
5302
|
+
await main_cjs.addSchemaToDb({
|
|
4927
5303
|
name: this.schemaName,
|
|
4928
5304
|
version: currentSchema.version,
|
|
4929
5305
|
createdAt: new Date(currentSchema.metadata.createdAt).getTime(),
|
|
4930
5306
|
updatedAt: new Date(currentSchema.metadata.updatedAt).getTime(),
|
|
4931
5307
|
}, currentSchema.id, JSON.stringify(currentSchema, null, 2), true);
|
|
5308
|
+
const dbSchema = await db
|
|
5309
|
+
.select()
|
|
5310
|
+
.from(main_cjs.schemas)
|
|
5311
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, this.schemaName))
|
|
5312
|
+
.limit(1);
|
|
4932
5313
|
// Collect all edited properties and convert them to SchemaPropertyUpdate format
|
|
4933
5314
|
const propertyUpdates = [];
|
|
5315
|
+
const { ModelProperty } = await Promise.resolve().then(function () { return require('./ModelProperty-BeJvgKMw.js'); });
|
|
4934
5316
|
for (const propertyKey of context._editedProperties) {
|
|
4935
5317
|
// Skip schema-level changes (like schema name changes)
|
|
4936
5318
|
if (propertyKey === 'schema:name') {
|
|
@@ -4944,7 +5326,7 @@ class Schema {
|
|
|
4944
5326
|
}
|
|
4945
5327
|
// Get ModelProperty instance from cache
|
|
4946
5328
|
const cacheKey = `${modelName}:${propertyName}`;
|
|
4947
|
-
const ModelPropertyClass = ModelProperty
|
|
5329
|
+
const ModelPropertyClass = ModelProperty;
|
|
4948
5330
|
const cachedInstance = ModelPropertyClass.instanceCache.get(cacheKey);
|
|
4949
5331
|
if (!cachedInstance) {
|
|
4950
5332
|
logger(`ModelProperty instance not found for ${cacheKey}`);
|
|
@@ -4953,29 +5335,48 @@ class Schema {
|
|
|
4953
5335
|
const modelProperty = cachedInstance.instance;
|
|
4954
5336
|
const propertyContext = modelProperty.getService().getSnapshot().context;
|
|
4955
5337
|
// Convert to SchemaPropertyUpdate
|
|
4956
|
-
const propertyUpdate = await
|
|
5338
|
+
const propertyUpdate = await convertPropertyToSchemaUpdate(propertyContext, modelName, propertyName);
|
|
4957
5339
|
propertyUpdates.push(propertyUpdate);
|
|
4958
5340
|
}
|
|
4959
5341
|
if (propertyUpdates.length === 0) {
|
|
5342
|
+
// When only new models were added, _editedProperties contains 'schema:models' and we write the full schema
|
|
5343
|
+
if (context._editedProperties.has('schema:models')) {
|
|
5344
|
+
const newFilePath = await writeFullSchemaNewVersion(this.schemaName, currentSchema);
|
|
5345
|
+
const fileContent = await main_cjs.BaseFileManager.readFileAsString(newFilePath);
|
|
5346
|
+
const publishedSchema = JSON.parse(fileContent);
|
|
5347
|
+
if (dbSchema.length > 0) {
|
|
5348
|
+
await db
|
|
5349
|
+
.update(main_cjs.schemas)
|
|
5350
|
+
.set({
|
|
5351
|
+
isDraft: false,
|
|
5352
|
+
isEdited: false,
|
|
5353
|
+
schemaFileId: publishedSchema.id,
|
|
5354
|
+
schemaData: JSON.stringify(publishedSchema, null, 2),
|
|
5355
|
+
version: publishedSchema.version,
|
|
5356
|
+
updatedAt: new Date(publishedSchema.metadata.updatedAt).getTime(),
|
|
5357
|
+
})
|
|
5358
|
+
.where(drizzleOrm.eq(main_cjs.schemas.id, dbSchema[0].id));
|
|
5359
|
+
}
|
|
5360
|
+
this._service.send({
|
|
5361
|
+
type: 'clearDraft',
|
|
5362
|
+
_dbUpdatedAt: new Date(publishedSchema.metadata.updatedAt).getTime(),
|
|
5363
|
+
_dbVersion: publishedSchema.version,
|
|
5364
|
+
});
|
|
5365
|
+
return newFilePath;
|
|
5366
|
+
}
|
|
4960
5367
|
logger('No valid property updates to save');
|
|
4961
5368
|
return '';
|
|
4962
5369
|
}
|
|
4963
5370
|
// STEP 2: Save to new schema version (writes file)
|
|
4964
|
-
const newFilePath = await
|
|
5371
|
+
const newFilePath = await updateModelProperties(this.schemaName, propertyUpdates);
|
|
4965
5372
|
// STEP 3: After file is written, update database to mark as published (isDraft = false)
|
|
4966
5373
|
// Load the file to get the final schema with IDs
|
|
4967
|
-
const
|
|
4968
|
-
const fileContent = await BaseFileManager.readFileAsString(newFilePath);
|
|
5374
|
+
const fileContent = await main_cjs.BaseFileManager.readFileAsString(newFilePath);
|
|
4969
5375
|
const publishedSchema = JSON.parse(fileContent);
|
|
4970
5376
|
// Update database record: set isDraft = false and update schemaFileId
|
|
4971
|
-
const dbSchema = await db
|
|
4972
|
-
.select()
|
|
4973
|
-
.from(schemasTable)
|
|
4974
|
-
.where(drizzleOrm.eq(schemasTable.name, this.schemaName))
|
|
4975
|
-
.limit(1);
|
|
4976
5377
|
if (dbSchema.length > 0) {
|
|
4977
5378
|
await db
|
|
4978
|
-
.update(
|
|
5379
|
+
.update(main_cjs.schemas)
|
|
4979
5380
|
.set({
|
|
4980
5381
|
isDraft: false,
|
|
4981
5382
|
isEdited: false, // Clear isEdited flag after saving to file
|
|
@@ -4984,11 +5385,11 @@ class Schema {
|
|
|
4984
5385
|
version: publishedSchema.version,
|
|
4985
5386
|
updatedAt: new Date(publishedSchema.metadata.updatedAt).getTime(),
|
|
4986
5387
|
})
|
|
4987
|
-
.where(drizzleOrm.eq(
|
|
5388
|
+
.where(drizzleOrm.eq(main_cjs.schemas.id, dbSchema[0].id));
|
|
4988
5389
|
}
|
|
4989
5390
|
else {
|
|
4990
5391
|
// Create new record if it doesn't exist (shouldn't happen, but safety)
|
|
4991
|
-
await addSchemaToDb({
|
|
5392
|
+
await main_cjs.addSchemaToDb({
|
|
4992
5393
|
name: this.schemaName,
|
|
4993
5394
|
version: publishedSchema.version,
|
|
4994
5395
|
createdAt: new Date(publishedSchema.metadata.createdAt).getTime(),
|
|
@@ -5003,11 +5404,10 @@ class Schema {
|
|
|
5003
5404
|
_dbVersion: publishedSchema.version,
|
|
5004
5405
|
});
|
|
5005
5406
|
// Clear edited flags on all ModelProperty instances and in database
|
|
5006
|
-
const { properties: propertiesTable, models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.schema; });
|
|
5007
5407
|
for (const propertyKey of context._editedProperties) {
|
|
5008
5408
|
const [modelName, propertyName] = propertyKey.split(':');
|
|
5009
5409
|
const cacheKey = `${modelName}:${propertyName}`;
|
|
5010
|
-
const ModelPropertyClass = ModelProperty
|
|
5410
|
+
const ModelPropertyClass = ModelProperty;
|
|
5011
5411
|
const cachedInstance = ModelPropertyClass.instanceCache.get(cacheKey);
|
|
5012
5412
|
if (cachedInstance) {
|
|
5013
5413
|
const modelProperty = cachedInstance.instance;
|
|
@@ -5021,23 +5421,23 @@ class Schema {
|
|
|
5021
5421
|
if (db && modelName && propertyName) {
|
|
5022
5422
|
// Find model by name
|
|
5023
5423
|
const modelRecords = await db
|
|
5024
|
-
.select({ id:
|
|
5025
|
-
.from(
|
|
5026
|
-
.where(drizzleOrm.eq(
|
|
5424
|
+
.select({ id: main_cjs.models.id })
|
|
5425
|
+
.from(main_cjs.models)
|
|
5426
|
+
.where(drizzleOrm.eq(main_cjs.models.name, modelName))
|
|
5027
5427
|
.limit(1);
|
|
5028
5428
|
if (modelRecords.length > 0) {
|
|
5029
5429
|
// Find property by name and modelId
|
|
5030
5430
|
const propertyRecords = await db
|
|
5031
|
-
.select({ id:
|
|
5032
|
-
.from(
|
|
5033
|
-
.where(drizzleOrm.and(drizzleOrm.eq(
|
|
5431
|
+
.select({ id: main_cjs.properties.id })
|
|
5432
|
+
.from(main_cjs.properties)
|
|
5433
|
+
.where(drizzleOrm.and(drizzleOrm.eq(main_cjs.properties.name, propertyName), drizzleOrm.eq(main_cjs.properties.modelId, modelRecords[0].id)))
|
|
5034
5434
|
.limit(1);
|
|
5035
5435
|
if (propertyRecords.length > 0) {
|
|
5036
5436
|
// Clear isEdited flag in database
|
|
5037
5437
|
await db
|
|
5038
|
-
.update(
|
|
5438
|
+
.update(main_cjs.properties)
|
|
5039
5439
|
.set({ isEdited: false })
|
|
5040
|
-
.where(drizzleOrm.eq(
|
|
5440
|
+
.where(drizzleOrm.eq(main_cjs.properties.id, propertyRecords[0].id));
|
|
5041
5441
|
}
|
|
5042
5442
|
}
|
|
5043
5443
|
}
|
|
@@ -5051,19 +5451,18 @@ class Schema {
|
|
|
5051
5451
|
try {
|
|
5052
5452
|
if (db && context._dbId) {
|
|
5053
5453
|
// Get all models for this schema
|
|
5054
|
-
const { modelSchemas, models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.schema; });
|
|
5055
5454
|
const modelRecords = await db
|
|
5056
|
-
.select({ id:
|
|
5057
|
-
.from(modelSchemas)
|
|
5058
|
-
.innerJoin(
|
|
5059
|
-
.where(drizzleOrm.eq(modelSchemas.schemaId, context._dbId));
|
|
5455
|
+
.select({ id: main_cjs.models.id })
|
|
5456
|
+
.from(main_cjs.modelSchemas)
|
|
5457
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
5458
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, context._dbId));
|
|
5060
5459
|
// Clear isEdited flag for all models
|
|
5061
5460
|
for (const modelRecord of modelRecords) {
|
|
5062
5461
|
if (modelRecord.id) {
|
|
5063
5462
|
await db
|
|
5064
|
-
.update(
|
|
5463
|
+
.update(main_cjs.models)
|
|
5065
5464
|
.set({ isEdited: false })
|
|
5066
|
-
.where(drizzleOrm.eq(
|
|
5465
|
+
.where(drizzleOrm.eq(main_cjs.models.id, modelRecord.id));
|
|
5067
5466
|
}
|
|
5068
5467
|
}
|
|
5069
5468
|
}
|
|
@@ -5166,14 +5565,11 @@ class Schema {
|
|
|
5166
5565
|
saveDraftLogger(`Client check: cacheIsStale=${cacheIsStale}, shouldRecheck=${shouldRecheck}, cachedValue=${cachedClientInitialized}, timeSinceCheck=${now - clientCheckTime}ms`);
|
|
5167
5566
|
if (shouldRecheck) {
|
|
5168
5567
|
try {
|
|
5169
|
-
|
|
5170
|
-
const { getClient } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.ClientManager$1; });
|
|
5171
|
-
const { ClientManagerState } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.constants; });
|
|
5172
|
-
const client = getClient();
|
|
5568
|
+
const client = main_cjs.getClient();
|
|
5173
5569
|
const clientSnapshot = client.getService().getSnapshot();
|
|
5174
5570
|
// Check if state is IDLE (primary check) - isInitialized is set in entry action so should be true
|
|
5175
5571
|
// But we check it as a secondary safeguard
|
|
5176
|
-
const isIdle = clientSnapshot.value === ClientManagerState.IDLE;
|
|
5572
|
+
const isIdle = clientSnapshot.value === main_cjs.ClientManagerState.IDLE;
|
|
5177
5573
|
const isInitialized = clientSnapshot.context.isInitialized;
|
|
5178
5574
|
// If state is IDLE, trust it even if isInitialized isn't set yet (entry action should set it)
|
|
5179
5575
|
// This aligns with useIsClientReady which only checks the state value
|
|
@@ -5239,11 +5635,7 @@ class Schema {
|
|
|
5239
5635
|
_editedProperties: new Set(),
|
|
5240
5636
|
};
|
|
5241
5637
|
}
|
|
5242
|
-
const
|
|
5243
|
-
const { generateId } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.index; });
|
|
5244
|
-
const { BaseDb } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.BaseDb$1; });
|
|
5245
|
-
const { schemas: schemasTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.SchemaSchema; });
|
|
5246
|
-
const db = BaseDb.getAppDb();
|
|
5638
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
5247
5639
|
if (!db) {
|
|
5248
5640
|
throw new Error('Database not found');
|
|
5249
5641
|
}
|
|
@@ -5262,8 +5654,8 @@ class Schema {
|
|
|
5262
5654
|
logger(`Looking up schema by schemaFileId: ${context.id}`);
|
|
5263
5655
|
const schemasById = await db
|
|
5264
5656
|
.select()
|
|
5265
|
-
.from(
|
|
5266
|
-
.where(drizzleOrm.eq(
|
|
5657
|
+
.from(main_cjs.schemas)
|
|
5658
|
+
.where(drizzleOrm.eq(main_cjs.schemas.schemaFileId, context.id)) // id is now the schemaFileId (string)
|
|
5267
5659
|
.limit(1);
|
|
5268
5660
|
if (schemasById.length > 0) {
|
|
5269
5661
|
const foundRecord = schemasById[0];
|
|
@@ -5283,8 +5675,8 @@ class Schema {
|
|
|
5283
5675
|
logger(`Looking up schema by name "${lookupName}" (oldName: ${oldName}, finalNewName: ${finalNewName})`);
|
|
5284
5676
|
const existingSchemas = await db
|
|
5285
5677
|
.select()
|
|
5286
|
-
.from(
|
|
5287
|
-
.where(drizzleOrm.eq(
|
|
5678
|
+
.from(main_cjs.schemas)
|
|
5679
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, lookupName))
|
|
5288
5680
|
.limit(10); // Get multiple to find drafts
|
|
5289
5681
|
logger(`Found ${existingSchemas.length} records with name "${lookupName}"`);
|
|
5290
5682
|
// If name changed, prefer draft records; otherwise prefer any record
|
|
@@ -5317,7 +5709,7 @@ class Schema {
|
|
|
5317
5709
|
}
|
|
5318
5710
|
// Build current schema state from context
|
|
5319
5711
|
// Use existing schemaFileId if we found one, otherwise use the one from context, or generate new
|
|
5320
|
-
const schemaFileId = existingSchemaId || context.id || generateId(); // id is now the schemaFileId (string)
|
|
5712
|
+
const schemaFileId = existingSchemaId || context.id || main_cjs.generateId(); // id is now the schemaFileId (string)
|
|
5321
5713
|
// Build metadata - if name changed, use newName, otherwise use context metadata
|
|
5322
5714
|
// Always ensure metadata.name matches finalNewName to prevent inconsistencies
|
|
5323
5715
|
const currentMetadata = context.metadata ? {
|
|
@@ -5374,14 +5766,14 @@ class Schema {
|
|
|
5374
5766
|
saveDraftLogger(`Setting schemaFileId to ${finalSchemaFileId} (was null)`);
|
|
5375
5767
|
}
|
|
5376
5768
|
await db
|
|
5377
|
-
.update(
|
|
5769
|
+
.update(main_cjs.schemas)
|
|
5378
5770
|
.set(updateData)
|
|
5379
|
-
.where(drizzleOrm.eq(
|
|
5771
|
+
.where(drizzleOrm.eq(main_cjs.schemas.id, existingSchemaRecord.id));
|
|
5380
5772
|
// Verify what was saved by reading it back
|
|
5381
5773
|
const verifyRecord = await db
|
|
5382
5774
|
.select()
|
|
5383
|
-
.from(
|
|
5384
|
-
.where(drizzleOrm.eq(
|
|
5775
|
+
.from(main_cjs.schemas)
|
|
5776
|
+
.where(drizzleOrm.eq(main_cjs.schemas.id, existingSchemaRecord.id))
|
|
5385
5777
|
.limit(1);
|
|
5386
5778
|
if (verifyRecord.length > 0 && verifyRecord[0].schemaData) {
|
|
5387
5779
|
try {
|
|
@@ -5391,15 +5783,15 @@ class Schema {
|
|
|
5391
5783
|
saveDraftLogger(`ERROR: isDraft is not true after save! Expected true, got ${verifyRecord[0].isDraft}. This will cause the schema to load from file instead of database!`);
|
|
5392
5784
|
// Try to fix it immediately
|
|
5393
5785
|
await db
|
|
5394
|
-
.update(
|
|
5786
|
+
.update(main_cjs.schemas)
|
|
5395
5787
|
.set({ isDraft: true })
|
|
5396
|
-
.where(drizzleOrm.eq(
|
|
5788
|
+
.where(drizzleOrm.eq(main_cjs.schemas.id, existingSchemaRecord.id));
|
|
5397
5789
|
saveDraftLogger(`Attempted to fix isDraft by setting it to true again`);
|
|
5398
5790
|
// Verify the fix
|
|
5399
5791
|
const fixedRecord = await db
|
|
5400
5792
|
.select()
|
|
5401
|
-
.from(
|
|
5402
|
-
.where(drizzleOrm.eq(
|
|
5793
|
+
.from(main_cjs.schemas)
|
|
5794
|
+
.where(drizzleOrm.eq(main_cjs.schemas.id, existingSchemaRecord.id))
|
|
5403
5795
|
.limit(1);
|
|
5404
5796
|
if (fixedRecord.length > 0) {
|
|
5405
5797
|
saveDraftLogger(`After fix attempt: isDraft=${fixedRecord[0].isDraft}`);
|
|
@@ -5441,8 +5833,8 @@ class Schema {
|
|
|
5441
5833
|
// Try to find by looking for ANY draft with the old name (even if it doesn't match exactly)
|
|
5442
5834
|
const allDrafts = await db
|
|
5443
5835
|
.select()
|
|
5444
|
-
.from(
|
|
5445
|
-
.where(drizzleOrm.eq(
|
|
5836
|
+
.from(main_cjs.schemas)
|
|
5837
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, oldName))
|
|
5446
5838
|
.limit(1);
|
|
5447
5839
|
if (allDrafts.length > 0 && allDrafts[0].id) {
|
|
5448
5840
|
const foundRecord = allDrafts[0];
|
|
@@ -5450,7 +5842,7 @@ class Schema {
|
|
|
5450
5842
|
// Update the existing record with the new name
|
|
5451
5843
|
// CRITICAL: Ensure schemaFileId matches schema.id
|
|
5452
5844
|
await db
|
|
5453
|
-
.update(
|
|
5845
|
+
.update(main_cjs.schemas)
|
|
5454
5846
|
.set({
|
|
5455
5847
|
name: finalNewName,
|
|
5456
5848
|
schemaData: JSON.stringify(currentSchema, null, 2),
|
|
@@ -5459,7 +5851,7 @@ class Schema {
|
|
|
5459
5851
|
updatedAt: new Date(currentSchema.metadata.updatedAt).getTime(),
|
|
5460
5852
|
isDraft: true, // Ensure it's marked as a draft when saving via _saveDraftToDb
|
|
5461
5853
|
})
|
|
5462
|
-
.where(drizzleOrm.eq(
|
|
5854
|
+
.where(drizzleOrm.eq(main_cjs.schemas.id, foundRecord.id));
|
|
5463
5855
|
// Update context with id (schemaFileId) and conflict detection metadata
|
|
5464
5856
|
try {
|
|
5465
5857
|
const snapshot = this._service.getSnapshot();
|
|
@@ -5495,7 +5887,7 @@ class Schema {
|
|
|
5495
5887
|
throw new Error(`Failed to update schema name: existing record not found or update failed`);
|
|
5496
5888
|
}
|
|
5497
5889
|
// Otherwise, use addSchemaToDb which will handle create/update logic (for normal saves, not name changes)
|
|
5498
|
-
await addSchemaToDb({
|
|
5890
|
+
await main_cjs.addSchemaToDb({
|
|
5499
5891
|
name: finalNewName,
|
|
5500
5892
|
version: currentSchema.version,
|
|
5501
5893
|
createdAt: new Date(currentSchema.metadata.createdAt).getTime(),
|
|
@@ -5570,6 +5962,68 @@ class Schema {
|
|
|
5570
5962
|
}
|
|
5571
5963
|
}
|
|
5572
5964
|
}
|
|
5965
|
+
/**
|
|
5966
|
+
* Destroy the schema instance completely: remove from caches, delete from database (cascade),
|
|
5967
|
+
* and stop the service. Uses shared destroy helpers.
|
|
5968
|
+
*/
|
|
5969
|
+
async destroy() {
|
|
5970
|
+
const context = this._getSnapshotContext();
|
|
5971
|
+
const schemaFileId = context.id;
|
|
5972
|
+
const schemaName = context.schemaName;
|
|
5973
|
+
main_cjs.clearDestroySubscriptions(this, {
|
|
5974
|
+
instanceState: schemaInstanceState,
|
|
5975
|
+
onUnload: () => schemaInstanceState.delete(this),
|
|
5976
|
+
});
|
|
5977
|
+
const cacheKeys = [];
|
|
5978
|
+
if (schemaFileId)
|
|
5979
|
+
cacheKeys.push(schemaFileId);
|
|
5980
|
+
if (schemaName)
|
|
5981
|
+
cacheKeys.push(schemaName);
|
|
5982
|
+
main_cjs.forceRemoveFromCaches(this, {
|
|
5983
|
+
getCacheKeys: () => cacheKeys,
|
|
5984
|
+
caches: [
|
|
5985
|
+
Schema.instanceCacheById,
|
|
5986
|
+
Schema.instanceCacheByName,
|
|
5987
|
+
],
|
|
5988
|
+
});
|
|
5989
|
+
await main_cjs.runDestroyLifecycle(this, {
|
|
5990
|
+
getService: (instance) => instance._service,
|
|
5991
|
+
doDestroy: async () => {
|
|
5992
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
5993
|
+
if (!db || !schemaFileId)
|
|
5994
|
+
return;
|
|
5995
|
+
const schemaRecords = await db
|
|
5996
|
+
.select({ id: main_cjs.schemas.id })
|
|
5997
|
+
.from(main_cjs.schemas)
|
|
5998
|
+
.where(drizzleOrm.eq(main_cjs.schemas.schemaFileId, schemaFileId));
|
|
5999
|
+
if (schemaRecords.length === 0)
|
|
6000
|
+
return;
|
|
6001
|
+
const schemaIds = schemaRecords
|
|
6002
|
+
.map((r) => r.id)
|
|
6003
|
+
.filter((id) => id != null);
|
|
6004
|
+
if (schemaIds.length === 0)
|
|
6005
|
+
return;
|
|
6006
|
+
const { inArray } = await import('drizzle-orm');
|
|
6007
|
+
const joinRows = await db
|
|
6008
|
+
.select({ modelId: main_cjs.modelSchemas.modelId })
|
|
6009
|
+
.from(main_cjs.modelSchemas)
|
|
6010
|
+
.where(inArray(main_cjs.modelSchemas.schemaId, schemaIds));
|
|
6011
|
+
const modelIds = [
|
|
6012
|
+
...new Set(joinRows
|
|
6013
|
+
.map((r) => r.modelId)
|
|
6014
|
+
.filter((id) => id != null)),
|
|
6015
|
+
];
|
|
6016
|
+
await db.delete(main_cjs.modelSchemas).where(inArray(main_cjs.modelSchemas.schemaId, schemaIds));
|
|
6017
|
+
for (const modelId of modelIds) {
|
|
6018
|
+
await db.delete(main_cjs.properties).where(drizzleOrm.eq(main_cjs.properties.modelId, modelId));
|
|
6019
|
+
}
|
|
6020
|
+
for (const modelId of modelIds) {
|
|
6021
|
+
await db.delete(main_cjs.models).where(drizzleOrm.eq(main_cjs.models.id, modelId));
|
|
6022
|
+
}
|
|
6023
|
+
await db.delete(main_cjs.schemas).where(drizzleOrm.eq(main_cjs.schemas.schemaFileId, schemaFileId));
|
|
6024
|
+
},
|
|
6025
|
+
});
|
|
6026
|
+
}
|
|
5573
6027
|
/**
|
|
5574
6028
|
* Set up liveQuery subscription to watch for model changes in the database
|
|
5575
6029
|
* This enables cross-instance synchronization (e.g., changes in other tabs/windows)
|
|
@@ -5581,10 +6035,7 @@ class Schema {
|
|
|
5581
6035
|
}
|
|
5582
6036
|
main_cjs.setupEntityLiveQuery(this, {
|
|
5583
6037
|
getEntityId: async (schema) => {
|
|
5584
|
-
const
|
|
5585
|
-
const { schemas: schemasTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.schema; });
|
|
5586
|
-
const { eq } = await import('drizzle-orm');
|
|
5587
|
-
const db = BaseDb.getAppDb();
|
|
6038
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
5588
6039
|
if (!db) {
|
|
5589
6040
|
return undefined;
|
|
5590
6041
|
}
|
|
@@ -5595,8 +6046,8 @@ class Schema {
|
|
|
5595
6046
|
}
|
|
5596
6047
|
const schemaRecords = await db
|
|
5597
6048
|
.select()
|
|
5598
|
-
.from(
|
|
5599
|
-
.where(eq(
|
|
6049
|
+
.from(main_cjs.schemas)
|
|
6050
|
+
.where(drizzleOrm.eq(main_cjs.schemas.name, schemaName))
|
|
5600
6051
|
.limit(1);
|
|
5601
6052
|
if (schemaRecords.length === 0 || !schemaRecords[0].id) {
|
|
5602
6053
|
return undefined;
|
|
@@ -5604,22 +6055,19 @@ class Schema {
|
|
|
5604
6055
|
return schemaRecords[0].id;
|
|
5605
6056
|
},
|
|
5606
6057
|
buildQuery: async (schemaId) => {
|
|
5607
|
-
const
|
|
5608
|
-
const { modelSchemas, models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.schema; });
|
|
5609
|
-
const { eq } = await import('drizzle-orm');
|
|
5610
|
-
const db = BaseDb.getAppDb();
|
|
6058
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
5611
6059
|
if (!db) {
|
|
5612
6060
|
throw new Error('Database not available');
|
|
5613
6061
|
}
|
|
5614
|
-
return BaseDb.liveQuery(db
|
|
6062
|
+
return main_cjs.BaseDb.liveQuery(db
|
|
5615
6063
|
.select({
|
|
5616
|
-
modelId: modelSchemas.modelId,
|
|
5617
|
-
modelName:
|
|
5618
|
-
modelFileId:
|
|
6064
|
+
modelId: main_cjs.modelSchemas.modelId,
|
|
6065
|
+
modelName: main_cjs.models.name,
|
|
6066
|
+
modelFileId: main_cjs.models.schemaFileId,
|
|
5619
6067
|
})
|
|
5620
|
-
.from(modelSchemas)
|
|
5621
|
-
.innerJoin(
|
|
5622
|
-
.where(eq(modelSchemas.schemaId, typeof schemaId === 'string' ? parseInt(schemaId, 10) : schemaId)));
|
|
6068
|
+
.from(main_cjs.modelSchemas)
|
|
6069
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
6070
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, typeof schemaId === 'string' ? parseInt(schemaId, 10) : schemaId)));
|
|
5623
6071
|
},
|
|
5624
6072
|
extractEntityIds: (rows) => rows.map(row => row.modelFileId).filter(Boolean),
|
|
5625
6073
|
updateContext: (schema, ids) => {
|
|
@@ -5678,16 +6126,12 @@ class Schema {
|
|
|
5678
6126
|
}
|
|
5679
6127
|
},
|
|
5680
6128
|
createChildInstances: async (ids) => {
|
|
5681
|
-
const { Model } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.Model$1; });
|
|
5682
6129
|
for (const id of ids) {
|
|
5683
|
-
await Model.createById(id);
|
|
6130
|
+
await main_cjs.Model.createById(id);
|
|
5684
6131
|
}
|
|
5685
6132
|
},
|
|
5686
6133
|
queryInitialData: async (schemaId) => {
|
|
5687
|
-
const
|
|
5688
|
-
const { modelSchemas, models: modelsTable } = await Promise.resolve().then(function () { return require('./index-BmIVfqGN.js'); }).then(function (n) { return n.schema; });
|
|
5689
|
-
const { eq } = await import('drizzle-orm');
|
|
5690
|
-
const db = BaseDb.getAppDb();
|
|
6134
|
+
const db = main_cjs.BaseDb.getAppDb();
|
|
5691
6135
|
if (!db) {
|
|
5692
6136
|
return [];
|
|
5693
6137
|
}
|
|
@@ -5696,13 +6140,13 @@ class Schema {
|
|
|
5696
6140
|
try {
|
|
5697
6141
|
const initialModels = await db
|
|
5698
6142
|
.select({
|
|
5699
|
-
modelId: modelSchemas.modelId,
|
|
5700
|
-
modelName:
|
|
5701
|
-
modelFileId:
|
|
6143
|
+
modelId: main_cjs.modelSchemas.modelId,
|
|
6144
|
+
modelName: main_cjs.models.name,
|
|
6145
|
+
modelFileId: main_cjs.models.schemaFileId,
|
|
5702
6146
|
})
|
|
5703
|
-
.from(modelSchemas)
|
|
5704
|
-
.innerJoin(
|
|
5705
|
-
.where(eq(modelSchemas.schemaId, typeof schemaId === 'string' ? parseInt(schemaId, 10) : schemaId));
|
|
6147
|
+
.from(main_cjs.modelSchemas)
|
|
6148
|
+
.innerJoin(main_cjs.models, drizzleOrm.eq(main_cjs.modelSchemas.modelId, main_cjs.models.id))
|
|
6149
|
+
.where(drizzleOrm.eq(main_cjs.modelSchemas.schemaId, typeof schemaId === 'string' ? parseInt(schemaId, 10) : schemaId));
|
|
5706
6150
|
logger(`[Schema._setupLiveQuerySubscription] Initial query found ${initialModels.length} models`);
|
|
5707
6151
|
if (initialModels.length > 0) {
|
|
5708
6152
|
return initialModels.map((row) => ({
|
|
@@ -5758,5 +6202,7 @@ var Schema$1 = /*#__PURE__*/Object.freeze({
|
|
|
5758
6202
|
});
|
|
5759
6203
|
|
|
5760
6204
|
exports.Schema = Schema;
|
|
5761
|
-
exports.
|
|
5762
|
-
|
|
6205
|
+
exports.Schema$1 = Schema$1;
|
|
6206
|
+
exports.convertPropertyToSchemaUpdate = convertPropertyToSchemaUpdate;
|
|
6207
|
+
exports.updateModelProperties = updateModelProperties;
|
|
6208
|
+
//# sourceMappingURL=Schema-CVs9J6eP.js.map
|