@platform/cell.schema
Version:
URI and database schemas for the `cell.os`.
185 lines (184 loc) • 6.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var File_1 = require("../File");
var Ref_1 = require("../Ref");
var Uri_1 = require("../Uri");
var Url_1 = require("../Url");
var Schema = (function () {
function Schema() {
}
Schema.mime = common_1.Mime;
Schema.uri = Uri_1.Uri;
Schema.file = File_1.FileSchema;
Schema.ref = Ref_1.RefSchema;
Schema.cuid = common_1.cuid;
Schema.slug = common_1.slug;
Schema.hash = common_1.hash;
Schema.coord = common_1.coord;
Schema.Urls = Url_1.Urls;
Schema.urls = function (host) { return Url_1.Urls.create(host); };
Schema.ns = function (id) { return new NsSchema({ id: id }); };
Schema.query = {
cells: '/CELL/*',
rows: '/ROW/*',
columns: '/COL/*',
files: '/FILE/*',
};
Schema.from = {
ns: function (input) {
return from({
input: input,
toUri: function (path) { return NsSchema.uri({ path: path }); },
toPath: function (uri) { return Schema.ns(uri).path; },
});
},
cell: function (input) {
return from({
input: input,
toUri: function (path) { return CoordSchema.uri({ path: path }); },
toPath: function (uri) {
var parts = Uri_1.Uri.parse(uri).parts;
return Schema.ns(parts.ns).cell(parts.key).path;
},
});
},
row: function (input) {
return from({
input: input,
toUri: function (path) { return CoordSchema.uri({ path: path }); },
toPath: function (uri) {
var parts = Uri_1.Uri.parse(uri).parts;
return Schema.ns(parts.ns).row(parts.key).path;
},
});
},
column: function (input) {
return from({
input: input,
toUri: function (path) { return CoordSchema.uri({ path: path }); },
toPath: function (uri) {
var parts = Uri_1.Uri.parse(uri).parts;
return Schema.ns(parts.ns).column(parts.key).path;
},
});
},
file: function (input) {
return from({
input: input,
toUri: function (path) { return File_1.FileSchema.uri({ path: path }); },
toPath: function (uri) {
var parts = Uri_1.Uri.parse(uri).parts;
return Schema.ns(parts.ns).file(parts.file).path;
},
});
},
};
return Schema;
}());
exports.Schema = Schema;
var NsSchema = (function () {
function NsSchema(args) {
var id = args.id || common_1.cuid();
if (Uri_1.Uri.is.uri(id)) {
var uri = Uri_1.Uri.parse(id);
if (uri.error) {
throw new Error(uri.error.message);
}
if (uri.parts.type !== 'NS') {
throw new Error("The given URI does not represent a namespace (\"" + uri.toString() + "\").");
}
id = uri.parts.id;
}
this.id = id;
this.path = "NS/" + id;
this.uri = Uri_1.Uri.create.ns(id);
}
NsSchema.prototype.cell = function (key) {
var uri = Uri_1.Uri.create.cell(this.id, key);
return new CoordSchema({ type: 'CELL', nsPath: this.path, id: key, uri: uri });
};
NsSchema.prototype.column = function (key) {
var uri = Uri_1.Uri.create.column(this.id, key);
return new CoordSchema({ type: 'COL', nsPath: this.path, id: key, uri: uri });
};
NsSchema.prototype.row = function (key) {
var uri = Uri_1.Uri.create.row(this.id, key);
return new CoordSchema({ type: 'ROW', nsPath: this.path, id: key, uri: uri });
};
NsSchema.prototype.file = function (fileid) {
var uri = Uri_1.Uri.create.file(this.id, fileid);
return File_1.FileSchema.create({ nsPath: this.path, fileid: fileid, uri: uri });
};
NsSchema.uri = function (args) {
var parts = args.path.split('/');
var type = parts[0];
var id = parts[1];
if (type === 'NS') {
return Uri_1.Uri.create.ns(id);
}
throw new Error("Model path could not be converted to URI (\"" + args.path + "\")");
};
return NsSchema;
}());
exports.NsSchema = NsSchema;
var CoordSchema = (function () {
function CoordSchema(args) {
this.id = args.id;
this.type = args.type;
this.path = args.nsPath + "/" + args.type + "/" + this.id;
this._uri = args.uri;
}
CoordSchema.uri = function (args) {
var parts = args.path.split('/');
var ns = parts[1];
var type = parts[2];
var key = parts[3];
if (type === 'CELL') {
return Uri_1.Uri.create.cell(ns, key);
}
if (type === 'ROW') {
return Uri_1.Uri.create.row(ns, key);
}
if (type === 'COL') {
return Uri_1.Uri.create.column(ns, key);
}
throw new Error("Model path could not be converted to URI (\"" + args.path + "\")");
};
Object.defineProperty(CoordSchema.prototype, "uri", {
get: function () {
return Uri_1.Uri.parse(this._uri).parts;
},
enumerable: true,
configurable: true
});
return CoordSchema;
}());
exports.CoordSchema = CoordSchema;
var from = function (args) {
var path = '';
var uri = '';
if (typeof args.input === 'object') {
path = args.input.path;
uri = args.toUri(path);
}
else {
if (Uri_1.Uri.is.uri(args.input)) {
uri = args.input;
path = args.toPath(uri);
}
else {
path = args.input;
uri = args.toUri(path);
}
}
if (!path) {
throw new Error("Model schema [path] could not be derived.");
}
if (!uri) {
throw new Error("Model URI could not be derived.");
}
var parts = Uri_1.Uri.parse(uri);
return tslib_1.__assign(tslib_1.__assign({}, parts), { path: path });
};