Skip to content

feat: createAdapter and createTable #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 19, 2025
Merged

Conversation

productdevbook
Copy link
Owner

@productdevbook productdevbook commented Apr 19, 2025

import type { PluginSchema } from 'unadapter/types'
import { createAdapter, createTable, mergePluginSchemas } from 'unadapter'
import { memoryAdapter } from 'unadapter/memory'

const db = {
  user: [],
  session: [],
}

interface CustomOptions {
  appName?: string
  plugins?: {
    schema?: PluginSchema
  }[]
  user?: {
    fields?: {
      name?: string
      email?: string
      emailVerified?: string
      image?: string
      createdAt?: string
    }
  }
}

const tables = createTable<CustomOptions>((options) => {
  const { user, account, ..._pluginTables } = mergePluginSchemas<CustomOptions>(options) || {}

  return {
    user: {
      modelName: 'user',
      fields: {
        name: {
          type: 'string',
          required: true,
          fieldName: options?.user?.fields?.name || 'name',
          sortable: true,
        },
        email: {
          type: 'string',
          unique: true,
          required: true,
          fieldName: options?.user?.fields?.email || 'email',
          sortable: true,
        },
        emailVerified: {
          type: 'boolean',
          defaultValue: () => false,
          required: true,
          fieldName: options?.user?.fields?.emailVerified || 'emailVerified',
        },
        image: {
          type: 'string',
          required: false,
          fieldName: options?.user?.fields?.image || 'image',
        },
        createdAt: {
          type: 'date',
          defaultValue: () => new Date(),
          required: true,
          fieldName: options?.user?.fields?.createdAt || 'createdAt',
        },
        ...user?.fields,
        ...options?.user?.fields,
      },
    },
  }
})
const adapter = createAdapter(tables, {
  database: memoryAdapter(
    db,
    {},
  ),
  plugins: [{
    schema: {
      user: {
        modelName: 'user',
        fields: {
          header: {
            type: 'string',
            required: true,
            fieldName: 'header',
          },
        },
      },
    },
  }],
})

// eslint-disable-next-line antfu/no-top-level-await
const user = await adapter.create({
  model: 'user',
  data: {
    name: 'John Doe',
    email: '[email protected]',
    emailVerified: true,
    createdAt: new Date(),
    updatedAt: new Date(),
  },
})
// eslint-disable-next-line antfu/no-top-level-await
const _users = await adapter.findMany({
  model: 'user',
  where: [
    {
      field: 'email',
      value: '[email protected]',
      operator: 'eq',
    },
  ],
})

console.log('Found users:', user, db)
// console.log('Found users:', user, db)
❯ tsx playground/memory.ts
Found users: {
  name: 'John Doe',
  email: '[email protected]',
  emailVerified: true,
  image: undefined,
  createdAt: 2025-04-19T07:29:02.109Z,
  header: undefined,
  id: 'fbe90b82-47d7-481f-8423-5f7c1f0bd7c6'
} {
  user: [
    {
      name: 'John Doe',
      email: '[email protected]',
      emailVerified: true,
      createdAt: 2025-04-19T07:29:02.109Z,
      id: 'fbe90b82-47d7-481f-8423-5f7c1f0bd7c6'
    }
  ],
  session: []
}

Copy link

github-actions bot commented Apr 19, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 73.69% 2124 / 2882
🔵 Statements 73.69% 2124 / 2882
🔵 Functions 83.73% 103 / 123
🔵 Branches 76.62% 518 / 676
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/adapters/create/index.ts 83.41% 84.06% 88.88% 83.41% 93, 95-96, 109-131, 133-137, 167-178, 186-189, 223-226, 239, 275-279, 288-289, 356-357, 361, 363-369, 440-441, 508-509, 566-579, 855-888, 890-898, 916-933
src/adapters/create/types.ts 100% 100% 100% 100%
src/adapters/drizzle/drizzle-adapter.ts 76.36% 73.52% 92.85% 76.36% 70-73, 76-79, 107-113, 123-130, 135-138, 155-156, 159-162, 165-168, 185-186, 189-190, 203-209, 215-216, 234-237, 240-243, 297-307
src/adapters/kysely/dialect.ts 64.1% 57.69% 100% 64.1% 10-11, 13-14, 17-18, 20-21, 23-24, 30-31, 40-41, 47-51, 61-65, 76-79
src/adapters/kysely/kysely-adapter.ts 79.6% 76.28% 92.85% 79.6% 109-111, 113-114, 116-117, 164-184, 188-189, 220-221, 241-242, 287-288, 303-304, 310-337, 350-351, 365-366
src/adapters/memory/memory-adapter.ts 94.55% 86.66% 91.66% 94.55% 37, 39-40, 52-53, 123-124, 163
src/adapters/mongodb/mongodb-adapter.ts 82.74% 70.1% 94.11% 82.74% 22-23, 31-32, 39-41, 43-46, 49-50, 63-64, 69-81, 88-89, 105-107, 140, 176, 181-182, 184-185, 187-188, 190-191, 193-194, 206, 225-226, 257-258, 305-313
src/adapters/prisma/prisma-adapter.ts 81.81% 73.58% 92.3% 81.81% 92-93, 110-112, 116-120, 136-139, 152-155, 171-174, 192-201, 208-211
src/db/get-migration.ts 68.84% 78.08% 60% 68.84% 67-80, 92-96, 99-103, 134-139, 141-143, 146-169, 227-228, 230-231, 233-234, 238-258, 307-309
src/db/get-schema.ts 83.78% 66.66% 100% 83.78% 32-37
src/db/schema.ts 0% 100% 100% 0% 7-207
src/utils/create.ts 88.88% 66.66% 100% 88.88% 13-14
src/utils/index.ts 100% 100% 100% 100%
src/utils/plugin.ts 4.54% 100% 0% 4.54% 6-30
Generated in workflow #53 for commit 9073952 by the Vitest Coverage Report Action

@productdevbook productdevbook self-assigned this Apr 19, 2025
@productdevbook productdevbook requested a review from Copilot April 19, 2025 07:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces the new functions createAdapter and createTable with updated type definitions throughout the codebase. Key changes include the renaming of UnDbSchema to TablesSchema, enhanced generic handling in adapter functions, and updates to documentation and playground examples to reflect these changes.

Reviewed Changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 2 comments.

File Description
src/utils/create.ts Added createAdapter and createTable functions with better generics.
src/types/* Updated types from UnDbSchema to TablesSchema.
src/adapters/* Refactored adapter implementations to support new type definitions.
playground/memory.ts & README.md Updated examples and documentation to align with new types.

@@ -66,7 +66,7 @@ export async function createKyselyAdapter(config: AnyOptions) {

let dialect: Dialect | undefined

const databaseType = getDatabaseType(db)
const databaseType = getDatabaseType(db as any)
Copy link
Preview

Copilot AI Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using 'as any' for the db parameter. Instead, consider refining the type of db to maintain type safety throughout the adapter.

Suggested change
const databaseType = getDatabaseType(db as any)
const databaseType = getDatabaseType(db)

Copilot uses AI. Check for mistakes.

Comment on lines +481 to 482
options: options as any,
})
Copy link
Preview

Copilot AI Apr 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid casting options to any. Strengthen the type constraint on options to ensure proper type safety in the adapter instance creation.

Suggested change
options: options as any,
})
options: options as AdapterOptions,

Copilot uses AI. Check for mistakes.

@productdevbook productdevbook merged commit 5000cc2 into main Apr 19, 2025
1 check passed
@productdevbook productdevbook deleted the feat/create-adapter-dx branch April 19, 2025 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant