nitro-drizzle 0.2.5 → 0.2.6

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.
@@ -1,4 +1,4 @@
1
- import { genAugmentation, genInlineTypeImport, genString, genTypeImport } from "knitwork";
1
+ import { genAugmentation, genExport, genInlineTypeImport, genString, genTypeImport } from "knitwork";
2
2
  //#region src/context/internal/templates.ts
3
3
  function runtimeDeclarations(datasources) {
4
4
  if (!datasources.length) return "";
@@ -12,30 +12,29 @@ function runtimeDeclarations(datasources) {
12
12
  function genSchemaType(imports) {
13
13
  return imports.map((id) => `typeof import('${id}')`).join(" & ");
14
14
  }
15
- function moduleDeclarations(datasources) {
16
- if (!datasources.length) return "";
17
- return [genAugmentation("nitro-drizzle/module", { DatasourceOptions: [{}, { extends: `Record<${datasources.map((d) => d.name).map((n) => genString(n)).join(" | ")}, { connector: string; }>` }] })].join("\n");
15
+ function genReference(reference) {
16
+ return `/// <reference ${Object.entries(reference).map(([prop, value]) => `${prop}="${value}"`).join("")} />`;
18
17
  }
19
- function augmentations(datasources) {
20
- return { "nitro-drizzle/augmentations.d.ts": [
21
- runtimeDeclarations(datasources),
22
- moduleDeclarations(datasources),
18
+ function moduleTypeDeclarations(datasources) {
19
+ if (!datasources.length) return {};
20
+ const names = datasources.map((d) => d.name);
21
+ const moduleId = "nitro-drizzle/module";
22
+ return { "nitro-drizzle/module.d.ts": [
23
+ genReference({ types: moduleId }),
24
+ genAugmentation(moduleId, { DatasourceOptions: [{}, { extends: `Record<${names.map((n) => genString(n)).join(" | ")}, { connector: string; }>` }] }),
23
25
  `export {};`
24
26
  ].join("\n") };
25
27
  }
26
28
  function dialectDeclarations(datasources) {
27
29
  return datasources.filter((d) => d.enabled).map(({ name, dialect }) => {
28
30
  return `
29
- declare module "#nitro-drizzle/dialects/${name}" {
30
- export * from "nitro-drizzle/dialects/${dialect}";
31
- }
32
- `;
31
+ declare module "#nitro-drizzle/dialects/${name}" {
32
+ ${genExport(`nitro-drizzle/dialects/${dialect}`, "*")}
33
+ }
34
+ `;
33
35
  }).join("\n");
34
36
  }
35
- function declarations(datasources) {
36
- return { "#nitro-drizzle/*": { "nitro-drizzle/declarations.d.ts": [dialectDeclarations(datasources)].join("\n") } };
37
- }
38
37
  //#endregion
39
- export { augmentations, declarations };
38
+ export { dialectDeclarations, genReference, moduleTypeDeclarations, runtimeDeclarations };
40
39
 
41
40
  //# sourceMappingURL=templates.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"templates.mjs","names":[],"sources":["../../../src/context/internal/templates.ts"],"sourcesContent":["import { genTypeImport, genAugmentation, genInlineTypeImport, genString } from \"knitwork\";\nimport type { DatasourceInfo } from \"..\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nfunction runtimeDeclarations(datasources: DatasourceInfo[]) {\n if (!datasources.length) {\n return \"\";\n }\n\n return [\n genTypeImport(\"nitro-drizzle/runtime\", [\"ToDatasourceProvider\"]),\n genAugmentation(\"nitro-drizzle/runtime\", {\n DatasourceRegistry: Object.fromEntries(\n datasources\n .filter((datasource) => {\n return datasource.enabled;\n })\n .map(({ name, imports }): [string, string] => {\n const schemaType = genSchemaType(imports.schema);\n const driverType = genInlineTypeImport(imports.connector);\n return [name, `${\"ToDatasourceProvider\"}<${schemaType}, ${driverType}<${schemaType}>>`];\n }),\n ),\n }),\n ].join(\"\\n\");\n}\n\nfunction genSchemaType(imports: string[]) {\n return imports.map((id) => `typeof import('${id}')`).join(\" & \");\n}\n\nfunction moduleDeclarations(datasources: DatasourceInfo[]) {\n if (!datasources.length) {\n return \"\";\n }\n\n const names = datasources.map((d) => d.name);\n\n return [\n genAugmentation(\"nitro-drizzle/module\", {\n DatasourceOptions: [\n {},\n {\n extends: `Record<${names.map((n) => genString(n)).join(\" | \")}, { connector: string; }>`,\n },\n ],\n }),\n ].join(\"\\n\");\n}\n\nexport function augmentations(datasources: DatasourceInfo[]): VirtualModules<`${string}.d.ts`> {\n return {\n \"nitro-drizzle/augmentations.d.ts\": [\n runtimeDeclarations(datasources),\n moduleDeclarations(datasources),\n /* ts */ `export {};`,\n ].join(\"\\n\"),\n };\n}\n\nfunction dialectDeclarations(datasources: DatasourceInfo[]) {\n return datasources\n .filter((d) => d.enabled)\n .map(({ name, dialect }) => {\n return /*ts*/ `\n declare module \"#nitro-drizzle/dialects/${name}\" {\n export * from \"nitro-drizzle/dialects/${dialect}\";\n }\n `;\n })\n .join(\"\\n\");\n}\n\nexport function declarations(\n datasources: DatasourceInfo[],\n): Record<string, VirtualModules<`${string}.d.ts`>> {\n return {\n \"#nitro-drizzle/*\": {\n \"nitro-drizzle/declarations.d.ts\": [dialectDeclarations(datasources)].join(\"\\n\"),\n },\n };\n}\n"],"mappings":";;AAIA,SAAS,oBAAoB,aAA+B;CAC1D,IAAI,CAAC,YAAY,QACf,OAAO;CAGT,OAAO,CACL,cAAc,yBAAyB,CAAC,sBAAsB,CAAC,GAC/D,gBAAgB,yBAAyB,EACvC,oBAAoB,OAAO,YACzB,YACG,QAAQ,eAAe;EACtB,OAAO,WAAW;CACpB,CAAC,CAAC,CACD,KAAK,EAAE,MAAM,cAAgC;EAC5C,MAAM,aAAa,cAAc,QAAQ,MAAM;EAE/C,OAAO,CAAC,MAAM,wBAA6B,WAAW,IADnC,oBAAoB,QAAQ,SACoB,EAAE,GAAG,WAAW,GAAG;CACxF,CAAC,CACL,EACF,CAAC,CACH,CAAC,CAAC,KAAK,IAAI;AACb;AAEA,SAAS,cAAc,SAAmB;CACxC,OAAO,QAAQ,KAAK,OAAO,kBAAkB,GAAG,GAAG,CAAC,CAAC,KAAK,KAAK;AACjE;AAEA,SAAS,mBAAmB,aAA+B;CACzD,IAAI,CAAC,YAAY,QACf,OAAO;CAKT,OAAO,CACL,gBAAgB,wBAAwB,EACtC,mBAAmB,CACjB,CAAC,GACD,EACE,SAAS,UAPH,YAAY,KAAK,MAAM,EAAE,IAOR,CAAC,CAAC,KAAK,MAAM,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE,2BAChE,CACF,EACF,CAAC,CACH,CAAC,CAAC,KAAK,IAAI;AACb;AAEA,SAAgB,cAAc,aAAiE;CAC7F,OAAO,EACL,oCAAoC;EAClC,oBAAoB,WAAW;EAC/B,mBAAmB,WAAW;EACrB;CACX,CAAC,CAAC,KAAK,IAAI,EACb;AACF;AAEA,SAAS,oBAAoB,aAA+B;CAC1D,OAAO,YACJ,QAAQ,MAAM,EAAE,OAAO,CAAC,CACxB,KAAK,EAAE,MAAM,cAAc;EAC1B,OAAc;gDAC4B,KAAK;gDACL,QAAQ;;;CAGpD,CAAC,CAAC,CACD,KAAK,IAAI;AACd;AAEA,SAAgB,aACd,aACkD;CAClD,OAAO,EACL,oBAAoB,EAClB,mCAAmC,CAAC,oBAAoB,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,EACjF,EACF;AACF"}
1
+ {"version":3,"file":"templates.mjs","names":[],"sources":["../../../src/context/internal/templates.ts"],"sourcesContent":["import {\n genTypeImport,\n genAugmentation,\n genInlineTypeImport,\n genString,\n genExport,\n} from \"knitwork\";\nimport type { DatasourceInfo } from \"..\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nexport function runtimeDeclarations(datasources: DatasourceInfo[]) {\n if (!datasources.length) {\n return \"\";\n }\n\n return [\n genTypeImport(\"nitro-drizzle/runtime\", [\"ToDatasourceProvider\"]),\n genAugmentation(\"nitro-drizzle/runtime\", {\n DatasourceRegistry: Object.fromEntries(\n datasources\n .filter((datasource) => {\n return datasource.enabled;\n })\n .map(({ name, imports }): [string, string] => {\n const schemaType = genSchemaType(imports.schema);\n const driverType = genInlineTypeImport(imports.connector);\n return [name, `${\"ToDatasourceProvider\"}<${schemaType}, ${driverType}<${schemaType}>>`];\n }),\n ),\n }),\n ].join(\"\\n\");\n}\n\nfunction genSchemaType(imports: string[]) {\n return imports.map((id) => `typeof import('${id}')`).join(\" & \");\n}\n\nexport type TypeReference = { types: string };\n\nexport type PathReference = { path: string };\n\nexport function genReference(reference: TypeReference | PathReference) {\n return /* ts */ `/// <reference ${Object.entries(reference)\n .map(([prop, value]) => `${prop}=\"${value}\"`)\n .join(\"\")} />`;\n}\n\nexport function moduleTypeDeclarations(\n datasources: DatasourceInfo[],\n): VirtualModules<`${string}.d.ts`> {\n if (!datasources.length) {\n return {};\n }\n\n const names = datasources.map((d) => d.name);\n\n const moduleId = \"nitro-drizzle/module\";\n\n const content = [\n genReference({ types: moduleId }),\n genAugmentation(moduleId, {\n DatasourceOptions: [\n {},\n {\n extends: `Record<${names.map((n) => genString(n)).join(\" | \")}, { connector: string; }>`,\n },\n ],\n }),\n /* ts */ `export {};`,\n ].join(\"\\n\");\n\n return {\n \"nitro-drizzle/module.d.ts\": content,\n };\n}\n\nexport function dialectDeclarations(datasources: DatasourceInfo[]) {\n return datasources\n .filter((d) => d.enabled)\n .map(({ name, dialect }) => {\n return /*ts*/ `\n declare module \"#nitro-drizzle/dialects/${name}\" {\n ${genExport(`nitro-drizzle/dialects/${dialect}`, \"*\")}\n }\n `;\n })\n .join(\"\\n\");\n}\n"],"mappings":";;AAUA,SAAgB,oBAAoB,aAA+B;CACjE,IAAI,CAAC,YAAY,QACf,OAAO;CAGT,OAAO,CACL,cAAc,yBAAyB,CAAC,sBAAsB,CAAC,GAC/D,gBAAgB,yBAAyB,EACvC,oBAAoB,OAAO,YACzB,YACG,QAAQ,eAAe;EACtB,OAAO,WAAW;CACpB,CAAC,CAAC,CACD,KAAK,EAAE,MAAM,cAAgC;EAC5C,MAAM,aAAa,cAAc,QAAQ,MAAM;EAE/C,OAAO,CAAC,MAAM,wBAA6B,WAAW,IADnC,oBAAoB,QAAQ,SACoB,EAAE,GAAG,WAAW,GAAG;CACxF,CAAC,CACL,EACF,CAAC,CACH,CAAC,CAAC,KAAK,IAAI;AACb;AAEA,SAAS,cAAc,SAAmB;CACxC,OAAO,QAAQ,KAAK,OAAO,kBAAkB,GAAG,GAAG,CAAC,CAAC,KAAK,KAAK;AACjE;AAMA,SAAgB,aAAa,WAA0C;CACrE,OAAgB,kBAAkB,OAAO,QAAQ,SAAS,CAAC,CACxD,KAAK,CAAC,MAAM,WAAW,GAAG,KAAK,IAAI,MAAM,EAAE,CAAC,CAC5C,KAAK,EAAE,EAAE;AACd;AAEA,SAAgB,uBACd,aACkC;CAClC,IAAI,CAAC,YAAY,QACf,OAAO,CAAC;CAGV,MAAM,QAAQ,YAAY,KAAK,MAAM,EAAE,IAAI;CAE3C,MAAM,WAAW;CAejB,OAAO,EACL,6BAdc;EACd,aAAa,EAAE,OAAO,SAAS,CAAC;EAChC,gBAAgB,UAAU,EACxB,mBAAmB,CACjB,CAAC,GACD,EACE,SAAS,UAAU,MAAM,KAAK,MAAM,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE,2BAChE,CACF,EACF,CAAC;EACQ;CACX,CAAC,CAAC,KAAK,IAG8B,EACrC;AACF;AAEA,SAAgB,oBAAoB,aAA+B;CACjE,OAAO,YACJ,QAAQ,MAAM,EAAE,OAAO,CAAC,CACxB,KAAK,EAAE,MAAM,cAAc;EAC1B,OAAc;kDAC8B,KAAK;YAC3C,UAAU,0BAA0B,WAAW,GAAG,EAAE;;;CAG5D,CAAC,CAAC,CACD,KAAK,IAAI;AACd"}
@@ -1,71 +1,67 @@
1
- import { genImport, genObjectFromRaw, genSafeVariableName, genString } from "knitwork";
1
+ import { genExport, genImport, genObjectFromRaw, genObjectFromRawEntries, genSafeVariableName } from "knitwork";
2
2
  //#region src/context/internal/virtual.ts
3
- function genDatabaseModuleVariableName(dbModuleIndex) {
3
+ function genDatasourceModuleVariableName(dbModuleIndex) {
4
4
  return genSafeVariableName(`dbModule${dbModuleIndex}`);
5
5
  }
6
+ function genMergedSchemaVariableName(dbModuleIndex) {
7
+ return genSafeVariableName(`mergedSchema${dbModuleIndex}`);
8
+ }
6
9
  const genSchemaModuleVariableName = (dbModuleIndex, schemaIndex) => genSafeVariableName(`schemaModule${dbModuleIndex}_${schemaIndex}`);
7
10
  function mergeSchemaModules(schemaIds, dbModuleIndex) {
8
- return schemaIds.map((_, schemaModuleIdIndex) => {
9
- return ", " + genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex);
10
- }).join("");
11
+ return `
12
+ Object.assign(
13
+ ${[`{}`, ...schemaIds.map((_, schemaModuleIdIndex) => genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex))].join(",")}
14
+ )
15
+ `;
11
16
  }
12
17
  function runtimeVirtualModule(datasources) {
13
- const parts = datasources.map(({ name, imports: { schema, connector } }, dbModuleIndex) => {
14
- const databaseModuleVariableName = genDatabaseModuleVariableName(dbModuleIndex);
18
+ const parts = datasources.filter((datasource) => datasource.enabled).map(({ name, imports: { schema, connector } }, datasourceIndex) => {
19
+ const datasourceModuleVariableName = genDatasourceModuleVariableName(datasourceIndex);
20
+ const imports = [genImport(connector, datasourceModuleVariableName), ...schema.map((schemaModuleId, schemaIndex) => {
21
+ return genImport(schemaModuleId, {
22
+ name: "*",
23
+ as: genSchemaModuleVariableName(datasourceIndex, schemaIndex)
24
+ });
25
+ })];
26
+ const mergedSchemaVariableName = genMergedSchemaVariableName(datasourceIndex);
15
27
  return {
16
- imports: [genImport(connector, databaseModuleVariableName), ...schema.map((schemaModuleId, schemaModuleIdIndex) => {
17
- return genImport(schemaModuleId, {
18
- name: "*",
19
- as: genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex)
20
- });
21
- })].join("\n"),
22
- code: `
23
- datasourceRegistry[${genString(name)}] = ${genObjectFromRaw({
24
- create: `function (config) { return ${databaseModuleVariableName}(config, this.schema) }`,
25
- schema: `Object.assign({}${mergeSchemaModules(schema, dbModuleIndex)})`
26
- })};
27
- `
28
+ imports,
29
+ schemaDefinitions: [`const ${mergedSchemaVariableName} = ${mergeSchemaModules(schema, datasourceIndex)};`],
30
+ entries: [[name, genObjectFromRaw({
31
+ create: `(config) => ${datasourceModuleVariableName}(config, ${mergedSchemaVariableName})`,
32
+ schema: mergedSchemaVariableName
33
+ })]]
28
34
  };
29
- }).reduce((acc, { imports, code }) => {
35
+ }).reduce((acc, { imports, schemaDefinitions, entries }) => {
30
36
  return {
31
- imports: [...acc.imports, imports],
32
- code: [...acc.code, code]
37
+ imports: [...acc.imports, ...imports],
38
+ schemaDefinitions: [...acc.schemaDefinitions, ...schemaDefinitions],
39
+ entries: [...acc.entries, ...entries]
33
40
  };
34
41
  }, {
35
42
  imports: [],
36
- code: []
43
+ schemaDefinitions: [],
44
+ entries: []
37
45
  });
38
46
  const datasourceRegistryVarName = genSafeVariableName("datasourceRegistry");
39
47
  return `
40
- ${parts.imports.join("\n")}
41
-
42
- const ${datasourceRegistryVarName} = {};
43
-
44
- ${parts.code.join("\n")}
48
+ ${parts.imports.join("\n")}
49
+
50
+ ${parts.schemaDefinitions.join("\n")}
51
+
52
+ const ${datasourceRegistryVarName} = ${genObjectFromRawEntries(parts.entries)};
45
53
 
46
- export function useDatasourceRegistry() {
47
- return ${datasourceRegistryVarName};
48
- }
49
- `;
50
- }
51
- /**
52
- * Generates virtual modules for runtime datasource access.
53
- * Creates module code that registers datasource providers and exposes a registry.
54
- * @param datasources - Array of datasource information
55
- * @returns Object mapping module IDs to their code
56
- */
57
- function virtualModules(datasources) {
58
- return {
59
- "#nitro-drizzle/runtime": runtimeVirtualModule(datasources),
60
- ...dialectVirtualModules(datasources)
61
- };
54
+ export function useDatasourceRegistry() {
55
+ return ${datasourceRegistryVarName};
56
+ }
57
+ `;
62
58
  }
63
59
  function dialectVirtualModules(datasources) {
64
60
  return Object.fromEntries(datasources.filter((d) => d.enabled).map(({ name, dialect }) => {
65
- return [`#nitro-drizzle/dialects/${name}`, `export * from "nitro-drizzle/dialects/${dialect}";`];
61
+ return [`#nitro-drizzle/dialects/${name}`, genExport(`nitro-drizzle/dialects/${dialect}`, "*")];
66
62
  }));
67
63
  }
68
64
  //#endregion
69
- export { virtualModules };
65
+ export { dialectVirtualModules, runtimeVirtualModule };
70
66
 
71
67
  //# sourceMappingURL=virtual.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"virtual.mjs","names":[],"sources":["../../../src/context/internal/virtual.ts"],"sourcesContent":["import { genImport, genObjectFromRaw, genSafeVariableName, genString } from \"knitwork\";\nimport type { DatasourceInfo } from \"..\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nfunction genDatabaseModuleVariableName(dbModuleIndex: number) {\n return genSafeVariableName(`dbModule${dbModuleIndex}`);\n}\n\nconst genSchemaModuleVariableName = (dbModuleIndex: number, schemaIndex: number) =>\n genSafeVariableName(`schemaModule${dbModuleIndex}_${schemaIndex}`);\n\nfunction mergeSchemaModules(schemaIds: string[], dbModuleIndex: number) {\n return schemaIds\n .map((_, schemaModuleIdIndex) => {\n return \", \" + genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex);\n })\n .join(\"\");\n}\n\nfunction runtimeVirtualModule(datasources: DatasourceInfo[]) {\n const parts = datasources\n .map(({ name, imports: { schema, connector } }, dbModuleIndex) => {\n const databaseModuleVariableName = genDatabaseModuleVariableName(dbModuleIndex);\n\n const imports = [\n genImport(connector, databaseModuleVariableName),\n ...schema.map((schemaModuleId, schemaModuleIdIndex) => {\n return genImport(schemaModuleId, {\n name: \"*\",\n as: genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex),\n });\n }),\n ].join(\"\\n\");\n\n const code = /* js */ `\n datasourceRegistry[${genString(name)}] = ${genObjectFromRaw({\n create: /* js */ `function (config) { return ${databaseModuleVariableName}(config, this.schema) }`,\n schema: `Object.assign({}${mergeSchemaModules(schema, dbModuleIndex)})`,\n })};\n `;\n\n return {\n imports,\n code,\n };\n })\n .reduce(\n (acc, { imports, code }) => {\n return {\n imports: [...acc.imports, imports],\n code: [...acc.code, code],\n };\n },\n { imports: [] as string[], code: [] as string[] },\n );\n\n const datasourceRegistryVarName = genSafeVariableName(\"datasourceRegistry\");\n\n return /* js */ `\n ${parts.imports.join(\"\\n\")}\n \n const ${datasourceRegistryVarName} = {};\n \n ${parts.code.join(\"\\n\")}\n\n export function useDatasourceRegistry() {\n return ${datasourceRegistryVarName};\n }\n `;\n}\n\n/**\n * Generates virtual modules for runtime datasource access.\n * Creates module code that registers datasource providers and exposes a registry.\n * @param datasources - Array of datasource information\n * @returns Object mapping module IDs to their code\n */\nexport function virtualModules(datasources: DatasourceInfo[]): VirtualModules {\n return {\n \"#nitro-drizzle/runtime\": runtimeVirtualModule(datasources),\n ...dialectVirtualModules(datasources),\n };\n}\n\n// Replaced with aliases\nfunction dialectVirtualModules(datasources: DatasourceInfo[]): VirtualModules {\n return Object.fromEntries(\n datasources\n .filter((d) => d.enabled)\n .map(({ name, dialect }) => {\n return [\n `#nitro-drizzle/dialects/${name}`,\n `export * from \"nitro-drizzle/dialects/${dialect}\";`,\n ];\n }),\n );\n}\n"],"mappings":";;AAIA,SAAS,8BAA8B,eAAuB;CAC5D,OAAO,oBAAoB,WAAW,eAAe;AACvD;AAEA,MAAM,+BAA+B,eAAuB,gBAC1D,oBAAoB,eAAe,cAAc,GAAG,aAAa;AAEnE,SAAS,mBAAmB,WAAqB,eAAuB;CACtE,OAAO,UACJ,KAAK,GAAG,wBAAwB;EAC/B,OAAO,OAAO,4BAA4B,eAAe,mBAAmB;CAC9E,CAAC,CAAC,CACD,KAAK,EAAE;AACZ;AAEA,SAAS,qBAAqB,aAA+B;CAC3D,MAAM,QAAQ,YACX,KAAK,EAAE,MAAM,SAAS,EAAE,QAAQ,eAAe,kBAAkB;EAChE,MAAM,6BAA6B,8BAA8B,aAAa;EAmB9E,OAAO;GACL,SAlBc,CACd,UAAU,WAAW,0BAA0B,GAC/C,GAAG,OAAO,KAAK,gBAAgB,wBAAwB;IACrD,OAAO,UAAU,gBAAgB;KAC/B,MAAM;KACN,IAAI,4BAA4B,eAAe,mBAAmB;IACpE,CAAC;GACH,CAAC,CACH,CAAC,CAAC,KAAK,IAUC;GACN,MAAA;4BARoB,UAAU,IAAI,EAAE,MAAM,iBAAiB;IAC1D,QAAiB,8BAA8B,2BAA2B;IAC1E,QAAQ,mBAAmB,mBAAmB,QAAQ,aAAa,EAAE;GACvE,CAAC,EAAE;;EAMJ;CACF,CAAC,CAAC,CACD,QACE,KAAK,EAAE,SAAS,WAAW;EAC1B,OAAO;GACL,SAAS,CAAC,GAAG,IAAI,SAAS,OAAO;GACjC,MAAM,CAAC,GAAG,IAAI,MAAM,IAAI;EAC1B;CACF,GACA;EAAE,SAAS,CAAC;EAAe,MAAM,CAAC;CAAc,CAClD;CAEF,MAAM,4BAA4B,oBAAoB,oBAAoB;CAE1E,OAAgB;OACX,MAAM,QAAQ,KAAK,IAAI,EAAE;;aAEnB,0BAA0B;;OAEhC,MAAM,KAAK,KAAK,IAAI,EAAE;;;gBAGb,0BAA0B;;;AAG1C;;;;;;;AAQA,SAAgB,eAAe,aAA+C;CAC5E,OAAO;EACL,0BAA0B,qBAAqB,WAAW;EAC1D,GAAG,sBAAsB,WAAW;CACtC;AACF;AAGA,SAAS,sBAAsB,aAA+C;CAC5E,OAAO,OAAO,YACZ,YACG,QAAQ,MAAM,EAAE,OAAO,CAAC,CACxB,KAAK,EAAE,MAAM,cAAc;EAC1B,OAAO,CACL,2BAA2B,QAC3B,yCAAyC,QAAQ,GACnD;CACF,CAAC,CACL;AACF"}
1
+ {"version":3,"file":"virtual.mjs","names":[],"sources":["../../../src/context/internal/virtual.ts"],"sourcesContent":["import {\n genExport,\n genImport,\n genObjectFromRaw,\n genObjectFromRawEntries,\n genSafeVariableName,\n} from \"knitwork\";\nimport type { DatasourceInfo } from \"..\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nfunction genDatasourceModuleVariableName(dbModuleIndex: number) {\n return genSafeVariableName(`dbModule${dbModuleIndex}`);\n}\n\nfunction genMergedSchemaVariableName(dbModuleIndex: number) {\n return genSafeVariableName(`mergedSchema${dbModuleIndex}`);\n}\n\nconst genSchemaModuleVariableName = (dbModuleIndex: number, schemaIndex: number) =>\n genSafeVariableName(`schemaModule${dbModuleIndex}_${schemaIndex}`);\n\nfunction mergeSchemaModules(schemaIds: string[], dbModuleIndex: number) {\n return /* js */ `\n Object.assign(\n ${[\n /* js */ `{}`,\n ...schemaIds.map((_, schemaModuleIdIndex) =>\n genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex),\n ),\n ].join(\",\")}\n )\n `;\n}\n\nexport function runtimeVirtualModule(datasources: DatasourceInfo[]) {\n const parts = datasources\n .filter((datasource) => datasource.enabled)\n .map(({ name, imports: { schema, connector } }, datasourceIndex) => {\n const datasourceModuleVariableName = genDatasourceModuleVariableName(datasourceIndex);\n\n const imports = [\n genImport(connector, datasourceModuleVariableName),\n ...schema.map((schemaModuleId, schemaIndex) => {\n return genImport(schemaModuleId, {\n name: \"*\",\n as: genSchemaModuleVariableName(datasourceIndex, schemaIndex),\n });\n }),\n ];\n\n const mergedSchemaVariableName = genMergedSchemaVariableName(datasourceIndex);\n const mergedSchemaObject = mergeSchemaModules(schema, datasourceIndex);\n\n const schemaDefinitions = [\n /* js */ `const ${mergedSchemaVariableName} = ${mergedSchemaObject};`,\n ];\n\n const entries: [string, string][] = [\n [\n name,\n genObjectFromRaw({\n create: /* js */ `(config) => ${datasourceModuleVariableName}(config, ${mergedSchemaVariableName})`,\n schema: mergedSchemaVariableName,\n }),\n ],\n ];\n\n return {\n imports,\n schemaDefinitions,\n entries,\n };\n })\n .reduce(\n (acc, { imports, schemaDefinitions, entries }) => {\n return {\n imports: [...acc.imports, ...imports],\n schemaDefinitions: [...acc.schemaDefinitions, ...schemaDefinitions],\n entries: [...acc.entries, ...entries],\n };\n },\n {\n imports: [] as string[],\n schemaDefinitions: [] as string[],\n entries: [] as [string, string][],\n },\n );\n\n const datasourceRegistryVarName = genSafeVariableName(\"datasourceRegistry\");\n\n return /* js */ `\n ${parts.imports.join(\"\\n\")}\n \n ${parts.schemaDefinitions.join(\"\\n\")}\n \n const ${datasourceRegistryVarName} = ${genObjectFromRawEntries(parts.entries)};\n\n export function useDatasourceRegistry() {\n return ${datasourceRegistryVarName};\n }\n `;\n}\n\n// Replaced with aliases\nexport function dialectVirtualModules(\n datasources: DatasourceInfo[],\n): VirtualModules<`#nitro-drizzle/${string}`> {\n return Object.fromEntries(\n datasources\n .filter((d) => d.enabled)\n .map(({ name, dialect }) => {\n return [\n `#nitro-drizzle/dialects/${name}`,\n genExport(`nitro-drizzle/dialects/${dialect}`, \"*\"),\n ] as const;\n }),\n );\n}\n"],"mappings":";;AAUA,SAAS,gCAAgC,eAAuB;CAC9D,OAAO,oBAAoB,WAAW,eAAe;AACvD;AAEA,SAAS,4BAA4B,eAAuB;CAC1D,OAAO,oBAAoB,eAAe,eAAe;AAC3D;AAEA,MAAM,+BAA+B,eAAuB,gBAC1D,oBAAoB,eAAe,cAAc,GAAG,aAAa;AAEnE,SAAS,mBAAmB,WAAqB,eAAuB;CACtE,OAAgB;;QAEV,CACS,MACT,GAAG,UAAU,KAAK,GAAG,wBACnB,4BAA4B,eAAe,mBAAmB,CAChE,CACF,CAAC,CAAC,KAAK,GAAG,EAAE;;;AAGlB;AAEA,SAAgB,qBAAqB,aAA+B;CAClE,MAAM,QAAQ,YACX,QAAQ,eAAe,WAAW,OAAO,CAAC,CAC1C,KAAK,EAAE,MAAM,SAAS,EAAE,QAAQ,eAAe,oBAAoB;EAClE,MAAM,+BAA+B,gCAAgC,eAAe;EAEpF,MAAM,UAAU,CACd,UAAU,WAAW,4BAA4B,GACjD,GAAG,OAAO,KAAK,gBAAgB,gBAAgB;GAC7C,OAAO,UAAU,gBAAgB;IAC/B,MAAM;IACN,IAAI,4BAA4B,iBAAiB,WAAW;GAC9D,CAAC;EACH,CAAC,CACH;EAEA,MAAM,2BAA2B,4BAA4B,eAAe;EAiB5E,OAAO;GACL;GACA,mBAAA,CAfS,SAAS,yBAAyB,KAHlB,mBAAmB,QAAQ,eAGa,EAAE,EAenD;GAChB,SAAA,CAZA,CACE,MACA,iBAAiB;IACf,QAAiB,eAAe,6BAA6B,WAAW,yBAAyB;IACjG,QAAQ;GACV,CAAC,CACH,CAMM;EACR;CACF,CAAC,CAAC,CACD,QACE,KAAK,EAAE,SAAS,mBAAmB,cAAc;EAChD,OAAO;GACL,SAAS,CAAC,GAAG,IAAI,SAAS,GAAG,OAAO;GACpC,mBAAmB,CAAC,GAAG,IAAI,mBAAmB,GAAG,iBAAiB;GAClE,SAAS,CAAC,GAAG,IAAI,SAAS,GAAG,OAAO;EACtC;CACF,GACA;EACE,SAAS,CAAC;EACV,mBAAmB,CAAC;EACpB,SAAS,CAAC;CACZ,CACF;CAEF,MAAM,4BAA4B,oBAAoB,oBAAoB;CAE1E,OAAgB;MACZ,MAAM,QAAQ,KAAK,IAAI,EAAE;;MAEzB,MAAM,kBAAkB,KAAK,IAAI,EAAE;;YAE7B,0BAA0B,KAAK,wBAAwB,MAAM,OAAO,EAAE;;;eAGnE,0BAA0B;;;AAGzC;AAGA,SAAgB,sBACd,aAC4C;CAC5C,OAAO,OAAO,YACZ,YACG,QAAQ,MAAM,EAAE,OAAO,CAAC,CACxB,KAAK,EAAE,MAAM,cAAc;EAC1B,OAAO,CACL,2BAA2B,QAC3B,UAAU,0BAA0B,WAAW,GAAG,CACpD;CACF,CAAC,CACL;AACF"}
@@ -15,15 +15,13 @@ interface Context {
15
15
  * Returns a list of all resolved datasource information.
16
16
  */
17
17
  datasources(): MaybePromise<DatasourceInfo[]>;
18
- /**
19
- * Returns type declaration code for all datasources.
20
- */
21
- augmentations(): MaybePromise<VirtualModules>;
22
- declarations(): MaybePromise<Record<string, VirtualModules>>;
23
18
  /**
24
19
  * Returns virtual module code for runtime datasource access.
25
20
  */
26
21
  virtualModules(): MaybePromise<VirtualModules>;
22
+ virtualTypeDeclarations(): MaybePromise<Record<string, VirtualModules>>;
23
+ moduleTypeDeclarations(): MaybePromise<VirtualModules<`${string}.d.ts`>>;
24
+ runtimeTypeDeclarations(): MaybePromise<VirtualModules<`${string}.d.ts`>>;
27
25
  }
28
26
  /**
29
27
  * Glob pattern or array of patterns to match Drizzle configuration files.
@@ -74,6 +72,7 @@ interface ContextOptions {
74
72
  datasource: Record<string, {
75
73
  connector: string;
76
74
  }>;
75
+ plugins: readonly string[];
77
76
  }
78
77
  /**
79
78
  * Creates a Context instance for managing Drizzle datasource configurations.
package/dist/context.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  import { mapAsync } from "./context/internal/async.mjs";
2
2
  import { accent } from "./context/internal/logger.mjs";
3
3
  import { resolveFiles } from "./context/internal/fs.mjs";
4
- import { augmentations, declarations } from "./context/internal/templates.mjs";
4
+ import { dialectDeclarations, genReference, moduleTypeDeclarations, runtimeDeclarations } from "./context/internal/templates.mjs";
5
5
  import { transformDrizzleConfig } from "./context/internal/config.mjs";
6
- import { virtualModules } from "./context/internal/virtual.mjs";
6
+ import { dialectVirtualModules, runtimeVirtualModule } from "./context/internal/virtual.mjs";
7
7
  import { colorize } from "consola/utils";
8
8
  //#region src/context/index.ts
9
9
  var DefaultContext = class {
@@ -51,14 +51,28 @@ var DefaultContext = class {
51
51
  }
52
52
  return this.#datasources;
53
53
  }
54
- async augmentations() {
55
- return augmentations(await this.datasources());
54
+ async virtualTypeDeclarations() {
55
+ return { "#nitro-drizzle/*": { "nitro-drizzle/virtual.d.ts": [dialectDeclarations([...await this.datasources()].filter((d) => d.enabled))].join("\n") } };
56
56
  }
57
- async declarations() {
58
- return declarations(await this.datasources());
57
+ async moduleTypeDeclarations() {
58
+ return moduleTypeDeclarations(await this.datasources());
59
+ }
60
+ async runtimeTypeDeclarations() {
61
+ const datasources = await this.datasources();
62
+ return { "nitro-drizzle/runtime.d.ts": [
63
+ ...[...(/* @__PURE__ */ new Set([{ types: "nitro-drizzle/runtime" }, ...this.#options.plugins.map((pluginId) => {
64
+ return { types: pluginId };
65
+ })])).values()].map((reference) => genReference(reference)),
66
+ runtimeDeclarations(datasources),
67
+ `export {};`
68
+ ].join("\n") };
59
69
  }
60
70
  async virtualModules() {
61
- return virtualModules(await this.datasources());
71
+ const datasources = await this.datasources();
72
+ return {
73
+ "#nitro-drizzle/runtime": runtimeVirtualModule(datasources),
74
+ ...dialectVirtualModules(datasources)
75
+ };
62
76
  }
63
77
  reload() {
64
78
  this.#datasources = null;
@@ -1 +1 @@
1
- {"version":3,"file":"context.mjs","names":["#options","#datasources"],"sources":["../src/context/index.ts"],"sourcesContent":["import type { ConsolaInstance } from \"consola\";\nimport { colorize, type ColorName } from \"consola/utils\";\nimport type { Config, Config as DrizzleConfig } from \"drizzle-kit\";\n\nimport { mapAsync } from \"./internal/async\";\nimport { accent } from \"./internal/logger\";\nimport { resolveFiles } from \"./internal/fs\";\nimport { augmentations, declarations } from \"./internal/templates\";\nimport { transformDrizzleConfig } from \"./internal/config\";\nimport { virtualModules } from \"./internal/virtual\";\n\nimport type { MaybePromise, VirtualModules } from \"nitro-drizzle/shared\";\n\n/**\n * Context interface for managing Drizzle datasource configurations.\n */\nexport interface Context {\n /**\n * Reloads the context, clearing any cached datasource information.\n */\n reload(): MaybePromise<void>;\n /**\n * Returns a list of all resolved datasource information.\n */\n datasources(): MaybePromise<DatasourceInfo[]>;\n /**\n * Returns type declaration code for all datasources.\n */\n augmentations(): MaybePromise<VirtualModules>;\n\n declarations(): MaybePromise<Record<string, VirtualModules>>;\n /**\n * Returns virtual module code for runtime datasource access.\n */\n virtualModules(): MaybePromise<VirtualModules>;\n}\n\nclass DefaultContext implements Context {\n #datasources: DatasourceInfo[] | null = null;\n readonly #options: ContextOptions;\n\n constructor(options: ContextOptions) {\n this.#options = options;\n }\n\n async datasources() {\n const {\n logger,\n baseDir,\n configPattern,\n resolver,\n datasource: datasourceOptions,\n } = this.#options;\n\n if (!this.#datasources) {\n logger?.info(\"Searching drizzle datasources in\", colorize(\"blue\", baseDir));\n\n const drizzleConfigsResolvedPaths = await resolveFiles(\n baseDir,\n [configPattern].flat().map((pattern) => \"*/\" + pattern),\n );\n\n let datasources: DatasourceInfo[] = await mapAsync(\n drizzleConfigsResolvedPaths,\n async (path) => {\n const config = await this.#options.resolver.import<DrizzleConfig>(path, {\n default: true,\n });\n const [_, dirName] = path.match(/(.+\\/(.+))\\/.+$/)!.slice(1, 3) as [string, string];\n\n return await transformDrizzleConfig(config, {\n cwd: this.#options.cwd,\n path,\n dirName,\n resolver,\n });\n },\n );\n\n datasources = Object.entries(datasourceOptions).reduce((_datasources, [name, options]) => {\n const datasource = datasources.find(\n (d) => d.name == name && options.connector == (d.driver ? d.driver : d.dialect),\n );\n if (!datasource) {\n throw new Error(\"Unable to resolve datasource \" + name);\n }\n return [..._datasources, datasource];\n }, [] as DatasourceInfo[]);\n\n logger?.info(\n \"Found drizzle datasources:\",\n datasources\n .toSorted((datasource) => {\n return datasource.enabled ? -1 : 1;\n })\n .map((datasource) => {\n let msg = [datasource.name];\n let color: ColorName;\n if (datasource.enabled) {\n color = \"greenBright\";\n } else {\n color = \"gray\";\n msg.push(\"(disabled)\");\n }\n return colorize(color, msg.join(\" \"));\n })\n .join(\", \"),\n );\n\n const enabledDatasources = datasources.filter((d) => d.enabled);\n\n logger?.info(\n accent`Using ${enabledDatasources.length} of ${datasources.length} resolved datasources` +\n (enabledDatasources.length > 0 ? \":\" : \"\"),\n enabledDatasources\n .map((datasource) => {\n return [\n colorize(\"greenBright\", datasource.name),\n colorize(\"yellow\", \"(\" + (datasource.driver || datasource.dialect) + \")\"),\n ].join(\" \");\n })\n .join(\", \"),\n );\n\n this.#datasources = datasources;\n }\n\n return this.#datasources;\n }\n\n async augmentations(): Promise<VirtualModules> {\n return augmentations(await this.datasources());\n }\n\n async declarations(): Promise<Record<string, VirtualModules>> {\n return declarations(await this.datasources());\n }\n\n async virtualModules(): Promise<VirtualModules> {\n return virtualModules(await this.datasources());\n }\n\n reload() {\n this.#datasources = null;\n }\n}\n\n/**\n * Glob pattern or array of patterns to match Drizzle configuration files.\n */\nexport type ConfigPattern = string | readonly string[];\n\n/**\n * Resolver interface for importing modules and resolving paths.\n */\nexport interface Resolver {\n /**\n * Resolves a module ID to its full path.\n */\n resolve(id: string): string;\n /**\n * Attempts to resolve a module ID, returns undefined if not found.\n */\n tryResolve(id: string): string | undefined;\n /**\n * Imports a module dynamically.\n * @param id - Module identifier to import\n * @param options - Import options, set default: true to import default export\n */\n import<T>(id: string, options?: { default?: boolean }): Promise<T>;\n}\n\nexport interface ContextOptions {\n logger?: ConsolaInstance;\n /**\n * Current working directory\n */\n cwd: string;\n /**\n * Base project directory to search drizzle config files\n */\n baseDir: string;\n /**\n * Pattern for drizzle config files\n */\n configPattern: ConfigPattern;\n /**\n * Nuxt Kit resolver\n */\n resolver: Resolver;\n /**\n * Connector options\n */\n datasource: Record<string, { connector: string }>;\n}\n\n/**\n * Creates a Context instance for managing Drizzle datasource configurations.\n * @param options - Configuration options for the context\n * @returns A Context instance\n */\nexport function createContext(options: ContextOptions): Context {\n return new DefaultContext(options);\n}\n\n/**\n * Information about a resolved Drizzle datasource.\n */\nexport interface DatasourceInfo {\n /** Directory name of the datasource (derived from config file location). */\n dirName: string;\n /** Unique name identifier for the datasource. */\n name: string;\n /** Whether the datasource is enabled (filtered by connector configuration). */\n enabled: boolean;\n /** Database dialect (e.g., 'sqlite', 'postgresql', 'mysql'). */\n dialect: string;\n /** Driver type if specified in config, otherwise undefined. */\n driver: string | undefined;\n /** Import paths for schema, connector, and helpers. */\n imports: {\n config: string;\n schema: string[];\n connector: string;\n helpers: string;\n };\n /** Migration configuration for this datasource. */\n migrations: {\n assets?: string;\n config?: Config[\"migrations\"];\n };\n}\n"],"mappings":";;;;;;;;AAqCA,IAAM,iBAAN,MAAwC;CACtC,eAAwC;CACxC;CAEA,YAAY,SAAyB;EACnC,KAAKA,WAAW;CAClB;CAEA,MAAM,cAAc;EAClB,MAAM,EACJ,QACA,SACA,eACA,UACA,YAAY,sBACV,KAAKA;EAET,IAAI,CAAC,KAAKC,cAAc;GACtB,QAAQ,KAAK,oCAAoC,SAAS,QAAQ,OAAO,CAAC;GAO1E,IAAI,cAAgC,MAAM,SACxC,MANwC,aACxC,SACA,CAAC,aAAa,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,YAAY,OAAO,OAAO,CACxD,GAIE,OAAO,SAAS;IACd,MAAM,SAAS,MAAM,KAAKD,SAAS,SAAS,OAAsB,MAAM,EACtE,SAAS,KACX,CAAC;IACD,MAAM,CAAC,GAAG,WAAW,KAAK,MAAM,iBAAiB,CAAC,CAAE,MAAM,GAAG,CAAC;IAE9D,OAAO,MAAM,uBAAuB,QAAQ;KAC1C,KAAK,KAAKA,SAAS;KACnB;KACA;KACA;IACF,CAAC;GACH,CACF;GAEA,cAAc,OAAO,QAAQ,iBAAiB,CAAC,CAAC,QAAQ,cAAc,CAAC,MAAM,aAAa;IACxF,MAAM,aAAa,YAAY,MAC5B,MAAM,EAAE,QAAQ,QAAQ,QAAQ,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,QACzE;IACA,IAAI,CAAC,YACH,MAAM,IAAI,MAAM,kCAAkC,IAAI;IAExD,OAAO,CAAC,GAAG,cAAc,UAAU;GACrC,GAAG,CAAC,CAAqB;GAEzB,QAAQ,KACN,8BACA,YACG,UAAU,eAAe;IACxB,OAAO,WAAW,UAAU,KAAK;GACnC,CAAC,CAAC,CACD,KAAK,eAAe;IACnB,IAAI,MAAM,CAAC,WAAW,IAAI;IAC1B,IAAI;IACJ,IAAI,WAAW,SACb,QAAQ;SACH;KACL,QAAQ;KACR,IAAI,KAAK,YAAY;IACvB;IACA,OAAO,SAAS,OAAO,IAAI,KAAK,GAAG,CAAC;GACtC,CAAC,CAAC,CACD,KAAK,IAAI,CACd;GAEA,MAAM,qBAAqB,YAAY,QAAQ,MAAM,EAAE,OAAO;GAE9D,QAAQ,KACN,MAAM,SAAS,mBAAmB,OAAO,MAAM,YAAY,OAAO,0BAC/D,mBAAmB,SAAS,IAAI,MAAM,KACzC,mBACG,KAAK,eAAe;IACnB,OAAO,CACL,SAAS,eAAe,WAAW,IAAI,GACvC,SAAS,UAAU,OAAO,WAAW,UAAU,WAAW,WAAW,GAAG,CAC1E,CAAC,CAAC,KAAK,GAAG;GACZ,CAAC,CAAC,CACD,KAAK,IAAI,CACd;GAEA,KAAKC,eAAe;EACtB;EAEA,OAAO,KAAKA;CACd;CAEA,MAAM,gBAAyC;EAC7C,OAAO,cAAc,MAAM,KAAK,YAAY,CAAC;CAC/C;CAEA,MAAM,eAAwD;EAC5D,OAAO,aAAa,MAAM,KAAK,YAAY,CAAC;CAC9C;CAEA,MAAM,iBAA0C;EAC9C,OAAO,eAAe,MAAM,KAAK,YAAY,CAAC;CAChD;CAEA,SAAS;EACP,KAAKA,eAAe;CACtB;AACF;;;;;;AAwDA,SAAgB,cAAc,SAAkC;CAC9D,OAAO,IAAI,eAAe,OAAO;AACnC"}
1
+ {"version":3,"file":"context.mjs","names":["#options","#datasources"],"sources":["../src/context/index.ts"],"sourcesContent":["import type { ConsolaInstance } from \"consola\";\nimport { colorize, type ColorName } from \"consola/utils\";\nimport type { Config, Config as DrizzleConfig } from \"drizzle-kit\";\n\nimport { mapAsync } from \"./internal/async\";\nimport { accent } from \"./internal/logger\";\nimport { resolveFiles } from \"./internal/fs\";\nimport {\n dialectDeclarations,\n genReference,\n moduleTypeDeclarations,\n runtimeDeclarations,\n} from \"./internal/templates\";\nimport { transformDrizzleConfig } from \"./internal/config\";\nimport { dialectVirtualModules, runtimeVirtualModule } from \"./internal/virtual\";\n\nimport type { MaybePromise, VirtualModules } from \"nitro-drizzle/shared\";\n\n/**\n * Context interface for managing Drizzle datasource configurations.\n */\nexport interface Context {\n /**\n * Reloads the context, clearing any cached datasource information.\n */\n reload(): MaybePromise<void>;\n /**\n * Returns a list of all resolved datasource information.\n */\n datasources(): MaybePromise<DatasourceInfo[]>;\n /**\n * Returns virtual module code for runtime datasource access.\n */\n virtualModules(): MaybePromise<VirtualModules>;\n virtualTypeDeclarations(): MaybePromise<Record<string, VirtualModules>>;\n moduleTypeDeclarations(): MaybePromise<VirtualModules<`${string}.d.ts`>>;\n runtimeTypeDeclarations(\n // options?: RuntimeDeclarationOptions,\n ): MaybePromise<VirtualModules<`${string}.d.ts`>>;\n}\n\nclass DefaultContext implements Context {\n #datasources: DatasourceInfo[] | null = null;\n readonly #options: ContextOptions;\n\n constructor(options: ContextOptions) {\n this.#options = options;\n }\n\n async datasources() {\n const {\n logger,\n baseDir,\n configPattern,\n resolver,\n datasource: datasourceOptions,\n } = this.#options;\n\n if (!this.#datasources) {\n logger?.info(\"Searching drizzle datasources in\", colorize(\"blue\", baseDir));\n\n const drizzleConfigsResolvedPaths = await resolveFiles(\n baseDir,\n [configPattern].flat().map((pattern) => \"*/\" + pattern),\n );\n\n let datasources: DatasourceInfo[] = await mapAsync(\n drizzleConfigsResolvedPaths,\n async (path) => {\n const config = await this.#options.resolver.import<DrizzleConfig>(path, {\n default: true,\n });\n const [_, dirName] = path.match(/(.+\\/(.+))\\/.+$/)!.slice(1, 3) as [string, string];\n\n return await transformDrizzleConfig(config, {\n cwd: this.#options.cwd,\n path,\n dirName,\n resolver,\n });\n },\n );\n\n datasources = Object.entries(datasourceOptions).reduce((_datasources, [name, options]) => {\n const datasource = datasources.find(\n (d) => d.name == name && options.connector == (d.driver ? d.driver : d.dialect),\n );\n if (!datasource) {\n throw new Error(\"Unable to resolve datasource \" + name);\n }\n return [..._datasources, datasource];\n }, [] as DatasourceInfo[]);\n\n logger?.info(\n \"Found drizzle datasources:\",\n datasources\n .toSorted((datasource) => {\n return datasource.enabled ? -1 : 1;\n })\n .map((datasource) => {\n let msg = [datasource.name];\n let color: ColorName;\n if (datasource.enabled) {\n color = \"greenBright\";\n } else {\n color = \"gray\";\n msg.push(\"(disabled)\");\n }\n return colorize(color, msg.join(\" \"));\n })\n .join(\", \"),\n );\n\n const enabledDatasources = datasources.filter((d) => d.enabled);\n\n logger?.info(\n accent`Using ${enabledDatasources.length} of ${datasources.length} resolved datasources` +\n (enabledDatasources.length > 0 ? \":\" : \"\"),\n enabledDatasources\n .map((datasource) => {\n return [\n colorize(\"greenBright\", datasource.name),\n colorize(\"yellow\", \"(\" + (datasource.driver || datasource.dialect) + \")\"),\n ].join(\" \");\n })\n .join(\", \"),\n );\n\n this.#datasources = datasources;\n }\n\n return this.#datasources;\n }\n\n async virtualTypeDeclarations(): Promise<Record<string, VirtualModules<`${string}.d.ts`>>> {\n const datasources = [...(await this.datasources())].filter((d) => d.enabled);\n return {\n \"#nitro-drizzle/*\": {\n \"nitro-drizzle/virtual.d.ts\": [dialectDeclarations(datasources)].join(\"\\n\"),\n },\n };\n }\n\n async moduleTypeDeclarations(): Promise<VirtualModules> {\n return moduleTypeDeclarations(await this.datasources());\n }\n\n async runtimeTypeDeclarations(\n // options?: RuntimeDeclarationOptions,\n ): Promise<VirtualModules<`${string}.d.ts`>> {\n const datasources = await this.datasources();\n const references = new Set([\n { types: \"nitro-drizzle/runtime\" },\n ...this.#options.plugins.map((pluginId) => {\n return { types: pluginId };\n }),\n ]);\n const content = [\n ...[...references.values()].map((reference) => genReference(reference)),\n runtimeDeclarations(datasources),\n /* ts */ `export {};`,\n ].join(\"\\n\");\n return {\n \"nitro-drizzle/runtime.d.ts\": content,\n };\n }\n\n async virtualModules(): Promise<VirtualModules<`#nitro-drizzle/${string}`>> {\n const datasources = await this.datasources();\n return {\n \"#nitro-drizzle/runtime\": runtimeVirtualModule(datasources),\n ...dialectVirtualModules(datasources),\n };\n }\n\n reload() {\n this.#datasources = null;\n }\n}\n\n/**\n * Glob pattern or array of patterns to match Drizzle configuration files.\n */\nexport type ConfigPattern = string | readonly string[];\n\n/**\n * Resolver interface for importing modules and resolving paths.\n */\nexport interface Resolver {\n /**\n * Resolves a module ID to its full path.\n */\n resolve(id: string): string;\n /**\n * Attempts to resolve a module ID, returns undefined if not found.\n */\n tryResolve(id: string): string | undefined;\n /**\n * Imports a module dynamically.\n * @param id - Module identifier to import\n * @param options - Import options, set default: true to import default export\n */\n import<T>(id: string, options?: { default?: boolean }): Promise<T>;\n}\n\nexport interface ContextOptions {\n logger?: ConsolaInstance;\n /**\n * Current working directory\n */\n cwd: string;\n /**\n * Base project directory to search drizzle config files\n */\n baseDir: string;\n /**\n * Pattern for drizzle config files\n */\n configPattern: ConfigPattern;\n /**\n * Nuxt Kit resolver\n */\n resolver: Resolver;\n /**\n * Connector options\n */\n datasource: Record<string, { connector: string }>;\n\n plugins: readonly string[];\n}\n\n/**\n * Creates a Context instance for managing Drizzle datasource configurations.\n * @param options - Configuration options for the context\n * @returns A Context instance\n */\nexport function createContext(options: ContextOptions): Context {\n return new DefaultContext(options);\n}\n\n/**\n * Information about a resolved Drizzle datasource.\n */\nexport interface DatasourceInfo {\n /** Directory name of the datasource (derived from config file location). */\n dirName: string;\n /** Unique name identifier for the datasource. */\n name: string;\n /** Whether the datasource is enabled (filtered by connector configuration). */\n enabled: boolean;\n /** Database dialect (e.g., 'sqlite', 'postgresql', 'mysql'). */\n dialect: string;\n /** Driver type if specified in config, otherwise undefined. */\n driver: string | undefined;\n /** Import paths for schema, connector, and helpers. */\n imports: {\n config: string;\n schema: string[];\n connector: string;\n helpers: string;\n };\n /** Migration configuration for this datasource. */\n migrations: {\n assets?: string;\n config?: Config[\"migrations\"];\n };\n}\n"],"mappings":";;;;;;;;AAyCA,IAAM,iBAAN,MAAwC;CACtC,eAAwC;CACxC;CAEA,YAAY,SAAyB;EACnC,KAAKA,WAAW;CAClB;CAEA,MAAM,cAAc;EAClB,MAAM,EACJ,QACA,SACA,eACA,UACA,YAAY,sBACV,KAAKA;EAET,IAAI,CAAC,KAAKC,cAAc;GACtB,QAAQ,KAAK,oCAAoC,SAAS,QAAQ,OAAO,CAAC;GAO1E,IAAI,cAAgC,MAAM,SACxC,MANwC,aACxC,SACA,CAAC,aAAa,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,YAAY,OAAO,OAAO,CACxD,GAIE,OAAO,SAAS;IACd,MAAM,SAAS,MAAM,KAAKD,SAAS,SAAS,OAAsB,MAAM,EACtE,SAAS,KACX,CAAC;IACD,MAAM,CAAC,GAAG,WAAW,KAAK,MAAM,iBAAiB,CAAC,CAAE,MAAM,GAAG,CAAC;IAE9D,OAAO,MAAM,uBAAuB,QAAQ;KAC1C,KAAK,KAAKA,SAAS;KACnB;KACA;KACA;IACF,CAAC;GACH,CACF;GAEA,cAAc,OAAO,QAAQ,iBAAiB,CAAC,CAAC,QAAQ,cAAc,CAAC,MAAM,aAAa;IACxF,MAAM,aAAa,YAAY,MAC5B,MAAM,EAAE,QAAQ,QAAQ,QAAQ,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,QACzE;IACA,IAAI,CAAC,YACH,MAAM,IAAI,MAAM,kCAAkC,IAAI;IAExD,OAAO,CAAC,GAAG,cAAc,UAAU;GACrC,GAAG,CAAC,CAAqB;GAEzB,QAAQ,KACN,8BACA,YACG,UAAU,eAAe;IACxB,OAAO,WAAW,UAAU,KAAK;GACnC,CAAC,CAAC,CACD,KAAK,eAAe;IACnB,IAAI,MAAM,CAAC,WAAW,IAAI;IAC1B,IAAI;IACJ,IAAI,WAAW,SACb,QAAQ;SACH;KACL,QAAQ;KACR,IAAI,KAAK,YAAY;IACvB;IACA,OAAO,SAAS,OAAO,IAAI,KAAK,GAAG,CAAC;GACtC,CAAC,CAAC,CACD,KAAK,IAAI,CACd;GAEA,MAAM,qBAAqB,YAAY,QAAQ,MAAM,EAAE,OAAO;GAE9D,QAAQ,KACN,MAAM,SAAS,mBAAmB,OAAO,MAAM,YAAY,OAAO,0BAC/D,mBAAmB,SAAS,IAAI,MAAM,KACzC,mBACG,KAAK,eAAe;IACnB,OAAO,CACL,SAAS,eAAe,WAAW,IAAI,GACvC,SAAS,UAAU,OAAO,WAAW,UAAU,WAAW,WAAW,GAAG,CAC1E,CAAC,CAAC,KAAK,GAAG;GACZ,CAAC,CAAC,CACD,KAAK,IAAI,CACd;GAEA,KAAKC,eAAe;EACtB;EAEA,OAAO,KAAKA;CACd;CAEA,MAAM,0BAAqF;EAEzF,OAAO,EACL,oBAAoB,EAClB,8BAA8B,CAAC,oBAHf,CAAC,GAAI,MAAM,KAAK,YAAY,CAAE,CAAC,CAAC,QAAQ,MAAM,EAAE,OAGH,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,EAC5E,EACF;CACF;CAEA,MAAM,yBAAkD;EACtD,OAAO,uBAAuB,MAAM,KAAK,YAAY,CAAC;CACxD;CAEA,MAAM,0BAEuC;EAC3C,MAAM,cAAc,MAAM,KAAK,YAAY;EAY3C,OAAO,EACL,8BANc;GACd,GAAG,CAAC,oBAAG,IAPc,IAAI,CACzB,EAAE,OAAO,wBAAwB,GACjC,GAAG,KAAKD,SAAS,QAAQ,KAAK,aAAa;IACzC,OAAO,EAAE,OAAO,SAAS;GAC3B,CAAC,CACH,CAEkB,EAAA,CAAE,OAAO,CAAC,CAAC,CAAC,KAAK,cAAc,aAAa,SAAS,CAAC;GACtE,oBAAoB,WAAW;GACtB;EACX,CAAC,CAAC,KAAK,IAE+B,EACtC;CACF;CAEA,MAAM,iBAAsE;EAC1E,MAAM,cAAc,MAAM,KAAK,YAAY;EAC3C,OAAO;GACL,0BAA0B,qBAAqB,WAAW;GAC1D,GAAG,sBAAsB,WAAW;EACtC;CACF;CAEA,SAAS;EACP,KAAKC,eAAe;CACtB;AACF;;;;;;AA0DA,SAAgB,cAAc,SAAkC;CAC9D,OAAO,IAAI,eAAe,OAAO;AACnC"}
@@ -0,0 +1,12 @@
1
+ //#region src/module/internal/plugins.ts
2
+ function enablePlugins(nitroOptions, moduleOptions) {
3
+ const plugins = [];
4
+ if (moduleOptions.migrations && moduleOptions.migrations.migrateOnInit) plugins.push("migrate");
5
+ plugins.push("init");
6
+ nitroOptions.plugins.push(...plugins.map((pluginName) => `nitro-drizzle/plugins/${pluginName}`));
7
+ return plugins;
8
+ }
9
+ //#endregion
10
+ export { enablePlugins };
11
+
12
+ //# sourceMappingURL=plugins.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugins.mjs","names":[],"sources":["../../../src/module/internal/plugins.ts"],"sourcesContent":["import type { NitroOptions } from \"nitropack\";\nimport type { ModuleOptions } from \"..\";\n\nexport function enablePlugins(\n nitroOptions: NitroOptions,\n moduleOptions: ModuleOptions,\n): readonly string[] {\n const plugins: (\"init\" | \"migrate\")[] = [];\n\n if (moduleOptions.migrations && moduleOptions.migrations.migrateOnInit) {\n plugins.push(\"migrate\");\n }\n\n plugins.push(\"init\");\n\n nitroOptions.plugins.push(...plugins.map((pluginName) => `nitro-drizzle/plugins/${pluginName}`));\n\n return plugins;\n}\n"],"mappings":";AAGA,SAAgB,cACd,cACA,eACmB;CACnB,MAAM,UAAkC,CAAC;CAEzC,IAAI,cAAc,cAAc,cAAc,WAAW,eACvD,QAAQ,KAAK,SAAS;CAGxB,QAAQ,KAAK,MAAM;CAEnB,aAAa,QAAQ,KAAK,GAAG,QAAQ,KAAK,eAAe,yBAAyB,YAAY,CAAC;CAE/F,OAAO;AACT"}
@@ -2,18 +2,21 @@ import { relativeWithDot } from "./path.mjs";
2
2
  import { writeFile } from "./fs.mjs";
3
3
  import { dirname, join, resolve } from "pathe";
4
4
  //#region src/module/internal/types.ts
5
- async function addAugmentations(nitroOptions, nitroTypes, augmentations) {
5
+ function addTsConfigInclude(nitroTypes, paths) {
6
6
  nitroTypes.tsConfig ||= {};
7
7
  nitroTypes.tsConfig.include ||= [];
8
- const paths = await writeTypeFiles(nitroOptions, augmentations);
9
8
  nitroTypes.tsConfig.include.push(...paths);
10
9
  }
10
+ async function addAugmentations(nitroOptions, nitroTypes, augmentations) {
11
+ addTsConfigInclude(nitroTypes, await writeTypeFiles(nitroOptions, augmentations));
12
+ }
11
13
  async function addDeclarations(nitroOptions, nitroTypes, declarations) {
12
14
  nitroTypes.tsConfig ||= {};
15
+ nitroTypes.tsConfig.compilerOptions ||= {};
16
+ nitroTypes.tsConfig.compilerOptions.paths ||= {};
13
17
  for (const alias in declarations) {
14
- nitroTypes.tsConfig.compilerOptions ||= {};
15
- nitroTypes.tsConfig.compilerOptions.paths ||= {};
16
- nitroTypes.tsConfig.compilerOptions.paths[alias] = await writeTypeFiles(nitroOptions, declarations[alias]);
18
+ const paths = await writeTypeFiles(nitroOptions, declarations[alias]);
19
+ nitroTypes.tsConfig.compilerOptions.paths[alias] = paths.map((path) => path.replace(/\.d\.ts$/, ""));
17
20
  }
18
21
  }
19
22
  function typesDir(nitroOptions) {
@@ -37,6 +40,6 @@ async function getContent(module) {
37
40
  return "string" == typeof module ? module : await module();
38
41
  }
39
42
  //#endregion
40
- export { addAugmentations, addDeclarations };
43
+ export { addAugmentations, addDeclarations, addTsConfigInclude };
41
44
 
42
45
  //# sourceMappingURL=types.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.mjs","names":[],"sources":["../../../src/module/internal/types.ts"],"sourcesContent":["import type { NitroOptions, NitroTypes, VirtualModule } from \"nitropack/types\";\nimport { dirname, join, resolve } from \"pathe\";\nimport { relativeWithDot } from \"./path\";\nimport { writeFile } from \"./fs\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nexport async function addAugmentations(\n nitroOptions: NitroOptions,\n nitroTypes: NitroTypes,\n augmentations: VirtualModules,\n) {\n nitroTypes.tsConfig ||= {};\n nitroTypes.tsConfig.include ||= [];\n const paths = await writeTypeFiles(nitroOptions, augmentations);\n nitroTypes.tsConfig.include.push(...paths);\n}\n\nexport async function addDeclarations(\n nitroOptions: NitroOptions,\n nitroTypes: NitroTypes,\n declarations: Record<string, VirtualModules>,\n) {\n nitroTypes.tsConfig ||= {};\n\n for (const alias in declarations) {\n nitroTypes.tsConfig.compilerOptions ||= {};\n nitroTypes.tsConfig.compilerOptions.paths ||= {};\n nitroTypes.tsConfig.compilerOptions.paths[alias] = await writeTypeFiles(\n nitroOptions,\n declarations[alias],\n );\n }\n}\n\nfunction typesDir(nitroOptions: NitroOptions) {\n return join(nitroOptions.buildDir, \"types\");\n}\n\nfunction tsconfigDir(nitroOptions: NitroOptions) {\n const tsConfigPath = resolve(nitroOptions.buildDir, nitroOptions.typescript.tsconfigPath);\n const tsconfigDir = dirname(tsConfigPath);\n return tsconfigDir;\n}\n\nasync function writeTypeFiles(nitroOptions: NitroOptions, modules: VirtualModules) {\n const paths: string[] = [];\n const dir = typesDir(nitroOptions);\n for (const filename in modules) {\n const typesFilename = join(dir, filename);\n await writeFile(typesFilename, await getContent(modules[filename]));\n const relativeFilename = relativeWithDot(tsconfigDir(nitroOptions), typesFilename);\n paths.push(relativeFilename);\n }\n return paths;\n}\n\nasync function getContent(module: VirtualModule) {\n return \"string\" == typeof module ? module : await module();\n}\n"],"mappings":";;;;AAMA,eAAsB,iBACpB,cACA,YACA,eACA;CACA,WAAW,aAAa,CAAC;CACzB,WAAW,SAAS,YAAY,CAAC;CACjC,MAAM,QAAQ,MAAM,eAAe,cAAc,aAAa;CAC9D,WAAW,SAAS,QAAQ,KAAK,GAAG,KAAK;AAC3C;AAEA,eAAsB,gBACpB,cACA,YACA,cACA;CACA,WAAW,aAAa,CAAC;CAEzB,KAAK,MAAM,SAAS,cAAc;EAChC,WAAW,SAAS,oBAAoB,CAAC;EACzC,WAAW,SAAS,gBAAgB,UAAU,CAAC;EAC/C,WAAW,SAAS,gBAAgB,MAAM,SAAS,MAAM,eACvD,cACA,aAAa,MACf;CACF;AACF;AAEA,SAAS,SAAS,cAA4B;CAC5C,OAAO,KAAK,aAAa,UAAU,OAAO;AAC5C;AAEA,SAAS,YAAY,cAA4B;CAG/C,OADoB,QADC,QAAQ,aAAa,UAAU,aAAa,WAAW,YACrC,CACtB;AACnB;AAEA,eAAe,eAAe,cAA4B,SAAyB;CACjF,MAAM,QAAkB,CAAC;CACzB,MAAM,MAAM,SAAS,YAAY;CACjC,KAAK,MAAM,YAAY,SAAS;EAC9B,MAAM,gBAAgB,KAAK,KAAK,QAAQ;EACxC,MAAM,UAAU,eAAe,MAAM,WAAW,QAAQ,SAAS,CAAC;EAClE,MAAM,mBAAmB,gBAAgB,YAAY,YAAY,GAAG,aAAa;EACjF,MAAM,KAAK,gBAAgB;CAC7B;CACA,OAAO;AACT;AAEA,eAAe,WAAW,QAAuB;CAC/C,OAAO,YAAY,OAAO,SAAS,SAAS,MAAM,OAAO;AAC3D"}
1
+ {"version":3,"file":"types.mjs","names":[],"sources":["../../../src/module/internal/types.ts"],"sourcesContent":["import type { NitroOptions, NitroTypes, VirtualModule } from \"nitropack/types\";\nimport { dirname, join, resolve } from \"pathe\";\nimport { relativeWithDot } from \"./path\";\nimport { writeFile } from \"./fs\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nexport function addTsConfigInclude(nitroTypes: NitroTypes, paths: readonly string[]) {\n nitroTypes.tsConfig ||= {};\n nitroTypes.tsConfig.include ||= [];\n nitroTypes.tsConfig.include.push(...paths);\n}\n\nexport async function addAugmentations(\n nitroOptions: NitroOptions,\n nitroTypes: NitroTypes,\n augmentations: VirtualModules<`${string}.d.ts`>,\n) {\n const paths = await writeTypeFiles(nitroOptions, augmentations);\n addTsConfigInclude(nitroTypes, paths);\n}\n\nexport async function addDeclarations(\n nitroOptions: NitroOptions,\n nitroTypes: NitroTypes,\n declarations: Record<string, VirtualModules<`${string}.d.ts`>>,\n) {\n nitroTypes.tsConfig ||= {};\n nitroTypes.tsConfig.compilerOptions ||= {};\n nitroTypes.tsConfig.compilerOptions.paths ||= {};\n\n for (const alias in declarations) {\n const paths = await writeTypeFiles(nitroOptions, declarations[alias]);\n\n nitroTypes.tsConfig.compilerOptions.paths[alias] = paths.map((path) =>\n path.replace(/\\.d\\.ts$/, \"\"),\n );\n }\n}\n\nfunction typesDir(nitroOptions: NitroOptions) {\n return join(nitroOptions.buildDir, \"types\");\n}\n\nfunction tsconfigDir(nitroOptions: NitroOptions) {\n const tsConfigPath = resolve(nitroOptions.buildDir, nitroOptions.typescript.tsconfigPath);\n const tsconfigDir = dirname(tsConfigPath);\n return tsconfigDir;\n}\n\nasync function writeTypeFiles(nitroOptions: NitroOptions, modules: VirtualModules) {\n const paths: string[] = [];\n const dir = typesDir(nitroOptions);\n for (const filename in modules) {\n const typesFilename = join(dir, filename);\n await writeFile(typesFilename, await getContent(modules[filename]));\n const relativeFilename = relativeWithDot(tsconfigDir(nitroOptions), typesFilename);\n paths.push(relativeFilename);\n }\n return paths;\n}\n\nasync function getContent(module: VirtualModule) {\n return \"string\" == typeof module ? module : await module();\n}\n"],"mappings":";;;;AAMA,SAAgB,mBAAmB,YAAwB,OAA0B;CACnF,WAAW,aAAa,CAAC;CACzB,WAAW,SAAS,YAAY,CAAC;CACjC,WAAW,SAAS,QAAQ,KAAK,GAAG,KAAK;AAC3C;AAEA,eAAsB,iBACpB,cACA,YACA,eACA;CAEA,mBAAmB,YAAY,MADX,eAAe,cAAc,aAAa,CAC1B;AACtC;AAEA,eAAsB,gBACpB,cACA,YACA,cACA;CACA,WAAW,aAAa,CAAC;CACzB,WAAW,SAAS,oBAAoB,CAAC;CACzC,WAAW,SAAS,gBAAgB,UAAU,CAAC;CAE/C,KAAK,MAAM,SAAS,cAAc;EAChC,MAAM,QAAQ,MAAM,eAAe,cAAc,aAAa,MAAM;EAEpE,WAAW,SAAS,gBAAgB,MAAM,SAAS,MAAM,KAAK,SAC5D,KAAK,QAAQ,YAAY,EAAE,CAC7B;CACF;AACF;AAEA,SAAS,SAAS,cAA4B;CAC5C,OAAO,KAAK,aAAa,UAAU,OAAO;AAC5C;AAEA,SAAS,YAAY,cAA4B;CAG/C,OADoB,QADC,QAAQ,aAAa,UAAU,aAAa,WAAW,YACrC,CACtB;AACnB;AAEA,eAAe,eAAe,cAA4B,SAAyB;CACjF,MAAM,QAAkB,CAAC;CACzB,MAAM,MAAM,SAAS,YAAY;CACjC,KAAK,MAAM,YAAY,SAAS;EAC9B,MAAM,gBAAgB,KAAK,KAAK,QAAQ;EACxC,MAAM,UAAU,eAAe,MAAM,WAAW,QAAQ,SAAS,CAAC;EAClE,MAAM,mBAAmB,gBAAgB,YAAY,YAAY,GAAG,aAAa;EACjF,MAAM,KAAK,gBAAgB;CAC7B;CACA,OAAO;AACT;AAEA,eAAe,WAAW,QAAuB;CAC/C,OAAO,YAAY,OAAO,SAAS,SAAS,MAAM,OAAO;AAC3D"}
package/dist/module.d.mts CHANGED
@@ -22,9 +22,6 @@ interface ModuleOptions {
22
22
  * Patterns to search Drizzle configs
23
23
  */
24
24
  configPattern: ConfigPattern;
25
- /**
26
- * @deprecated Use 'nitro.database' instead.
27
- */
28
25
  datasources?: DatasourceOptions;
29
26
  /**
30
27
  * Enable migrations storage
package/dist/module.mjs CHANGED
@@ -5,6 +5,7 @@ import { addInlineExternals } from "./module/internal/externals.mjs";
5
5
  import { reloadPlugin } from "./module/internal/reload.mjs";
6
6
  import { addPlugin } from "./module/internal/rollup.mjs";
7
7
  import { migrationsVirtualModule } from "./module/internal/virtual.mjs";
8
+ import { enablePlugins } from "./module/internal/plugins.mjs";
8
9
  import { defu } from "defu";
9
10
  import { resolve } from "pathe";
10
11
  import { resolveAlias } from "pathe/utils";
@@ -22,8 +23,9 @@ const defaultModuleOptions = {
22
23
  const module = {
23
24
  name: pkgName,
24
25
  async setup(nitro) {
25
- const resolver = createResolver(nitro.options.rootDir, { alias: nitro.options.alias });
26
26
  const moduleOptions = defu(nitro.options.drizzle, defaultModuleOptions);
27
+ const plugins = enablePlugins(nitro.options, moduleOptions);
28
+ const resolver = createResolver(nitro.options.rootDir, { alias: nitro.options.alias });
27
29
  const resolvedBaseDir = resolve(nitro.options.srcDir, resolveAlias(moduleOptions.baseDir, nitro.options.alias));
28
30
  const contextOptions = {
29
31
  cwd: process.cwd(),
@@ -31,23 +33,12 @@ const module = {
31
33
  baseDir: resolvedBaseDir,
32
34
  logger: nitro.logger,
33
35
  configPattern: moduleOptions.configPattern,
34
- datasource: { ...moduleOptions.datasources }
36
+ datasource: { ...moduleOptions.datasources },
37
+ plugins
35
38
  };
36
39
  const context = createContext(contextOptions);
37
40
  addInlineExternals(nitro.options);
38
- const plugins = {
39
- migrate: false,
40
- init: true
41
- };
42
41
  Object.assign(nitro.options.virtual, await context.virtualModules(), migrationsVirtualModule(await context.datasources(), moduleOptions.migrations));
43
- nitro.options.typescript = defu(nitro.options.typescript, { tsConfig: { compilerOptions: { paths: { "#nitro-drizzle/*": ["./nitro-drizzle/virtual"] } } } });
44
- nitro.hooks.hook("types:extend", async (types) => {
45
- const augmentations = await context.augmentations();
46
- await addAugmentations(nitro.options, types, augmentations);
47
- const declarations = await context.declarations();
48
- await addDeclarations(nitro.options, types, declarations);
49
- await addAugmentations(nitro.options, types, { "nitro-drizzle/module.d.ts": `import "nitro-drizzle/module";` });
50
- });
51
42
  if (nitro.options.imports) nitro.options.imports.imports ||= [];
52
43
  if (moduleOptions.migrations) {
53
44
  await updateMigrationAssets(context, nitro.options, moduleOptions.migrations);
@@ -58,27 +49,19 @@ const module = {
58
49
  handler: "nitro-drizzle/migrations/task"
59
50
  };
60
51
  }
61
- if (moduleOptions.migrations.migrateOnInit) plugins.migrate = true;
62
52
  }
63
53
  nitro.hooks.hook("rollup:before", async (nitro, config) => {
64
54
  await addPlugin(config, reloadPlugin(nitro, contextOptions));
65
55
  });
66
- const enabledPlugins = Object.entries(plugins).reduce((plugins, [pluginName, enabled]) => {
67
- if (enabled) plugins.push(pluginName);
68
- return plugins;
69
- }, []).map((pluginName) => {
70
- return `nitro-drizzle/plugins/${pluginName}`;
71
- });
72
- for (const pluginId of enabledPlugins) nitro.options.plugins.push(pluginId);
73
- if (enabledPlugins.length) nitro.hooks.hook("types:extend", async (types) => {
56
+ nitro.hooks.hook("types:extend", async (types) => {
57
+ const runtimeTypeDeclarations = await context.runtimeTypeDeclarations();
58
+ const moduleTypeDeclarations = await context.moduleTypeDeclarations();
74
59
  await addAugmentations(nitro.options, types, {
75
- "nitro-drizzle/module.d.ts": `
76
- import "nitro-drizzle/module";
77
- `,
78
- "nitro-drizzle/plugins.d.ts": enabledPlugins.map((pluginId) => {
79
- return `import "${pluginId}";`;
80
- }).join("\n")
60
+ ...runtimeTypeDeclarations,
61
+ ...moduleTypeDeclarations
81
62
  });
63
+ const virtualTypeDeclarations = await context.virtualTypeDeclarations();
64
+ await addDeclarations(nitro.options, types, virtualTypeDeclarations);
82
65
  });
83
66
  }
84
67
  };
@@ -1 +1 @@
1
- {"version":3,"file":"module.mjs","names":[],"sources":["../src/module/index.ts"],"sourcesContent":["import type { NitroModule } from \"nitropack/types\";\nimport { defu } from \"defu\";\nimport { resolve } from \"pathe\";\nimport { resolveAlias } from \"pathe/utils\";\n\nimport { createContext, type ConfigPattern, type ContextOptions } from \"nitro-drizzle/context\";\nimport { pkgName } from \"nitro-drizzle/meta\";\n\nimport { addAugmentations, addDeclarations } from \"./internal/types\";\nimport { createResolver } from \"./internal/resolver\";\nimport { updateMigrationAssets, type MigrationOptions } from \"./internal/assets\";\nimport { addInlineExternals } from \"./internal/externals\";\nimport { reloadPlugin } from \"./internal/reload\";\nimport { addPlugin } from \"./internal/rollup\";\nimport { migrationsVirtualModule } from \"./internal/virtual\";\n\n/**\n * Datasource-specific configuration options.\n * This interface is augmented by the module with connector-specific options.\n */\nexport interface DatasourceOptions {\n // augmented by module\n}\n\ndeclare module \"nitropack/types\" {\n interface NitroOptions {\n drizzle: ModuleOptions;\n }\n}\n\nexport interface ModuleOptions {\n /**\n * Directory to search datasources\n */\n baseDir: string;\n /**\n * Patterns to search Drizzle configs\n */\n configPattern: ConfigPattern;\n /**\n * @deprecated Use 'nitro.database' instead.\n */\n datasources?: DatasourceOptions;\n\n /**\n * Enable migrations storage\n */\n migrations: false | MigrationOptions;\n}\n\nconst defaultModuleOptions: ModuleOptions = {\n baseDir: \"~/drizzle\",\n configPattern: [\"drizzle.config.*\", \"drizzle-*.config.*\"],\n migrations: {\n storageBase: \"drizzle:migrations\",\n migrateOnInit: false,\n },\n};\n\nconst module: NitroModule = {\n name: pkgName,\n async setup(nitro) {\n const resolver = createResolver(nitro.options.rootDir, {\n alias: nitro.options.alias,\n });\n const moduleOptions = defu(nitro.options.drizzle, defaultModuleOptions);\n\n const resolvedBaseDir = resolve(\n nitro.options.srcDir,\n resolveAlias(moduleOptions.baseDir, nitro.options.alias),\n );\n const contextOptions: ContextOptions = {\n cwd: process.cwd(),\n resolver: resolver,\n baseDir: resolvedBaseDir,\n logger: nitro.logger,\n configPattern: moduleOptions.configPattern,\n datasource: { ...moduleOptions.datasources },\n };\n const context = createContext(contextOptions);\n\n // add inline externals\n addInlineExternals(nitro.options);\n\n // specify plugin names to preserve order\n const plugins: Record<\"init\" | \"migrate\", boolean> = {\n migrate: false,\n init: true,\n };\n\n // add virtual modules\n Object.assign(\n nitro.options.virtual,\n await context.virtualModules(),\n migrationsVirtualModule(await context.datasources(), moduleOptions.migrations),\n );\n\n nitro.options.typescript = defu(nitro.options.typescript, {\n tsConfig: {\n compilerOptions: {\n paths: {\n \"#nitro-drizzle/*\": [\"./nitro-drizzle/virtual\"],\n },\n },\n },\n });\n\n // extend types\n nitro.hooks.hook(\"types:extend\", async (types) => {\n const augmentations = await context.augmentations();\n await addAugmentations(nitro.options, types, augmentations);\n\n const declarations = await context.declarations();\n await addDeclarations(nitro.options, types, declarations);\n\n await addAugmentations(nitro.options, types, {\n \"nitro-drizzle/module.d.ts\": /* ts */ `import \"nitro-drizzle/module\";`,\n });\n });\n\n // auto-imports\n if (nitro.options.imports) {\n nitro.options.imports.imports ||= [];\n\n // TODO\n // nitro.options.imports.imports.push({\n // name: 'useMigrations',\n // as: 'useDrizzleMigrations',\n // from: 'nitro-drizzle/runtime',\n // })\n }\n\n // server assets\n if (moduleOptions.migrations) {\n await updateMigrationAssets(context, nitro.options, moduleOptions.migrations);\n\n if (nitro.options.experimental.tasks) {\n nitro.options.tasks ||= {};\n nitro.options.tasks[\"drizzle:migrate\"] = {\n description: \"Run drizzle migrations for a datasource.\",\n handler: \"nitro-drizzle/migrations/task\",\n };\n }\n\n if (moduleOptions.migrations.migrateOnInit) {\n plugins.migrate = true;\n }\n }\n\n // watch options\n nitro.hooks.hook(\"rollup:before\", async (nitro, config) => {\n await addPlugin(config, reloadPlugin(nitro, contextOptions));\n });\n\n // add plugins\n const enabledPlugins = Object.entries(plugins)\n .reduce((plugins, [pluginName, enabled]) => {\n if (enabled) {\n plugins.push(pluginName);\n }\n return plugins;\n }, [] as string[])\n .map((pluginName) => {\n return `nitro-drizzle/plugins/${pluginName}`;\n });\n\n for (const pluginId of enabledPlugins) {\n nitro.options.plugins.push(pluginId);\n }\n\n if (enabledPlugins.length) {\n nitro.hooks.hook(\"types:extend\", async (types) => {\n await addAugmentations(nitro.options, types, {\n \"nitro-drizzle/module.d.ts\": /* ts */ `\n import \"nitro-drizzle/module\";\n `,\n \"nitro-drizzle/plugins.d.ts\": enabledPlugins\n .map((pluginId) => {\n return `import \"${pluginId}\";`;\n })\n .join(\"\\n\"),\n });\n });\n }\n },\n};\n\nexport default module;\n"],"mappings":";;;;;;;;;;;;;AAkDA,MAAM,uBAAsC;CAC1C,SAAS;CACT,eAAe,CAAC,oBAAoB,oBAAoB;CACxD,YAAY;EACV,aAAa;EACb,eAAe;CACjB;AACF;AAEA,MAAM,SAAsB;CAC1B,MAAM;CACN,MAAM,MAAM,OAAO;EACjB,MAAM,WAAW,eAAe,MAAM,QAAQ,SAAS,EACrD,OAAO,MAAM,QAAQ,MACvB,CAAC;EACD,MAAM,gBAAgB,KAAK,MAAM,QAAQ,SAAS,oBAAoB;EAEtE,MAAM,kBAAkB,QACtB,MAAM,QAAQ,QACd,aAAa,cAAc,SAAS,MAAM,QAAQ,KAAK,CACzD;EACA,MAAM,iBAAiC;GACrC,KAAK,QAAQ,IAAI;GACP;GACV,SAAS;GACT,QAAQ,MAAM;GACd,eAAe,cAAc;GAC7B,YAAY,EAAE,GAAG,cAAc,YAAY;EAC7C;EACA,MAAM,UAAU,cAAc,cAAc;EAG5C,mBAAmB,MAAM,OAAO;EAGhC,MAAM,UAA+C;GACnD,SAAS;GACT,MAAM;EACR;EAGA,OAAO,OACL,MAAM,QAAQ,SACd,MAAM,QAAQ,eAAe,GAC7B,wBAAwB,MAAM,QAAQ,YAAY,GAAG,cAAc,UAAU,CAC/E;EAEA,MAAM,QAAQ,aAAa,KAAK,MAAM,QAAQ,YAAY,EACxD,UAAU,EACR,iBAAiB,EACf,OAAO,EACL,oBAAoB,CAAC,yBAAyB,EAChD,EACF,EACF,EACF,CAAC;EAGD,MAAM,MAAM,KAAK,gBAAgB,OAAO,UAAU;GAChD,MAAM,gBAAgB,MAAM,QAAQ,cAAc;GAClD,MAAM,iBAAiB,MAAM,SAAS,OAAO,aAAa;GAE1D,MAAM,eAAe,MAAM,QAAQ,aAAa;GAChD,MAAM,gBAAgB,MAAM,SAAS,OAAO,YAAY;GAExD,MAAM,iBAAiB,MAAM,SAAS,OAAO,EAC3C,6BAAsC,iCACxC,CAAC;EACH,CAAC;EAGD,IAAI,MAAM,QAAQ,SAChB,MAAM,QAAQ,QAAQ,YAAY,CAAC;EAWrC,IAAI,cAAc,YAAY;GAC5B,MAAM,sBAAsB,SAAS,MAAM,SAAS,cAAc,UAAU;GAE5E,IAAI,MAAM,QAAQ,aAAa,OAAO;IACpC,MAAM,QAAQ,UAAU,CAAC;IACzB,MAAM,QAAQ,MAAM,qBAAqB;KACvC,aAAa;KACb,SAAS;IACX;GACF;GAEA,IAAI,cAAc,WAAW,eAC3B,QAAQ,UAAU;EAEtB;EAGA,MAAM,MAAM,KAAK,iBAAiB,OAAO,OAAO,WAAW;GACzD,MAAM,UAAU,QAAQ,aAAa,OAAO,cAAc,CAAC;EAC7D,CAAC;EAGD,MAAM,iBAAiB,OAAO,QAAQ,OAAO,CAAC,CAC3C,QAAQ,SAAS,CAAC,YAAY,aAAa;GAC1C,IAAI,SACF,QAAQ,KAAK,UAAU;GAEzB,OAAO;EACT,GAAG,CAAC,CAAa,CAAC,CACjB,KAAK,eAAe;GACnB,OAAO,yBAAyB;EAClC,CAAC;EAEH,KAAK,MAAM,YAAY,gBACrB,MAAM,QAAQ,QAAQ,KAAK,QAAQ;EAGrC,IAAI,eAAe,QACjB,MAAM,MAAM,KAAK,gBAAgB,OAAO,UAAU;GAChD,MAAM,iBAAiB,MAAM,SAAS,OAAO;IAC3C,6BAAsC;;;IAGtC,8BAA8B,eAC3B,KAAK,aAAa;KACjB,OAAO,WAAW,SAAS;IAC7B,CAAC,CAAC,CACD,KAAK,IAAI;GACd,CAAC;EACH,CAAC;CAEL;AACF"}
1
+ {"version":3,"file":"module.mjs","names":[],"sources":["../src/module/index.ts"],"sourcesContent":["import type { NitroModule } from \"nitropack/types\";\nimport { defu } from \"defu\";\nimport { resolve } from \"pathe\";\nimport { resolveAlias } from \"pathe/utils\";\n\nimport { createContext, type ConfigPattern, type ContextOptions } from \"nitro-drizzle/context\";\nimport { pkgName } from \"nitro-drizzle/meta\";\n\nimport { addAugmentations, addDeclarations } from \"./internal/types\";\nimport { createResolver } from \"./internal/resolver\";\nimport { updateMigrationAssets, type MigrationOptions } from \"./internal/assets\";\nimport { addInlineExternals } from \"./internal/externals\";\nimport { reloadPlugin } from \"./internal/reload\";\nimport { addPlugin } from \"./internal/rollup\";\nimport { migrationsVirtualModule } from \"./internal/virtual\";\nimport { enablePlugins } from \"./internal/plugins\";\n\n/**\n * Datasource-specific configuration options.\n * This interface is augmented by the module with connector-specific options.\n */\nexport interface DatasourceOptions {\n // augmented by module\n}\n\ndeclare module \"nitropack/types\" {\n interface NitroOptions {\n drizzle: ModuleOptions;\n }\n}\n\nexport interface ModuleOptions {\n /**\n * Directory to search datasources\n */\n baseDir: string;\n /**\n * Patterns to search Drizzle configs\n */\n configPattern: ConfigPattern;\n\n datasources?: DatasourceOptions;\n\n /**\n * Enable migrations storage\n */\n migrations: false | MigrationOptions;\n}\n\nconst defaultModuleOptions: ModuleOptions = {\n baseDir: \"~/drizzle\",\n configPattern: [\"drizzle.config.*\", \"drizzle-*.config.*\"],\n migrations: {\n storageBase: \"drizzle:migrations\",\n migrateOnInit: false,\n },\n};\n\nconst module: NitroModule = {\n name: pkgName,\n async setup(nitro) {\n const moduleOptions = defu(nitro.options.drizzle, defaultModuleOptions);\n\n const plugins = enablePlugins(nitro.options, moduleOptions);\n\n const resolver = createResolver(nitro.options.rootDir, {\n alias: nitro.options.alias,\n });\n\n const resolvedBaseDir = resolve(\n nitro.options.srcDir,\n resolveAlias(moduleOptions.baseDir, nitro.options.alias),\n );\n\n const contextOptions: ContextOptions = {\n cwd: process.cwd(),\n resolver: resolver,\n baseDir: resolvedBaseDir,\n logger: nitro.logger,\n configPattern: moduleOptions.configPattern,\n datasource: { ...moduleOptions.datasources },\n plugins,\n };\n\n const context = createContext(contextOptions);\n\n // add inline externals\n addInlineExternals(nitro.options);\n\n // add virtual modules\n Object.assign(\n nitro.options.virtual,\n await context.virtualModules(),\n migrationsVirtualModule(await context.datasources(), moduleOptions.migrations),\n );\n\n // auto-imports\n if (nitro.options.imports) {\n nitro.options.imports.imports ||= [];\n\n // TODO\n // nitro.options.imports.imports.push({\n // name: 'useMigrations',\n // as: 'useDrizzleMigrations',\n // from: 'nitro-drizzle/runtime',\n // })\n }\n\n // server assets\n if (moduleOptions.migrations) {\n await updateMigrationAssets(context, nitro.options, moduleOptions.migrations);\n\n if (nitro.options.experimental.tasks) {\n nitro.options.tasks ||= {};\n nitro.options.tasks[\"drizzle:migrate\"] = {\n description: \"Run drizzle migrations for a datasource.\",\n handler: \"nitro-drizzle/migrations/task\",\n };\n }\n }\n\n // watch options\n nitro.hooks.hook(\"rollup:before\", async (nitro, config) => {\n await addPlugin(config, reloadPlugin(nitro, contextOptions));\n });\n\n // extend types\n nitro.hooks.hook(\"types:extend\", async (types) => {\n const runtimeTypeDeclarations = await context.runtimeTypeDeclarations();\n const moduleTypeDeclarations = await context.moduleTypeDeclarations();\n\n await addAugmentations(nitro.options, types, {\n ...runtimeTypeDeclarations,\n ...moduleTypeDeclarations,\n });\n\n const virtualTypeDeclarations = await context.virtualTypeDeclarations();\n await addDeclarations(nitro.options, types, virtualTypeDeclarations);\n });\n },\n};\n\nexport default module;\n"],"mappings":";;;;;;;;;;;;;;AAiDA,MAAM,uBAAsC;CAC1C,SAAS;CACT,eAAe,CAAC,oBAAoB,oBAAoB;CACxD,YAAY;EACV,aAAa;EACb,eAAe;CACjB;AACF;AAEA,MAAM,SAAsB;CAC1B,MAAM;CACN,MAAM,MAAM,OAAO;EACjB,MAAM,gBAAgB,KAAK,MAAM,QAAQ,SAAS,oBAAoB;EAEtE,MAAM,UAAU,cAAc,MAAM,SAAS,aAAa;EAE1D,MAAM,WAAW,eAAe,MAAM,QAAQ,SAAS,EACrD,OAAO,MAAM,QAAQ,MACvB,CAAC;EAED,MAAM,kBAAkB,QACtB,MAAM,QAAQ,QACd,aAAa,cAAc,SAAS,MAAM,QAAQ,KAAK,CACzD;EAEA,MAAM,iBAAiC;GACrC,KAAK,QAAQ,IAAI;GACP;GACV,SAAS;GACT,QAAQ,MAAM;GACd,eAAe,cAAc;GAC7B,YAAY,EAAE,GAAG,cAAc,YAAY;GAC3C;EACF;EAEA,MAAM,UAAU,cAAc,cAAc;EAG5C,mBAAmB,MAAM,OAAO;EAGhC,OAAO,OACL,MAAM,QAAQ,SACd,MAAM,QAAQ,eAAe,GAC7B,wBAAwB,MAAM,QAAQ,YAAY,GAAG,cAAc,UAAU,CAC/E;EAGA,IAAI,MAAM,QAAQ,SAChB,MAAM,QAAQ,QAAQ,YAAY,CAAC;EAWrC,IAAI,cAAc,YAAY;GAC5B,MAAM,sBAAsB,SAAS,MAAM,SAAS,cAAc,UAAU;GAE5E,IAAI,MAAM,QAAQ,aAAa,OAAO;IACpC,MAAM,QAAQ,UAAU,CAAC;IACzB,MAAM,QAAQ,MAAM,qBAAqB;KACvC,aAAa;KACb,SAAS;IACX;GACF;EACF;EAGA,MAAM,MAAM,KAAK,iBAAiB,OAAO,OAAO,WAAW;GACzD,MAAM,UAAU,QAAQ,aAAa,OAAO,cAAc,CAAC;EAC7D,CAAC;EAGD,MAAM,MAAM,KAAK,gBAAgB,OAAO,UAAU;GAChD,MAAM,0BAA0B,MAAM,QAAQ,wBAAwB;GACtE,MAAM,yBAAyB,MAAM,QAAQ,uBAAuB;GAEpE,MAAM,iBAAiB,MAAM,SAAS,OAAO;IAC3C,GAAG;IACH,GAAG;GACL,CAAC;GAED,MAAM,0BAA0B,MAAM,QAAQ,wBAAwB;GACtE,MAAM,gBAAgB,MAAM,SAAS,OAAO,uBAAuB;EACrE,CAAC;CACH;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"config.mjs","names":[],"sources":["../../../src/runtime/internal/config.ts"],"sourcesContent":["import { defu } from \"defu\";\nimport { useNitroApp, useRuntimeConfig } from \"nitropack/runtime\";\n\nimport type { Datasources, Config } from \"..\";\n\nconst datasourceConfig: Partial<Config> = {};\n\n/**\n * @internal\n */\nexport async function getCachedDatasourceConfig<TName extends keyof Datasources & string>(\n name: TName,\n) {\n if (name in datasourceConfig && !datasourceConfig[name]) {\n throw new Error(\n \"Cannot obtain datasource config. Do you try to obtain it inside the 'drizzle:config' hook?\",\n );\n } else {\n datasourceConfig[name] = undefined;\n\n const runtimeConfig = useRuntimeConfig();\n // const config = structuredClone<DrizzleRuntimeConfig[TName]>(runtimeConfig.drizzle?.[name] ?? {})\n const config = defu(runtimeConfig.drizzle?.[name], {});\n const nitro = useNitroApp();\n await nitro.hooks.callHook(\"drizzle:config\", name, config);\n\n datasourceConfig[name] = config;\n }\n\n return datasourceConfig[name];\n}\n\nexport function clearCachedConfig<TName extends keyof Datasources>(name: TName) {\n delete datasourceConfig[name];\n}\n"],"mappings":";;;AAKA,MAAM,mBAAoC,CAAC;;;;AAK3C,eAAsB,0BACpB,MACA;CACA,IAAI,QAAQ,oBAAoB,CAAC,iBAAiB,OAChD,MAAM,IAAI,MACR,4FACF;MACK;EACL,iBAAiB,QAAQ,KAAA;EAIzB,MAAM,SAAS,KAFO,iBAEU,CAAC,CAAC,UAAU,OAAO,CAAC,CAAC;EAErD,MADc,YACJ,CAAC,CAAC,MAAM,SAAS,kBAAkB,MAAM,MAAM;EAEzD,iBAAiB,QAAQ;CAC3B;CAEA,OAAO,iBAAiB;AAC1B;AAEA,SAAgB,kBAAmD,MAAa;CAC9E,OAAO,iBAAiB;AAC1B"}
1
+ {"version":3,"file":"config.mjs","names":[],"sources":["../../../src/runtime/internal/config.ts"],"sourcesContent":["import { defu } from \"defu\";\nimport { useNitroApp, useRuntimeConfig } from \"nitropack/runtime\";\n\nimport type { Datasources, DatasourceConfig } from \"..\";\n\nconst datasourceConfig: Partial<DatasourceConfig> = {};\n\n/**\n * @internal\n */\nexport async function getCachedDatasourceConfig<TName extends keyof Datasources & string>(\n name: TName,\n) {\n if (name in datasourceConfig && !datasourceConfig[name]) {\n throw new Error(\n \"Cannot obtain datasource config. Do you try to obtain it inside the 'drizzle:config' hook?\",\n );\n } else {\n datasourceConfig[name] = undefined;\n\n const runtimeConfig = useRuntimeConfig();\n // const config = structuredClone<DrizzleRuntimeConfig[TName]>(runtimeConfig.drizzle?.[name] ?? {})\n const config = defu(runtimeConfig.drizzle?.[name], {});\n const nitro = useNitroApp();\n await nitro.hooks.callHook(\"drizzle:config\", name, config);\n\n datasourceConfig[name] = config;\n }\n\n return datasourceConfig[name];\n}\n\nexport function clearCachedConfig<TName extends keyof Datasources>(name: TName) {\n delete datasourceConfig[name];\n}\n"],"mappings":";;;AAKA,MAAM,mBAA8C,CAAC;;;;AAKrD,eAAsB,0BACpB,MACA;CACA,IAAI,QAAQ,oBAAoB,CAAC,iBAAiB,OAChD,MAAM,IAAI,MACR,4FACF;MACK;EACL,iBAAiB,QAAQ,KAAA;EAIzB,MAAM,SAAS,KAFO,iBAEU,CAAC,CAAC,UAAU,OAAO,CAAC,CAAC;EAErD,MADc,YACJ,CAAC,CAAC,MAAM,SAAS,kBAAkB,MAAM,MAAM;EAEzD,iBAAiB,QAAQ;CAC3B;CAEA,OAAO,iBAAiB;AAC1B;AAEA,SAAgB,kBAAmD,MAAa;CAC9E,OAAO,iBAAiB;AAC1B"}
@@ -56,18 +56,18 @@ type DatasourceProviderConfig<T extends DatasourceProvider<any, any>> = T extend
56
56
  /**
57
57
  * Configuration type mapping datasource names to their configurations.
58
58
  */
59
- type Config = { [K in keyof DatasourceRegistry]: DatasourceProviderConfig<DatasourceRegistry[K]> };
59
+ type DatasourceConfig = { [K in keyof DatasourceRegistry]: DatasourceProviderConfig<DatasourceRegistry[K]> };
60
60
  /**
61
61
  * Runtime configuration type with primitive values for each datasource.
62
62
  * Used for Nitro runtime config.
63
63
  */
64
- type RuntimeConfig = { [K in keyof Config]: PrimitiveProps<Config[K]> };
64
+ type RuntimeConfig = { [K in keyof DatasourceConfig]: PrimitiveProps<DatasourceConfig[K]> };
65
65
  declare module "nitropack/types" {
66
66
  interface NitroRuntimeConfig {
67
67
  drizzle?: RuntimeConfig;
68
68
  }
69
69
  }
70
- type ConfigHookArgs = { [K in keyof Config]: [name: K, config: Config[K]] }[keyof Config];
70
+ type ConfigHookArgs = { [K in keyof DatasourceConfig]: [name: K, config: DatasourceConfig[K]] }[keyof DatasourceConfig];
71
71
  interface ConfigHooks {
72
72
  "drizzle:config": (...args: ConfigHookArgs) => void | Promise<void>;
73
73
  }
@@ -75,5 +75,5 @@ declare module "nitropack/types" {
75
75
  interface NitroRuntimeHooks extends ConfigHooks {}
76
76
  }
77
77
  //#endregion
78
- export { Config, DatabaseFactory, DatasourceProvider, DatasourceProviderConfig, DatasourceRegistry, Datasources, Primitive, PrimitiveProps, RuntimeConfig, ToDatasource, ToDatasourceProvider, UseDatasourceOptions, useDatasource };
78
+ export { DatabaseFactory, DatasourceConfig, DatasourceProvider, DatasourceProviderConfig, DatasourceRegistry, Datasources, Primitive, PrimitiveProps, RuntimeConfig, ToDatasource, ToDatasourceProvider, UseDatasourceOptions, useDatasource };
79
79
  //# sourceMappingURL=runtime.d.mts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nitro-drizzle",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Nitro module to integrate Drizzle ORM with ease.",
5
5
  "homepage": "https://github.com/Myshkouski/nitro-drizzle#readme",
6
6
  "bugs": {
@@ -94,6 +94,7 @@
94
94
  "build": "tsdown",
95
95
  "build:dev": "tsdown --watch",
96
96
  "fmt": "oxfmt",
97
+ "fmt:check": "oxfmt --check",
97
98
  "lint": "oxlint",
98
99
  "playground": "nitro dev playground",
99
100
  "test": "vitest run",