nitro-drizzle 0.0.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +247 -0
- package/dist/config/defineConfig.d.mts +410 -0
- package/dist/config/defineConfig.mjs +34 -0
- package/dist/config/defineConfig.mjs.map +1 -0
- package/dist/config.d.mts +2 -0
- package/dist/config.mjs +2 -0
- package/dist/context/internal/async.mjs +15 -0
- package/dist/context/internal/async.mjs.map +1 -0
- package/dist/context/internal/config.mjs +31 -0
- package/dist/context/internal/config.mjs.map +1 -0
- package/dist/context/internal/fs.mjs +15 -0
- package/dist/context/internal/fs.mjs.map +1 -0
- package/dist/context/internal/logger.mjs +20 -0
- package/dist/context/internal/logger.mjs.map +1 -0
- package/dist/context/internal/templates.mjs +40 -0
- package/dist/context/internal/templates.mjs.map +1 -0
- package/dist/context/internal/virtual.mjs +71 -0
- package/dist/context/internal/virtual.mjs.map +1 -0
- package/dist/context.d.mts +113 -0
- package/dist/context.mjs +78 -0
- package/dist/context.mjs.map +1 -0
- package/dist/dialects/index.d.mts +39 -0
- package/dist/dialects/index.mjs +1 -0
- package/dist/dialects/mysql.d.mts +30 -0
- package/dist/dialects/mysql.mjs +30 -0
- package/dist/dialects/mysql.mjs.map +1 -0
- package/dist/dialects/postgresql.d.mts +29 -0
- package/dist/dialects/postgresql.mjs +29 -0
- package/dist/dialects/postgresql.mjs.map +1 -0
- package/dist/dialects/sqlite.d.mts +29 -0
- package/dist/dialects/sqlite.mjs +29 -0
- package/dist/dialects/sqlite.mjs.map +1 -0
- package/dist/drivers/d1-http.d.mts +41 -0
- package/dist/drivers/d1-http.mjs +105 -0
- package/dist/drivers/d1-http.mjs.map +1 -0
- package/dist/drivers/d1.d.mts +26 -0
- package/dist/drivers/d1.mjs +52 -0
- package/dist/drivers/d1.mjs.map +1 -0
- package/dist/drivers/db0.d.mts +20 -0
- package/dist/drivers/db0.mjs +27 -0
- package/dist/drivers/db0.mjs.map +1 -0
- package/dist/drivers/index.d.mts +35 -0
- package/dist/drivers/index.mjs +14 -0
- package/dist/drivers/index.mjs.map +1 -0
- package/dist/drivers/internal/d1-dialect.mjs +41 -0
- package/dist/drivers/internal/d1-dialect.mjs.map +1 -0
- package/dist/drivers/internal/sql.mjs +6 -0
- package/dist/drivers/internal/sql.mjs.map +1 -0
- package/dist/drivers/mysql.d.mts +27 -0
- package/dist/drivers/mysql.mjs +32 -0
- package/dist/drivers/mysql.mjs.map +1 -0
- package/dist/drivers/pglite.d.mts +29 -0
- package/dist/drivers/pglite.mjs +29 -0
- package/dist/drivers/pglite.mjs.map +1 -0
- package/dist/drivers/postgresql.d.mts +27 -0
- package/dist/drivers/postgresql.mjs +30 -0
- package/dist/drivers/postgresql.mjs.map +1 -0
- package/dist/drivers/sqlite.d.mts +28 -0
- package/dist/drivers/sqlite.mjs +30 -0
- package/dist/drivers/sqlite.mjs.map +1 -0
- package/dist/meta.d.mts +8 -0
- package/dist/meta.mjs +9 -0
- package/dist/meta.mjs.map +1 -0
- package/dist/migrations/internal/migrate.d.mts +16 -0
- package/dist/migrations/internal/migrate.mjs +33 -0
- package/dist/migrations/internal/migrate.mjs.map +1 -0
- package/dist/migrations/migrate.d.mts +16 -0
- package/dist/migrations/migrate.mjs +23 -0
- package/dist/migrations/migrate.mjs.map +1 -0
- package/dist/migrations/task.d.mts +7 -0
- package/dist/migrations/task.mjs +11 -0
- package/dist/migrations/task.mjs.map +1 -0
- package/dist/migrations/useMigrations.d.mts +46 -0
- package/dist/migrations/useMigrations.mjs +57 -0
- package/dist/migrations/useMigrations.mjs.map +1 -0
- package/dist/migrations.d.mts +4 -0
- package/dist/migrations.mjs +3 -0
- package/dist/module/internal/assets.d.mts +12 -0
- package/dist/module/internal/assets.mjs +21 -0
- package/dist/module/internal/assets.mjs.map +1 -0
- package/dist/module/internal/externals.mjs +19 -0
- package/dist/module/internal/externals.mjs.map +1 -0
- package/dist/module/internal/fs.mjs +11 -0
- package/dist/module/internal/fs.mjs.map +1 -0
- package/dist/module/internal/path.mjs +11 -0
- package/dist/module/internal/path.mjs.map +1 -0
- package/dist/module/internal/reload.mjs +17 -0
- package/dist/module/internal/reload.mjs.map +1 -0
- package/dist/module/internal/resolver.mjs +27 -0
- package/dist/module/internal/resolver.mjs.map +1 -0
- package/dist/module/internal/rollup.mjs +8 -0
- package/dist/module/internal/rollup.mjs.map +1 -0
- package/dist/module/internal/types.mjs +39 -0
- package/dist/module/internal/types.mjs.map +1 -0
- package/dist/module/internal/virtual.mjs +23 -0
- package/dist/module/internal/virtual.mjs.map +1 -0
- package/dist/module.d.mts +37 -0
- package/dist/module.mjs +82 -0
- package/dist/module.mjs.map +1 -0
- package/dist/plugins/init.d.mts +24 -0
- package/dist/plugins/init.mjs +33 -0
- package/dist/plugins/init.mjs.map +1 -0
- package/dist/plugins/migrate.d.mts +22 -0
- package/dist/plugins/migrate.mjs +25 -0
- package/dist/plugins/migrate.mjs.map +1 -0
- package/dist/runtime/internal/config.mjs +24 -0
- package/dist/runtime/internal/config.mjs.map +1 -0
- package/dist/runtime/internal/createDatasource.mjs +15 -0
- package/dist/runtime/internal/createDatasource.mjs.map +1 -0
- package/dist/runtime/useDatasource.d.mts +19 -0
- package/dist/runtime/useDatasource.mjs +35 -0
- package/dist/runtime/useDatasource.mjs.map +1 -0
- package/dist/runtime.d.mts +79 -0
- package/dist/runtime.mjs +2 -0
- package/dist/shared.d.mts +12 -0
- package/dist/shared.mjs +1 -0
- package/dist/utils/usePrimaryKey.d.mts +14 -0
- package/dist/utils/usePrimaryKey.mjs +19 -0
- package/dist/utils/usePrimaryKey.mjs.map +1 -0
- package/dist/utils.d.mts +2 -0
- package/dist/utils.mjs +2 -0
- package/package.json +100 -3
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { relativeWithDot } from "./path.mjs";
|
|
2
|
+
import { writeFile } from "./fs.mjs";
|
|
3
|
+
import { join } from "pathe";
|
|
4
|
+
//#region src/module/internal/types.ts
|
|
5
|
+
async function addAugmentations(nitroOptions, nitroTypes, augmentations) {
|
|
6
|
+
nitroTypes.tsConfig ||= {};
|
|
7
|
+
nitroTypes.tsConfig.include ||= [];
|
|
8
|
+
const paths = await writeTypeFiles(nitroOptions, augmentations);
|
|
9
|
+
nitroTypes.tsConfig.include.push(...paths);
|
|
10
|
+
}
|
|
11
|
+
async function addDeclarations(nitroOptions, nitroTypes, declarations) {
|
|
12
|
+
nitroTypes.tsConfig ||= {};
|
|
13
|
+
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]);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function typesDir(nitroOptions) {
|
|
20
|
+
return join(nitroOptions.buildDir, "types");
|
|
21
|
+
}
|
|
22
|
+
async function writeTypeFiles(nitroOptions, modules) {
|
|
23
|
+
const paths = [];
|
|
24
|
+
const dir = typesDir(nitroOptions);
|
|
25
|
+
for (const filename in modules) {
|
|
26
|
+
const typesFilename = join(dir, filename);
|
|
27
|
+
await writeFile(typesFilename, await getContent(modules[filename]));
|
|
28
|
+
const relativeFilename = relativeWithDot(dir, typesFilename);
|
|
29
|
+
paths.push(relativeFilename);
|
|
30
|
+
}
|
|
31
|
+
return paths;
|
|
32
|
+
}
|
|
33
|
+
async function getContent(module) {
|
|
34
|
+
return "string" == typeof module ? module : await module();
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { addAugmentations, addDeclarations };
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=types.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","names":[],"sources":["../../../src/module/internal/types.ts"],"sourcesContent":["import type { NitroOptions, NitroTypes, VirtualModule } from \"nitropack/types\";\nimport { join } 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\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(dir, 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,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,KAAK,aAAa;EAC3D,MAAM,KAAK,gBAAgB;CAC7B;CACA,OAAO;AACT;AAEA,eAAe,WAAW,QAAuB;CAC/C,OAAO,YAAY,OAAO,SAAS,SAAS,MAAM,OAAO;AAC3D"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { genArrayFromRaw, genObjectFromRawEntries, genString } from "knitwork";
|
|
2
|
+
//#region src/module/internal/virtual.ts
|
|
3
|
+
function migrationsVirtualModule(datasources, options) {
|
|
4
|
+
if (!options) return {};
|
|
5
|
+
const parts = [];
|
|
6
|
+
const enabledDatasources = datasources.filter((d) => d.enabled);
|
|
7
|
+
parts.push(`
|
|
8
|
+
export const migrationConfig = ${genObjectFromRawEntries(enabledDatasources.map(({ name, migrations }) => {
|
|
9
|
+
return [name, JSON.stringify(migrations.config)];
|
|
10
|
+
}))};
|
|
11
|
+
`);
|
|
12
|
+
let migrateOnInit = enabledDatasources.map((d) => d.name);
|
|
13
|
+
if (options && Array.isArray(options.migrateOnInit)) migrateOnInit = migrateOnInit.filter((name) => options.migrateOnInit.includes(name));
|
|
14
|
+
parts.push(`
|
|
15
|
+
export const MIGRATIONS_STORAGE_BASE = ${genString(options.storageBase)};
|
|
16
|
+
export const MIGRATE_ON_INIT = ${genArrayFromRaw(migrateOnInit.map((name) => genString(name)))};
|
|
17
|
+
`);
|
|
18
|
+
return { "#nitro-drizzle/migrations": parts.join("\n") };
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { migrationsVirtualModule };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=virtual.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtual.mjs","names":[],"sources":["../../../src/module/internal/virtual.ts"],"sourcesContent":["import { genString, genArrayFromRaw, genObjectFromRawEntries } from \"knitwork\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\nimport type { MigrationOptions } from \"./assets\";\nimport type { DatasourceInfo } from \"nitro-drizzle/context\";\n\nexport function migrationsVirtualModule(\n datasources: DatasourceInfo[],\n options: false | MigrationOptions,\n): VirtualModules {\n if (!options) {\n return {};\n }\n\n const parts: string[] = [];\n\n const enabledDatasources = datasources.filter((d) => d.enabled);\n\n parts.push(/*js*/ `\n export const migrationConfig = ${genObjectFromRawEntries(\n enabledDatasources.map(({ name, migrations }) => {\n return [name, JSON.stringify(migrations.config)];\n }),\n )};\n `);\n\n let migrateOnInit = enabledDatasources.map((d) => d.name);\n if (options && Array.isArray(options.migrateOnInit)) {\n migrateOnInit = migrateOnInit.filter((name) =>\n (options.migrateOnInit as readonly string[]).includes(name),\n );\n }\n\n parts.push(/*js*/ `\n export const MIGRATIONS_STORAGE_BASE = ${genString(options.storageBase)};\n export const MIGRATE_ON_INIT = ${genArrayFromRaw(migrateOnInit.map((name) => genString(name)))};\n `);\n\n return {\n \"#nitro-drizzle/migrations\": parts.join(\"\\n\"),\n };\n}\n"],"mappings":";;AAKA,SAAgB,wBACd,aACA,SACgB;CAChB,IAAI,CAAC,SACH,OAAO,CAAC;CAGV,MAAM,QAAkB,CAAC;CAEzB,MAAM,qBAAqB,YAAY,QAAQ,MAAM,EAAE,OAAO;CAE9D,MAAM,KAAY;qCACiB,wBAC/B,mBAAmB,KAAK,EAAE,MAAM,iBAAiB;EAC/C,OAAO,CAAC,MAAM,KAAK,UAAU,WAAW,MAAM,CAAC;CACjD,CAAC,CACH,EAAE;GACH;CAED,IAAI,gBAAgB,mBAAmB,KAAK,MAAM,EAAE,IAAI;CACxD,IAAI,WAAW,MAAM,QAAQ,QAAQ,aAAa,GAChD,gBAAgB,cAAc,QAAQ,SACnC,QAAQ,cAAoC,SAAS,IAAI,CAC5D;CAGF,MAAM,KAAY;6CACyB,UAAU,QAAQ,WAAW,EAAE;qCACvC,gBAAgB,cAAc,KAAK,SAAS,UAAU,IAAI,CAAC,CAAC,EAAE;GAChG;CAED,OAAO,EACL,6BAA6B,MAAM,KAAK,IAAI,EAC9C;AACF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { MigrationOptions } from "./module/internal/assets.mjs";
|
|
2
|
+
import { ConfigPattern } from "nitro-drizzle/context";
|
|
3
|
+
import { NitroModule } from "nitropack/types";
|
|
4
|
+
|
|
5
|
+
//#region src/module/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Datasource-specific configuration options.
|
|
8
|
+
* This interface is augmented by the module with connector-specific options.
|
|
9
|
+
*/
|
|
10
|
+
interface DatasourceOptions {}
|
|
11
|
+
declare module "nitropack/types" {
|
|
12
|
+
interface NitroOptions {
|
|
13
|
+
drizzle: ModuleOptions;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
interface ModuleOptions {
|
|
17
|
+
/**
|
|
18
|
+
* Directory to search datasources
|
|
19
|
+
*/
|
|
20
|
+
baseDir: string;
|
|
21
|
+
/**
|
|
22
|
+
* Patterns to search Drizzle configs
|
|
23
|
+
*/
|
|
24
|
+
configPattern: ConfigPattern;
|
|
25
|
+
/**
|
|
26
|
+
* @deprecated Use 'nitro.database' instead.
|
|
27
|
+
*/
|
|
28
|
+
datasources?: DatasourceOptions;
|
|
29
|
+
/**
|
|
30
|
+
* Enable migrations storage
|
|
31
|
+
*/
|
|
32
|
+
migrations: false | MigrationOptions;
|
|
33
|
+
}
|
|
34
|
+
declare const module: NitroModule;
|
|
35
|
+
//#endregion
|
|
36
|
+
export { DatasourceOptions, ModuleOptions, module as default };
|
|
37
|
+
//# sourceMappingURL=module.d.mts.map
|
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { addAugmentations, addDeclarations } from "./module/internal/types.mjs";
|
|
2
|
+
import { createResolver } from "./module/internal/resolver.mjs";
|
|
3
|
+
import { updateMigrationAssets } from "./module/internal/assets.mjs";
|
|
4
|
+
import { addInlineExternals } from "./module/internal/externals.mjs";
|
|
5
|
+
import { reloadPlugin } from "./module/internal/reload.mjs";
|
|
6
|
+
import { addPlugin } from "./module/internal/rollup.mjs";
|
|
7
|
+
import { migrationsVirtualModule } from "./module/internal/virtual.mjs";
|
|
8
|
+
import { defu } from "defu";
|
|
9
|
+
import { resolve } from "pathe";
|
|
10
|
+
import { resolveAlias } from "pathe/utils";
|
|
11
|
+
import { createContext } from "nitro-drizzle/context";
|
|
12
|
+
import { pkgName } from "nitro-drizzle/meta";
|
|
13
|
+
//#region src/module/index.ts
|
|
14
|
+
const defaultModuleOptions = {
|
|
15
|
+
baseDir: "~/drizzle",
|
|
16
|
+
configPattern: ["drizzle.config.*", "drizzle-*.config.*"],
|
|
17
|
+
migrations: {
|
|
18
|
+
storageBase: "drizzle:migrations",
|
|
19
|
+
migrateOnInit: false
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const module = {
|
|
23
|
+
name: pkgName,
|
|
24
|
+
async setup(nitro) {
|
|
25
|
+
const resolver = createResolver(nitro.options.rootDir, { alias: nitro.options.alias });
|
|
26
|
+
const moduleOptions = defu(nitro.options.drizzle, defaultModuleOptions);
|
|
27
|
+
const resolvedBaseDir = resolve(nitro.options.srcDir, resolveAlias(moduleOptions.baseDir, nitro.options.alias));
|
|
28
|
+
const contextOptions = {
|
|
29
|
+
cwd: process.cwd(),
|
|
30
|
+
resolver,
|
|
31
|
+
baseDir: resolvedBaseDir,
|
|
32
|
+
logger: nitro.logger,
|
|
33
|
+
configPattern: moduleOptions.configPattern,
|
|
34
|
+
datasource: { ...moduleOptions.datasources }
|
|
35
|
+
};
|
|
36
|
+
const context = createContext(contextOptions);
|
|
37
|
+
addInlineExternals(nitro.options);
|
|
38
|
+
const plugins = {
|
|
39
|
+
migrate: false,
|
|
40
|
+
init: true
|
|
41
|
+
};
|
|
42
|
+
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
|
+
});
|
|
50
|
+
if (nitro.options.imports) nitro.options.imports.imports ||= [];
|
|
51
|
+
if (moduleOptions.migrations) {
|
|
52
|
+
await updateMigrationAssets(context, nitro.options, moduleOptions.migrations);
|
|
53
|
+
if (nitro.options.experimental.tasks) {
|
|
54
|
+
nitro.options.tasks ||= {};
|
|
55
|
+
nitro.options.tasks["drizzle:migrate"] = {
|
|
56
|
+
description: "Run drizzle migrations for a datasource.",
|
|
57
|
+
handler: "nitro-drizzle/migrations/task"
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (moduleOptions.migrations.migrateOnInit) plugins.migrate = true;
|
|
61
|
+
}
|
|
62
|
+
nitro.hooks.hook("rollup:before", async (nitro, config) => {
|
|
63
|
+
await addPlugin(config, reloadPlugin(nitro, contextOptions));
|
|
64
|
+
});
|
|
65
|
+
const enabledPlugins = Object.entries(plugins).reduce((plugins, [pluginName, enabled]) => {
|
|
66
|
+
if (enabled) plugins.push(pluginName);
|
|
67
|
+
return plugins;
|
|
68
|
+
}, []).map((pluginName) => {
|
|
69
|
+
return `nitro-drizzle/plugins/${pluginName}`;
|
|
70
|
+
});
|
|
71
|
+
for (const pluginId of enabledPlugins) nitro.options.plugins.push(pluginId);
|
|
72
|
+
if (enabledPlugins.length) nitro.hooks.hook("types:extend", async (types) => {
|
|
73
|
+
await addAugmentations(nitro.options, types, { "nitro-drizzle/plugins.d.ts": enabledPlugins.map((pluginId) => {
|
|
74
|
+
return `import "${pluginId}";`;
|
|
75
|
+
}).join("\n") });
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
export { module as default };
|
|
81
|
+
|
|
82
|
+
//# sourceMappingURL=module.mjs.map
|
|
@@ -0,0 +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\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/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;EAC1D,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,EAC3C,8BAA8B,eAC3B,KAAK,aAAa;IACjB,OAAO,WAAW,SAAS;GAC7B,CAAC,CAAC,CACD,KAAK,IAAI,EACd,CAAC;EACH,CAAC;CAEL;AACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/plugins/init.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Nitro plugin that triggers the 'drizzle:init' hook during server initialization.
|
|
4
|
+
* Used to run migration plugins at startup.
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: import("nitropack").NitroAppPlugin;
|
|
7
|
+
type ReadyState = "pending" | "done" | "error";
|
|
8
|
+
type InitHooks = {
|
|
9
|
+
"drizzle:init": () => void;
|
|
10
|
+
};
|
|
11
|
+
declare module "nitropack/types" {
|
|
12
|
+
interface NitroRuntimeHooks extends InitHooks {}
|
|
13
|
+
}
|
|
14
|
+
declare module "h3" {
|
|
15
|
+
interface H3EventContext {
|
|
16
|
+
drizzle: {
|
|
17
|
+
readyState: ReadyState;
|
|
18
|
+
waitReady: () => Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { InitHooks, ReadyState, _default as default };
|
|
24
|
+
//# sourceMappingURL=init.d.mts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
2
|
+
//#region src/plugins/init.ts
|
|
3
|
+
/**
|
|
4
|
+
* Nitro plugin that triggers the 'drizzle:init' hook during server initialization.
|
|
5
|
+
* Used to run migration plugins at startup.
|
|
6
|
+
*/
|
|
7
|
+
var init_default = defineNitroPlugin(async (nitro) => {
|
|
8
|
+
let initHook;
|
|
9
|
+
let readyState = "pending";
|
|
10
|
+
const waitReady = async () => {
|
|
11
|
+
await initHook;
|
|
12
|
+
};
|
|
13
|
+
nitro.hooks.hook("request", (event) => {
|
|
14
|
+
event.context.drizzle = {
|
|
15
|
+
readyState,
|
|
16
|
+
waitReady
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
initHook = new Promise((resolve, reject) => {
|
|
20
|
+
nitro.hooks.callHook("drizzle:init").then(() => {
|
|
21
|
+
readyState = "done";
|
|
22
|
+
resolve(readyState);
|
|
23
|
+
}).catch((err) => {
|
|
24
|
+
readyState = "error";
|
|
25
|
+
reject(err);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
await initHook;
|
|
29
|
+
});
|
|
30
|
+
//#endregion
|
|
31
|
+
export { init_default as default };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=init.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.mjs","names":[],"sources":["../../src/plugins/init.ts"],"sourcesContent":["import { defineNitroPlugin } from \"nitropack/runtime\";\n\n/**\n * Nitro plugin that triggers the 'drizzle:init' hook during server initialization.\n * Used to run migration plugins at startup.\n */\nexport default defineNitroPlugin(async (nitro) => {\n let initHook: Promise<ReadyState>;\n let readyState: ReadyState = \"pending\";\n\n const waitReady = async () => {\n await initHook;\n };\n\n nitro.hooks.hook(\"request\", (event) => {\n event.context.drizzle = {\n readyState,\n waitReady,\n };\n });\n\n initHook = new Promise((resolve, reject) => {\n nitro.hooks\n .callHook(\"drizzle:init\")\n .then(() => {\n readyState = \"done\";\n resolve(readyState);\n })\n .catch((err) => {\n readyState = \"error\";\n reject(err);\n });\n });\n\n await initHook;\n});\n\nexport type ReadyState = \"pending\" | \"done\" | \"error\";\n\nexport type InitHooks = {\n \"drizzle:init\": () => void;\n};\n\ndeclare module \"nitropack/types\" {\n interface NitroRuntimeHooks extends InitHooks {}\n}\n\ndeclare module \"h3\" {\n interface H3EventContext {\n drizzle: {\n readyState: ReadyState;\n waitReady: () => Promise<void>;\n };\n }\n}\n"],"mappings":";;;;;;AAMA,IAAA,eAAe,kBAAkB,OAAO,UAAU;CAChD,IAAI;CACJ,IAAI,aAAyB;CAE7B,MAAM,YAAY,YAAY;EAC5B,MAAM;CACR;CAEA,MAAM,MAAM,KAAK,YAAY,UAAU;EACrC,MAAM,QAAQ,UAAU;GACtB;GACA;EACF;CACF,CAAC;CAED,WAAW,IAAI,SAAS,SAAS,WAAW;EAC1C,MAAM,MACH,SAAS,cAAc,CAAC,CACxB,WAAW;GACV,aAAa;GACb,QAAQ,UAAU;EACpB,CAAC,CAAC,CACD,OAAO,QAAQ;GACd,aAAa;GACb,OAAO,GAAG;EACZ,CAAC;CACL,CAAC;CAED,MAAM;AACR,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DatasourceRegistry } from "nitro-drizzle/runtime";
|
|
2
|
+
import { MigrationResult } from "nitro-drizzle/migrations";
|
|
3
|
+
import { NitroAppPlugin } from "nitropack/types";
|
|
4
|
+
|
|
5
|
+
//#region src/plugins/migrate.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Nitro plugin that runs migrations on initialization.
|
|
8
|
+
* Registers hooks to run migrations for datasources configured with migrateOnInit.
|
|
9
|
+
*/
|
|
10
|
+
declare const plugin: NitroAppPlugin;
|
|
11
|
+
/** Type for migration hooks registered for each datasource. */
|
|
12
|
+
interface MigrationHooks {
|
|
13
|
+
"drizzle:migrate:before": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;
|
|
14
|
+
"drizzle:migrate": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;
|
|
15
|
+
"drizzle:migrate:after": (datasource: keyof DatasourceRegistry & string, result: MigrationResult) => Promise<void> | void;
|
|
16
|
+
}
|
|
17
|
+
declare module "nitropack/types" {
|
|
18
|
+
interface NitroRuntimeHooks extends MigrationHooks {}
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { MigrationHooks, plugin as default };
|
|
22
|
+
//# sourceMappingURL=migrate.d.mts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
2
|
+
import { MIGRATE_ON_INIT } from "#nitro-drizzle/migrations";
|
|
3
|
+
import { migrate } from "nitro-drizzle/migrations";
|
|
4
|
+
//#region src/plugins/migrate.ts
|
|
5
|
+
const MIGRATION_HOOK_KEY = "drizzle:migrate";
|
|
6
|
+
/**
|
|
7
|
+
* Nitro plugin that runs migrations on initialization.
|
|
8
|
+
* Registers hooks to run migrations for datasources configured with migrateOnInit.
|
|
9
|
+
*/
|
|
10
|
+
const plugin = defineNitroPlugin(async (nitro) => {
|
|
11
|
+
nitro.hooks.hook(MIGRATION_HOOK_KEY, async (name) => {
|
|
12
|
+
await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:before`, name);
|
|
13
|
+
const result = await migrate(name);
|
|
14
|
+
await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:after`, name, result);
|
|
15
|
+
});
|
|
16
|
+
nitro.hooks.hookOnce("drizzle:init", async () => {
|
|
17
|
+
await Promise.all(MIGRATE_ON_INIT.map((name) => {
|
|
18
|
+
return nitro.hooks.callHookParallel(MIGRATION_HOOK_KEY, name);
|
|
19
|
+
}));
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
//#endregion
|
|
23
|
+
export { plugin as default };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=migrate.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.mjs","names":[],"sources":["../../src/plugins/migrate.ts"],"sourcesContent":["import { defineNitroPlugin } from \"nitropack/runtime\";\nimport type { NitroAppPlugin } from \"nitropack/types\";\nimport { migrate, type MigrationResult } from \"nitro-drizzle/migrations\";\n\nimport { MIGRATE_ON_INIT } from \"#nitro-drizzle/migrations\";\nimport type { DatasourceRegistry } from \"nitro-drizzle/runtime\";\n\nconst MIGRATION_HOOK_KEY = \"drizzle:migrate\" as const;\n\n/**\n * Nitro plugin that runs migrations on initialization.\n * Registers hooks to run migrations for datasources configured with migrateOnInit.\n */\nconst plugin: NitroAppPlugin = defineNitroPlugin(async (nitro) => {\n nitro.hooks.hook(MIGRATION_HOOK_KEY, async (name) => {\n await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:before`, name);\n const result = await migrate(name);\n await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:after`, name, result);\n });\n\n nitro.hooks.hookOnce(\"drizzle:init\", async () => {\n await Promise.all(\n MIGRATE_ON_INIT.map((name) => {\n return nitro.hooks.callHookParallel(MIGRATION_HOOK_KEY, name);\n }),\n );\n });\n});\n\nexport default plugin;\n\n/** Type for migration hooks registered for each datasource. */\nexport interface MigrationHooks {\n \"drizzle:migrate:before\": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;\n \"drizzle:migrate\": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;\n \"drizzle:migrate:after\": (\n datasource: keyof DatasourceRegistry & string,\n result: MigrationResult,\n ) => Promise<void> | void;\n}\n\ndeclare module \"nitropack/types\" {\n interface NitroRuntimeHooks extends MigrationHooks {}\n}\n"],"mappings":";;;;AAOA,MAAM,qBAAqB;;;;;AAM3B,MAAM,SAAyB,kBAAkB,OAAO,UAAU;CAChE,MAAM,MAAM,KAAK,oBAAoB,OAAO,SAAS;EACnD,MAAM,MAAM,MAAM,SAAS,GAAG,mBAAmB,UAAU,IAAI;EAC/D,MAAM,SAAS,MAAM,QAAQ,IAAI;EACjC,MAAM,MAAM,MAAM,SAAS,GAAG,mBAAmB,SAAS,MAAM,MAAM;CACxE,CAAC;CAED,MAAM,MAAM,SAAS,gBAAgB,YAAY;EAC/C,MAAM,QAAQ,IACZ,gBAAgB,KAAK,SAAS;GAC5B,OAAO,MAAM,MAAM,iBAAiB,oBAAoB,IAAI;EAC9D,CAAC,CACH;CACF,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { defu } from "defu";
|
|
2
|
+
import { useNitroApp, useRuntimeConfig } from "nitropack/runtime";
|
|
3
|
+
//#region src/runtime/internal/config.ts
|
|
4
|
+
const datasourceConfig = {};
|
|
5
|
+
/**
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
async function getCachedDatasourceConfig(name) {
|
|
9
|
+
if (name in datasourceConfig && !datasourceConfig[name]) throw new Error("Cannot obtain datasource config. Do you try to obtain it inside the 'drizzle:config' hook?");
|
|
10
|
+
else {
|
|
11
|
+
datasourceConfig[name] = void 0;
|
|
12
|
+
const config = defu(useRuntimeConfig().drizzle?.[name], {});
|
|
13
|
+
await useNitroApp().hooks.callHook("drizzle:config", name, config);
|
|
14
|
+
datasourceConfig[name] = config;
|
|
15
|
+
}
|
|
16
|
+
return datasourceConfig[name];
|
|
17
|
+
}
|
|
18
|
+
function clearCachedConfig(name) {
|
|
19
|
+
delete datasourceConfig[name];
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { clearCachedConfig, getCachedDatasourceConfig };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=config.mjs.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getCachedDatasourceConfig } from "./config.mjs";
|
|
2
|
+
import { useDatasourceRegistry } from "#nitro-drizzle/runtime";
|
|
3
|
+
//#region src/runtime/internal/createDatasource.ts
|
|
4
|
+
/**
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
async function createDatasource(name) {
|
|
8
|
+
const datasourceProvider = useDatasourceRegistry()[name];
|
|
9
|
+
const datasourceConfig = await getCachedDatasourceConfig(name);
|
|
10
|
+
return await datasourceProvider.create(datasourceConfig);
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { createDatasource };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=createDatasource.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createDatasource.mjs","names":[],"sources":["../../../src/runtime/internal/createDatasource.ts"],"sourcesContent":["import { useDatasourceRegistry } from \"#nitro-drizzle/runtime\";\nimport type { DatasourceProvider, DatasourceRegistry } from \"..\";\nimport { getCachedDatasourceConfig } from \"./config\";\n\n/**\n * @internal\n */\nexport async function createDatasource(name: keyof DatasourceRegistry & string) {\n const datasourceRegistry = useDatasourceRegistry();\n const datasourceProvider = datasourceRegistry[name] as DatasourceProvider<any, any>;\n const datasourceConfig = await getCachedDatasourceConfig(name);\n return await datasourceProvider.create(datasourceConfig);\n}\n"],"mappings":";;;;;;AAOA,eAAsB,iBAAiB,MAAyC;CAE9E,MAAM,qBADqB,sBACiB,CAAC,CAAC;CAC9C,MAAM,mBAAmB,MAAM,0BAA0B,IAAI;CAC7D,OAAO,MAAM,mBAAmB,OAAO,gBAAgB;AACzD"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Datasources } from "../runtime.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/useDatasource.d.ts
|
|
4
|
+
/** Options for datasource creation and lifecycle management. */
|
|
5
|
+
type UseDatasourceOptions = Partial<{
|
|
6
|
+
/** Whether to automatically close the datasource when Nitro app closes. */autoClose: boolean;
|
|
7
|
+
}>;
|
|
8
|
+
/**
|
|
9
|
+
* Gets or creates a datasource instance by name.
|
|
10
|
+
* Caches the datasource for reuse.
|
|
11
|
+
* @template TName - The datasource name
|
|
12
|
+
* @param name - The datasource name
|
|
13
|
+
* @param options - Lifecycle options
|
|
14
|
+
* @returns The datasource instance
|
|
15
|
+
*/
|
|
16
|
+
declare function useDatasource<TName extends keyof Datasources & string>(name: TName, options?: UseDatasourceOptions): Promise<Datasources[TName]>;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { UseDatasourceOptions, useDatasource };
|
|
19
|
+
//# sourceMappingURL=useDatasource.d.mts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { clearCachedConfig } from "./internal/config.mjs";
|
|
2
|
+
import { createDatasource } from "./internal/createDatasource.mjs";
|
|
3
|
+
import { useNitroApp } from "nitropack/runtime";
|
|
4
|
+
//#region src/runtime/useDatasource.ts
|
|
5
|
+
const datasources = {};
|
|
6
|
+
/**
|
|
7
|
+
* Gets or creates a datasource instance by name.
|
|
8
|
+
* Caches the datasource for reuse.
|
|
9
|
+
* @template TName - The datasource name
|
|
10
|
+
* @param name - The datasource name
|
|
11
|
+
* @param options - Lifecycle options
|
|
12
|
+
* @returns The datasource instance
|
|
13
|
+
*/
|
|
14
|
+
async function useDatasource(name, options = {}) {
|
|
15
|
+
let datasourcePromise;
|
|
16
|
+
if (name in datasources) datasourcePromise = datasources[name];
|
|
17
|
+
else {
|
|
18
|
+
datasourcePromise = datasources[name] = createDatasource(name);
|
|
19
|
+
const { autoClose = true } = options;
|
|
20
|
+
if (autoClose) useNitroApp().hooks.hookOnce("close", async () => {
|
|
21
|
+
const datasource = await datasourcePromise;
|
|
22
|
+
clearCachedConfig(name);
|
|
23
|
+
cleanCachedDatasource(name);
|
|
24
|
+
await datasource.close();
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return await datasourcePromise;
|
|
28
|
+
}
|
|
29
|
+
function cleanCachedDatasource(name) {
|
|
30
|
+
delete datasources[name];
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useDatasource };
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=useDatasource.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDatasource.mjs","names":[],"sources":["../../src/runtime/useDatasource.ts"],"sourcesContent":["import type { Datasources } from \".\";\nimport { createDatasource } from \"./internal/createDatasource\";\nimport { useNitroApp } from \"nitropack/runtime\";\nimport { clearCachedConfig } from \"./internal/config\";\n\nconst datasources: {\n [K in keyof Datasources & string]?: Promise<Datasources[K]>;\n} = {};\n\n/** Options for datasource creation and lifecycle management. */\nexport type UseDatasourceOptions = Partial<{\n /** Whether to automatically close the datasource when Nitro app closes. */\n autoClose: boolean;\n}>;\n\n/**\n * Gets or creates a datasource instance by name.\n * Caches the datasource for reuse.\n * @template TName - The datasource name\n * @param name - The datasource name\n * @param options - Lifecycle options\n * @returns The datasource instance\n */\nexport async function useDatasource<TName extends keyof Datasources & string>(\n name: TName,\n options: UseDatasourceOptions = {},\n): Promise<Datasources[TName]> {\n let datasourcePromise: Promise<Datasources[TName]>;\n\n if (name in datasources) {\n datasourcePromise = datasources[name]!;\n } else {\n datasourcePromise = datasources[name] = createDatasource(name) as Datasources[TName];\n const { autoClose = true } = options;\n if (autoClose) {\n const nitro = useNitroApp();\n nitro.hooks.hookOnce(\"close\", async () => {\n const datasource = await datasourcePromise;\n\n clearCachedConfig(name);\n cleanCachedDatasource(name);\n\n await datasource.close();\n });\n }\n }\n\n return await datasourcePromise;\n}\n\nfunction cleanCachedDatasource<TName extends keyof Datasources & string>(name: TName) {\n delete datasources[name];\n}\n"],"mappings":";;;;AAKA,MAAM,cAEF,CAAC;;;;;;;;;AAgBL,eAAsB,cACpB,MACA,UAAgC,CAAC,GACJ;CAC7B,IAAI;CAEJ,IAAI,QAAQ,aACV,oBAAoB,YAAY;MAC3B;EACL,oBAAoB,YAAY,QAAQ,iBAAiB,IAAI;EAC7D,MAAM,EAAE,YAAY,SAAS;EAC7B,IAAI,WAEF,YAAI,CAAC,CAAC,MAAM,SAAS,SAAS,YAAY;GACxC,MAAM,aAAa,MAAM;GAEzB,kBAAkB,IAAI;GACtB,sBAAsB,IAAI;GAE1B,MAAM,WAAW,MAAM;EACzB,CAAC;CAEL;CAEA,OAAO,MAAM;AACf;AAEA,SAAS,sBAAgE,MAAa;CACpF,OAAO,YAAY;AACrB"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { UseDatasourceOptions, useDatasource } from "./runtime/useDatasource.mjs";
|
|
2
|
+
import { Datasource, DatasourceDriver, Schema } from "nitro-drizzle/drivers";
|
|
3
|
+
|
|
4
|
+
//#region src/runtime/index.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Provider interface for creating Drizzle datasource instances.
|
|
7
|
+
* @template TConfig - The configuration type for the datasource
|
|
8
|
+
* @template TDatasource - The datasource type that extends Datasource
|
|
9
|
+
*/
|
|
10
|
+
interface DatasourceProvider<TConfig, TDatasource extends Datasource<any, any>> {
|
|
11
|
+
create(config: TConfig): Promise<TDatasource> | TDatasource;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Extracts the DatasourceProvider type from a DatasourceDriver factory function.
|
|
15
|
+
* @template TSchema - The schema type
|
|
16
|
+
* @template TFactory - The driver factory type
|
|
17
|
+
*/
|
|
18
|
+
type ToDatasourceProvider<TSchema extends Schema, TFactory extends DatasourceDriver<any>> = TFactory extends ((config: infer TConfig, schema: TSchema) => infer TDatasource extends Datasource<any, any>) ? DatasourceProvider<TConfig, TDatasource> : never;
|
|
19
|
+
/**
|
|
20
|
+
* Extracts the Datasource type from a DatasourceProvider.
|
|
21
|
+
* @template T - The DatasourceProvider type
|
|
22
|
+
*/
|
|
23
|
+
type ToDatasource<T extends DatasourceProvider<any, any>> = T extends DatasourceProvider<any, infer TDatasource> ? TDatasource : never;
|
|
24
|
+
/**
|
|
25
|
+
* Factory function type for creating database instances.
|
|
26
|
+
* @template TConfig - The configuration type
|
|
27
|
+
* @template TDatabase - The database type
|
|
28
|
+
*/
|
|
29
|
+
interface DatabaseFactory<TConfig = any, TDatabase = any> {
|
|
30
|
+
(config: TConfig): Promise<TDatabase> | TDatabase;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Registry interface for datasource providers.
|
|
34
|
+
* This interface is augmented by the module to include all configured datasources.
|
|
35
|
+
*/
|
|
36
|
+
interface DatasourceRegistry {}
|
|
37
|
+
/**
|
|
38
|
+
* Mapped type of all datasources from the registry.
|
|
39
|
+
* Provides access to datasource instances by name.
|
|
40
|
+
*/
|
|
41
|
+
type Datasources = { readonly [K in keyof DatasourceRegistry]: ToDatasource<DatasourceRegistry[K]> };
|
|
42
|
+
/**
|
|
43
|
+
* Recursively extracts primitive properties from a type.
|
|
44
|
+
* @template T - The type to extract primitive properties from
|
|
45
|
+
*/
|
|
46
|
+
type PrimitiveProps<T> = { [K in keyof T]: T[K] extends Record<string, any> ? PrimitiveProps<T[K]> : T[K] extends ((...args: any[]) => infer R) ? Awaited<R> extends Primitive ? Awaited<R> : never : T[K] extends Primitive ? T[K] : never };
|
|
47
|
+
/**
|
|
48
|
+
* Primitive types supported by the runtime configuration.
|
|
49
|
+
*/
|
|
50
|
+
type Primitive = string | number | boolean | null | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Extracts the configuration type from a DatasourceProvider.
|
|
53
|
+
* @template T - The DatasourceProvider type
|
|
54
|
+
*/
|
|
55
|
+
type DatasourceProviderConfig<T extends DatasourceProvider<any, any>> = T extends DatasourceProvider<infer TConfig, any> ? TConfig : never;
|
|
56
|
+
/**
|
|
57
|
+
* Configuration type mapping datasource names to their configurations.
|
|
58
|
+
*/
|
|
59
|
+
type Config = { [K in keyof DatasourceRegistry]: DatasourceProviderConfig<DatasourceRegistry[K]> };
|
|
60
|
+
/**
|
|
61
|
+
* Runtime configuration type with primitive values for each datasource.
|
|
62
|
+
* Used for Nitro runtime config.
|
|
63
|
+
*/
|
|
64
|
+
type RuntimeConfig = { [K in keyof Config]: PrimitiveProps<Config[K]> };
|
|
65
|
+
declare module "nitropack/types" {
|
|
66
|
+
interface NitroRuntimeConfig {
|
|
67
|
+
drizzle?: RuntimeConfig;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
type ConfigHookArgs = { [K in keyof Config]: [name: K, config: Config[K]] }[keyof Config];
|
|
71
|
+
interface ConfigHooks {
|
|
72
|
+
"drizzle:config": (...args: ConfigHookArgs) => void | Promise<void>;
|
|
73
|
+
}
|
|
74
|
+
declare module "nitropack/types" {
|
|
75
|
+
interface NitroRuntimeHooks extends ConfigHooks {}
|
|
76
|
+
}
|
|
77
|
+
//#endregion
|
|
78
|
+
export { Config, DatabaseFactory, DatasourceProvider, DatasourceProviderConfig, DatasourceRegistry, Datasources, Primitive, PrimitiveProps, RuntimeConfig, ToDatasource, ToDatasourceProvider, UseDatasourceOptions, useDatasource };
|
|
79
|
+
//# sourceMappingURL=runtime.d.mts.map
|
package/dist/runtime.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { VirtualModule } from "nitropack/types";
|
|
2
|
+
|
|
3
|
+
//#region src/shared.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A type that can be either a value or a promise resolving to that value.
|
|
6
|
+
* @template T - The underlying type
|
|
7
|
+
*/
|
|
8
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
9
|
+
type VirtualModules<TName extends string = string> = Record<TName, VirtualModule>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { MaybePromise, VirtualModules };
|
|
12
|
+
//# sourceMappingURL=shared.d.mts.map
|
package/dist/shared.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Table } from "drizzle-orm";
|
|
2
|
+
import { InferPrimaryColumns } from "nitro-drizzle/dialects";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/usePrimaryKey.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Extracts primary key columns from a Drizzle table.
|
|
7
|
+
* @template T - The table type
|
|
8
|
+
* @param table - The Drizzle table
|
|
9
|
+
* @returns Object containing only primary key columns
|
|
10
|
+
*/
|
|
11
|
+
declare function usePrimaryColumns<T extends Table>(table: T): InferPrimaryColumns<T>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { usePrimaryColumns };
|
|
14
|
+
//# sourceMappingURL=usePrimaryKey.d.mts.map
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Table } from "drizzle-orm";
|
|
2
|
+
//#region src/utils/usePrimaryKey.ts
|
|
3
|
+
const ColumnsSymbol = Table.Symbol.Columns;
|
|
4
|
+
/**
|
|
5
|
+
* Extracts primary key columns from a Drizzle table.
|
|
6
|
+
* @template T - The table type
|
|
7
|
+
* @param table - The Drizzle table
|
|
8
|
+
* @returns Object containing only primary key columns
|
|
9
|
+
*/
|
|
10
|
+
function usePrimaryColumns(table) {
|
|
11
|
+
const columns = table[ColumnsSymbol];
|
|
12
|
+
return Object.fromEntries(Object.entries(columns).filter(([_, column]) => {
|
|
13
|
+
return column.primary;
|
|
14
|
+
}));
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { usePrimaryColumns };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=usePrimaryKey.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePrimaryKey.mjs","names":[],"sources":["../../src/utils/usePrimaryKey.ts"],"sourcesContent":["import { Table } from \"drizzle-orm\";\nimport type { InferPrimaryColumns } from \"nitro-drizzle/dialects\";\n\n// @ts-expect-error - internal Drizzle API\nconst ColumnsSymbol = Table.Symbol.Columns as symbol;\n\n/**\n * Extracts primary key columns from a Drizzle table.\n * @template T - The table type\n * @param table - The Drizzle table\n * @returns Object containing only primary key columns\n */\nexport function usePrimaryColumns<T extends Table>(table: T): InferPrimaryColumns<T> {\n // @ts-expect-error Internal Drizzle API\n const columns = table[ColumnsSymbol] as typeof table._.columns;\n return Object.fromEntries(\n Object.entries(columns).filter(([_, column]) => {\n return column.primary;\n }),\n ) as InferPrimaryColumns<T>;\n}\n"],"mappings":";;AAIA,MAAM,gBAAgB,MAAM,OAAO;;;;;;;AAQnC,SAAgB,kBAAmC,OAAkC;CAEnF,MAAM,UAAU,MAAM;CACtB,OAAO,OAAO,YACZ,OAAO,QAAQ,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,YAAY;EAC9C,OAAO,OAAO;CAChB,CAAC,CACH;AACF"}
|
package/dist/utils.d.mts
ADDED
package/dist/utils.mjs
ADDED