1.0.4 • Published 5 years ago

sql-qb v1.0.4

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

sql-query-builder

Sql query builder for educational purposes

Select:

    const { rows: user } = await db
        .select({
            username: 'rs-hub'
        })
        .table("users")
        .column(["id", "username"])
        .limit(1)
        .skip(1)
        .query();

    console.log(user); // [ { id: 8, username: 'rs-hub' } ]

Insert:

     const { rows } = await db
         .insert({
                username: 'michael',
            })
         .table('users')
         .returning(['id', 'username'])
         .query();

     console.log(rows); // [ { id: 12, username: 'michael' } ]

Create Table:

    const newTable = await db.createTable({
        id: {
            type: 'id',
        },
        comment: {
            type: 'text',
            constraints: ['notNull', 'unique']
        },
        userId: {
            type: 'int',
            constraints: ['notNull'],
            references: {
                table: 'users',
                field: 'id',
                method: 'onDeleteCascade',
            }
        }
    })
      .table('comments')
      .ifNotExist()
      .query();
    console.log('createTable');

Update:

    const update = await db
        .update({
            id: 6,
            username: 'rs-hub'
        })
        .table('users')
        .set({
            username: 'rs-hub',
        })
        .query();
    console.log(update);

Join:

     const { rows } = await db
         .select({
                username: 'rs-hub'
            })
         .table("users")
         .column(["users.id as userId", "posts.id as postId", "posts.text as postsText", "comments.comment"])
         .innerJoin({
                tables: ['posts', 'comments'],
                conditions: ['users.id = posts.userid', 'comments.postsid = posts.id']
            })
         .limit(1)
         .skip(1)
         .query();
         console.log(rows) // [ { userid: 8, postid: 1, poststext: 'hello', comment: 'Good' } ];
1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago