Skip to content

Instantly share code, notes, and snippets.

@roboncode
Last active April 11, 2019 17:58
Show Gist options
  • Save roboncode/09fbc3c117c4d180d9dbb3490c97f7bb to your computer and use it in GitHub Desktop.
Save roboncode/09fbc3c117c4d180d9dbb3490c97f7bb to your computer and use it in GitHub Desktop.
Orango - Complex Queries Concepts
const { append } = orango.funcs
orango.builder()
.add('numbers', append([1, 2, 3], [3, 4, 5], true))
.add('email', 'john@gmail.com')
.add('users', User.find().where({email: "@email"})
.return({
})
/*
LET ACTIVE = true
LET usrs = (
FOR u IN users
FILTER u.active == ACTIVE
UPDATE u WITH { message: "Hello, world!" } IN users
RETURN NEW
)
FOR u IN usrs
RETURN u
*/
const { extract } from orango.consts
orango
.commands()
.add('ACTIVE', false)
.add('usrs', User.find().where({ active: '${ACTIVE}' }).subquery(User.update().with({ message: "Hello, world!" })))
.add('updated_users', User.find().name('usrs'))
.return(extract('${updated_users}', 'firstName', 'lastName'))
@roboncode
Copy link
Author

const orango = require('orango')
const { keep } = orango.funcs

async function main() {
  const db = orango.get('examples')

  /**
   * Register Model
   */
  const UserModel = db.createModel({
    active: Boolean,
    firstName: String,
    lastName: String,
    email: String
  })

  class User extends UserModel {
    static findUsers(filters = {active : true}) {
      return this.find().where(filters)
    }

    static updateUsers(data, ...returnFields) {
      return this.update().with(data).return(keep("NEW", ...returnFields))
    }

    static async updateActiveUsers(data) {
      let updatedUsers = await db.query()
        .add('activeUsers', this.findUsers({ active: true })
        .query(this.updatedUsers(data, "firstName", "lastName")))
      return updatedUsers
    }
  }

  /**
   * Connect
   */
  await db.connect({
    url: "http://localhost:15100"
  })

  /*
    LET active = true
    LET activeUsers = (
      FOR u IN users
      FILTER u.active == active
          UPDATE u WITH { message: "Hello, world!" } IN users
          RETURN NEW
    )
    RETURN KEEP(activeUsers, "firstName", "lastName")
  */

  let updatedUsers = await User.updateActiveUsers({message: "Hello, world!"})
  print('You are here', updatedUsers)
}

main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment