Last active
September 29, 2016 17:04
-
-
Save cevek/838282e90694ab3be67049315c758f51 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
interface FindParams { | |
attrs?: string; | |
sql?: string; | |
where?: any; | |
model?: DAO<any>; | |
include?: (DAO<any> | FindParams)[]; | |
values?: any[], | |
orderBy?: string; | |
} | |
interface DAOWithParams { | |
dao: DAO<any>; | |
params: FindParams; | |
} | |
interface FindParamsWithMap<T> extends FindParams { | |
map: {[name: string]: DAO<any>}; | |
} | |
abstract class DAO<T> { | |
model: new () => T; | |
findAll(arg?: FindParams): T[] { | |
return null as any; | |
} | |
findAllWithRelationsMap<Map>(params?: FindParamsWithMap<Map>): {items: T[]; map: Map} { | |
return null as any; | |
} | |
create(item: T) { | |
return null as any; | |
} | |
} | |
function inject<T>(val: new() => T): T { | |
return new val(); | |
} | |
interface F { | |
name: string; | |
sdfa: string; | |
} | |
class Relation { | |
name: string; | |
type: number; | |
} | |
interface ForeignKey<T> { | |
name: string; | |
type: number; | |
model: any; | |
xxx: T; | |
} | |
type Field<T> = T | (F & string & number & Date); | |
type FKey<T> = ForeignKey<T> | number; | |
function field<T>(): Field<number> { | |
return null as any; | |
} | |
function fields<T>(f: new ()=>T): T { | |
return null as any; | |
} | |
class DBEntity { | |
protected static belongsTo<T>(to: new ()=>T, foreignKey: Field<any>): T { | |
return null as any; | |
} | |
protected static hasOne<T>(what: new ()=>T): T { | |
return null as any; | |
} | |
protected static hasMany<T>(what: new ()=>T): T[] { | |
return null as any; | |
} | |
protected static int<T>(): Field<number> { | |
return null as any; | |
} | |
protected static varchar<T>(): Field<string> { | |
return null as any; | |
} | |
} | |
interface FFF<T> { | |
new (): T; | |
DAO?: number; | |
} | |
namespace D { | |
export function checkRelations<T>(model: new ()=>T) { | |
} | |
export function belongsTo<T>(foreignKey: FKey<T>): T { | |
return null as any; | |
} | |
export function hasOne<T>(what: new ()=>T): T { | |
return null as any; | |
} | |
export function hasMany<T>(what: new ()=>T): T[] { | |
return null as any; | |
} | |
export function int<T>(): Field<number> { | |
return null as any; | |
} | |
export function varchar<T>(): Field<string> { | |
return null as any; | |
} | |
export function foreignKey<T>(to: new () => T): FKey<T> { | |
return null as any; | |
} | |
export function primaryKey(): Field<number> { | |
return null as any; | |
} | |
export function date(): Field<Date> { | |
return null as any; | |
} | |
} | |
class Post { | |
id = D.primaryKey(); | |
videoId = D.foreignKey(MediaFile); | |
audioId = D.foreignKey(MediaFile); | |
video? = D.belongsTo(this.id); | |
audio? = D.belongsTo(this.audioId); | |
lines? = D.hasMany(Line); | |
// static _ = fields(Post); | |
} | |
D.checkRelations(Post); | |
class PostDAO extends DAO<Post> { | |
model = Post; | |
} | |
class Line { | |
id = D.primaryKey(); | |
postId = D.foreignKey(Post); | |
enId = D.foreignKey(TextLine); | |
ruId = D.foreignKey(TextLine); | |
speakerId = D.foreignKey(Speaker); | |
en? = D.belongsTo(this.enId); | |
ru? = D.belongsTo(this.ruId); | |
post? = D.belongsTo(this.postId); | |
speaker? = D.belongsTo(this.speakerId); | |
} | |
D.checkRelations(Line); | |
class LineDAO extends DAO<Line> { | |
model = Line; | |
} | |
class TextLine { | |
id = D.primaryKey(); | |
postId = D.foreignKey(Post); | |
text = D.varchar(); | |
post? = D.belongsTo(this.postId); | |
line? = D.hasOne(Line); | |
} | |
D.checkRelations(TextLine); | |
class TextLineDAO extends DAO<TextLine> { | |
model = TextLine; | |
} | |
class MediaFile { | |
id = D.primaryKey(); | |
url = D.varchar(); | |
createdAt = D.date(); | |
} | |
D.checkRelations(MediaFile); | |
class MediaFileDAO extends DAO<MediaFile> { | |
model = MediaFile; | |
} | |
class Speaker { | |
id = D.primaryKey(); | |
photoId = D.foreignKey(MediaFile); | |
createdAt = D.date(); | |
photo? = D.belongsTo(this.photoId); | |
} | |
D.checkRelations(Speaker); | |
class SpeakerDAO extends DAO<Speaker> { | |
model = Speaker; | |
} | |
const Posts = inject(PostDAO); | |
const Lines = inject(LineDAO); | |
const TextLines = inject(TextLineDAO); | |
const MediaFiles = inject(MediaFileDAO); | |
const Speakers = inject(SpeakerDAO); | |
const posts = Posts.findAll(); | |
posts.map(post => post.videoId); | |
Posts.create({ | |
id: 1, | |
videoId: 1, | |
audioId: 1, | |
}); | |
MediaFiles.findAll().map(f => f.url.bold()); | |
MediaFiles.create({ | |
id: 1, | |
url: "asdf", | |
createdAt: new Date() | |
}); | |
const fileId = 1; | |
MediaFiles.findAll({where: 'userId=?', values: [fileId], include: [MediaFiles, Lines]}); | |
// MediaFiles.findAll({where: MediaFile.userId.equal(fileId), values: [fileId], include: [MediaFile, Line]}); | |
MediaFiles.findAll({where: {userId: fileId}, include: [MediaFiles, Lines]}); | |
MediaFiles.findAll({where: ['userId=?', fileId], include: [MediaFiles, Lines]}); | |
Posts.findAll({include: [MediaFiles, Lines]}); | |
Posts.findAllWithRelationsMap({ | |
include: [Lines, TextLines, Speakers, MediaFiles], | |
map: { | |
files: Speakers | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment