Skip to content

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

@ratacat

Description

@ratacat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions