nosqlax
Version:
NoSQLax is a modern, lightweight JavaScript Object Document Mapper(ODM) library that makes working with CouchDB a breeze. Inspired by CouchDB’s “Relax” philosophy and the chill vibes of Snorlax, NoSQLax takes the hassle out of managing your data, offering
73 lines (72 loc) • 3.75 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createActiveRecordEntity = createActiveRecordEntity;
exports.createDataMapperEntity = createDataMapperEntity;
const ActiveRecordEntity_1 = __importDefault(require("./ActiveRecordEntity"));
const DataMapperEntity_1 = __importDefault(require("./DataMapperEntity"));
function createEntityBase(name, type, schemaOrSchemaId, config, EntityClass) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const { methods, ajvOptions = {}, fieldMap = {} } = config;
// Dynamically define class
const DynamicEntityClass = (_a = class extends EntityClass {
constructor(data) {
super(data);
}
static initialize() {
const _super = Object.create(null, {
initialize: { get: () => super.initialize }
});
return __awaiter(this, void 0, void 0, function* () {
yield _super.initialize.call(this);
});
}
},
__setFunctionName(_a, "DynamicEntityClass"),
_a.type = type,
_a.schemaOrSchemaId = schemaOrSchemaId,
_a.fieldMap = config.fieldMap || {},
_a.ajvOptions = config.ajvOptions || {},
_a);
// Attach custom static methods
if (methods) {
Object.assign(DynamicEntityClass, methods);
}
Object.defineProperty(DynamicEntityClass, 'name', { value: name });
// Initialize schema, define accessors
yield DynamicEntityClass.initialize();
return DynamicEntityClass;
});
}
function createActiveRecordEntity(name, type, schemaOrSchemaId, dataSource, config) {
return __awaiter(this, void 0, void 0, function* () {
// Explicitly type the EntityClass as typeof ActiveRecordEntity
let EntityClass = yield createEntityBase(name, type, schemaOrSchemaId, config, ActiveRecordEntity_1.default);
// Automatically attach the dataSource with optional ajvOptions
EntityClass['attachDataSource'](dataSource, config.ajvOptions || {});
return EntityClass;
});
}
function createDataMapperEntity(name, type, schemaOrSchemaId, config) {
return __awaiter(this, void 0, void 0, function* () {
// Use the shared base function for creating the class
const EntityClass = yield createEntityBase(name, type, schemaOrSchemaId, config, DataMapperEntity_1.default);
return EntityClass;
});
}