asasvirtuais 2.2.0 → 2.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/package.json
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { z } from 'zod'
|
|
3
3
|
import { useMemo } from 'react'
|
|
4
|
-
import { createContextFromHook } from './hooks'
|
|
5
4
|
import type { TableInterface, TableSchema } from './interface'
|
|
5
|
+
import { InterfaceProvider } from './interface-provider'
|
|
6
6
|
|
|
7
|
-
export function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return useMemo(() => fetchInterface({ schema, baseUrl, headers }), [schema, baseUrl])
|
|
7
|
+
export function FetchInterfaceProvider<Schema extends TableSchema>({ schema, defaultTable, baseUrl, headers, children }: { schema: Schema, defaultTable?: string, baseUrl?: string, headers?: Record<string, string>, children: React.ReactNode }) {
|
|
8
|
+
const memo = useMemo(() => fetchInterface({ schema, defaultTable, baseUrl, headers }), [schema, defaultTable, baseUrl, headers])
|
|
9
|
+
return (
|
|
10
|
+
<InterfaceProvider interface={memo}>
|
|
11
|
+
{children}
|
|
12
|
+
</InterfaceProvider>
|
|
13
|
+
)
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
export const [FetchInterfaceProvider, useFetchInterface] = createContextFromHook(useFetchInterfaceProvider<any>)
|
|
18
|
-
|
|
19
16
|
export function fetchInterface<Schema extends TableSchema, Table extends string>({
|
|
20
17
|
schema, defaultTable, baseUrl = '/api/v1', headers = {}
|
|
21
18
|
}: {
|
|
@@ -2,19 +2,19 @@
|
|
|
2
2
|
import Dexie from 'dexie'
|
|
3
3
|
import { z } from 'zod'
|
|
4
4
|
import { useMemo } from 'react'
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
5
|
+
import type { DatabaseSchema, TableInterface, TableSchema } from './interface'
|
|
6
|
+
import { InterfaceProvider } from './interface-provider'
|
|
7
7
|
|
|
8
|
-
export function
|
|
9
|
-
dbName, schema,
|
|
10
|
-
}: {
|
|
11
|
-
dbName: string
|
|
12
|
-
schema: Schema
|
|
13
|
-
}) {
|
|
14
|
-
return useMemo(() => indexedInterface(dbName, schema), [dbName, schema])
|
|
15
|
-
}
|
|
8
|
+
export function IndexedInterfaceProvider<Schema extends DatabaseSchema>({ dbName, schema, children }: { dbName: string, schema: Schema, children: React.ReactNode }) {
|
|
16
9
|
|
|
17
|
-
|
|
10
|
+
const memo = useMemo(() => indexedInterface(dbName, schema), [dbName, schema])
|
|
11
|
+
|
|
12
|
+
return (
|
|
13
|
+
<InterfaceProvider interface={memo}>
|
|
14
|
+
{children}
|
|
15
|
+
</InterfaceProvider>
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Creates a TableInterface adapter for IndexedDB using Dexie.js.
|
|
@@ -31,19 +31,6 @@ export function indexedInterface<Schema extends DatabaseSchema>(
|
|
|
31
31
|
|
|
32
32
|
const db = new Dexie(dbName)
|
|
33
33
|
|
|
34
|
-
// Dynamically define the database schema for Dexie from the Zod schema.
|
|
35
|
-
// It marks 'id' as the primary key and indexes all other top-level readable fields.
|
|
36
|
-
const dexieSchema = Object.fromEntries(
|
|
37
|
-
Object.keys(schema).map(tableName => {
|
|
38
|
-
const fields = Object.keys(schema[tableName].readable.shape)
|
|
39
|
-
// 'id' is the primary key, the rest are indexed fields.
|
|
40
|
-
const indexedFields = fields.filter(f => f !== 'id').join(', ')
|
|
41
|
-
return [tableName, `id, ${indexedFields}`]
|
|
42
|
-
})
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
db.version(1).stores(dexieSchema)
|
|
46
|
-
|
|
47
34
|
type GenericReadable = z.infer<Schema[keyof Schema]['readable']>
|
|
48
35
|
type GenericWritable = z.infer<Schema[keyof Schema]['writable']>
|
|
49
36
|
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
import { useMemo } from 'react'
|
|
3
|
-
import { createContextFromHook } from './hooks'
|
|
4
3
|
import type { TableInterface, TableSchema } from './interface'
|
|
4
|
+
import { InterfaceProvider } from './interface-provider'
|
|
5
5
|
|
|
6
|
-
export function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
6
|
+
export function MemInterfaceProvider<TSchema extends TableSchema>({ children }: { children: React.ReactNode }) {
|
|
7
|
+
const memo = useMemo(() => memInterface<TSchema>(), [])
|
|
8
|
+
return (
|
|
9
|
+
<InterfaceProvider interface={memo}>
|
|
10
|
+
{children}
|
|
11
|
+
</InterfaceProvider>
|
|
12
|
+
)
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
export const [MemInterfaceProvider, useMemInterface] = createContextFromHook(useMemInterfaceProvider<any>)
|
|
15
|
-
|
|
16
15
|
export function memInterface<TSchema extends TableSchema>(): TableInterface<any, any> {
|
|
17
16
|
|
|
18
17
|
const store: Record<string, Record<string, any>> = {}
|