Skip to content

Instantly share code, notes, and snippets.

@miere
Created December 7, 2020 01:00
Show Gist options
  • Save miere/4299a8f5122f428d363cf848d3d64655 to your computer and use it in GitHub Desktop.
Save miere/4299a8f5122f428d363cf848d3d64655 to your computer and use it in GitHub Desktop.

Meta-Programming Challenge

This is an experiment to practice Meta-Programming skills in JavaScript.

Acceptance Criteria

  • Create a generic Repository implementation (from the Repository Pattern)
  • It should be able to 'create', 'update' or 'delete' objects from a MySQL database
  • It should not rely on any other parameter aside of the persisted object itself (as an ORM might do).

Sample Code

let conn = createConnection({
    connUrl: "mysql:localhost..."
})

let repository = new Repository('Users', conn); // {
//     id: 'INTEGER',
//     name: 'VARCHAR'    
// }

// Table called 'Users' on the database
let User = {}
User.prototype = {
    id: null,
    name: null
}

let user = new User()
user.id = 1
user.name = 'Mark'

repository.create(user) // new entry into the DB

user.name = 'Miere'

repository.update(user) // update name to be Miere instead of Mark

repository.delete(user) // this entry from the DB will be deleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment