UNPKG

dataorm

Version:
250 lines (196 loc) 5.99 kB
import { snakeCase } from 'change-case'; import Pluralize from 'pluralize'; function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } var Subject = /*#__PURE__*/function () { function Subject() {} Subject.subscribe = function subscribe(fn) { var _this = this; this.observers.push(fn); return function () { return _this.unsubscribe(fn); }; }; Subject.fire = function fire(data) { this.observers.forEach(function (fn) { fn(data); }); }; Subject.unsubscribe = function unsubscribe(fnr) { this.observers = this.observers.filter(function (fn) { return fn !== fnr; }); }; return Subject; }(); Subject.observers = []; var Model = /*#__PURE__*/function (_Subject) { _inheritsLoose(Model, _Subject); function Model() { return _Subject.apply(this, arguments) || this; } Model.get = function get() { return Subject; }; Model.find = function find(id) { var instance = new this(); var db1 = Database.getInstance(); var collection = db1.collection(instance['entity']); var result = collection.find(function (item) { return item.id === id; }); return result !== null && result !== void 0 ? result : null; }; Model.create = function create(entity) { var instance = new this(); var db1 = Database.getInstance(); var collection = db1.collection(instance['entity']); if (entity.hasOwnProperty('id') && collection.find(function (item) { return item.id === entity['id']; })) { throw new Error('Duplicate id'); } entity['id'] = entity.hasOwnProperty('id') ? entity['id'] : collection.length + 1; collection.push(entity); db1.setCollection(instance['entity'], collection); this.fire(collection); return entity !== null && entity !== void 0 ? entity : null; }; var _proto = Model.prototype; _proto.hasMany = function hasMany(relation) { return relation; }; _proto.belongsTo = function belongsTo(relation) { return relation; }; return Model; }(Subject); var Database = /*#__PURE__*/function () { function Database() { this.initialized = false; this.store = {}; this.entities = []; // private schemas: any = {}; this.dbConfig = { name: 'db', storage: 'LocalStorage' }; this.setStore(this.dbConfig); } var _proto = Database.prototype; _proto.collection = function collection(entity) { return this.store[this.dbConfig.name][entity]; }; _proto.setCollection = function setCollection(entity, collection) { console.log(collection, 'collection'); this.store[this.dbConfig.name][entity] = collection; }; _proto.generateConfig = function generateConfig(config) { var newConfig = this.dbConfig; Object.keys(this.dbConfig).forEach(function (key) { newConfig[key] = config[key]; }); return newConfig; }; _proto.checkModelTypeMappingCapabilities = function checkModelTypeMappingCapabilities(model) { if (model.prototype instanceof Model === false) { throw new Error('Invalid Model Bindings'); } var instance = new model(); if (instance.entity && this.entities.find(function (entity) { return entity.name == instance.entity; })) { throw new Error("Duplicate entity name for " + model.name); } }; _proto.createModelBinding = function createModelBinding(model) { var _instance$entity; Object.defineProperty(model, 'store', { value: this.store }); var instance = new model(); return { name: (_instance$entity = instance.entity) !== null && _instance$entity !== void 0 ? _instance$entity : snakeCase(Pluralize.plural(model.name)), model: model }; }; _proto.createSchema = function createSchema() { var _this = this; this.entities.forEach(function (entity) { _this.registerSchema(entity); }); }; _proto.registerSchema = function registerSchema(entity) { if (!this.store[this.dbConfig.name].hasOwnProperty(entity.name)) { this.store[this.dbConfig.name][entity.name] = []; } }; _proto.setStore = function setStore(dbConfig) { var _this$store; this.dbConfig = this.generateConfig(dbConfig); this.store = (_this$store = {}, _this$store[this.dbConfig.name] = {}, _this$store); }; Database.getInstance = function getInstance() { if (this.instance === null) { this.instance = new Database(); return this.instance; } return this.instance; }; _proto.config = function config(dbConfig) { if (dbConfig === void 0) { dbConfig = this.dbConfig; } if (this.initialized) { throw new Error('Database already initialized'); } this.setStore(dbConfig); return this; }; _proto.register = function register(model) { this.checkModelTypeMappingCapabilities(model); var entity = this.createModelBinding(model); this.entities.push(entity); return this; }; _proto.start = function start() { this.createSchema(); this.initialized = true; }; return Database; }(); Database.instance = null; var DB = /*#__PURE__*/Database.getInstance(); var IdAttrTypeData = { autoincrement: true }; var StringAttrTypeData = { nullable: true, "default": '' }; var Attr = /*#__PURE__*/function () { function Attr() {} Attr.generate = function generate(data, IdAttrTypeData) { return { data: data, IdAttrTypeData: IdAttrTypeData }; }; Attr.id = function id(data) { if (data === void 0) { data = IdAttrTypeData; } return this.generate(data, IdAttrTypeData); }; Attr.string = function string(data) { if (data === void 0) { data = StringAttrTypeData; } return this.generate(data, StringAttrTypeData); }; return Attr; }(); export { Attr, DB, Model }; //# sourceMappingURL=dataorm.esm.js.map