You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zapdb-kv/roadmap/test-schema-roundtrip.js

81 lines
2.7 KiB
JavaScript

"use strict";
let dummyMigrations = [
{ id: 1, operations: [
{ type: "createCollection", name: "users", operations: [
{ type: "createField", name: "username", fieldType: "string", required: true, attributes: {} },
{ type: "createField", name: "passwordHash", fieldType: "string", required: true, attributes: {} },
{ type: "createField", name: "emailAddress", fieldType: "string", required: false, attributes: {} },
{ type: "createField", name: "isActive", fieldType: "boolean", required: true, attributes: {} },
{ type: "createField", name: "registrationDate", fieldType: "date", required: true, attributes: { withTimezone: false }},
{ type: "createField", name: "invitesLeft", fieldType: "integer", required: true, attributes: {} },
]}
]},
{ id: 2, operations: [
{ type: "modifyCollection", name: "users", operations: [
{ type: "setFieldAttributes", name: "emailAddress", required: false, attributes: {} },
{ type: "setFieldAttributes", name: "isActive", required: true, attributes: {} },
{ type: "setFieldAttributes", name: "registrationDate", attributes: { withTimezone: true }},
{ type: "setFieldAttributes", name: "invitesLeft", attributes: { signed: false }},
]}
]},
];
let dummyItems = [{
username: "joepie91",
passwordHash: "foo",
emailAddress: "admin@cryto.net",
isActive: true,
registrationDate: new Date(),
invitesLeft: 100
}, {
username: "test",
passwordHash: "bar",
emailAddress: "test@cryto.net",
isActive: false,
registrationDate: new Date(),
invitesLeft: 0
}];
const reduceSchema = require("../src/schema/reducer");
const createRecordCoder = require("../src/storage-encoder/record-coder");
let schema = reduceSchema(dummyMigrations);
let orderedTableSchema = Object.entries(schema.tables.users.fields)
.map(([ name, settings ]) => {
let { fieldType, ... rest } = settings;
return { name, type: fieldType, ... rest };
})
.sort((a, b) => {
if (b.name < a.name) {
return 1;
} else if (b.name > a.name) {
return -1;
} else {
return 0;
}
});
let tableEncoder = createRecordCoder(orderedTableSchema);
let encodedItems = dummyItems.map((item) => tableEncoder.encode(item));
let decodedItems = encodedItems.map((item) => tableEncoder.decode(item.record));
console.log(tableEncoder);
console.log("# Schema:");
console.dir(schema, { depth: null });
console.log("# Input items:");
console.dir(dummyItems, { depth: null });
console.log("# Encoded items:");
console.dir(encodedItems, { depth: null });
console.log("# Decoded items:");
console.dir(decodedItems, { depth: null });
// MARKER: Auxiliary blob handling, somehow
// MARKER: Implement support for optional fields in record-coder; including encoding the presence mask into the encoded records