Skip to content

Instantly share code, notes, and snippets.

@spion
Forked from whoeverest/table.ts
Created November 6, 2015 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spion/73d340b118b6529cc7a7 to your computer and use it in GitHub Desktop.
Save spion/73d340b118b6529cc7a7 to your computer and use it in GitHub Desktop.
/// <reference path="anydb-sql.d.ts" />
import anydbsql = require('anydb-sql');
var db = anydbsql({
url: 'postgres://user:pass@host:port/database',
connections: { min: 2, max: 20 }
});
// Table Post
interface Post {
content: string;
userId: string;
date: string;
}
interface PostTable extends anydbsql.Table<Post> {
content: anydbsql.Column<string>;
userId: anydbsql.Column<string>;
date: anydbsql.Column<string>;
}
var post = <PostTable>db.define<Post>({
name: 'posts',
columns: {
content: {},
userId: {},
date: {}
}
});
// Table User
interface User {
id: string;
email: string;
password: string;
name: string;
}
interface UserTable extends anydbsql.Table<User> {
id: anydbsql.Column<string>;
email: anydbsql.Column<string>;
password: anydbsql.Column<string>;
name: anydbsql.Column<string>;
}
var user = <UserTable>db.define<User>({
name: 'users',
columns: {
id: { primaryKey: true },
email: {},
password: {},
name: {},
date: {}
},
has: {
posts: { from: 'posts', many: true },
group: { from: 'groups'}
}
});
user.select(user.name, post.content)
.from(user.join(post).on(user.id.equals(post.userId)))
.where(post.date.gt('123'))
.all(function(err: string, userposts: string) {
// res[0].name and res[0].content
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment