Skip to content

Commit

Permalink
Support optional relations (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulLeCam authored Feb 7, 2024
1 parent cf49049 commit 2097b0c
Show file tree
Hide file tree
Showing 11 changed files with 2,038 additions and 4,126 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
"@swc/core": "^1.4.0",
"@swc/jest": "^0.2.36",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"del-cli": "^5.1.0",
"eslint": "^8.56.0",
"eslint-config-3box": "^1.0.0",
"eslint-plugin-jest": "^27.6.3",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"turbo": "^1.12.2",
"turbo": "^1.12.3",
"typedoc": "0.25.7",
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "^5.3.3"
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"@composedb/devtools": "workspace:^",
"@composedb/devtools-node": "workspace:^",
"@composedb/runtime": "workspace:^",
"@oclif/core": "^3.18.2",
"@oclif/core": "^3.19.1",
"@oclif/plugin-help": "^6.0.12",
"@oclif/plugin-version": "^2.0.12",
"cli-table3": "^0.6.3",
Expand Down Expand Up @@ -107,7 +107,7 @@
"ajv": "^8.12.0",
"execa": "^8.0.1",
"jest-dev-server": "^9.0.2",
"oclif": "^4.4.4",
"oclif": "^4.4.7",
"strip-ansi": "~7.1.0"
},
"jest": {
Expand Down
22 changes: 21 additions & 1 deletion packages/devtools/test/__snapshots__/composite.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,27 @@ exports[`composite class Composite instance merge() merges composites into the i
],
},
],
"kjzl6hvfrbw6c9u8sp41biu3r0ghk8mw34od8jrjmpf2sw298dtgm49mcr0bzrl": [
"kjzl6hvfrbw6c85zxgdsynde91d2nq24dnpgy0fv8p5oopyzy401yrbip8debwb": [
{
"fields": [
{
"path": [
"title",
],
},
],
},
{
"fields": [
{
"path": [
"title",
],
},
],
},
],
"kjzl6hvfrbw6c8vrx18erjixmhj6a1u0jxl4x2pl6wlcif8enjavftoizd0edp3": [
{
"fields": [
{
Expand Down
58 changes: 58 additions & 0 deletions packages/devtools/test/__snapshots__/format-runtime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ exports[`Runtime format Favorite with SET account relation 1`] = `
exports[`Runtime format Note model definition with views 1`] = `
{
"accountData": {
"noteBookList": {
"name": "NoteBook",
"type": "connection",
},
"noteList": {
"name": "Note",
"type": "connection",
Expand All @@ -79,13 +83,34 @@ exports[`Runtime format Note model definition with views 1`] = `
"implements": [],
"interface": false,
},
"NoteBook": {
"accountRelation": {
"type": "list",
},
"id": "NoteBookID",
"implements": [],
"interface": false,
},
},
"objects": {
"Note": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"noteBook": {
"relation": {
"model": "NoteBookID",
"property": "noteBookID",
"source": "document",
},
"type": "view",
"viewType": "relation",
},
"noteBookID": {
"required": false,
"type": "streamid",
},
"status": {
"refName": "NoteStatus",
"refType": "enum",
Expand All @@ -106,6 +131,39 @@ exports[`Runtime format Note model definition with views 1`] = `
"viewType": "documentVersion",
},
},
"NoteBook": {
"author": {
"type": "view",
"viewType": "documentAccount",
},
"notes": {
"relation": {
"model": "NoteID",
"property": "noteBookID",
"source": "queryConnection",
},
"type": "view",
"viewType": "relation",
},
"notesCount": {
"relation": {
"model": "NoteID",
"property": "noteBookID",
"source": "queryCount",
},
"type": "view",
"viewType": "relation",
},
"title": {
"indexed": true,
"required": true,
"type": "string",
},
"version": {
"type": "view",
"viewType": "documentVersion",
},
},
},
}
`;
Expand Down
31 changes: 27 additions & 4 deletions packages/runtime/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ export function idToString(id: DocID): string {
return typeof id === 'string' ? StreamRef.from(id).toString() : id.toString()
}

/** @internal */
export function removeNullValues(content: Record<string, unknown>): Record<string, unknown> {
const copy = { ...content }
for (const [key, value] of Object.entries(copy)) {
if (value == null) {
delete copy[key]
} else if (Array.isArray(value)) {
copy[key] = value.map((item: unknown) => {
return typeof item === 'object' && !Array.isArray(item) && item != null
? removeNullValues(item as Record<string, unknown>)
: item
})
} else if (typeof value === 'object') {
copy[key] = removeNullValues(value as Record<string, unknown>)
}
}
return copy
}

/** @internal */
export function toMetadata(
model: StreamID | string,
Expand Down Expand Up @@ -132,8 +151,12 @@ export class DocumentLoader extends DataLoader<DocID, ModelInstanceDocument> {
content: T,
{ controller, ...options }: CreateOptions = {},
): Promise<ModelInstanceDocument<T>> {
const metadata = toMetadata(model, controller)
const stream = await ModelInstanceDocument.create<T>(this.#ceramic, content, metadata, options)
const stream = await ModelInstanceDocument.create<T>(
this.#ceramic,
removeNullValues(content) as T,
toMetadata(model, controller),
options,
)
this.cache(stream)
return stream
}
Expand Down Expand Up @@ -191,8 +214,8 @@ export class DocumentLoader extends DataLoader<DocID, ModelInstanceDocument> {
if (version != null && stream.commitId.toString() !== version) {
throw new Error('Stream version mismatch')
}
const newContent = replace ? content : { ...stream.content, ...content }
await stream.replace(newContent, options)
const newContent = replace ? content : { ...(stream.content ?? {}), ...content }
await stream.replace(removeNullValues(newContent) as T, options)
return stream
}
}
24 changes: 16 additions & 8 deletions packages/runtime/test/__snapshots__/runtime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -594,14 +594,6 @@ exports[`runtime can create a document using extra scalars 1`] = `
}
`;

exports[`runtime create a document with enum 1`] = `
{
"status": "DEFAULT",
"text": "Test node contents",
"title": "A test note",
}
`;

exports[`runtime create and query post with comments 1`] = `
"type Query {
"""Fetches objects given their IDs"""
Expand Down Expand Up @@ -1115,6 +1107,22 @@ exports[`runtime create and query ratings with filters and ordering 3`] = `
}
`;

exports[`runtime create and update a document with enum 1`] = `
{
"status": "DEFAULT",
"text": "Test node contents",
"title": "A test note",
}
`;

exports[`runtime create and update a document with enum 2`] = `
{
"status": null,
"text": "New note contents",
"title": "A test note",
}
`;

exports[`runtime interfaces queries 1`] = `
{
"data": {
Expand Down
Loading

0 comments on commit 2097b0c

Please sign in to comment.