UNPKG

@fuadop/react-native-store

Version:

A simple database base on react-native AsyncStorage.

188 lines (142 loc) 7.84 kB
'use strict'; var _asyncStorage = _interopRequireDefault(require("@react-native-async-storage/async-storage")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } 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, 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; } var Model = require('./model.js'); var Util = require('./util.js'); var Store = /*#__PURE__*/function () { function Store(opts) { _classCallCheck(this, Store); this.dbName = opts.dbName; } _createClass(Store, [{ key: "_getCurrentVersion", value: function () { var _getCurrentVersion2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(versionKey) { var currentVersion; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return _asyncStorage["default"].getItem(versionKey); case 2: currentVersion = _context.sent; currentVersion = currentVersion || 0; return _context.abrupt("return", parseFloat(currentVersion)); case 5: case "end": return _context.stop(); } } }, _callee); })); function _getCurrentVersion(_x) { return _getCurrentVersion2.apply(this, arguments); } return _getCurrentVersion; }() }, { key: "migrate", value: function () { var _migrate = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { var migrations, versionKey, currentVersion, target, _iterator, _step, migration; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: migrations = require('./migrations.js'); versionKey = "".concat(this.dbName, "_version"); _context2.next = 4; return this._getCurrentVersion(versionKey); case 4: currentVersion = _context2.sent; target = migrations.slice(-1)[0]; if (!(currentVersion == target.version)) { _context2.next = 8; break; } return _context2.abrupt("return"); case 8: _iterator = _createForOfIteratorHelper(migrations); _context2.prev = 9; _iterator.s(); case 11: if ((_step = _iterator.n()).done) { _context2.next = 20; break; } migration = _step.value; if (!(migration.version <= currentVersion)) { _context2.next = 15; break; } return _context2.abrupt("continue", 18); case 15: migration.perform(); _context2.next = 18; return _asyncStorage["default"].setItem(versionKey, migration.version.toString()); case 18: _context2.next = 11; break; case 20: _context2.next = 25; break; case 22: _context2.prev = 22; _context2.t0 = _context2["catch"](9); _iterator.e(_context2.t0); case 25: _context2.prev = 25; _iterator.f(); return _context2.finish(25); case 28: case "end": return _context2.stop(); } } }, _callee2, this, [[9, 22, 25, 28]]); })); function migrate() { return _migrate.apply(this, arguments); } return migrate; }() }, { key: "model", value: function model(modelName) { return new Model(modelName, this.dbName); } // clear store }, { key: "clear", value: function () { var _clear = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return _asyncStorage["default"].removeItem(this.dbName); case 2: case "end": return _context3.stop(); } } }, _callee3, this); })); function clear() { return _clear.apply(this, arguments); } return clear; }() }]); return Store; }(); module.exports = Store; // Store.model("user").get({ id:1 },{fite}).then().fail();