@lyxa.ai/core 1.0.57 → 1.0.58-beta

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.
@@ -26,6 +26,9 @@ async function initGlobalConnection(uri, environment) {
26
26
  existingConnection: globalConnection,
27
27
  ...options,
28
28
  });
29
+ if (!model.schema.path('deletedAt')) {
30
+ soft_delete_plugin_1.SoftDelete.applyToSchema(model.schema);
31
+ }
29
32
  resolve(model);
30
33
  }
31
34
  pendingModels.clear();
@@ -36,6 +39,9 @@ async function initGlobalConnection(uri, environment) {
36
39
  existingConnection: globalConnection,
37
40
  ...options,
38
41
  });
42
+ if (!model.schema.path('deletedAt')) {
43
+ soft_delete_plugin_1.SoftDelete.applyToSchema(model.schema);
44
+ }
39
45
  resolve(model);
40
46
  }
41
47
  catch (error) {
@@ -53,10 +59,14 @@ async function initGlobalConnection(uri, environment) {
53
59
  function createGlobalModel(Class, options) {
54
60
  const modelName = options?.schemaOptions?.collection || Class.name;
55
61
  if (globalConnection) {
56
- return (0, typegoose_1.getModelForClass)(Class, {
62
+ const model = (0, typegoose_1.getModelForClass)(Class, {
57
63
  existingConnection: globalConnection,
58
64
  ...options,
59
65
  });
66
+ if (!model.schema.path('deletedAt')) {
67
+ soft_delete_plugin_1.SoftDelete.applyToSchema(model.schema);
68
+ }
69
+ return model;
60
70
  }
61
71
  console.log(`Registering model ${modelName} to be initialized when global connection is ready`);
62
72
  let actualModel = null;
@@ -86,20 +96,14 @@ function createGlobalModel(Class, options) {
86
96
  function createGlobalDiscriminatorModel(baseModel, subClass, discriminatorName, options) {
87
97
  const modelName = discriminatorName || subClass.name;
88
98
  if (globalConnection) {
89
- if (baseModel instanceof Promise || baseModel.then) {
90
- const discriminatorPromise = (async () => {
91
- const resolvedBaseModel = await baseModel;
92
- return (0, typegoose_1.getDiscriminatorModelForClass)(resolvedBaseModel, subClass, discriminatorName, {
93
- existingConnection: globalConnection,
94
- ...options,
95
- });
96
- })();
97
- return createProxyForPromise(discriminatorPromise);
98
- }
99
- return (0, typegoose_1.getDiscriminatorModelForClass)(baseModel, subClass, discriminatorName, {
99
+ const model = (0, typegoose_1.getDiscriminatorModelForClass)(baseModel, subClass, discriminatorName, {
100
100
  existingConnection: globalConnection,
101
101
  ...options,
102
102
  });
103
+ if (!model.schema.path('deletedAt')) {
104
+ soft_delete_plugin_1.SoftDelete.applyToSchema(model.schema);
105
+ }
106
+ return model;
103
107
  }
104
108
  console.log(`Registering discriminator model ${modelName} to be initialized when global connection is ready`);
105
109
  let actualModel = null;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["libraries/mongo/index.ts"],"names":[],"mappings":";;AAkCA,oDAmDC;AAMD,8CAiDC;AAMD,wEAgEC;AAlND,uCAAkE;AAClE,oDAAwG;AAExG,qEAAqE;AAGrE,IAAA,0CAAqB,GAAE,CAAC;AAGxB,IAAI,gBAAwC,CAAC;AAC7C,IAAI,iBAAiB,GAA+B,IAAI,CAAC;AACzD,IAAI,aAAa,GAAG,IAAI,GAAG,EAOxB,CAAC;AAEJ,IAAI,0BAA0B,GAAG,IAAI,GAAG,EASrC,CAAC;AAKG,KAAK,UAAU,oBAAoB,CAAC,GAAW,EAAE,WAAmB;IAC1E,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACxB,iBAAiB,GAAG,IAAI,OAAO,CAAa,OAAO,CAAC,EAAE;YACrD,OAAO,CAAC,GAAG,CAAC,mDAAmD,WAAW,EAAE,CAAC,CAAC;YAC9E,MAAM,UAAU,GAAG,IAAA,2BAAgB,EAAC,GAAG,EAAE;gBACxC,MAAM,EAAE,QAAQ,WAAW,SAAS;aACpC,CAAC,CAAC;YAEH,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,IAAI,EAAE;gBACvC,OAAO,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAC;gBAC1D,gBAAgB,GAAG,UAAU,CAAC;gBAG9B,KAAK,MAAM,CAAC,SAAS,EAAE,E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iBAAiB,CAAC,CAAC;AACjD,CAAC;AAKD,SAAS,qBAAqB,CAAmB,OAAmB;IACnE,IAAI,aAAa,GAAa,IAAI,CAAC;IAEnC,OAAO,IAAI,KAAK,CAAC,EAAO,EAAE;QACzB,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ;YAEzB,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;gBACrB,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACnC,CAAC;YAED,OAAO,KAAK,WAAW,GAAG,IAAW;gBACpC,IAAI,CAAC,aAAa,EAAE,CAAC;oBACpB,aAAa,GAAG,MAAM,OAAO,CAAC;gBAC/B,CAAC;gBAED,MAAM,SAAS,GAAI,aAAqB,CAAC,IAAI,CAAC,CAAC;gBAC/C,IAAI,OAAO,SAAS,KAAK,UAAU,EAAE,CAAC;oBACrC,OAAO,SAAS,CAAC,KAAK,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;gBAC7C,CAAC;gBACD,OAAO,SAAS,CAAC;YAClB,CAAC,CAAC;QACH,CAAC;KACD,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import mongoose, { Connection, createConnection } from 'mongoose';\nimport { getDiscriminatorModelForClass, getModelForClass, ReturnModelType } from '@typegoose/typegoose';\nimport { AnyParamConstructor, IModelOptions } from '@typegoose/typegoose/lib/types';\nimport { applySoftDeletePlugin } from './plugins/soft-delete-plugin';\n\n// Apply the soft delete plugin globally before any connections or models\napplySoftDeletePlugin();\n\n// Track connection status\nlet globalConnection: Connection | undefined;\nlet connectionPromise: Promise<Connection> | null = null;\nlet pendingModels = new Map<\n\tstring,\n\t{\n\t\tClass: AnyParamConstructor<any>;\n\t\toptions?: IModelOptions;\n\t\tresolve: (model: any) => void;\n\t}\n>();\n\nlet pendingDiscriminatorModels = new Map<\n\tstring,\n\t{\n\t\tbaseModelPromise: Promise<any>; // Store the promise for the base model\n\t\tsubClass: AnyParamConstructor<any>;\n\t\tdiscriminatorName?: string;\n\t\toptions?: IModelOptions;\n\t\tresolve: (model: any) => void;\n\t}\n>();\n\n/**\n * Initialize the global database connection\n */\nexport async function initGlobalConnection(uri: string, environment: string): Promise<Connection> {\n\tif (!connectionPromise) {\n\t\tconnectionPromise = new Promise<Connection>(resolve => {\n\t\t\tconsole.log(`Initializing global connection for environment: ${environment}`);\n\t\t\tconst connection = createConnection(uri, {\n\t\t\t\tdbName: `lyxa-${environment}-global`,\n\t\t\t});\n\n\t\t\tconnection.once('connected', async () => {\n\t\t\t\tconsole.log('Global connection successfully established');\n\t\t\t\tglobalConnection = connection;\n\n\t\t\t\t// Initialize any pending models with the real connection\n\t\t\t\tfor (const [modelName, { Class, options, resolve }] of pendingModels.entries()) {\n\t\t\t\t\tconst model = getModelForClass(Class, {\n\t\t\t\t\t\texistingConnection: globalConnection,\n\t\t\t\t\t\t...options,\n\t\t\t\t\t});\n\t\t\t\t\tresolve(model);\n\t\t\t\t}\n\t\t\t\tpendingModels.clear();\n\n\t\t\t\t// Initialize any pending discriminator models\n\t\t\t\t// Process discriminators sequentially to ensure dependencies are resolved\n\t\t\t\tfor (const [\n\t\t\t\t\tmodelName,\n\t\t\t\t\t{ baseModelPromise, subClass, discriminatorName, options, resolve },\n\t\t\t\t] of pendingDiscriminatorModels.entries()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// Await the base model to be fully resolved\n\t\t\t\t\t\tconst actualBaseModel = await baseModelPromise;\n\n\t\t\t\t\t\t// Create the discriminator model with the actual base model\n\t\t\t\t\t\tconst model = getDiscriminatorModelForClass(actualBaseModel, subClass, discriminatorName, {\n\t\t\t\t\t\t\texistingConnection: globalConnection,\n\t\t\t\t\t\t\t...options,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tresolve(model);\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tconsole.error(`Failed to create discriminator model ${modelName}:`, error);\n\t\t\t\t\t\t// Resolve with error to unblock the promise chain\n\t\t\t\t\t\tresolve(Promise.reject(error));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpendingDiscriminatorModels.clear();\n\n\t\t\t\tresolve(connection);\n\t\t\t});\n\t\t});\n\t}\n\treturn connectionPromise;\n}\n\n/**\n * Creates a Typegoose model that will use the global connection when it's ready.\n * Safe to call during module initialization before initGlobalConnection.\n */\nexport function createGlobalModel<T extends AnyParamConstructor<any>>(\n\tClass: T,\n\toptions?: IModelOptions\n): ReturnModelType<T> {\n\tconst modelName = options?.schemaOptions?.collection || Class.name;\n\n\t// If the connection is already initialized, create the model directly\n\tif (globalConnection) {\n\t\treturn getModelForClass(Class, {\n\t\t\texistingConnection: globalConnection,\n\t\t\t...options,\n\t\t});\n\t}\n\n\tconsole.log(`Registering model ${modelName} to be initialized when global connection is ready`);\n\n\t// Create a lazy-loaded model instance that will initialize once the connection is ready\n\tlet actualModel: ReturnModelType<T> | null = null;\n\tlet modelReadyPromise: Promise<ReturnModelType<T>>;\n\n\t// Setup the promise that will resolve once the model is ready\n\tmodelReadyPromise = new Promise<ReturnModelType<T>>(resolve => {\n\t\t// Register this model to be initialized when connection is ready\n\t\tpendingModels.set(modelName, { Class, options, resolve });\n\t});\n\n\t// Create a proxy that forwards operations to the real model once it's available\n\tconst modelProxy = new Proxy({} as ReturnModelType<T>, {\n\t\tget(target, prop, receiver) {\n\t\t\t// Handle special case for 'then' to make the proxy thenable\n\t\t\tif (prop === 'then') {\n\t\t\t\treturn modelReadyPromise.then.bind(modelReadyPromise);\n\t\t\t}\n\n\t\t\treturn async function (...args: any[]) {\n\t\t\t\tif (!actualModel) {\n\t\t\t\t\tactualModel = await modelReadyPromise;\n\t\t\t\t}\n\n\t\t\t\tconst propValue = (actualModel as any)[prop];\n\t\t\t\tif (typeof propValue === 'function') {\n\t\t\t\t\treturn propValue.apply(actualModel, args);\n\t\t\t\t}\n\t\t\t\treturn propValue;\n\t\t\t};\n\t\t},\n\t});\n\n\treturn modelProxy;\n}\n\n/**\n * Creates a Typegoose discriminator model that will use the global connection when it's ready.\n * Safe to call during module initialization before initGlobalConnection.\n */\nexport function createGlobalDiscriminatorModel<\n\tBase extends AnyParamConstructor<any>,\n\tSub extends AnyParamConstructor<any>,\n>(\n\tbaseModel: ReturnModelType<Base>,\n\tsubClass: Sub,\n\tdiscriminatorName?: string,\n\toptions?: IModelOptions\n): ReturnModelType<Sub> {\n\tconst modelName = discriminatorName || subClass.name;\n\n\t// If the connection is already initialized, create the model directly\n\tif (globalConnection) {\n\t\t// Ensure baseModel is fully resolved if it's a promise or proxy\n\t\tif (baseModel instanceof Promise || (baseModel as any).then) {\n\t\t\t// Create a new promise that will resolve with the discriminator model\n\t\t\tconst discriminatorPromise = (async () => {\n\t\t\t\tconst resolvedBaseModel = await baseModel;\n\t\t\t\treturn getDiscriminatorModelForClass(resolvedBaseModel, subClass, discriminatorName, {\n\t\t\t\t\texistingConnection: globalConnection,\n\t\t\t\t\t...options,\n\t\t\t\t});\n\t\t\t})();\n\n\t\t\t// Return a proxy that will await the discriminator model\n\t\t\treturn createProxyForPromise(discriminatorPromise);\n\t\t}\n\n\t\t// If baseModel is not a promise, create the discriminator directly\n\t\treturn getDiscriminatorModelForClass(baseModel, subClass, discriminatorName, {\n\t\t\texistingConnection: globalConnection,\n\t\t\t...options,\n\t\t});\n\t}\n\n\tconsole.log(\n\t\t`Registering discriminator model ${modelName} to be initialized when global connection is ready`\n\t);\n\n\t// Create a lazy-loaded model instance that will initialize once the connection is ready\n\tlet actualModel: ReturnModelType<Sub> | null = null;\n\tlet modelReadyPromise: Promise<ReturnModelType<Sub>>;\n\n\t// Handle the case where baseModel might be a proxy or promise\n\tlet baseModelPromise: Promise<any>;\n\tif (baseModel instanceof Promise || (baseModel as any).then) {\n\t\tbaseModelPromise = Promise.resolve(baseModel);\n\t} else {\n\t\tbaseModelPromise = Promise.resolve(baseModel);\n\t}\n\n\t// Setup the promise that will resolve once the model is ready\n\tmodelReadyPromise = new Promise<ReturnModelType<Sub>>(resolve => {\n\t\t// Register this discriminator model to be initialized when connection is ready\n\t\tpendingDiscriminatorModels.set(modelName, {\n\t\t\tbaseModelPromise,\n\t\t\tsubClass,\n\t\t\tdiscriminatorName,\n\t\t\toptions,\n\t\t\tresolve,\n\t\t});\n\t});\n\n\treturn createProxyForPromise(modelReadyPromise);\n}\n\n/**\n * Helper function to create a proxy for a promise that will resolve to a model\n */\nfunction createProxyForPromise<T extends object>(promise: Promise<T>): T {\n\tlet resolvedValue: T | null = null;\n\n\treturn new Proxy({} as T, {\n\t\tget(target, prop, receiver) {\n\t\t\t// Handle special case for 'then' to make the proxy thenable\n\t\t\tif (prop === 'then') {\n\t\t\t\treturn promise.then.bind(promise);\n\t\t\t}\n\n\t\t\treturn async function (...args: any[]) {\n\t\t\t\tif (!resolvedValue) {\n\t\t\t\t\tresolvedValue = await promise;\n\t\t\t\t}\n\n\t\t\t\tconst propValue = (resolvedValue as any)[prop];\n\t\t\t\tif (typeof propValue === 'function') {\n\t\t\t\t\treturn propValue.apply(resolvedValue, args);\n\t\t\t\t}\n\t\t\t\treturn propValue;\n\t\t\t};\n\t\t},\n\t});\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["libraries/mongo/index.ts"],"names":[],"mappings":";;AAkCA,oDA6DC;AAMD,8CAwDC;AAMD,wEAuDC;AA1ND,uCAAkE;AAClE,oDAAwG;AAExG,qEAAiF;AAGjF,IAAA,0CAAqB,GAAE,CAAC;AAGxB,IAAI,gBAAwC,CAAC;AAC7C,IAAI,iBAAiB,GAA+B,IAAI,CAAC;AACzD,IAAI,aAAa,GAAG,IAAI,GAAG,EAOxB,CAAC;AAEJ,IAAI,0BAA0B,GAAG,IAAI,GAAG,EASrC,CAAC;AAKG,KAAK,UAAU,oBAAoB,CAAC,GAAW,EAAE,WAAmB;IAC1E,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACxB,iBAAiB,GAAG,IAAI,OAAO,CAAa,OAAO,CAAC,EAAE;YACrD,OAAO,CAAC,GAAG,CAAC,mDAA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CAAC;gBACD,OAAO,SAAS,CAAC;YAClB,CAAC,CAAC;QACH,CAAC;KACD,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import mongoose, { Connection, createConnection } from 'mongoose';\nimport { getDiscriminatorModelForClass, getModelForClass, ReturnModelType } from '@typegoose/typegoose';\nimport { AnyParamConstructor, IModelOptions } from '@typegoose/typegoose/lib/types';\nimport { applySoftDeletePlugin, SoftDelete } from './plugins/soft-delete-plugin';\n\n// Apply the soft delete plugin globally before any connections or models\napplySoftDeletePlugin();\n\n// Track connection status\nlet globalConnection: Connection | undefined;\nlet connectionPromise: Promise<Connection> | null = null;\nlet pendingModels = new Map<\n\tstring,\n\t{\n\t\tClass: AnyParamConstructor<any>;\n\t\toptions?: IModelOptions;\n\t\tresolve: (model: any) => void;\n\t}\n>();\n\nlet pendingDiscriminatorModels = new Map<\n\tstring,\n\t{\n\t\tbaseModelPromise: Promise<any>; // Store the promise for the base model\n\t\tsubClass: AnyParamConstructor<any>;\n\t\tdiscriminatorName?: string;\n\t\toptions?: IModelOptions;\n\t\tresolve: (model: any) => void;\n\t}\n>();\n\n/**\n * Initialize the global database connection\n */\nexport async function initGlobalConnection(uri: string, environment: string): Promise<Connection> {\n\tif (!connectionPromise) {\n\t\tconnectionPromise = new Promise<Connection>(resolve => {\n\t\t\tconsole.log(`Initializing global connection for environment: ${environment}`);\n\t\t\tconst connection = createConnection(uri, {\n\t\t\t\tdbName: `lyxa-${environment}-global`,\n\t\t\t});\n\n\t\t\tconnection.once('connected', async () => {\n\t\t\t\tconsole.log('Global connection successfully established');\n\t\t\t\tglobalConnection = connection;\n\n\t\t\t\t// Initialize any pending models with the real connection\n\t\t\t\tfor (const [modelName, { Class, options, resolve }] of pendingModels.entries()) {\n\t\t\t\t\tconst model = getModelForClass(Class, {\n\t\t\t\t\t\texistingConnection: globalConnection,\n\t\t\t\t\t\t...options,\n\t\t\t\t\t});\n\t\t\t\t\t// Apply soft delete plugin if not present\n\t\t\t\t\tif (!model.schema.path('deletedAt')) {\n\t\t\t\t\t\tSoftDelete.applyToSchema(model.schema);\n\t\t\t\t\t}\n\t\t\t\t\tresolve(model);\n\t\t\t\t}\n\t\t\t\tpendingModels.clear();\n\n\t\t\t\t// Initialize any pending discriminator models\n\t\t\t\t// Process discriminators sequentially to ensure dependencies are resolved\n\t\t\t\tfor (const [\n\t\t\t\t\tmodelName,\n\t\t\t\t\t{ baseModelPromise, subClass, discriminatorName, options, resolve },\n\t\t\t\t] of pendingDiscriminatorModels.entries()) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// Await the base model to be fully resolved\n\t\t\t\t\t\tconst actualBaseModel = await baseModelPromise;\n\n\t\t\t\t\t\t// Create the discriminator model with the actual base model\n\t\t\t\t\t\tconst model = getDiscriminatorModelForClass(actualBaseModel, subClass, discriminatorName, {\n\t\t\t\t\t\t\texistingConnection: globalConnection,\n\t\t\t\t\t\t\t...options,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\t// Apply soft delete plugin if not present\n\t\t\t\t\t\tif (!model.schema.path('deletedAt')) {\n\t\t\t\t\t\t\tSoftDelete.applyToSchema(model.schema);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tresolve(model);\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\tconsole.error(`Failed to create discriminator model ${modelName}:`, error);\n\t\t\t\t\t\t// Resolve with error to unblock the promise chain\n\t\t\t\t\t\tresolve(Promise.reject(error));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpendingDiscriminatorModels.clear();\n\n\t\t\t\tresolve(connection);\n\t\t\t});\n\t\t});\n\t}\n\treturn connectionPromise;\n}\n\n/**\n * Creates a Typegoose model that will use the global connection when it's ready.\n * Safe to call during module initialization before initGlobalConnection.\n */\nexport function createGlobalModel<T extends AnyParamConstructor<any>>(\n\tClass: T,\n\toptions?: IModelOptions\n): ReturnModelType<T> {\n\tconst modelName = options?.schemaOptions?.collection || Class.name;\n\n\t// If the connection is already initialized, create the model directly\n\tif (globalConnection) {\n\t\tconst model = getModelForClass(Class, {\n\t\t\texistingConnection: globalConnection,\n\t\t\t...options,\n\t\t});\n\n\t\t// Apply soft delete plugin if not already applied\n\t\tif (!model.schema.path('deletedAt')) {\n\t\t\tSoftDelete.applyToSchema(model.schema);\n\t\t}\n\n\t\treturn model;\n\t}\n\n\tconsole.log(`Registering model ${modelName} to be initialized when global connection is ready`);\n\n\t// Create a lazy-loaded model instance that will initialize once the connection is ready\n\tlet actualModel: ReturnModelType<T> | null = null;\n\tlet modelReadyPromise: Promise<ReturnModelType<T>>;\n\n\t// Setup the promise that will resolve once the model is ready\n\tmodelReadyPromise = new Promise<ReturnModelType<T>>(resolve => {\n\t\t// Register this model to be initialized when connection is ready\n\t\tpendingModels.set(modelName, { Class, options, resolve });\n\t});\n\n\t// Create a proxy that forwards operations to the real model once it's available\n\tconst modelProxy = new Proxy({} as ReturnModelType<T>, {\n\t\tget(target, prop, receiver) {\n\t\t\t// Handle special case for 'then' to make the proxy thenable\n\t\t\tif (prop === 'then') {\n\t\t\t\treturn modelReadyPromise.then.bind(modelReadyPromise);\n\t\t\t}\n\n\t\t\treturn async function (...args: any[]) {\n\t\t\t\tif (!actualModel) {\n\t\t\t\t\tactualModel = await modelReadyPromise;\n\t\t\t\t}\n\n\t\t\t\tconst propValue = (actualModel as any)[prop];\n\t\t\t\tif (typeof propValue === 'function') {\n\t\t\t\t\treturn propValue.apply(actualModel, args);\n\t\t\t\t}\n\t\t\t\treturn propValue;\n\t\t\t};\n\t\t},\n\t});\n\n\treturn modelProxy;\n}\n\n/**\n * Creates a Typegoose discriminator model that will use the global connection when it's ready.\n * Safe to call during module initialization before initGlobalConnection.\n */\nexport function createGlobalDiscriminatorModel<\n\tBase extends AnyParamConstructor<any>,\n\tSub extends AnyParamConstructor<any>,\n>(\n\tbaseModel: ReturnModelType<Base>,\n\tsubClass: Sub,\n\tdiscriminatorName?: string,\n\toptions?: IModelOptions\n): ReturnModelType<Sub> {\n\tconst modelName = discriminatorName || subClass.name;\n\n\t// If the connection is already initialized, create the model directly\n\tif (globalConnection) {\n\t\tconst model = getDiscriminatorModelForClass(baseModel, subClass, discriminatorName, {\n\t\t\texistingConnection: globalConnection,\n\t\t\t...options,\n\t\t});\n\n\t\t// Apply soft delete plugin if not already applied\n\t\tif (!model.schema.path('deletedAt')) {\n\t\t\tSoftDelete.applyToSchema(model.schema);\n\t\t}\n\n\t\treturn model;\n\t}\n\n\tconsole.log(\n\t\t`Registering discriminator model ${modelName} to be initialized when global connection is ready`\n\t);\n\n\t// Create a lazy-loaded model instance that will initialize once the connection is ready\n\tlet actualModel: ReturnModelType<Sub> | null = null;\n\tlet modelReadyPromise: Promise<ReturnModelType<Sub>>;\n\n\t// Handle the case where baseModel might be a proxy or promise\n\tlet baseModelPromise: Promise<any>;\n\tif (baseModel instanceof Promise || (baseModel as any).then) {\n\t\tbaseModelPromise = Promise.resolve(baseModel);\n\t} else {\n\t\tbaseModelPromise = Promise.resolve(baseModel);\n\t}\n\n\t// Setup the promise that will resolve once the model is ready\n\tmodelReadyPromise = new Promise<ReturnModelType<Sub>>(resolve => {\n\t\t// Register this discriminator model to be initialized when connection is ready\n\t\tpendingDiscriminatorModels.set(modelName, {\n\t\t\tbaseModelPromise,\n\t\t\tsubClass,\n\t\t\tdiscriminatorName,\n\t\t\toptions,\n\t\t\tresolve,\n\t\t});\n\t});\n\n\treturn createProxyForPromise(modelReadyPromise);\n}\n\n/**\n * Helper function to create a proxy for a promise that will resolve to a model\n */\nfunction createProxyForPromise<T extends object>(promise: Promise<T>): T {\n\tlet resolvedValue: T | null = null;\n\n\treturn new Proxy({} as T, {\n\t\tget(target, prop, receiver) {\n\t\t\t// Handle special case for 'then' to make the proxy thenable\n\t\t\tif (prop === 'then') {\n\t\t\t\treturn promise.then.bind(promise);\n\t\t\t}\n\n\t\t\treturn async function (...args: any[]) {\n\t\t\t\tif (!resolvedValue) {\n\t\t\t\t\tresolvedValue = await promise;\n\t\t\t\t}\n\n\t\t\t\tconst propValue = (resolvedValue as any)[prop];\n\t\t\t\tif (typeof propValue === 'function') {\n\t\t\t\t\treturn propValue.apply(resolvedValue, args);\n\t\t\t\t}\n\t\t\t\treturn propValue;\n\t\t\t};\n\t\t},\n\t});\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lyxa.ai/core",
3
- "version": "1.0.57",
3
+ "version": "1.0.58-beta",
4
4
  "description": "The Core system of the Lyxa services.",
5
5
  "author": "elie <42282499+Internalizable@users.noreply.github.com>",
6
6
  "license": "MIT",