Skip to content

Commit 5000cc2

Browse files
feat: createAdapter and createTable (#6)
* refactor: restructure getTables function to be part of createAdapter options * refactor: simplify adapter creation and enhance type definitions * refactor: remove commented-out code for clarity * refactor: enhance adapter and table creation with improved type definitions * fix: type * refactor: enhance type definitions in createAdapter and AdapterOptions for better type safety * refactor: improve type definitions and simplify adapter creation for enhanced type safety * refactor: update drizzleAdapter calls to correctly pass getAuthTables as a separate argument * refactor: remove getAuthTables argument from kyselyAdapter calls for cleaner interface * refactor: simplify adapter function signatures by removing unnecessary model type parameters * refactor: enhance type safety in adapter interfaces and configurations * refactor: streamline adapter creation logic in createAdapter function * refactor: remove getAuthTables argument from mongodbAdapter instantiation for cleaner code * refactor: remove unused type imports from adapter files for cleaner code * refactor: cast options and db to any for improved type handling in adapter functions * refactor: replace UnDbSchema with TablesSchema for improved type consistency across adapters and utilities * chore: up * chore: update tsdown to version 0.9.0 and adjust tsdown.config for improved build settings * refactor: enhance adapter usage with custom schema and plugin support in README * docs: update README to enhance structure, add features, and improve clarity
1 parent 79c051b commit 5000cc2

38 files changed

+1573
-870
lines changed

README.md

Lines changed: 429 additions & 453 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@
108108
"bumpp": "^10.1.0",
109109
"deepmerge": "^4.3.1",
110110
"drizzle-orm": "^0.42.0",
111-
"eslint": "^9.24.0",
112-
"kysely": "^0.28.0",
111+
"eslint": "^9.25.0",
112+
"kysely": "^0.28.1",
113113
"mongodb": "^6.15.0",
114114
"mysql2": "^3.14.0",
115115
"pg": "^8.14.1",
116116
"prisma": "^6.6.0",
117117
"tarn": "^3.0.2",
118118
"tedious": "^18.6.1",
119+
"tsdown": "^0.9.0",
119120
"typescript": "^5.8.3",
120121
"unbuild": "^3.5.0",
121122
"vite-tsconfig-paths": "^5.1.4",

playground/memory.ts

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
import type { AdapterOptions, UnDbSchema } from 'unadapter/types'
1+
import type { PluginSchema } from 'unadapter/types'
2+
import { createAdapter, createTable, mergePluginSchemas } from 'unadapter'
23
import { memoryAdapter } from 'unadapter/memory'
34

45
const db = {
56
user: [],
67
session: [],
78
}
89

9-
export function getTables(options?: AdapterOptions) {
10+
interface CustomOptions {
11+
appName?: string
12+
plugins?: {
13+
schema?: PluginSchema
14+
}[]
15+
user?: {
16+
fields?: {
17+
name?: string
18+
email?: string
19+
emailVerified?: string
20+
image?: string
21+
createdAt?: string
22+
}
23+
}
24+
}
25+
26+
const tables = createTable<CustomOptions>((options) => {
27+
const { user, account, ..._pluginTables } = mergePluginSchemas<CustomOptions>(options) || {}
28+
1029
return {
1130
user: {
1231
modelName: 'user',
@@ -41,26 +60,32 @@ export function getTables(options?: AdapterOptions) {
4160
required: true,
4261
fieldName: options?.user?.fields?.createdAt || 'createdAt',
4362
},
44-
updatedAt: {
45-
type: 'date',
46-
defaultValue: () => new Date(),
47-
required: true,
48-
fieldName: options?.user?.fields?.updatedAt || 'updatedAt',
63+
...user?.fields,
64+
...options?.user?.fields,
65+
},
66+
},
67+
}
68+
})
69+
const adapter = createAdapter(tables, {
70+
database: memoryAdapter(
71+
db,
72+
{},
73+
),
74+
plugins: [{
75+
schema: {
76+
user: {
77+
modelName: 'user',
78+
fields: {
79+
header: {
80+
type: 'string',
81+
required: true,
82+
fieldName: 'header',
83+
},
4984
},
5085
},
5186
},
52-
} satisfies UnDbSchema
53-
}
54-
55-
// Initialize the adapter
56-
const createAdapter = memoryAdapter(
57-
db,
58-
getTables,
59-
{},
60-
)
61-
62-
// Create an adapter instance
63-
const adapter = createAdapter({})
87+
}],
88+
})
6489

6590
// eslint-disable-next-line antfu/no-top-level-await
6691
const user = await adapter.create({

0 commit comments

Comments
 (0)