Skip to content

Commit 13ea2db

Browse files
committed
packages/modeldb: add PrimitiveProperty objects to relations for source and target primary keys
1 parent b1c27e5 commit 13ea2db

File tree

8 files changed

+26
-25
lines changed

8 files changed

+26
-25
lines changed

packages/modeldb-durable-objects/src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ export class RelationAPI {
539539
this.targetIndex = `${relation.source}/${relation.sourceProperty}/target`
540540

541541
const columns = [
542-
`_source ${primitiveColumnTypes[relation.sourceType]} NOT NULL`,
543-
`_target ${primitiveColumnTypes[relation.targetType]} NOT NULL`,
542+
`_source ${primitiveColumnTypes[relation.sourcePrimaryKey.type]} NOT NULL`,
543+
`_target ${primitiveColumnTypes[relation.targetPrimaryKey.type]} NOT NULL`,
544544
]
545545

546546
db.exec(`CREATE TABLE IF NOT EXISTS "${this.table}" (${columns.join(", ")})`)

packages/modeldb-pg/src/api.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,8 @@ export class RelationAPI {
682682
// Initialize tables
683683
const relationApi = new RelationAPI(client, relation)
684684
const columns = [
685-
`_source ${primitiveColumnTypes[relation.sourceType]} NOT NULL`,
686-
`_target ${primitiveColumnTypes[relation.targetType]} NOT NULL`,
685+
`_source ${primitiveColumnTypes[relation.sourcePrimaryKey.type]} NOT NULL`,
686+
`_target ${primitiveColumnTypes[relation.targetPrimaryKey.type]} NOT NULL`,
687687
]
688688

689689
const queries = []
@@ -713,13 +713,14 @@ export class RelationAPI {
713713
}
714714

715715
private decodeRelationTarget(target: PostgresPrimitiveValue): PrimaryKeyValue {
716-
if (this.relation.targetType === "integer") {
716+
const { type } = this.relation.targetPrimaryKey
717+
if (type === "integer") {
717718
assert(typeof target === "string", "internal error - expected integer primary key")
718719
return parseInt(target, 10)
719-
} else if (this.relation.targetType === "string") {
720+
} else if (type === "string") {
720721
assert(typeof target === "string", "internal error - expected string primary key")
721722
return target
722-
} else if (this.relation.targetType === "bytes") {
723+
} else if (type === "bytes") {
723724
if (Buffer.isBuffer(target)) {
724725
return fromBuffer(target)
725726
} else {

packages/modeldb-sqlite-expo/src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ export class RelationAPI {
553553
this.targetIndex = `${relation.source}/${relation.sourceProperty}/target`
554554

555555
const columns = [
556-
`_source ${primitiveColumnTypes[relation.sourceType]} NOT NULL`,
557-
`_target ${primitiveColumnTypes[relation.targetType]} NOT NULL`,
556+
`_source ${primitiveColumnTypes[relation.sourcePrimaryKey.type]} NOT NULL`,
557+
`_target ${primitiveColumnTypes[relation.targetPrimaryKey.type]} NOT NULL`,
558558
]
559559

560560
db.execSync(`CREATE TABLE IF NOT EXISTS "${this.table}" (${columns.join(", ")})`)

packages/modeldb-sqlite-wasm/src/ModelAPI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ export class RelationAPI {
566566
this.targetIndex = `${relation.source}/${relation.sourceProperty}/target`
567567

568568
const columns = [
569-
`_source ${primitiveColumnTypes[relation.sourceType]} NOT NULL`,
570-
`_target ${primitiveColumnTypes[relation.targetType]} NOT NULL`,
569+
`_source ${primitiveColumnTypes[relation.sourcePrimaryKey.type]} NOT NULL`,
570+
`_target ${primitiveColumnTypes[relation.targetPrimaryKey.type]} NOT NULL`,
571571
]
572572

573573
db.exec(`CREATE TABLE IF NOT EXISTS "${this.table}" (${columns.join(", ")})`)

packages/modeldb-sqlite/src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,8 @@ export class RelationAPI {
559559
this.targetIndex = `${relation.source}/${relation.sourceProperty}/target`
560560

561561
const columns = [
562-
`_source ${primitiveColumnTypes[relation.sourceType]} NOT NULL`,
563-
`_target ${primitiveColumnTypes[relation.targetType]} NOT NULL`,
562+
`_source ${primitiveColumnTypes[relation.sourcePrimaryKey.type]} NOT NULL`,
563+
`_target ${primitiveColumnTypes[relation.targetPrimaryKey.type]} NOT NULL`,
564564
]
565565

566566
db.exec(`CREATE TABLE IF NOT EXISTS "${this.table}" (${columns.join(", ")})`)

packages/modeldb/src/config.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Config, Model, ModelSchema, PrimitiveType, Property, PropertyType, Rela
44
import { namePattern } from "./utils.js"
55

66
export function parseConfig(init: ModelSchema): Config {
7-
const relations: Omit<Relation, "sourceType" | "targetType">[] = []
7+
const relations: Omit<Relation, "sourcePrimaryKey" | "targetPrimaryKey">[] = []
88
const models: Model[] = []
99

1010
for (const [modelName, { $indexes, $primary, ...rest }] of Object.entries(init)) {
@@ -71,20 +71,20 @@ export function parseConfig(init: ModelSchema): Config {
7171
throw new Error(`invalid model schema: invalid relation source "${relation.source}" (no such model)`)
7272
}
7373

74-
const sourcePrimaryProperty = sourceModel.properties.find((property) => property.name === sourceModel.primaryKey)
75-
assert(sourcePrimaryProperty !== undefined)
76-
assert(sourcePrimaryProperty.kind === "primitive")
74+
const sourcePrimaryKey = sourceModel.properties.find((property) => property.name === sourceModel.primaryKey)
75+
assert(sourcePrimaryKey !== undefined)
76+
assert(sourcePrimaryKey.kind === "primitive")
7777

7878
const targetModel = models.find((model) => model.name === relation.target)
7979
if (targetModel === undefined) {
8080
throw new Error(`invalid model schema: invalid relation target "${relation.target}" (no such model)`)
8181
}
8282

83-
const targetPrimaryProperty = targetModel.properties.find((property) => property.name === targetModel.primaryKey)
84-
assert(targetPrimaryProperty !== undefined)
85-
assert(targetPrimaryProperty.kind === "primitive")
83+
const targetPrimaryKey = targetModel.properties.find((property) => property.name === targetModel.primaryKey)
84+
assert(targetPrimaryKey !== undefined)
85+
assert(targetPrimaryKey.kind === "primitive")
8686

87-
return { ...relation, sourceType: sourcePrimaryProperty.type, targetType: targetPrimaryProperty.type }
87+
return { ...relation, sourcePrimaryKey, targetPrimaryKey }
8888
}),
8989
models,
9090
}

packages/modeldb/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export type Property = PrimitiveProperty | ReferenceProperty | RelationProperty
3737
export type Relation = {
3838
source: string
3939
sourceProperty: string
40-
sourceType: PrimitiveType
40+
sourcePrimaryKey: PrimitiveProperty
4141
target: string
42-
targetType: PrimitiveType
42+
targetPrimaryKey: PrimitiveProperty
4343
indexed: boolean
4444
}
4545

packages/modeldb/test/config.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ test("parse config", (t) => {
7070
{
7171
source: "room",
7272
sourceProperty: "members",
73-
sourceType: "string",
73+
sourcePrimaryKey: { name: "id", kind: "primitive", type: "string", nullable: false },
7474
target: "user",
75-
targetType: "string",
75+
targetPrimaryKey: { name: "id", kind: "primitive", type: "string", nullable: false },
7676
indexed: true,
7777
},
7878
],

0 commit comments

Comments
 (0)