UNPKG

dataorm

Version:
3 lines (2 loc) 3.3 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("change-case"),n=(t=require("pluralize"))&&"object"==typeof t&&"default"in t?t.default:t,i=function(){function t(){}return t.subscribe=function(t){var e=this;return this.observers.push(t),function(){return e.unsubscribe(t)}},t.fire=function(t){this.observers.forEach((function(e){e(t)}))},t.unsubscribe=function(t){this.observers=this.observers.filter((function(e){return e!==t}))},t}();i.observers=[];var r=function(t){var e,n;function r(){return t.apply(this,arguments)||this}n=t,(e=r).prototype=Object.create(n.prototype),e.prototype.constructor=e,e.__proto__=n,r.get=function(){return i},r.find=function(t){var e=new this,n=o.getInstance().collection(e.entity).find((function(e){return e.id===t}));return null!=n?n:null},r.create=function(t){var e=new this,n=o.getInstance(),i=n.collection(e.entity);if(t.hasOwnProperty("id")&&i.find((function(e){return e.id===t.id})))throw new Error("Duplicate id");return t.id=t.hasOwnProperty("id")?t.id:i.length+1,i.push(t),n.setCollection(e.entity,i),this.fire(i),null!=t?t:null};var s=r.prototype;return s.hasMany=function(t){return t},s.belongsTo=function(t){return t},r}(i),o=function(){function t(){this.initialized=!1,this.store={},this.entities=[],this.dbConfig={name:"db",storage:"LocalStorage"},this.setStore(this.dbConfig)}var i=t.prototype;return i.collection=function(t){return this.store[this.dbConfig.name][t]},i.setCollection=function(t,e){console.log(e,"collection"),this.store[this.dbConfig.name][t]=e},i.generateConfig=function(t){var e=this.dbConfig;return Object.keys(this.dbConfig).forEach((function(n){e[n]=t[n]})),e},i.checkModelTypeMappingCapabilities=function(t){if(t.prototype instanceof r==0)throw new Error("Invalid Model Bindings");var e=new t;if(e.entity&&this.entities.find((function(t){return t.name==e.entity})))throw new Error("Duplicate entity name for "+t.name)},i.createModelBinding=function(t){var i;return Object.defineProperty(t,"store",{value:this.store}),{name:null!==(i=(new t).entity)&&void 0!==i?i:e.snakeCase(n.plural(t.name)),model:t}},i.createSchema=function(){var t=this;this.entities.forEach((function(e){t.registerSchema(e)}))},i.registerSchema=function(t){this.store[this.dbConfig.name].hasOwnProperty(t.name)||(this.store[this.dbConfig.name][t.name]=[])},i.setStore=function(t){var e;this.dbConfig=this.generateConfig(t),this.store=((e={})[this.dbConfig.name]={},e)},t.getInstance=function(){return null===this.instance?(this.instance=new t,this.instance):this.instance},i.config=function(t){if(void 0===t&&(t=this.dbConfig),this.initialized)throw new Error("Database already initialized");return this.setStore(t),this},i.register=function(t){this.checkModelTypeMappingCapabilities(t);var e=this.createModelBinding(t);return this.entities.push(e),this},i.start=function(){this.createSchema(),this.initialized=!0},t}();o.instance=null;var s=o.getInstance(),a={autoincrement:!0},c={nullable:!0,default:""};exports.Attr=function(){function t(){}return t.generate=function(t,e){return{data:t,IdAttrTypeData:e}},t.id=function(t){return void 0===t&&(t=a),this.generate(t,a)},t.string=function(t){return void 0===t&&(t=c),this.generate(t,c)},t}(),exports.DB=s,exports.Model=r; //# sourceMappingURL=dataorm.cjs.production.min.js.map