UNPKG

@dbml/core

Version:
338 lines (310 loc) 9.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TableRecord = exports.Table = exports.Ref = exports.Index = exports.Field = exports.Enum = exports.Endpoint = void 0; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /* eslint-disable camelcase */ /* eslint-disable max-classes-per-file */ var Index = /*#__PURE__*/function () { /** * @param {{ * name: string, * unique: boolean, * pk: boolean, * type: string, * columns: {value: string, type: 'column' | 'string' | 'expression'}[], * }} param0 */ function Index(_ref) { var name = _ref.name, unique = _ref.unique, pk = _ref.pk, type = _ref.type, columns = _ref.columns; _classCallCheck(this, Index); /** @type {string} */ this.name = name; /** @type {string} */ this.type = type; /** @type {boolean} */ this.unique = unique; /** @type {boolean} */ this.pk = pk; /** @type {{value: string, type: 'column' | 'string' | 'expression'}[]} */ this.columns = columns; } _createClass(Index, [{ key: "toJSON", value: function toJSON() { return { name: this.name, type: this.type, unique: this.unique, pk: this.pk, columns: this.columns }; } }]); return Index; }(); exports.Index = Index; var Field = /*#__PURE__*/function () { /** @type {boolean} */ // fk; /** * @param {{ * name: string, * type: {type_name: string, schemaName: string}, * not_null: boolean, * increment: boolean, * dbdefault: {value: string, type: 'string' | 'number' | 'boolean' | 'expression'}, * unique: boolean, * pk: boolean, * note: {value: string} * }} param0 */ function Field(_ref2) { var name = _ref2.name, type = _ref2.type, not_null = _ref2.not_null, increment = _ref2.increment, dbdefault = _ref2.dbdefault, unique = _ref2.unique, pk = _ref2.pk, note = _ref2.note; _classCallCheck(this, Field); /** @type {string} */ this.name = name; /** @type {{type_name: string, schemaName: string}} */ this.type = type; /** @type {boolean} */ this.not_null = not_null; /** @type {boolean} */ this.increment = increment; /** @type {{value: string, type: 'string' | 'number' | 'boolean' | 'expression'}} */ this.dbdefault = dbdefault; /** @type {boolean} */ this.unique = unique; /** @type {boolean} */ this.pk = pk; /** @type {{value: string}} */ this.note = note; } _createClass(Field, [{ key: "toJSON", value: function toJSON() { return { name: this.name, type: this.type, not_null: this.not_null, increment: this.increment, dbdefault: this.dbdefault, unique: this.unique, pk: this.pk, note: this.note }; } }]); return Field; }(); exports.Field = Field; var Table = /*#__PURE__*/function () { /** * @param {{ * name: string, * schemaName: string, * fields: Field[], * indexes: Index[], * note: {value: string} * }} param0 */ function Table(_ref3) { var name = _ref3.name, schemaName = _ref3.schemaName, fields = _ref3.fields, indexes = _ref3.indexes, note = _ref3.note; _classCallCheck(this, Table); /** @type {string} */ this.name = name; /** @type {string} */ this.schemaName = schemaName; /** @type {Field[]} */ this.fields = fields || []; /** @type {Index[]} */ this.indexes = indexes || []; /** @type {{value: string}} */ this.note = note; } _createClass(Table, [{ key: "toJSON", value: function toJSON() { var _this$fields, _this$indexes; return { name: this.name, schemaName: this.schemaName, fields: (_this$fields = this.fields) === null || _this$fields === void 0 ? void 0 : _this$fields.map(function (f) { return f.toJSON(); }), indexes: (_this$indexes = this.indexes) === null || _this$indexes === void 0 ? void 0 : _this$indexes.map(function (i) { return i.toJSON(); }), note: this.note }; } }]); return Table; }(); exports.Table = Table; var Endpoint = /*#__PURE__*/function () { /** * @param {{ * tableName: string, * schemaName: string, * fieldNames: string[], * relation: '*' | '1' * }} param0 */ function Endpoint(_ref4) { var tableName = _ref4.tableName, schemaName = _ref4.schemaName, fieldNames = _ref4.fieldNames, relation = _ref4.relation; _classCallCheck(this, Endpoint); /** @type {string} */ this.tableName = tableName; /** @type {string} */ this.schemaName = schemaName; /** @type {string[]} */ this.fieldNames = fieldNames; /** @type {'*' | '1'} */ this.relation = relation; } _createClass(Endpoint, [{ key: "toJSON", value: function toJSON() { return { tableName: this.tableName, schemaName: this.schemaName, fieldNames: this.fieldNames, relation: this.relation }; } }]); return Endpoint; }(); exports.Endpoint = Endpoint; var Ref = /*#__PURE__*/function () { /** * @param {{ * name: string, * endpoints: Endpoint[], * onDelete: string, * onUpdate: string * }} param0 */ function Ref(_ref5) { var name = _ref5.name, endpoints = _ref5.endpoints, onDelete = _ref5.onDelete, onUpdate = _ref5.onUpdate; _classCallCheck(this, Ref); /** @type {string} */ this.name = name; /** @type {Endpoint[]} */ this.endpoints = endpoints || []; /** @type {string} */ this.onDelete = onDelete; /** @type {string} */ this.onUpdate = onUpdate; } _createClass(Ref, [{ key: "toJSON", value: function toJSON() { return { name: this.name, onDelete: this.onDelete, onUpdate: this.onUpdate, endpoints: this.endpoints.map(function (e) { return e.toJSON(); }) }; } }]); return Ref; }(); exports.Ref = Ref; var Enum = /*#__PURE__*/function () { function Enum(_ref6) { var name = _ref6.name, schemaName = _ref6.schemaName, values = _ref6.values; _classCallCheck(this, Enum); /** @type {string} */ this.name = name; /** @type {string} */ this.schemaName = schemaName; /** @type {{name: string}[]} */ this.values = values; } _createClass(Enum, [{ key: "toJSON", value: function toJSON() { return { name: this.name, schemaName: this.schemaName, values: this.values }; } }]); return Enum; }(); exports.Enum = Enum; var TableRecord = /*#__PURE__*/function () { /** * @param {{ * tableName: string, * columns: string[], * values: { * value: any, * type: string, * }[] * schemaName?: string, * }} param0 */ function TableRecord(_ref7) { var tableName = _ref7.tableName, columns = _ref7.columns, values = _ref7.values, _ref7$schemaName = _ref7.schemaName, schemaName = _ref7$schemaName === void 0 ? undefined : _ref7$schemaName; _classCallCheck(this, TableRecord); /** @type {string} */ this.tableName = tableName; /** @type {string | undefined} */ this.schemaName = schemaName; /** @type {string[]} */ this.columns = columns; /** @type {{value: any, type: string}[]} */ this.values = values; } _createClass(TableRecord, [{ key: "toJSON", value: function toJSON() { return { tableName: this.tableName, schemaName: this.schemaName, columns: this.columns, values: this.values }; } }]); return TableRecord; }(); exports.TableRecord = TableRecord;