Skip to content

Commit

Permalink
fix!: revise schema for data & config store records [1/6] (#72)
Browse files Browse the repository at this point in the history
* fix!: update protobuf and JSONSchema definitions

* fix: Add old JSONSchema definitions (Mapeo <=v5)

* delete "old" protobuf & jsonSchema defs

* Updates to proto & JSONSchema defs
  • Loading branch information
gmaclennan authored Aug 1, 2023
1 parent ddbd70d commit 71ce3f6
Show file tree
Hide file tree
Showing 30 changed files with 620 additions and 474 deletions.
133 changes: 28 additions & 105 deletions examples/schema_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,33 @@
import { encode, decode, validate } from '../index.js'
import Hypercore from 'hypercore'
import ram from 'random-access-memory'
import { randomBytes } from 'node:crypto'

// FILTER_1
// const obj = {
// id: randomBytes(32).toString('hex'),
// type: 'filter',
// schemaVersion: 1,
// created_at: new Date().toJSON(),
// filter: ['observation'],
// name: 'john',
// }

// DEVICE
// const obj = {
// schemaType: 'Device',
// schemaVersion: 1,
// id: randomBytes(32).toString('hex'),
// action: 'device:add',
// authorId: randomBytes(32).toString('hex'),
// projectId: randomBytes(32).toString('hex'),
// signature: 'hi',
// authorIndex: 10,
// deviceIndex: 10,
// }

// ROLE
// const obj = {
// id: randomBytes(32).toString('hex'),
// schemaType: 'Role',
// schemaVersion: 1,
// role: 'project-creator',
// created_at: new Date(),
// projectId: randomBytes(32).toString('hex'),
// action: 'role:set',
// signature: 'hi',
// authorIndex: 10,
// deviceIndex: 10,
// }

// CORE OWNERSHIP
// const obj = {
// type: 'coreOwnership',
// schemaVersion: 1,
// id: randomBytes(32).toString('hex'),
// coreId: randomBytes(32).toString('hex'),
// projectId: randomBytes(32).toString('hex'),
// storeType: 'blob',
// authorIndex: 10,
// deviceIndex: 10,
// action: 'core:owner',
// }

// PRESET_1
// const obj = {
// id: randomBytes(32).toString('hex'),
// type: 'Preset',
// schemaVersion: 1,
// tags: { nature: 'tree' },
// geometry: ['point'],
// name: 'john',
// }

// FIELD_1
const obj = {
id: randomBytes(32).toString('hex'),
schemaType: 'Project',
schemaVersion: 1,
name: 'My Project',
created_at: new Date().toJSON(),
}

// OBSERVATION 4
// const obj = {
// id: randomBytes(32).toString('hex'),
// schemaType: 'observation',
// schemaVersion: 4,
// created_at: new Date().toJSON(),
// }

// OBSERVATION 5
// const obj = {
// id: randomBytes(32).toString('hex'),
// type: 'Observation',
// schemaVersion: 5,
// created_at: new Date().toJSON(),
// }

const record = encode(obj)

const core = new Hypercore(ram, { valueEncoding: 'binary' })
await core.ready()
core.append(record)

try {
const index = 0
const data = await core.get(index)
const decodedData = decode(data, { coreId: core.key, seq: index })
console.log('decoded', decodedData)
console.log('VALID?', validate(decodedData))
if (Buffer.compare(data, record) !== 0) {
throw new Error(`data doesn't match: ${data} != ${record}`)
} else {
console.log('data matches <3')
import { docs } from '../test/docs.js'

const objs = docs.good

Object.keys(objs).forEach(test)

async function test(key) {
const obj = objs[key]
const record = encode(obj)
const k = obj.schemaType || obj.type
const core = new Hypercore(ram, { valueEncoding: 'binary' })
await core.ready()
core.append(record)

try {
const index = 0
const data = await core.get(index)
console.log(`trying ${k}`)
const decodedData = decode(data, { coreId: core.key, seq: index })
console.log('data', decodedData)
console.log(`VALID? `, validate(decodedData), '\n')
if (Buffer.compare(data, record) !== 0) {
throw new Error(`data doesn't match: ${data} != ${record}`)
} else {
console.log('data matches <3')
}
} catch (err) {
console.log(err)
}
} catch (err) {
console.log(err)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 9 additions & 6 deletions proto/common/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package mapeo;

import "google/protobuf/timestamp.proto";


message Common_1 {
google.protobuf.Timestamp created_at = 1;
optional string deviceId = 2;
// 32-byte random generated number
bytes id = 3;
repeated string links = 4;
optional google.protobuf.Timestamp timestamp = 5;
optional string userId = 6;
optional bytes id = 1 [(required) = true];
message Link {
bytes coreId = 1;
int32 seq = 2;
}
repeated Link links = 2;
google.protobuf.Timestamp createdAt = 3 [(required) = true];
google.protobuf.Timestamp updatedAt = 4 [(required) = true];
}
/* ignored fields and differences from common.json jsonSchema
* id is a byte buffer here and a string in jsonSchema
Expand Down
21 changes: 14 additions & 7 deletions proto/coreOwnership/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ syntax = "proto3";
package mapeo;

import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";
import "common/v1.proto";
import "options.proto";

message CoreOwnership_1 {
// **DO NOT CHANGE dataTypeId** generated with `openssl rand -hex 6`
option (dataTypeId) = "9d4d39390125";
option (schemaName) = "coreOwnership";

Common_1 common = 1;
string action = 2;
string coreId = 3;
string projectId = 4;
string storeType = 5;
string signature = 6;
int32 authorIndex = 7;
int32 deviceIndex = 8;

string action = 5;
string coreId = 6;
string projectId = 7;
string storeType = 8;
string signature = 9;
int32 authorIndex = 10;
int32 deviceIndex = 11;
}
19 changes: 13 additions & 6 deletions proto/device/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ syntax = "proto3";
package mapeo;

import "google/protobuf/struct.proto";
import "google/protobuf/timestamp.proto";
import "common/v1.proto";
import "options.proto";

message Device_1 {
// **DO NOT CHANGE dataTypeId** generated with `openssl rand -hex 6`
option (dataTypeId) = "e96e6c68fcc0";
option (schemaName) = "device";

Common_1 common = 1;
string action = 2;
string authorId = 3;
string projectId = 4;
string signature = 5;
int32 authorIndex = 6;
int32 deviceIndex = 7;

string action = 5;
string authorId = 6;
string projectId = 7;
string signature = 8;
int32 authorIndex = 9;
int32 deviceIndex = 10;
}
11 changes: 0 additions & 11 deletions proto/field/v1.proto

This file was deleted.

40 changes: 40 additions & 0 deletions proto/field/v2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
syntax = "proto3";
package mapeo;

import "google/protobuf/any.proto";
import "google/protobuf/timestamp.proto";
import "tags/v1.proto";
import "common/v1.proto";
import "options.proto";

message Field_2 {
// **DO NOT CHANGE dataTypeId** generated with `openssl rand -hex 6`
option (dataTypeId) = "9c5abfbee243";
option (schemaName) = "field";

Common_1 common = 1;

optional string tagKey = 5 [(required) = true];
enum Type {
text = 0;
number = 1;
selectOne = 2;
selectMultiple = 3;
}
Type type = 6 [(required) = true];
optional string label = 7 [(required) = true];
enum Appearance {
multiline = 0;
singleline = 1;
}
Appearance appearance = 8;
bool snakeCase = 9;
message Option {
string label = 1;
TagValue_1.PrimitiveValue value = 2;
}
repeated Option options = 10;
bool universal = 11;
optional string placeholder = 12;
optional string helperText = 13;
}
16 changes: 0 additions & 16 deletions proto/filter/v1.proto

This file was deleted.

48 changes: 0 additions & 48 deletions proto/observation/v4.proto

This file was deleted.

Loading

0 comments on commit 71ce3f6

Please sign in to comment.