Skip to content

Commit

Permalink
fix: extract correct schema w/out schemaName param
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Aug 1, 2023
1 parent d8786c8 commit c23cf09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
11 changes: 7 additions & 4 deletions lib/schema-to-drizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,18 @@ True if JSONSchema value is required
@typedef {import('@mapeo/schema').MapeoDoc} MapeoDoc
*/

/**
@typedef {{ [K in MapeoDoc['schemaName']]: Extract<MapeoDoc, { schemaName: K }> }} SchemaNameMap
*/

/**
@template {JSONSchema7WithProps} TSchema
@template {MapeoDoc['schemaName']} TSchemaName
@template {Extract<MapeoDoc, { schemaName: TSchemaName }>} TObjectType
@param {TSchemaName} schemaName
@template {TSchema extends { properties: { schemaName: { enum: readonly string[] }}} ? TSchema['properties']['schemaName']['enum'][0] extends MapeoDoc['schemaName'] ? TSchema['properties']['schemaName']['enum'][0] : never : never} TSchemaName
@template {Extract<MapeoDoc, TSchemaName>} TObjectType
@param {TSchema} schema
@returns {SchemaToDrizzleColumns<TSchema, TObjectType>}
*/
export function jsonSchemaToDrizzleColumns(schemaName, schema) {
export function jsonSchemaToDrizzleColumns(schema) {
if (schema.type !== 'object' || !schema.properties) {
throw new Error('Cannot process JSONSchema as SQL table')
}
Expand Down
11 changes: 2 additions & 9 deletions lib/schema.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { sqliteTable } from 'drizzle-orm/sqlite-core'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import Database from 'better-sqlite3'
import { dereferencedDocSchemas as schemas } from '@mapeo/schema'
import { jsonSchemaToDrizzleColumns as toColumns } from './schema-to-drizzle'
import { jsonSchemaToDrizzleColumns as toColumns } from './schema-to-drizzle.js'

export const observationTable = sqliteTable(
'observation',
toColumns('observation', schemas.observation)
toColumns(schemas.observation)
)

const sqlite = new Database('sqlite.db')
const db = drizzle(sqlite)

const o = db.select().from(observationTable).get()

0 comments on commit c23cf09

Please sign in to comment.