diff --git a/src/definitions/custom/modepress.d.ts b/src/definitions/custom/modepress.d.ts index afd5df20..0eb6207a 100644 --- a/src/definitions/custom/modepress.d.ts +++ b/src/definitions/custom/modepress.d.ts @@ -360,7 +360,19 @@ /** * Sets if this item is required. This will throw an error on the item if the value is not set before validation */ - public setRequired( val: boolean ); + public setRequired( val: boolean ): SchemaItem; + + /** + * Gets if this item is read only. If true, then the value can only be set when the item is created + * and any future updates are ignored + */ + public getReadOnly(): boolean; + + /** + * Sets if this item is required. If true, then the value can only be set when the item is created + * and any future updates are ignored + */ + public setReadOnly( val: boolean ): SchemaItem; /** * Gets if this item is indexable by mongodb diff --git a/src/definitions/generated/modepress.d.ts b/src/definitions/generated/modepress.d.ts index e852ce68..b639ee19 100644 --- a/src/definitions/generated/modepress.d.ts +++ b/src/definitions/generated/modepress.d.ts @@ -360,7 +360,19 @@ declare namespace Modepress { /** * Sets if this item is required. This will throw an error on the item if the value is not set before validation */ - public setRequired( val: boolean ); + public setRequired( val: boolean ): SchemaItem; + + /** + * Gets if this item is read only. If true, then the value can only be set when the item is created + * and any future updates are ignored + */ + public getReadOnly(): boolean; + + /** + * Sets if this item is required. If true, then the value can only be set when the item is created + * and any future updates are ignored + */ + public setReadOnly( val: boolean ): SchemaItem; /** * Gets if this item is indexable by mongodb diff --git a/src/models/model.ts b/src/models/model.ts index 00600c7a..8a9053c6 100644 --- a/src/models/model.ts +++ b/src/models/model.ts @@ -229,8 +229,8 @@ export abstract class Model { } /** - * Deletes a instance and all its dependencies are updated or deleted accordingly - */ + * Deletes a instance and all its dependencies are updated or deleted accordingly + */ private async deleteInstance( instance: ModelInstance ): Promise { let foreignModel: Model; const optionalDependencies = instance.dbEntry._optionalDependencies; @@ -305,14 +305,14 @@ export abstract class Model { } /** - * Updates a selection of instances. The update process will fetch all instances, validate the new data and check that + * Updates a selection of instances. The update process will fetch all instances, validate the new data and check that * unique fields are still being respected. An array is returned of each instance along with an error string if anything went wrong * with updating the specific instance. - * @param selector The selector for updating instances + * @param selector The selector for updating instances * @param data The data object that will attempt to set the instance's schema variables - * @returns {Promise>} An array of objects that contains the field error and instance. Error is false if nothing + * @returns {Promise>} An array of objects that contains the field error and instance. Error is false if nothing * went wrong when updating the specific instance, and a string message if something did in fact go wrong - */ + */ async update( selector: any, data: T ): Promise> { const toRet: UpdateRequest = { error: false, @@ -329,7 +329,7 @@ export abstract class Model { // If we have data, then set the variables if ( data ) - instance.schema.set( data ); + instance.schema.set( data, false ); try { // Make sure the new updates are valid @@ -409,7 +409,7 @@ export abstract class Model { // If we have data, then set the variables if ( data ) - newInstance.schema.set( data ); + newInstance.schema.set( data, true ); const unique = await this.checkUniqueness( newInstance ); diff --git a/src/models/schema-items/schema-item.ts b/src/models/schema-items/schema-item.ts index 4afd2947..db334539 100644 --- a/src/models/schema-items/schema-item.ts +++ b/src/models/schema-items/schema-item.ts @@ -12,6 +12,7 @@ export class SchemaItem { private _indexable: boolean; private _required: boolean; private _modified: boolean; + private _readOnly: boolean; constructor( name: string, value: T ) { this.name = name; @@ -22,6 +23,7 @@ export class SchemaItem { this._indexable = false; this._required = false; this._modified = false; + this._readOnly = false; } /** @@ -63,6 +65,21 @@ export class SchemaItem { return this; } + /** + * Gets if this item is read only. If true, then the value can only be set when the item is created + * and any future updates are ignored + */ + public getReadOnly(): boolean { return this._readOnly; } + + /** + * Sets if this item is required. If true, then the value can only be set when the item is created + * and any future updates are ignored + */ + public setReadOnly( val: boolean ): SchemaItem { + this._readOnly = val; + return this; + } + /** * Gets if this item represents a unique value in the database. An example might be a username */ diff --git a/src/models/schema.ts b/src/models/schema.ts index 1c509517..d8dd92c2 100644 --- a/src/models/schema.ts +++ b/src/models/schema.ts @@ -28,14 +28,15 @@ export class Schema { /** * Sets a schema value by name * @param data The data object we are setting + * @param allowReadOnlyValues If true, then readonly values can be overwritten (Usually the case when the item is first created) */ - set( data: any ) { + set( data: any, allowReadOnlyValues: boolean ) { const items = this._items, l = items.length; for ( const i in data ) { for ( let ii = 0; ii < l; ii++ ) - if ( items[ ii ].name === i ) + if ( items[ ii ].name === i && ( items[ ii ].getReadOnly() === false || allowReadOnlyValues ) ) items[ ii ].setValue( data[ i ] ); } }