Last active
November 6, 2015 18:00
-
-
Save whoeverest/cdaff0e991eed8b1feab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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