当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript db.table函数代码示例

本文整理汇总了TypeScript中db/db.table函数的典型用法代码示例。如果您正苦于以下问题:TypeScript table函数的具体用法?TypeScript table怎么用?TypeScript table使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了table函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: syncPostToGrapher

export async function syncPostToGrapher(postId: number): Promise<string|undefined> {
    const rows = await wpdb.query("SELECT * FROM wp_posts WHERE ID = ? AND post_status != 'trash'", [postId])

    const matchingRows = await db.table(Post.table).where({ id: postId })
    const existsInGrapher = !!matchingRows.length

    const wpPost = rows[0]
    const postRow = wpPost ? {
        id: wpPost.ID,
        title: wpPost.post_title,
        slug: wpPost.post_name.replace(/__/g, '/'),
        type: wpPost.post_type,
        status: wpPost.post_status,
        content: wpPost.post_content,
        published_at: wpPost.post_date_gmt === "0000-00-00 00:00:00" ? null : wpPost.post_date_gmt,
        updated_at: wpPost.post_modified_gmt === "0000-00-00 00:00:00" ? "1970-01-01 00:00:00" : wpPost.post_modified_gmt    
    } as Post.Row : undefined

    await db.knex().transaction(async t => {
        if (!postRow && existsInGrapher) {
            // Delete from grapher
            await t.table(Post.table).where({ 'id': postId }).delete()
        } else if (postRow && !existsInGrapher) {
            await t.table(Post.table).insert(postRow)
        } else if (postRow && existsInGrapher) {
            await t.table(Post.table).where('id', '=', postRow.id).update(postRow)
        }
    })

    const newPost = (await Post.select('slug').from(db.table(Post.table).where({ id: postId })))[0]
    return newPost ? newPost.slug : undefined
}
开发者ID:OurWorldInData,项目名称:owid-grapher,代码行数:32,代码来源:Post.ts

示例2: toDatapackage

    // Return object representing datapackage.json for this dataset
    async toDatapackage(): Promise<any> {
        // XXX
        const sources = await Source.find({ datasetId: this.id })
        const variables = await db.table(Variable.table).where({ datasetId: this.id }) as Variable.Row[]
        const tags = await db.query(`SELECT t.id, t.name FROM dataset_tags dt JOIN tags t ON t.id=dt.tagId WHERE dt.datasetId=?`, [this.id])

        const initialFields = [
            { name: "Entity", type: "string" },
            { name: "Year", type: "year" }
        ]

        const dataPackage = {
            name: this.name,
            title: this.name,
            id: this.id,
            description: (sources[0] && sources[0].description && sources[0].description.additionalInfo)||"",
            sources: sources.map(s => s.toDatapackage()),
            owidTags: tags.map((t: any) => t.name),
            resources: [{
                path: `${this.name}.csv`,
                schema: {
                    fields: initialFields.concat(variables.map(v => ({
                        name: v.name,
                        type: "any",
                        description: v.description,
                        owidDisplaySettings: v.display
                    })))
                }
            }]
        }

        return dataPackage
    }
开发者ID:OurWorldInData,项目名称:owid-grapher,代码行数:34,代码来源:Dataset.ts

示例3: main

async function main() {
    try {
        const categoriesByPostId = await wpdb.getCategoriesByPostId()

        const tagsByPostId = await wpdb.getTagsByPostId()
        const postRows = await wpdb.query("select * from wp_posts where (post_type='page' or post_type='post') AND post_status != 'trash'")
    
        for (const post of postRows) {
            const categories = categoriesByPostId.get(post.ID)||[]

            let tagNames = categories.map(t => decodeHTML(t))

            const matchingTags = await Tag.select('id', 'name', 'isBulkImport').from(
                db.table(Tag.table).whereIn('name', tagNames).andWhere({ isBulkImport: false })
            )

            const existingTags = await Tag.select('id').from(
                db.table(Tag.table)
                    .join('post_tags', { 'post_tags.tag_id': 'tags.id' })
                    .where({ 'post_tags.post_id': post.ID })
            )

            const tagIds = matchingTags.map(t => t.id).concat(existingTags.map(t => t.id))
            // if (matchingTags.map(t => t.name).includes(post.post_title)) {
            //     tagIds.push(1640)
            // }


            // const matchingTags = await Tag.select('id', 'name', 'isBulkImport').from(
            //     db.knex().from(Tag.table).whereIn('name', tagNames).andWhere({ isBulkImport: false })
            // )
            // let tagIds = matchingTags.map(t => t.id)
            // if (matchingTags.map(t => t.name).includes(post.post_title)) {
            //     tagIds.push(1640)
            // }
            await Post.setTags(post.ID, _.uniq(tagIds))
        }
    } finally {
        await wpdb.end()
        await db.end()
    }
}
开发者ID:OurWorldInData,项目名称:owid-grapher,代码行数:42,代码来源:postCategoriesToGrapher.ts

示例4: bySlug

 export async function bySlug(slug: string): Promise<Post.Row|undefined> {
     return Post.rows(await db.table('posts').where({ slug: slug }))[0]
 }
开发者ID:OurWorldInData,项目名称:owid-grapher,代码行数:3,代码来源:Post.ts

示例5: async

api.get('/posts/:postId.json', async (req: Request, res: Response) => {
    const postId = expectInt(req.params.postId)
    const post = await db.table(Post.table).where({ id: postId }).select('*').first() as Post.Row
    return camelCaseProperties(post)
})
开发者ID:OurWorldInData,项目名称:owid-grapher,代码行数:5,代码来源:api.ts

示例6: getPostTags

async function getPostTags(postId: number) {
    return await db.table("post_tags")
        .select("tags.id", "tags.name")
        .where({ post_id: postId })
        .join("tags", "tags.id", "=", "post_tags.tag_id") as Tag[]
}
开发者ID:OurWorldInData,项目名称:owid-grapher,代码行数:6,代码来源:indexToAlgolia.ts


注:本文中的db/db.table函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。