UNPKG

fewer

Version:

A minimal ORM for Node.js.

90 lines 3.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const sq_1 = __importDefault(require("@fewer/sq")); var AssociationType; (function (AssociationType) { AssociationType["HAS_ONE"] = "hasOne"; AssociationType["HAS_MANY"] = "hasMany"; AssociationType["BELONGS_TO"] = "belongsTo"; })(AssociationType = exports.AssociationType || (exports.AssociationType = {})); // NOTE: We need to stash const BASE_TYPE = Symbol('base-type'); const FK_TYPE = Symbol('fk-type'); const IS_CHAINED = Symbol('is-chanined'); class Association { constructor(type, associate, runningQuery, foreignKey) { this.type = type; this.associate = associate; this.runningQuery = runningQuery; this.foreignKey = foreignKey; } [("@@INTERNAL_TYPE" /* INTERNAL_TYPE */, "@@RESOLVED_TYPE" /* RESOLVED_TYPE */, "@@JOINS" /* JOINS */, "@@SCHEMA_TYPE" /* SCHEMA_TYPE */, "@@TO_SQ_SELECT" /* TO_SQ_SELECT */)]() { return this.selectQuery(); } getTableName() { return this.associate.getTableName(); } pluck(...columns) { return new Association(this.type, this.associate, this.selectQuery().pluck(...columns), this.foreignKey); } pluckAs(name, alias) { return new Association(this.type, this.associate, this.selectQuery().pluck([name, alias]), this.foreignKey); } limit(amount) { return new Association(this.type, this.associate, this.selectQuery().limit(amount), this.foreignKey); } offset(amount) { return new Association(this.type, this.associate, this.selectQuery().offset(amount), this.foreignKey); } order() { throw new Error('not yet implemented'); // return new Association(this.type, this.associate.order()); } where(wheres) { return new Association(this.type, this.associate, this.selectQuery().where(wheres), this.foreignKey); } load(name, association) { let keys; if (association.type === 'belongsTo') { keys = [association.foreignKey, this.associate.primaryKey]; } else { keys = [this.associate.primaryKey, association.foreignKey]; } return new Association(this.type, this.associate, this.selectQuery().load(name, keys, association["@@TO_SQ_SELECT" /* TO_SQ_SELECT */]()), this.foreignKey); } // TODO: fix this join(name, association) { let keys; if (association.type === 'belongsTo') { keys = [association.foreignKey, this.associate.primaryKey]; } else { keys = [this.associate.primaryKey, association.foreignKey]; } return new Association(this.type, this.associate, this.selectQuery().join(name, keys, association.getTableName(), association["@@TO_SQ_SELECT" /* TO_SQ_SELECT */]()), this.foreignKey); } selectQuery() { if (!this.runningQuery) { this.runningQuery = sq_1.default.select(this.associate.getTableName()); } return this.runningQuery; } } exports.Association = Association; function createBelongsTo(associate, foreignKey) { return new Association(AssociationType.BELONGS_TO, associate, undefined, foreignKey); } exports.createBelongsTo = createBelongsTo; function createHasOne(base, associate, foreignKey) { return new Association(AssociationType.HAS_ONE, associate, undefined, foreignKey); } exports.createHasOne = createHasOne; function createHasMany(base, associate, foreignKey) { return new Association(AssociationType.HAS_MANY, associate, undefined, foreignKey); } exports.createHasMany = createHasMany; //# sourceMappingURL=index.js.map