Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ratacat opened this issue Apr 4, 2020 · 2 comments
Open

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

ratacat opened this issue Apr 4, 2020 · 2 comments

Comments

@ratacat
Copy link
Contributor

ratacat commented Apr 4, 2020

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
Copy link
Contributor Author

ratacat commented Apr 6, 2020

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

@seanohue
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants