deesse 0.2.13 → 0.3.0
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 +17 -34
- package/dist/index.d.ts +23 -32
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -126
- package/dist/index.types.d.ts +15 -0
- package/dist/index.types.d.ts.map +1 -0
- package/dist/index.types.js +1 -0
- package/dist/next.d.ts +3 -0
- package/dist/next.d.ts.map +1 -0
- package/dist/next.js +7 -0
- package/dist/paths.d.ts +3 -0
- package/dist/paths.d.ts.map +1 -0
- package/dist/paths.js +2 -0
- package/dist/plugin-api.d.ts +3 -0
- package/dist/plugin-api.d.ts.map +1 -0
- package/dist/plugin-api.js +5 -0
- package/dist/react-types.d.ts +30 -0
- package/dist/react-types.d.ts.map +1 -0
- package/dist/react-types.js +1 -0
- package/dist/react.d.ts +9 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +15 -0
- package/dist/runtime-types.d.ts +20 -0
- package/dist/runtime-types.d.ts.map +1 -0
- package/dist/runtime-types.js +1 -0
- package/dist/runtime.d.ts +11 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +169 -0
- package/dist/types.d.ts +298 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/package.json +52 -39
- package/.turbo/turbo-build.log +0 -4
- package/.turbo/turbo-lint.log +0 -19
- package/.turbo/turbo-type-check.log +0 -4
- package/dist/cache.d.ts +0 -16
- package/dist/cache.d.ts.map +0 -1
- package/dist/cache.js +0 -59
- package/dist/cache.js.map +0 -1
- package/dist/client.d.ts +0 -816
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -23
- package/dist/client.js.map +0 -1
- package/dist/config/define.d.ts +0 -61
- package/dist/config/define.d.ts.map +0 -1
- package/dist/config/define.js +0 -66
- package/dist/config/define.js.map +0 -1
- package/dist/config/index.d.ts +0 -7
- package/dist/config/index.d.ts.map +0 -1
- package/dist/config/index.js +0 -4
- package/dist/config/index.js.map +0 -1
- package/dist/config/page.d.ts +0 -30
- package/dist/config/page.d.ts.map +0 -1
- package/dist/config/page.js +0 -22
- package/dist/config/page.js.map +0 -1
- package/dist/config/plugin.d.ts +0 -10
- package/dist/config/plugin.d.ts.map +0 -1
- package/dist/config/plugin.js +0 -10
- package/dist/config/plugin.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/admin.d.ts +0 -33
- package/dist/lib/admin.d.ts.map +0 -1
- package/dist/lib/admin.js +0 -69
- package/dist/lib/admin.js.map +0 -1
- package/dist/lib/validation.d.ts +0 -32
- package/dist/lib/validation.d.ts.map +0 -1
- package/dist/lib/validation.js +0 -80
- package/dist/lib/validation.js.map +0 -1
- package/dist/server.d.ts +0 -9
- package/dist/server.d.ts.map +0 -1
- package/dist/server.js +0 -19
- package/dist/server.js.map +0 -1
- package/src/cache.ts +0 -81
- package/src/client.ts +0 -30
- package/src/config/define.ts +0 -135
- package/src/config/index.ts +0 -6
- package/src/config/page.ts +0 -54
- package/src/config/plugin.ts +0 -21
- package/src/index.ts +0 -166
- package/src/server.ts +0 -28
- package/tsconfig.json +0 -12
package/src/cache.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
// Generic cache utility with immutable state
|
|
2
|
-
|
|
3
|
-
export type CacheState<T> = {
|
|
4
|
-
instances: Map<string, T>;
|
|
5
|
-
promises: Map<string, Promise<T>>;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export const emptyCache = <T>(): CacheState<T> => ({
|
|
9
|
-
instances: new Map(),
|
|
10
|
-
promises: new Map(),
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
export const setCacheInstance = <T>(state: CacheState<T>, key: string, instance: T): CacheState<T> => ({
|
|
14
|
-
instances: new Map(state.instances).set(key, instance),
|
|
15
|
-
promises: (() => {
|
|
16
|
-
const m = new Map(state.promises);
|
|
17
|
-
m.delete(key);
|
|
18
|
-
return m;
|
|
19
|
-
})(),
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
export const setCachePromise = <T>(state: CacheState<T>, key: string, promise: Promise<T>): CacheState<T> => ({
|
|
23
|
-
instances: state.instances,
|
|
24
|
-
promises: new Map(state.promises).set(key, promise),
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
export const getCacheEntry = <T>(state: CacheState<T>, key: string): T | Promise<T> | undefined => {
|
|
28
|
-
const instance = state.instances.get(key);
|
|
29
|
-
if (instance !== undefined) return instance;
|
|
30
|
-
return state.promises.get(key);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export const removeCachePromise = <T>(state: CacheState<T>, key: string): CacheState<T> => ({
|
|
34
|
-
instances: state.instances,
|
|
35
|
-
promises: (() => {
|
|
36
|
-
const m = new Map(state.promises);
|
|
37
|
-
m.delete(key);
|
|
38
|
-
return m;
|
|
39
|
-
})(),
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
export type CreateCache = <T, Options>(
|
|
43
|
-
createInstance: (options: Options) => Promise<T>
|
|
44
|
-
) => {
|
|
45
|
-
get(key: string, options: Options): Promise<T>;
|
|
46
|
-
getState(): Readonly<CacheState<T>>;
|
|
47
|
-
clear(): void;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const createCache: CreateCache = <T, Options>(
|
|
51
|
-
createInstance: (options: Options) => Promise<T>
|
|
52
|
-
) => {
|
|
53
|
-
let state: CacheState<T> = emptyCache<T>();
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
async get(key: string, options: Options): Promise<T> {
|
|
57
|
-
const cached = getCacheEntry(state, key);
|
|
58
|
-
if (cached !== undefined) return cached as T;
|
|
59
|
-
|
|
60
|
-
const promise = createInstance(options);
|
|
61
|
-
state = setCachePromise(state, key, promise);
|
|
62
|
-
|
|
63
|
-
try {
|
|
64
|
-
const instance = await promise;
|
|
65
|
-
state = setCacheInstance(state, key, instance);
|
|
66
|
-
return instance;
|
|
67
|
-
} catch (err) {
|
|
68
|
-
state = removeCachePromise(state, key);
|
|
69
|
-
throw err;
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
getState(): Readonly<CacheState<T>> {
|
|
74
|
-
return state;
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
clear(): void {
|
|
78
|
-
state = emptyCache<T>();
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
};
|
package/src/client.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { createAuthClient } from "better-auth/react";
|
|
2
|
-
import type { BetterAuthClientOptions } from "better-auth/client";
|
|
3
|
-
|
|
4
|
-
export interface DeesseClientOptions {
|
|
5
|
-
auth: BetterAuthClientOptions;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Creates a type-safe authentication client.
|
|
10
|
-
* Wraps better-auth's createAuthClient with a simpler API.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* import { createClient } from "deesse";
|
|
15
|
-
*
|
|
16
|
-
* export const client = createClient({
|
|
17
|
-
* auth: {
|
|
18
|
-
* baseURL: "/api/auth",
|
|
19
|
-
* },
|
|
20
|
-
* });
|
|
21
|
-
*
|
|
22
|
-
* // In a component:
|
|
23
|
-
* const { data, isPending } = client.auth.useSession();
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export function createClient(
|
|
27
|
-
options: DeesseClientOptions,
|
|
28
|
-
) {
|
|
29
|
-
return { auth: createAuthClient(options.auth) };
|
|
30
|
-
}
|
package/src/config/define.ts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
2
|
-
import type { BetterAuthPlugin } from 'better-auth';
|
|
3
|
-
import type { Plugin } from './plugin';
|
|
4
|
-
import type { PageTree } from './page';
|
|
5
|
-
import type { AdminHeaderConfig } from '@deessejs/admin';
|
|
6
|
-
import { admin } from 'better-auth/plugins';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* User schema type - each application defines their own schema.
|
|
10
|
-
* Use generics to preserve Drizzle type safety.
|
|
11
|
-
*/
|
|
12
|
-
export type Config<TSchema extends Record<string, unknown> = Record<string, never>> = {
|
|
13
|
-
name?: string;
|
|
14
|
-
database: PostgresJsDatabase<TSchema>;
|
|
15
|
-
plugins?: Plugin[];
|
|
16
|
-
pages?: PageTree[];
|
|
17
|
-
secret: string;
|
|
18
|
-
auth: AuthConfig;
|
|
19
|
-
admin?: {
|
|
20
|
-
header?: AdminHeaderConfig;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Auth configuration - all fields optional for partial override
|
|
26
|
-
*/
|
|
27
|
-
export type AuthConfig = {
|
|
28
|
-
baseURL: string;
|
|
29
|
-
plugins?: BetterAuthPlugin[];
|
|
30
|
-
emailAndPassword?: {
|
|
31
|
-
enabled?: boolean;
|
|
32
|
-
allowSignUp?: boolean;
|
|
33
|
-
requireEmailVerification?: boolean;
|
|
34
|
-
};
|
|
35
|
-
session?: {
|
|
36
|
-
maxAge?: number;
|
|
37
|
-
updateAge?: number;
|
|
38
|
-
};
|
|
39
|
-
trustedOrigins?: string[];
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Internal config type used at runtime - includes admin plugin
|
|
44
|
-
* Preserves the schema generic for type safety.
|
|
45
|
-
*/
|
|
46
|
-
export type InternalConfig<TSchema extends Record<string, unknown> = Record<string, never>> = Config<TSchema> & {
|
|
47
|
-
auth: {
|
|
48
|
-
baseURL: string;
|
|
49
|
-
plugins: BetterAuthPlugin[];
|
|
50
|
-
emailAndPassword: { enabled: boolean };
|
|
51
|
-
session: { maxAge: number };
|
|
52
|
-
trustedOrigins: string[];
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Deep merge two objects, with source overriding target for nested objects.
|
|
58
|
-
* Arrays are concatenated, primitive overrides replace values.
|
|
59
|
-
*/
|
|
60
|
-
function deepMerge<T extends Record<string, unknown>>(target: T, source: Partial<T>): T {
|
|
61
|
-
const result = { ...target };
|
|
62
|
-
for (const key of Object.keys(source) as (keyof T)[]) {
|
|
63
|
-
const targetValue = target[key];
|
|
64
|
-
const sourceValue = source[key];
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
typeof targetValue === 'object' &&
|
|
68
|
-
targetValue !== null &&
|
|
69
|
-
!Array.isArray(targetValue) &&
|
|
70
|
-
typeof sourceValue === 'object' &&
|
|
71
|
-
sourceValue !== null &&
|
|
72
|
-
!Array.isArray(sourceValue)
|
|
73
|
-
) {
|
|
74
|
-
result[key] = deepMerge(
|
|
75
|
-
targetValue as Record<string, unknown>,
|
|
76
|
-
sourceValue as Record<string, unknown>
|
|
77
|
-
) as T[typeof key];
|
|
78
|
-
} else if (sourceValue !== undefined) {
|
|
79
|
-
result[key] = sourceValue as T[typeof key];
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return result;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Global config storage for getDeesse() without arguments.
|
|
87
|
-
* Stored at module level to persist across HMR.
|
|
88
|
-
* Uses 'any' for the schema since it's a runtime singleton.
|
|
89
|
-
*/
|
|
90
|
-
let globalConfig: InternalConfig<any> | undefined;
|
|
91
|
-
|
|
92
|
-
export function defineConfig<TSchema extends Record<string, unknown>>(
|
|
93
|
-
config: Config<TSchema>
|
|
94
|
-
): InternalConfig<TSchema> {
|
|
95
|
-
// Default auth configuration - admin() is always first
|
|
96
|
-
const defaultAuth = {
|
|
97
|
-
plugins: [admin()] as BetterAuthPlugin[],
|
|
98
|
-
emailAndPassword: { enabled: true },
|
|
99
|
-
session: { maxAge: 60 * 60 * 24 * 7 }, // 7 days
|
|
100
|
-
trustedOrigins: [config.auth.baseURL],
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// Merge user config with defaults
|
|
104
|
-
const mergedAuth = {
|
|
105
|
-
baseURL: config.auth.baseURL,
|
|
106
|
-
secret: config.secret,
|
|
107
|
-
plugins: [...defaultAuth.plugins, ...(config.auth.plugins || [])],
|
|
108
|
-
emailAndPassword: deepMerge(defaultAuth.emailAndPassword, config.auth.emailAndPassword || {}),
|
|
109
|
-
session: deepMerge(defaultAuth.session, config.auth.session || {}),
|
|
110
|
-
trustedOrigins: config.auth.trustedOrigins || defaultAuth.trustedOrigins,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const internalConfig: InternalConfig<TSchema> = {
|
|
114
|
-
...config,
|
|
115
|
-
auth: mergedAuth,
|
|
116
|
-
} as InternalConfig<TSchema>;
|
|
117
|
-
|
|
118
|
-
// Store globally for getDeesse() without arguments
|
|
119
|
-
globalConfig = internalConfig;
|
|
120
|
-
|
|
121
|
-
return internalConfig;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Get the global config stored by defineConfig().
|
|
126
|
-
* Throws if defineConfig() has not been called.
|
|
127
|
-
*/
|
|
128
|
-
export function getGlobalConfig<TSchema extends Record<string, unknown> = Record<string, never>>(): InternalConfig<TSchema> {
|
|
129
|
-
if (!globalConfig) {
|
|
130
|
-
throw new Error(
|
|
131
|
-
"Deesse config not defined. Call defineConfig() before accessing getDeesse() without arguments."
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
return globalConfig as InternalConfig<TSchema>;
|
|
135
|
-
}
|
package/src/config/index.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { defineConfig } from "./define.js";
|
|
2
|
-
export type { Config, InternalConfig } from "./define.js";
|
|
3
|
-
export { plugin } from "./plugin.js";
|
|
4
|
-
export type { Plugin } from "./plugin.js";
|
|
5
|
-
export { page, section } from "./page.js";
|
|
6
|
-
export type { Page, Section, PageTree } from "./page.js";
|
package/src/config/page.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { ReactNode } from "react";
|
|
2
|
-
import type { LucideIcon } from "lucide-react";
|
|
3
|
-
|
|
4
|
-
export type Page = {
|
|
5
|
-
type: "page";
|
|
6
|
-
name: string;
|
|
7
|
-
slug: string;
|
|
8
|
-
icon?: LucideIcon;
|
|
9
|
-
content: ReactNode | null;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type Section = {
|
|
13
|
-
type: "section";
|
|
14
|
-
name: string;
|
|
15
|
-
slug: string;
|
|
16
|
-
bottom?: boolean;
|
|
17
|
-
children: PageTree[];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type PageTree = Page | Section;
|
|
21
|
-
|
|
22
|
-
function toSlug(name: string): string {
|
|
23
|
-
return name.toLowerCase().replace(/\s+/g, "-");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function page(config: {
|
|
27
|
-
name: string;
|
|
28
|
-
slug?: string;
|
|
29
|
-
icon?: LucideIcon;
|
|
30
|
-
content: ReactNode | null;
|
|
31
|
-
}): Page {
|
|
32
|
-
return {
|
|
33
|
-
type: "page",
|
|
34
|
-
name: config.name,
|
|
35
|
-
slug: config.slug ?? toSlug(config.name),
|
|
36
|
-
icon: config.icon,
|
|
37
|
-
content: config.content,
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function section(config: {
|
|
42
|
-
name: string;
|
|
43
|
-
slug?: string;
|
|
44
|
-
bottom?: boolean;
|
|
45
|
-
children: PageTree[];
|
|
46
|
-
}): Section {
|
|
47
|
-
return {
|
|
48
|
-
type: "section",
|
|
49
|
-
name: config.name,
|
|
50
|
-
slug: config.slug ?? toSlug(config.name),
|
|
51
|
-
bottom: config.bottom,
|
|
52
|
-
children: config.children,
|
|
53
|
-
};
|
|
54
|
-
}
|
package/src/config/plugin.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { type ZodSchema } from "zod";
|
|
2
|
-
|
|
3
|
-
export type Plugin = {
|
|
4
|
-
name: string;
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
export type PluginConfig<TParams = never> = {
|
|
8
|
-
name: string;
|
|
9
|
-
schema?: ZodSchema<TParams>;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export function plugin<TParams = never>(
|
|
13
|
-
config: PluginConfig<TParams>
|
|
14
|
-
): (params: TParams) => Plugin {
|
|
15
|
-
return (params) => {
|
|
16
|
-
if (config.schema) {
|
|
17
|
-
config.schema.parse(params);
|
|
18
|
-
}
|
|
19
|
-
return { name: config.name };
|
|
20
|
-
};
|
|
21
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
// @deessejs/deesse core package
|
|
2
|
-
|
|
3
|
-
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
4
|
-
import { createDeesse, type Deesse } from "./server.js";
|
|
5
|
-
import { getGlobalConfig, type InternalConfig } from "./config/define.js";
|
|
6
|
-
|
|
7
|
-
export { defineConfig } from "./config/index.js";
|
|
8
|
-
export type { Config, InternalConfig } from "./config/index.js";
|
|
9
|
-
export { plugin } from "./config/index.js";
|
|
10
|
-
export type { Plugin } from "./config/index.js";
|
|
11
|
-
export { page, section } from "./config/index.js";
|
|
12
|
-
export type { Page, Section, PageTree } from "./config/index.js";
|
|
13
|
-
|
|
14
|
-
export { z } from "zod";
|
|
15
|
-
export type { ZodSchema } from "zod";
|
|
16
|
-
|
|
17
|
-
export { sql } from "drizzle-orm";
|
|
18
|
-
|
|
19
|
-
export type { Deesse } from "./server.js";
|
|
20
|
-
|
|
21
|
-
export { createClient } from "./client.js";
|
|
22
|
-
export type { DeesseClientOptions } from "./client.js";
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Symbol-based global storage for Deesse instance.
|
|
26
|
-
* Using Symbol.for ensures uniqueness across the process.
|
|
27
|
-
*/
|
|
28
|
-
const DEESSE_GLOBAL_KEY = Symbol.for("@deessejs/core.instance");
|
|
29
|
-
|
|
30
|
-
interface GlobalDeesseCache {
|
|
31
|
-
instance: Deesse | undefined;
|
|
32
|
-
config: InternalConfig | undefined;
|
|
33
|
-
pool: unknown | undefined;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function getGlobalCache(): GlobalDeesseCache {
|
|
37
|
-
const g = global as typeof global & {
|
|
38
|
-
[DEESSE_GLOBAL_KEY]?: GlobalDeesseCache;
|
|
39
|
-
};
|
|
40
|
-
if (!g[DEESSE_GLOBAL_KEY]) {
|
|
41
|
-
g[DEESSE_GLOBAL_KEY] = {
|
|
42
|
-
instance: undefined,
|
|
43
|
-
config: undefined,
|
|
44
|
-
pool: undefined,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
return g[DEESSE_GLOBAL_KEY]!;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Deep equality check for config comparison.
|
|
52
|
-
* Required because config objects are recreated on HMR.
|
|
53
|
-
* Note: We do NOT compare database pools - the pool reference from $client
|
|
54
|
-
* may return new wrapper objects on each access, causing false positives.
|
|
55
|
-
*/
|
|
56
|
-
function isConfigEqual(a: InternalConfig, b: InternalConfig): boolean {
|
|
57
|
-
if (a.secret !== b.secret) return false;
|
|
58
|
-
if (a.name !== b.name) return false;
|
|
59
|
-
if (a.auth.baseURL !== b.auth.baseURL) return false;
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Extract pool reference from database.
|
|
65
|
-
* For pg Pool passed to drizzle-orm/node-postgres, the pool is stored in $client.
|
|
66
|
-
*/
|
|
67
|
-
function extractPool(db: PostgresJsDatabase): unknown {
|
|
68
|
-
return (db as unknown as { $client?: unknown }).$client;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Get the Deesse singleton instance.
|
|
73
|
-
* Cached on global to persist across HMR.
|
|
74
|
-
*
|
|
75
|
-
* Can be called without arguments if defineConfig() was called first,
|
|
76
|
-
* or with a config for explicit instantiation.
|
|
77
|
-
*/
|
|
78
|
-
export const getDeesse = async (
|
|
79
|
-
config?: InternalConfig
|
|
80
|
-
): Promise<Deesse> => {
|
|
81
|
-
const effectiveConfig = config ?? getGlobalConfig();
|
|
82
|
-
const cache = getGlobalCache();
|
|
83
|
-
|
|
84
|
-
// Case 1: Instance exists and config is semantically equal
|
|
85
|
-
if (cache.instance && cache.config && isConfigEqual(cache.config, effectiveConfig)) {
|
|
86
|
-
return cache.instance;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Case 2: Instance exists but config changed - hot reload
|
|
90
|
-
if (
|
|
91
|
-
cache.instance &&
|
|
92
|
-
cache.config &&
|
|
93
|
-
!isConfigEqual(cache.config, effectiveConfig)
|
|
94
|
-
) {
|
|
95
|
-
console.info("[deesse] Config changed, performing hot reload...");
|
|
96
|
-
|
|
97
|
-
// Close the old pool before creating new instance (with 5s timeout)
|
|
98
|
-
if (cache.pool) {
|
|
99
|
-
const oldPool = cache.pool as { end?: () => Promise<void> };
|
|
100
|
-
if (typeof oldPool.end === "function") {
|
|
101
|
-
await Promise.race([
|
|
102
|
-
oldPool.end(),
|
|
103
|
-
new Promise((resolve) => setTimeout(() => resolve(undefined), 5000)),
|
|
104
|
-
]).catch(console.error);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
// Create new instance with new config
|
|
109
|
-
const instance = createDeesse(effectiveConfig);
|
|
110
|
-
cache.pool = extractPool(instance.database);
|
|
111
|
-
cache.instance = instance;
|
|
112
|
-
cache.config = effectiveConfig;
|
|
113
|
-
|
|
114
|
-
return instance;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Case 3: No instance exists - create one
|
|
118
|
-
const instance = createDeesse(effectiveConfig);
|
|
119
|
-
cache.pool = extractPool(instance.database);
|
|
120
|
-
cache.instance = instance;
|
|
121
|
-
cache.config = effectiveConfig;
|
|
122
|
-
|
|
123
|
-
return instance;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Clear the Deesse singleton cache.
|
|
128
|
-
* Primarily useful for testing.
|
|
129
|
-
*/
|
|
130
|
-
export const clearDeesseCache = (): void => {
|
|
131
|
-
const cache = getGlobalCache();
|
|
132
|
-
cache.instance = undefined;
|
|
133
|
-
cache.config = undefined;
|
|
134
|
-
cache.pool = undefined;
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Graceful shutdown - call this on process exit.
|
|
139
|
-
* Ensures all database connections are properly closed.
|
|
140
|
-
*/
|
|
141
|
-
export const shutdownDeesse = async (): Promise<void> => {
|
|
142
|
-
const cache = getGlobalCache();
|
|
143
|
-
|
|
144
|
-
if (cache.pool) {
|
|
145
|
-
console.info("[deesse] Closing database pool...");
|
|
146
|
-
const pool = cache.pool as { end?: () => Promise<void> };
|
|
147
|
-
if (pool && typeof pool.end === "function") {
|
|
148
|
-
await pool.end();
|
|
149
|
-
}
|
|
150
|
-
cache.pool = undefined;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
cache.instance = undefined;
|
|
154
|
-
cache.config = undefined;
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
// Register graceful shutdown hooks
|
|
158
|
-
if (process.env["NODE_ENV"] !== "test") {
|
|
159
|
-
const shutdown = () => {
|
|
160
|
-
shutdownDeesse()
|
|
161
|
-
.catch(console.error)
|
|
162
|
-
.finally(() => process.exit(0));
|
|
163
|
-
};
|
|
164
|
-
process.on("SIGINT", shutdown);
|
|
165
|
-
process.on("SIGTERM", shutdown);
|
|
166
|
-
}
|
package/src/server.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
2
|
-
import type { Auth } from "better-auth";
|
|
3
|
-
import type { InternalConfig } from "./config/define";
|
|
4
|
-
import { betterAuth } from "better-auth";
|
|
5
|
-
import { drizzleAdapter } from "@better-auth/drizzle-adapter";
|
|
6
|
-
|
|
7
|
-
export type Deesse = {
|
|
8
|
-
auth: Auth;
|
|
9
|
-
database: PostgresJsDatabase;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export function createDeesse(config: InternalConfig): Deesse {
|
|
13
|
-
const auth = betterAuth({
|
|
14
|
-
database: drizzleAdapter(config.database, {
|
|
15
|
-
provider: "pg",
|
|
16
|
-
}),
|
|
17
|
-
baseURL: config.auth.baseURL,
|
|
18
|
-
secret: config.secret,
|
|
19
|
-
emailAndPassword: config.auth.emailAndPassword,
|
|
20
|
-
trustedOrigins: config.auth.trustedOrigins,
|
|
21
|
-
plugins: config.auth.plugins,
|
|
22
|
-
}) as Auth;
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
auth,
|
|
26
|
-
database: config.database,
|
|
27
|
-
};
|
|
28
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../../tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"composite": false,
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"moduleResolution": "bundler",
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"rootDir": "./src"
|
|
9
|
-
},
|
|
10
|
-
"include": ["src/**/*"],
|
|
11
|
-
"exclude": ["node_modules", "dist"]
|
|
12
|
-
}
|