RanvierMUD / core

Core engine code for Ranvier
https://ranviermud.com
MIT License
45 stars 41 forks source link

datasources could support an init function (for databases) #115

Open ratacat opened 4 years ago

ratacat commented 4 years ago

So I've gone through and experimented with setting up a sqlite datasource by @coderintherye, and one thing that I was struggling with is a way to initialize the same datasource into different entityLoaders. For example, currently if you wanted to use the same sqlite datasource for players and accounts, you'd have to write two separate datasources(with their own constructors)...or put your initialize calls into the fetch/update functions which doesn't seem ideal.

The init() function needs to be separate from the constructor, because the constructor is called when the datasource is created, not when the entityLoader is created.

I added a few lines of code into EntityLoader.js to allow each EntityLoader to make a call to an init() function in the datasource which would obliviate this need.

you could add this method and call it in the constructor

  init(){
    if (!this.dataSource.init || typeof this.dataSource.init !== "function") return
    return this.dataSource.init(this.config);
  }
  constructor(dataSource, config = {}) {
    this.dataSource = dataSource;
    this.config = config;
    this.init()
  }

Then all you need is an init() method in the DataSource that takes in the entityloader config and initializes the database / tables as needed.

ratacat commented 4 years ago

I'm happy to open a pull request, just thought I'd run this by you guys for input first! @shawncplus @seanohue

seanohue commented 4 years ago

Sorry about the late response to this -- this makes sense to me so far, though I'm going to take a look at the SQLite data source as well. Definitely take a swing at it, esp. since your solution seems like it would be backwards compatible with other datasources.