Skip to content

Commit

Permalink
feat: include single type check
Browse files Browse the repository at this point in the history
  • Loading branch information
JulissaDantes committed Mar 21, 2024
1 parent 69e6f52 commit b3198de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class ModelInstanceDocumentHandler implements StreamHandler<ModelInstance
state: StreamState<ModelInstanceDocumentStateMetadata>,
context: StreamReaderWriter
): Promise<StreamState> {
const deterministicTypes = ['set', 'single']
// Retrieve the payload
const payload = commitData.commit
StreamUtils.assertCommitLinksToState(state, payload)
Expand Down Expand Up @@ -196,7 +197,7 @@ export class ModelInstanceDocumentHandler implements StreamHandler<ModelInstance
const oldContent = state.content ?? {}
const newContent = jsonpatch.applyPatch(oldContent, payload.data, undefined, false).newDocument
const modelStream = await context.loadStream<Model>(metadata.model)
const isSetType = modelStream.content.accountRelation.type === 'set'
const isDetType = deterministicTypes.includes(modelStream.content.accountRelation.type)
const isFirstDataCommit = !state.log.some((c) => c.type === EventType.DATA)

await this._validateContent(
Expand All @@ -205,7 +206,7 @@ export class ModelInstanceDocumentHandler implements StreamHandler<ModelInstance
newContent,
false,
payload,
isSetType && isFirstDataCommit
isDetType && isFirstDataCommit
)
await this._validateUnique(
modelStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const MODEL_DEFINITION_SINGLE: ModelDefinition = {
type: 'object',
additionalProperties: false,
properties: {
one: { type: 'string', minLength: 2 },
myData: {
type: 'integer',
maximum: 10000,
Expand All @@ -55,6 +56,7 @@ const MODEL_DEFINITION_SINGLE: ModelDefinition = {
},
required: ['myData'],
},
immutableFields: ['one'],
}

const MODEL_DEFINITION_SET: ModelDefinition = {
Expand Down Expand Up @@ -253,7 +255,8 @@ describe('ModelInstanceDocument API http-client tests', () => {
expect(doc.state.log[1].type).toEqual(EventType.DATA)
})

test('Can upsert immutable fields in a set relation model', async () => {
test('Can upsert immutable fields in a set/single relation model', async () => {
// set
const doc = await ModelInstanceDocument.set(
ceramic,
{ controller: ceramic.did!.id, model: modelSet.id },
Expand All @@ -268,6 +271,16 @@ describe('ModelInstanceDocument API http-client tests', () => {
expect(doc.state.log.length).toEqual(2)
expect(doc.state.log[0].type).toEqual(EventType.INIT)
expect(doc.state.log[1].type).toEqual(EventType.DATA)
// single
const singleDoc = await ModelInstanceDocument.single(ceramic, midSingleMetadata)
expect(singleDoc.content).toBeNull()
const singleNewContent = { one: 'foo', myData: 1 }
await singleDoc.replace(singleNewContent)

expect(singleDoc.content).toEqual(singleNewContent)
expect(singleDoc.state.log.length).toEqual(2)
expect(singleDoc.state.log[0].type).toEqual(EventType.INIT)
expect(singleDoc.state.log[1].type).toEqual(EventType.DATA)
})

test(`Cannot create document with relation that isn't a valid streamid`, async () => {
Expand Down

0 comments on commit b3198de

Please sign in to comment.