UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 4.95 kB
{"version":3,"file":"runtime-schema-cache.mjs","names":[],"sources":["../../../../../src/libs/collection/schema/runtime/runtime-schema-cache.ts"],"sourcesContent":["import type { LucidErrorData } from \"../../../../types/errors.js\";\nimport type { ServiceContext } from \"../../../../utils/services/types.js\";\nimport cacheKeys from \"../../../kv/cache-keys.js\";\nimport type { CollectionSchema } from \"../types.js\";\nimport { getCollectionSignature } from \"./cache-signature.js\";\n\ntype CachedSchemaResponse =\n\t| {\n\t\t\tdata: CollectionSchema;\n\t\t\terror: undefined;\n\t }\n\t| {\n\t\t\tdata: undefined;\n\t\t\terror: LucidErrorData;\n\t };\n\nconst schemaCache = new Map<string, CollectionSchema>();\nconst inFlightSchemaLoads = new Map<string, Promise<CachedSchemaResponse>>();\n\nconst getSchemaKey = (collectionKey: string, signature: string) =>\n\t`${cacheKeys.collection.schema(collectionKey)}:${signature}`;\n\nconst getCollectionSignatureByKey = (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): string | undefined => {\n\tconst collection = context.config.collections.find(\n\t\t(c) => c.key === collectionKey,\n\t);\n\tif (!collection) return undefined;\n\n\treturn getCollectionSignature(collection);\n};\n\n/**\n * Gets a runtime schema from memory/KV cache using the current collection\n * config signature.\n */\nexport const getRuntimeSchemaFromCache = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): Promise<CollectionSchema | undefined> => {\n\tconst signature = getCollectionSignatureByKey(context, collectionKey);\n\tif (!signature) return undefined;\n\n\tconst cacheKey = getSchemaKey(collectionKey, signature);\n\n\tconst memoryCached = schemaCache.get(cacheKey);\n\tif (memoryCached) return memoryCached;\n\n\tconst kvCached = await context.kv.get<CollectionSchema>(context, {\n\t\tkey: cacheKey,\n\t});\n\tif (!kvCached) return undefined;\n\n\tschemaCache.set(cacheKey, kvCached);\n\treturn kvCached;\n};\n\nexport const hasRuntimeSchema = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n): Promise<boolean> => {\n\treturn (\n\t\t(await getRuntimeSchemaFromCache(context, collectionKey)) !== undefined\n\t);\n};\n\nexport const setRuntimeSchema = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n\tschema: CollectionSchema,\n): Promise<void> => {\n\tconst signature = getCollectionSignatureByKey(context, collectionKey);\n\tif (!signature) return;\n\n\tconst cacheKey = getSchemaKey(collectionKey, signature);\n\n\tschemaCache.set(cacheKey, schema);\n\tawait context.kv.set(context, {\n\t\tkey: cacheKey,\n\t\tvalue: schema,\n\t});\n};\n\nexport const resolveRuntimeSchema = async (\n\tcontext: ServiceContext,\n\tcollectionKey: string,\n\tresolver: () => Promise<CachedSchemaResponse>,\n): Promise<CachedSchemaResponse> => {\n\tconst signature = getCollectionSignatureByKey(context, collectionKey);\n\tif (!signature) {\n\t\treturn await resolver();\n\t}\n\n\tconst cacheKey = getSchemaKey(collectionKey, signature);\n\tconst cachedSchema = await getRuntimeSchemaFromCache(context, collectionKey);\n\tif (cachedSchema) {\n\t\treturn {\n\t\t\tdata: cachedSchema,\n\t\t\terror: undefined,\n\t\t};\n\t}\n\n\tconst inFlightSchema = inFlightSchemaLoads.get(cacheKey);\n\tif (inFlightSchema) return inFlightSchema;\n\n\tconst pending = (async (): Promise<CachedSchemaResponse> => {\n\t\ttry {\n\t\t\tconst result = await resolver();\n\t\t\tif (!result.error) {\n\t\t\t\tawait setRuntimeSchema(context, collectionKey, result.data);\n\t\t\t}\n\t\t\treturn result;\n\t\t} finally {\n\t\t\tinFlightSchemaLoads.delete(cacheKey);\n\t\t}\n\t})();\n\n\tinFlightSchemaLoads.set(cacheKey, pending);\n\n\treturn pending;\n};\n"],"mappings":"yGAgBA,MAAM,EAAc,IAAI,IAClB,EAAsB,IAAI,IAE1B,GAAgB,EAAuB,IAC5C,GAAG,EAAU,WAAW,OAAO,CAAa,EAAE,GAAG,IAE5C,GACL,EACA,IACwB,CACxB,IAAM,EAAa,EAAQ,OAAO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