UNPKG

@avonjs/avonjs

Version:

A fluent Node.js API generator.

76 lines (75 loc) 2.11 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const HasAttributes_1 = __importDefault(require("../Mixins/HasAttributes")); class Fluent extends (0, HasAttributes_1.default)(class { }) { attributes; constructor(attributes = {}) { super(); this.attributes = attributes; // i tested some approach but it have problem with private member assignments // biome-ignore lint/correctness/noConstructorReturn: i don't have any solution for it return new Proxy(this, { get: (parent, property, receiver) => { // handle exists method if (property in parent) { return parent[property]; } return parent.getAttribute(property); }, set: (model, key, value) => { if (key in model) { model[key] = value; } else { model.setAttribute(key, value ?? true); } return true; }, }); } /** * Set the attributes. */ setAttributes(attributes) { for (const key in attributes) { this.setAttribute(key, attributes[key]); } return this; } /** * Set value for the given key. */ setAttribute(key, value) { super.setAttributeValue(key, value); return this; } /** * Get value for the given key. */ getAttribute(key) { return super.getAttributeValue(key); } /** * Get the model key. */ getKey() { return this.getAttribute(this.getKeyName()); } /** * Get primary key name of the model. */ getKeyName() { return 'id'; } /** * Convert attributes to JSON string. */ toJson() { return JSON.stringify(this.getAttributes()); } } exports.default = Fluent;