-
Notifications
You must be signed in to change notification settings - Fork 0
Database Drivers
A database driver allows you to interface with a database while still supporting the model functionality. We also offer an interface to build your own custom drivers to interface with more outside of the built-in ones.
The currently built in providers are MongoDB
and MySQL
, built in providers are defined by importing Drivers
from the rewyre
package and using the correct property in your database definition.
To define a database item, while creating your app you need to define an array.
import { Framework, Drivers } from 'rewyre';
const framework = new Framework({
port: 8080,
database: true, // Enable the driver.
databases: [
{
unique: 'main',
host: 'localhost',
port: 27017,
name: 'some-database-name',
driver: Drivers.MONGO,
default: true,
},
],
});
Now we have a valid database defined, we have given it a unique name (this is for non-default databases) and also set it to our default, so any models we do not define the database, will fallback to using this instance.
Now we are going to add a new database for MySQL.
import { Framework, Drivers } from 'rewyre';
const framework = new Framework({
port: 8080,
database: true, // Enable the driver.
databases: [
{
unique: 'main',
host: 'localhost',
port: 27017,
name: 'some-database-name',
driver: Drivers.MONGO,
default: true,
},
{
unique: 'legacy',
host: 'localhost',
port: 3306,
name: 'another-database-name',
driver: Drivers.MYSQL,
},
],
});
This is great we now have two databases, now any models we define using the normal methods (not defining a database) will only use the Mongo config, so to add a new model that uses our new legacy
config we can do the following.
import { Model, AbstractModel } from 'rewyre';
@Model('legacy_users', 'user', {
username: 'string',
password: 'string',
is_blocked: 'boolean',
}, 'legacy')
export class LegacyUsersModel extends AbstractModel {}
Now we have created a new modal, and then the last parameter to the decorator is the unique name of the database config. This means that when using that model, it will use that config instead of the default.
Now let's say we have a unique database, or maybe it's an API endpoint instead? Well either way we can interface with it, in the example we are going to pretend it's an API endpoint for simplicity.
To make our custom driver we create a new file like the following (I shall default all required methods).
import { Driver, IDatabaseDriver, IDatabaseItem } from 'rewyre';
@Driver('legacy_api')
export class LegacyApiDatabaseDriver implements IDatabaseDriver {
// Define an engine (this is usually what powers this driver, mysql, mongo, fetch API, etc.)
public engine = 'fetch';
// Define the constructor and then assign the details (this contains the database details from the config).
public constructor(protected details: IDatabaseItem) {}
// All methods that require you to implement.
public async findOne(collection: string, query: any, options?: any): Promise<any> {}
public async find(collection: string, query: any, options?: any): Promise<any[]> {}
public async count(collection: string, query: any): Promise<number> {}
public async insertOne(collection: string, record: any): Promise<string | number> {}
public async insertMany(collection: string, records: Array<any>): Promise<string[] | number[]> {}
public async updateOne(collection: string, query: any, update: any): Promise<boolean> {}
public async updateMany(collection: string, query: any, update: any): Promise<boolean> {}
public async deleteOne(collection: string, query: any): Promise<boolean> {}
public async deleteMany(collection: string, query: any): Promise<boolean> {}
// Get the instance, this is a required method, but you can simply return null if no instance (like API).
public getInstance(): any {
return null;
}
}
As you can see this is pretty simple to do, you just need to cater for the above and it will work, so then the next step is registering this driver (you can of course do anything you want in those methods - just make sure they return the correct data).
To register our custom driver.
import { Framework, Drivers } from 'rewyre';
import { LegacyApiDatabaseDriver } from './driver/legacy-api.ts';
const framework = new Framework({
port: 8080,
database: true, // Enable the driver.
databases: [
{
unique: 'main',
host: 'localhost',
port: 27017,
name: 'some-database-name',
driver: Drivers.MONGO,
default: true,
},
{
unique: 'legacy',
host: 'localhost',
port: 3306,
name: 'another-database-name',
driver: Drivers.MYSQL,
},
{
unique: 'api_data',
driver: 'legacy_api', // The name given in the driver decorator.
customDriver: true, // Tells the framework it is a custom driver.
},
],
});
// Regiser it like any other module.
framework.register([ LegacyApiDatabaseDriver ]);