|
|
|
@ -1,3 +1,7 @@
|
|
|
|
|
/**
|
|
|
|
|
* @module flitter-orm/src/model/Model
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const { Injectable } = require('flitter-di')
|
|
|
|
|
const Schema = require('../schema/Schema')
|
|
|
|
|
const Filter = require('../filter/Filter')
|
|
|
|
@ -6,6 +10,7 @@ const Scope = require('./Scope')
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The base model class. All model implementations should extend from this.
|
|
|
|
|
* @extends module:flitter-di/src/Injectable~Injectable
|
|
|
|
|
*/
|
|
|
|
|
class Model extends Injectable {
|
|
|
|
|
|
|
|
|
@ -28,20 +33,20 @@ class Model extends Injectable {
|
|
|
|
|
/**
|
|
|
|
|
* The holding variable for the instantiated schema.
|
|
|
|
|
* @private
|
|
|
|
|
* @type {Schema|boolean}
|
|
|
|
|
* @type {module:flitter-orm/src/schema/Schema~Schema|boolean}
|
|
|
|
|
*/
|
|
|
|
|
static __schema_instance = false
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Array of instantiated references to Scopes that should be applied
|
|
|
|
|
* to this model.
|
|
|
|
|
* @type {Array<Scope>}
|
|
|
|
|
* @type {Array<module:flitter-orm/src/model/Scope~Scope>}
|
|
|
|
|
*/
|
|
|
|
|
static scopes = []
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The instantiated schema for this model.
|
|
|
|
|
* @returns {Schema}
|
|
|
|
|
* @returns {module:flitter-orm/src/schema/Schema~Schema}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
static get __schema() {
|
|
|
|
@ -76,7 +81,7 @@ class Model extends Injectable {
|
|
|
|
|
*
|
|
|
|
|
* @param {object} [filter = {}] - the query filters for the cursor
|
|
|
|
|
* @param {object} [opts = {}] - optional settings
|
|
|
|
|
* @returns {Cursor}
|
|
|
|
|
* @returns {mongodb/Cursor}
|
|
|
|
|
*/
|
|
|
|
|
static async cursor(filter = {}, opts = {}) {
|
|
|
|
|
filter = (await this.filter()).absorb(filter).write()
|
|
|
|
@ -91,7 +96,7 @@ class Model extends Injectable {
|
|
|
|
|
*
|
|
|
|
|
* @param {object} [filter = {}] - the query filters for the cursor
|
|
|
|
|
* @param {object} [opts = {}] - optional settings
|
|
|
|
|
* @returns {Promise<Array<Model>>}
|
|
|
|
|
* @returns {Promise<Array<module:flitter-orm/src/model/Model~Model>>}
|
|
|
|
|
*/
|
|
|
|
|
static async find(filter, opts) {
|
|
|
|
|
const cursor = await this.cursor(filter, opts)
|
|
|
|
@ -112,7 +117,7 @@ class Model extends Injectable {
|
|
|
|
|
*
|
|
|
|
|
* @param {string|ObjectId} id - the ID of the model to query
|
|
|
|
|
* @param {object} [opts = {}] - optional settings
|
|
|
|
|
* @returns {Promise<Model>}
|
|
|
|
|
* @returns {Promise<Array<module:flitter-orm/src/model/Model~Model>>}
|
|
|
|
|
*/
|
|
|
|
|
static async findById(id, opts) {
|
|
|
|
|
if ( !(id instanceof ObjectId) ) {
|
|
|
|
@ -133,7 +138,7 @@ class Model extends Injectable {
|
|
|
|
|
*
|
|
|
|
|
* @param {object} [filter = {}] - the query filters for the cursor
|
|
|
|
|
* @param {object} [opts = {}] - optional settings
|
|
|
|
|
* @returns {Promise<Model>}
|
|
|
|
|
* @returns {Promise<Array<module:flitter-orm/src/model/Model~Model>>}
|
|
|
|
|
*/
|
|
|
|
|
static async findOne(filter, opts) {
|
|
|
|
|
const cursor = await this.cursor(filter, opts)
|
|
|
|
@ -191,7 +196,7 @@ class Model extends Injectable {
|
|
|
|
|
/**
|
|
|
|
|
* Create a new programmatic filter for this class,
|
|
|
|
|
* pre-loaded with any scopes.
|
|
|
|
|
* @returns {Filter}
|
|
|
|
|
* @returns {module:flitter-orm/src/filter/Filter~Filter}
|
|
|
|
|
*/
|
|
|
|
|
static async filter() {
|
|
|
|
|
let filter = new Filter(this)
|
|
|
|
@ -229,7 +234,7 @@ class Model extends Injectable {
|
|
|
|
|
* in the schema for this model, and, in so doing, will cast those values and fill in
|
|
|
|
|
* the specified defaults. These changes will be added to this instance after the save.
|
|
|
|
|
*
|
|
|
|
|
* @returns {Promise<Model>} - the current instance with updated properties
|
|
|
|
|
* @returns {Promise<module:flitter-orm/src/model/Model~Model>} - the current instance with updated properties
|
|
|
|
|
*/
|
|
|
|
|
async save() {
|
|
|
|
|
const schema = this.constructor.__schema
|
|
|
|
@ -267,7 +272,7 @@ class Model extends Injectable {
|
|
|
|
|
* specified property and immediately save the record.
|
|
|
|
|
* @param {string} field
|
|
|
|
|
* @param {*} value
|
|
|
|
|
* @returns {Promise<Model>}
|
|
|
|
|
* @returns {Promise<module:flitter-orm/src/model/Model~Model>}
|
|
|
|
|
*/
|
|
|
|
|
async set(field, value) {
|
|
|
|
|
this[field] = value
|
|
|
|
@ -278,7 +283,7 @@ class Model extends Injectable {
|
|
|
|
|
* Delete the current instance of this model from the database.
|
|
|
|
|
* This will remove the model's ID from this instance. Other properties
|
|
|
|
|
* will remain unchanged.
|
|
|
|
|
* @returns {Promise<Model>}
|
|
|
|
|
* @returns {Promise<module:flitter-orm/src/model/Model~Model>}
|
|
|
|
|
*/
|
|
|
|
|
async delete() {
|
|
|
|
|
if ( this._id ) {
|
|
|
|
@ -361,6 +366,11 @@ class Model extends Injectable {
|
|
|
|
|
return current_object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @deprecated
|
|
|
|
|
* @returns {Promise<Object>}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
async __db_object() {
|
|
|
|
|
const schema = this.constructor.__schema
|
|
|
|
|
const shallow_object = {}
|
|
|
|
@ -394,7 +404,7 @@ class Model extends Injectable {
|
|
|
|
|
* Normally, this would be automatically returned by a named method
|
|
|
|
|
* on the sub-class that implements the relationship.
|
|
|
|
|
*
|
|
|
|
|
* @param {Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {module:flitter-orm/src/model/Model~Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {string} local_key - local key of the field to match
|
|
|
|
|
* @param {string} [foreign_key = ''] - foreign key of the field to match (if none provided, assume the same as local_key)
|
|
|
|
|
* @returns {Promise<Model|undefined>} - the matching model instance
|
|
|
|
@ -412,10 +422,10 @@ class Model extends Injectable {
|
|
|
|
|
* Normally, this would be automatically returned by a named method
|
|
|
|
|
* on the sub-class that implements the relationship.
|
|
|
|
|
*
|
|
|
|
|
* @param {Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {module:flitter-orm/src/model/Model~Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {string} local_key - local key of the field to match
|
|
|
|
|
* @param {string} [foreign_key = ''] - foreign key of the field to match (if none provided, assume the same as local_key)
|
|
|
|
|
* @returns {Promise<Model|undefined>} - the matching model instance
|
|
|
|
|
* @returns {Promise<module:flitter-orm/src/model/Model~Model|undefined>} - the matching model instance
|
|
|
|
|
*/
|
|
|
|
|
async belongs_to_one(OtherModel, local_key, foreign_key = '') {
|
|
|
|
|
return this.has_one(OtherModel, local_key, foreign_key)
|
|
|
|
@ -428,10 +438,10 @@ class Model extends Injectable {
|
|
|
|
|
* Normally, this would be automatically returned by a named method
|
|
|
|
|
* on the sub-class that implements the relationship.
|
|
|
|
|
*
|
|
|
|
|
* @param {Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {module:flitter-orm/src/model/Model~Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {string} local_key - local key of the field to match
|
|
|
|
|
* @param {string} [foreign_key = ''] - foreign key of the field to match (if none provided, assume the same as local_key)
|
|
|
|
|
* @returns {Promise<Array<Model>>} - the matching model instances
|
|
|
|
|
* @returns {Promise<Array<module:flitter-orm/src/model/Model~Model>>} - the matching model instances
|
|
|
|
|
*/
|
|
|
|
|
async has_many(OtherModel, local_key, foreign_key = '') {
|
|
|
|
|
if ( !foreign_key ) foreign_key = local_key
|
|
|
|
@ -446,10 +456,10 @@ class Model extends Injectable {
|
|
|
|
|
* Normally, this would be automatically returned by a named method
|
|
|
|
|
* on the sub-class that implements the relationship.
|
|
|
|
|
*
|
|
|
|
|
* @param {Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {module:flitter-orm/src/model/Model~Model} OtherModel - static class of the other model
|
|
|
|
|
* @param {string} local_key - local key of the field to match
|
|
|
|
|
* @param {string} [foreign_key = ''] - foreign key of the field to match (if none provided, assume the same as local_key)
|
|
|
|
|
* @returns {Promise<Array<Model>>} - the matching model instances
|
|
|
|
|
* @returns {Promise<Array<module:flitter-orm/src/model/Model~Model>>} - the matching model instances
|
|
|
|
|
*/
|
|
|
|
|
async belongs_to_many(OtherModel, local_key, foreign_key = '') {
|
|
|
|
|
return this.has_many(OtherModel, local_key, foreign_key)
|
|
|
|
|