brianc / node-sql

SQL generation for node.js
MIT License
1.05k stars 191 forks source link

Table definition API that is ES6 and TypeScript friendly #324

Open spion opened 8 years ago

spion commented 8 years ago

Example:

class UserTable extends Table<{id: string; name: string}> {
  id = new Column<string>({primaryKey: true})
  name = new Column<string>();
}

let user = new UserTable(db).withOptions({
  tableName: "users", 
  engine: "innodb", 
  // ... other options
})

The important part being that columns are known as properties to the compiler / typechecker and can subsequently be used when building expressions:

user.where(user.name.eq("test")) // ok since user.name is Column<string>
maxnordlund commented 6 years ago

Yes please, though I suspect just type level generics won't be enough. Also for people like me that use ES2015 classes, but not typescript (i.e. the parts already supported natively by node so no need for transpiling).

class UserTable extends Table {
  static get id { return new Column("int", {primaryKey: true}) }
  static get name { return new Column("int", {default: ""}) }
}

... // Same as above