Skip to content

Instantly share code, notes, and snippets.

View robertdempsey's full-sized avatar

Robert Dempsey robertdempsey

View GitHub Profile
{
"$id": "https://example.com/person.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
@robertdempsey
robertdempsey / more-realistic-example-schema.json
Last active February 28, 2020 10:51
An example JSON Schema output
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyBaseObject": {
"additionalProperties": false,
"properties": {
"number": {
"description": "This is a number. We can set the max and min like so",
"maximum": 9,
@robertdempsey
robertdempsey / MyTypescriptTypes.ts
Created February 28, 2020 10:47
Example Typescript input for ts-json-schema-generator
export interface MyBaseObject {
/**
* This is a number. We can set the max and min like so
* @minimum 3
* @maximum 9
*/
number: number;
/**
* This is a string. We can set the maxLength like so
@robertdempsey
robertdempsey / TypeGuardWithTypePredicate.ts
Last active February 28, 2020 11:03
Example of a type guard with a type predicate using an ajv validator to determine if the object matches a schema
// where you might have something like this and check a single property exists
isMyType(toCheck: any): toCheck is MyType {
return !!toCheck.propertyOfMyType
}
/**
Now we can have a generic function that takes type, and we can verify
that the whole object validates against a schema, rather than just checking
a property or two
*/
@robertdempsey
robertdempsey / RetrieveAndUpdateEntireState.ts
Last active March 11, 2020 09:53
Retrieves a document from MongoDB twice, updates it twice, and writes it twice. Points out the danger with doing this in parallel.
// Receive notification of update of property 'b' for system_id: 123
const notification_b = {
system_id: 123,
b: 4
}
// Retrieve state of system_id: 123 from MongoDB
const current_state_before_b_change = {
system_id: 123,
a: 1,
@robertdempsey
robertdempsey / ExampleSQLStatementToUpdateSingleProperty.sql
Last active March 6, 2020 11:13
SQL table 'system_states' showing how our system state data would be represented in SQL.
UPDATE system_states
SET a = 4
WHERE system_id = 123;
UPDATE system_states
SET b = 5
WHERE system_id = 123;
@robertdempsey
robertdempsey / ExampleMongoDB$SetStatement.ts
Last active March 8, 2020 10:40
Updates values for properties 'a' and 'b' of System with system_id 123.
/** create connection pool to system DB in MongoDB
* assume we are using https://www.npmjs.com/package/mongodb
* assume we begin with:
{
system_id: 123,
a: 1,
b: 2,
c: 3
}
*/
@robertdempsey
robertdempsey / ExampleMongoDB$setOnInsertStatement.ts
Last active March 8, 2020 10:44
Will set the property 'created_at' to the current date and time when an existing document is not found
/** create connection pool to system DB in MongoDB
* assume we are using https://www.npmjs.com/package/mongodb
* assume we start with no document with property matching 'system_id: 123'
*/
system_states_collection.update(
{ system_id: 123 },
{
$set: { a: 4 },
$setOnInsert: { created_at: new Date() }
@robertdempsey
robertdempsey / ExampleMongoDB$pushStatement.ts
Last active March 8, 2020 11:18
Pushes an object containing 'saved_at' with today's date, 'saved_by' with a user name into the save_history property
/** create connection pool to system DB in MongoDB
* assume we are using https://www.npmjs.com/package/mongodb
* assume we begin with:
{
system_id: 123,
a: 1,
b: 2,
c: 3
}
*/
@robertdempsey
robertdempsey / ExampleMongoDB$pullStatement.ts
Last active March 8, 2020 10:49
Pulls an item from an existing array
/** create connection pool to system DB in MongoDB
* assume we are using https://www.npmjs.com/package/mongodb
* assume we begin with:
{
system_id: 123,
a: 1,
b: 2,
c: 3,
watcher_emails: ['somebody@emailaddress.com']
}