UNPKG

@lucidcms/core

Version:

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

1 lines 3.19 kB
{"version":3,"file":"resolve-database-adapter.mjs","names":[],"sources":["../../../src/libs/runtime/resolve-database-adapter.ts"],"sourcesContent":["import { LucidError } from \"../../utils/errors/index.js\";\nimport type DatabaseAdapter from \"../db/adapter-base.js\";\nimport type {\n\tDatabaseAdapterCreator,\n\tDatabaseAdapterFactory,\n} from \"../db/adapter-factory.js\";\nimport type { DatabaseAdapterValue, EnvironmentVariables } from \"./types.js\";\n\nconst isPromiseLike = (value: unknown): value is PromiseLike<unknown> => {\n\treturn (\n\t\t(typeof value === \"object\" || typeof value === \"function\") &&\n\t\tvalue !== null &&\n\t\t\"then\" in value\n\t);\n};\n\nconst isDatabaseAdapter = (value: unknown): value is DatabaseAdapter => {\n\tif (!value || typeof value !== \"object\") {\n\t\treturn false;\n\t}\n\n\tconst adapter = value as Partial<DatabaseAdapter>;\n\n\treturn (\n\t\ttypeof adapter.adapter === \"string\" &&\n\t\ttypeof adapter.connect === \"function\" &&\n\t\ttypeof adapter.inferSchema === \"function\" &&\n\t\ttypeof adapter.dropAllTables === \"function\"\n\t);\n};\n\nconst isDatabaseAdapterFactory = (\n\tvalue: unknown,\n): value is DatabaseAdapterFactory => {\n\tif (!value || (typeof value !== \"object\" && typeof value !== \"function\")) {\n\t\treturn false;\n\t}\n\n\tconst factory = value as Partial<DatabaseAdapterFactory> &\n\t\tPartial<DatabaseAdapterCreator>;\n\n\tif (\n\t\ttypeof value === \"function\" &&\n\t\tfactory.__lucidDatabaseAdapterCreator !== true\n\t) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\ttypeof factory.adapter === \"string\" && typeof factory.resolve === \"function\"\n\t);\n};\n\nexport const resolveDatabaseAdapter = async (\n\tdb: DatabaseAdapterValue,\n\tenv: EnvironmentVariables | undefined,\n): Promise<DatabaseAdapter> => {\n\tconst resolvedValue = isPromiseLike(db) ? await db : db;\n\tconst maybeAdapter = isDatabaseAdapterFactory(resolvedValue)\n\t\t? resolvedValue.resolve(env ?? {})\n\t\t: resolvedValue;\n\tconst adapter = isPromiseLike(maybeAdapter)\n\t\t? await maybeAdapter\n\t\t: maybeAdapter;\n\n\tif (!isDatabaseAdapter(adapter)) {\n\t\tthrow new LucidError({\n\t\t\tmessage:\n\t\t\t\t\"Lucid could not resolve the configured database adapter. Pass a database adapter instance or adapter-level env factory to `configureLucid({ db })`, such as `db: libsql((env) => ({ ... }))`. Top-level `db: (env) => ...` callbacks are not supported.\",\n\t\t});\n\t}\n\n\treturn adapter;\n};\n\nexport default resolveDatabaseAdapter;\n"],"mappings":"kDAQA,MAAM,EAAiB,IAEpB,OAAO,GAAU,UAAY,OAAO,GAAU,aAC/C,IAAU,MACV,SAAU,EAIN,EAAqB,GAA6C,CACvE,GAAI,CAAC,GAAS,OAAO,GAAU,SAC9B,MAAO,GAGR,IAAM,EAAU,EAEhB,OACC,OAAO,EAAQ,SAAY,UAC3B,OAAO,EAAQ,SAAY,YAC3B,OAAO,EAAQ,aAAgB,YAC/B,OAAO,EAAQ,eAAkB,UAEnC,EAEM,EACL,GACqC,CACrC,GAAI,CAAC,GAAU,OAAO,GAAU,UAAY,OAAO,GAAU,WAC5D,MAAO,GAGR,IAAM,EAAU,EAUhB,OANC,OAAO,GAAU,YACjB,EAAQ,gCAAkC,GAEnC,GAIP,OAAO,EAAQ,SAAY,UAAY,OAAO,EAAQ,SAAY,UAEpE,EAEa,EAAyB,MACrC,EACA,IAC8B,CAC9B,IAAM,EAAgB,EAAc,CAAE,EAAI,MAAM,EAAK,EAC/C,EAAe,EAAyB,CAAa,EACxD,EAAc,QAAQ,GAAO,CAAC,CAAC,EAC/B,EACG,EAAU,EAAc,CAAY,EACvC,MAAM,EACN,EAEH,GAAI,CAAC,EAAkB,CAAO,EAC7B,MAAM,IAAI,EAAW,CACpB,QACC,yPACF,CAAC,EAGF,OAAO,CACR"}