Skip to content

Commit

Permalink
Schem items can now be readOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed Nov 28, 2016
1 parent 806df30 commit 76febdb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
14 changes: 13 additions & 1 deletion src/definitions/custom/modepress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>;

/**
* 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<T>;

/**
* Gets if this item is indexable by mongodb
Expand Down
14 changes: 13 additions & 1 deletion src/definitions/generated/modepress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>;

/**
* 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<T>;

/**
* Gets if this item is indexable by mongodb
Expand Down
16 changes: 8 additions & 8 deletions src/models/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Modepress.IModelEntry> ): Promise<number> {
let foreignModel: Model;
const optionalDependencies = instance.dbEntry._optionalDependencies;
Expand Down Expand Up @@ -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<UpdateRequest<T>>} An array of objects that contains the field error and instance. Error is false if nothing
* @returns {Promise<UpdateRequest<T>>} 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<T>( selector: any, data: T ): Promise<UpdateRequest<T>> {
const toRet: UpdateRequest<T> = {
error: false,
Expand All @@ -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
Expand Down Expand Up @@ -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 );

Expand Down
17 changes: 17 additions & 0 deletions src/models/schema-items/schema-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class SchemaItem<T> {
private _indexable: boolean;
private _required: boolean;
private _modified: boolean;
private _readOnly: boolean;

constructor( name: string, value: T ) {
this.name = name;
Expand All @@ -22,6 +23,7 @@ export class SchemaItem<T> {
this._indexable = false;
this._required = false;
this._modified = false;
this._readOnly = false;
}

/**
Expand Down Expand Up @@ -63,6 +65,21 @@ export class SchemaItem<T> {
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<T> {
this._readOnly = val;
return this;
}

/**
* Gets if this item represents a unique value in the database. An example might be a username
*/
Expand Down
5 changes: 3 additions & 2 deletions src/models/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );
}
}
Expand Down

0 comments on commit 76febdb

Please sign in to comment.