UNPKG

@platform/cell.schema

Version:

URI and database schemas for the `cell.os`.

187 lines (186 loc) 6.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoordSchema = exports.NsSchema = exports.Schema = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); var Fs_1 = require("../Fs"); var Ref_1 = require("../Ref"); var Uri_1 = require("../Uri"); var Url_1 = require("../Url"); var Encoding_1 = require("../Encoding"); var Squash_1 = require("../Squash"); exports.Schema = { Mime: common_1.Mime, File: Fs_1.FileSchema, Ref: Ref_1.RefSchema, cuid: common_1.cuid, slug: common_1.slug, Hash: common_1.Hash, coord: common_1.coord, encoding: Encoding_1.Encoding, Squash: Squash_1.Squash, Uri: Uri_1.Uri, Url: Url_1.Url, Urls: Url_1.Urls, urls: function (host) { return Url_1.Urls.create(host); }, ns: function (id) { return new NsSchema({ id: id }); }, query: { cells: '/CELL/*', rows: '/ROW/*', columns: '/COL/*', files: '/FILE/*', }, from: { ns: function (input) { return from({ input: input, toUri: function (path) { return NsSchema.uri({ path: path }); }, toPath: function (uri) { return exports.Schema.ns(uri).path; }, }); }, cell: function (input) { return from({ input: input, toUri: function (path) { return CoordSchema.uri({ path: path }); }, toPath: function (uri) { var _a = Uri_1.Uri.cell(uri), ns = _a.ns, key = _a.key; return exports.Schema.ns(ns).cell(key).path; }, }); }, row: function (input) { return from({ input: input, toUri: function (path) { return CoordSchema.uri({ path: path }); }, toPath: function (uri) { var _a = Uri_1.Uri.row(uri), ns = _a.ns, key = _a.key; return exports.Schema.ns(ns).row(key).path; }, }); }, column: function (input) { return from({ input: input, toUri: function (path) { return CoordSchema.uri({ path: path }); }, toPath: function (uri) { var _a = Uri_1.Uri.column(uri), ns = _a.ns, key = _a.key; return exports.Schema.ns(ns).column(key).path; }, }); }, file: function (input) { return from({ input: input, toUri: function (path) { return Fs_1.FileSchema.uri({ path: path }); }, toPath: function (uri) { var _a = Uri_1.Uri.file(uri), ns = _a.ns, file = _a.file; return exports.Schema.ns(ns).file(file).path; }, }); }, }, }; var NsSchema = (function () { function NsSchema(args) { var id = args.id || (0, 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 (\"".concat(uri.toString(), "\").")); } id = uri.parts.id; } this.id = id; this.path = "NS/".concat(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 Fs_1.FileSchema.toObject({ 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 (\"".concat(args.path, "\")")); }; return NsSchema; }()); exports.NsSchema = NsSchema; var CoordSchema = (function () { function CoordSchema(args) { this.id = args.id; this.type = args.type; this.path = "".concat(args.nsPath, "/").concat(args.type, "/").concat(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 (\"".concat(args.path, "\")")); }; Object.defineProperty(CoordSchema.prototype, "uri", { get: function () { return Uri_1.Uri.parse(this._uri).parts; }, enumerable: false, 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 }); };