Skip to content
Danny edited this page May 26, 2021 · 6 revisions

Feature: Models

This section will describe building a model for the people object.



1. Create the structure.
The below will create a new folder: ./src/model and then add a file: ./src/model/people.ts.

mkdir ./src/model
touch ./src/model/people.ts



2. Create the model code.
The below is an example of a model and expects you add the code into the ./src/model/people.ts file.

import { Model, AbstractModel } from 'rewyre';

@Model('people', 'general', {
	id: 'string',
	name: 'string',
	email: 'string',
	position: 'string',
})
export class PeopleModel extends AbstractModel {}

As you can see the extended class contains a series of pre-defined sets of data, so if you want to add more control, you can create new functions (or overriding) in the above class.

You can access the mongo collection using: this.collection inside any method inside the above class.



3. Inject the model to our controller.
The below code will amend the controller we made in the previous example to support the model instead, you will see we have removed the people object.

// We have added a new import from rewyre for the ModelRecordID which is mapped to ObjectID from MongoDB
// and then we have imported the model from people for type hinting.
import { Controller, Route, AbstractController, IReturn, IContext } from 'rewyre';
import { PeopleModel } from '../model/people';

// Here we are injecting the people model, this can also be an array of many injects.
@Inject('people')
@Controller('/', 'people')
export class PeopleController extends AbstractController {

	// We are defining the model and then using the ! to say we know it will definitely be that
	// and be available on use of this class.
	private people!: PeopleModel;

	@Route('GET', '/')
	public async list(context: IContext): Promise<IReturn> {
		const people: any = this.people.find({});
		return { status: 200, content: people };
	}

	@Route('POST', '/')
	public async create(context: IContext): Promise<IReturn> {
		await this.people.insertOne(context.body);
		return { status: 201 };
	}

	@Route('DELETE', '/:id')
	public async delete(context: IContext): Promise<IReturn> {
		await this.people.deleteOne({ _id: new ModelRecordID(context.params.id) });
		return { status: 204 };
	}

	@Route('GET', '/:id')
	public async get(context: IContext): Promise<IReturn> {
		const person: any = await this.tasks.findOne({ _id: new ModelRecordID(context.params.id) });
		return { status: 200, content: person };
	}
}



4. Register the model.
So we have a model, but we have not registered it, so let's go back to our ./src/application.ts file and register it.

import { Framework } from 'rewyre';
import { PeopleController } from '../controller/people';

// Import the model.
import { PeopleModel } from '../model/people';

(async () => {

	const application: Framework = new Framework({
		port: 8080,
		database: true,
		databases: [
			{
				unique: 'main',
				host: 'localhost',
				port: 27017,
				name: 'demo-rewyre-app',
				driver: Drivers.MONGO,
				default: true,
			},
		],
	});

	// Register the class.
	application.register([ PeopleController, PeopleModel ]);

	await application.start();
});



Using Other/Custom Drivers.

To use a custom or non-default driver, the fourth parameter should be the unique name from the config, example:

@Model('people', 'general', {
	id: 'string',
	name: 'string',
	email: 'string',
	position: 'string',
}, 'my_custom_driver')
export class PeopleModel extends AbstractModel {}

Useful Links:

Clone this wiki locally