Skip to content

Instantly share code, notes, and snippets.

@ahmadseder
Created February 15, 2023 19:04
Show Gist options
  • Save ahmadseder/5071930acd143afd95eb5cb705646316 to your computer and use it in GitHub Desktop.
Save ahmadseder/5071930acd143afd95eb5cb705646316 to your computer and use it in GitHub Desktop.
Canaan CQRS
# You need to clone the project and run it
# For the CQRS and domains create everything under a file called CQRS.js
# For handling the flow create a file called services.js
@ahmadseder
Copy link
Author

Check the utils file to get all the needed functionality
You will need to use two domains ( projects, and user ) and should be named like this
The project app will be like this

{
  balance: 0,
  projects: [],
  selected: {},
  entries:[],
}

So when creating commands or queries the paths will be selectedProject, balance,entries[], and projects[] mind the brackets and check the documentation if you haven't already
The query doesn't require [] so you can directly define the path as entries for example

{
  balance: 0,
  projects: [],
}

For the user it will be

{
info:{}
}

so the path will info

@ahmadseder
Copy link
Author

Import this file to https://www.json-rule-editor.com/#/home and initialise the rules engine

{
	"name": "Demo",
	"attributes": [
		{
			"name": "data",
			"type": "object"
		},
		{
			"name": "context",
			"type": "object"
		},
		{
			"name": "user.data.info.subscriptionType",
			"type": "string"
		},
		{
			"name": "type",
			"type": "string"
		},
		{
			"name": "CanaanApp.data.commandCounter.projects-addEntry.count",
			"type": "number"
		}
	],
	"decisions": [
		{
			"conditions": {
				"all": [
					{
						"fact": "data",
						"operator": "equal",
						"value": "Free",
						"path": ".user.data.info.subscriptionType"
					},
					{
						"fact": "context",
						"operator": "equal",
						"value": "addEntry",
						"path": ".type"
					},
					{
						"fact": "data",
						"operator": "greaterThanInclusive",
						"value": "3",
						"path": ".CanaanApp.data.commandCounter.projects-addEntry.count"
					}
				]
			},
			"event": {
				"type": "FreeSubscriptionLimitExceeded",
				"params": {
					"message": "You are on a free plan You can't add on more than 3 logs per visit, Upgrade your plan to add more"
				}
			}
		}
	]
}

@ahmadseder
Copy link
Author

To use the subscribe with React use

const [some,setSome] = useState(); 
useEffect(()=>{
domain.subscribe(query,setSome);
},[]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment