Skip to content

Instantly share code, notes, and snippets.

@jchris
Created January 22, 2018 22:51
Show Gist options
  • Save jchris/cf6fc6ccb1dda357b8890a762878f79a to your computer and use it in GitHub Desktop.
Save jchris/cf6fc6ccb1dda357b8890a762878f79a to your computer and use it in GitHub Desktop.
import faunadb, {query as q} from 'faunadb'
const client = new faunadb.Client({
secret: "x"
});
// jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
async function runThese() {
var edits = 20
var timestamps = {}
const newPost = await client.query(q.Create(q.Class("posts"), { data: { title: "Init" } }))
const ref = newPost.ref
console.log(newPost)
for(var i=0; i < edits; i++){
var edit = await client.query(q.Update(ref, { data: { title: "Edit " + (i+1) } }))
timestamps[edit.ts] = edit.data.title
}
console.log(Object.values(timestamps)) // ['Edit 1','Edit 2','Edit 3','Edit 4','Edit 5','Edit 6','Edit 7','Edit 8','Edit 9','Edit 10','Edit 11','Edit 12','Edit 13','Edit 14','Edit 15','Edit 16','Edit 17','Edit 18','Edit 19','Edit 20']
// Test1
// I expect this to return [ 'Edit 19', 'Edit 20'] and it does.
var Test1 = await client.query(q.Map(
q.Paginate(ref, {events: true, size: 2}),
function(event) {
return q.Get(q.Select('resource', event), q.Select('ts', event))
}
))
console.log(timestamps[Test1.after.ts]) // Edit 16 (Should this be Edit 18?)
console.log(Test1.before) // undefined
console.log(Test1.data.map(item => item.data.title)) // [ 'Edit 19', 'Edit 20' ]
// Test2 (after Test1.after.ts)
// Based on Test1 I expect this to return [ 'Edit 17', 'Edit 18']. It does not...
var Test2 = await client.query(q.Map(
q.Paginate(ref, {events: true, size: 2, after: Test1.after.ts}),
function(event) {
return q.Get(q.Select('resource', event), q.Select('ts', event))
}
)).catch((e)=> console.log(e))
console.log(timestamps[Test2.after.ts]) // Edit 17
console.log(timestamps[Test2.before.ts]) // Edit 16
console.log(Test2.data.map(item => item.data.title)) // ['Edit 19', 'Edit 20']
// Test3 (before Test1.after.ts)
// I don't know what to expect here. But not nothing...
var Test3 = await client.query(q.Map(
q.Paginate(ref, {events: true, size: 2, before: Test1.after.ts}),
function(event) {
return q.Get(q.Select('resource', event), q.Select('ts', event))
}
)).catch((e)=> console.log(e))
console.log(timestamps[Test3.after.ts]) // Edit 16
console.log(Test3.before) // undefined
console.log(Test3.data.map(item => item.data.title)) // []
// Test4 (after: 0)
// I expect this to return [ 'Init', 'Edit 1']
var Test4 = await client.query(q.Map(
q.Paginate(ref, {events: true, size: 2, after: 0}),
function(event) {
return q.Get(q.Select('resource', event), q.Select('ts', event))
}
))
console.log(timestamps[Test4.after.ts]) // Edit 16
console.log(Test4.before) // { ts: 0, action: 'create' }
console.log(Test4.data.map(item => item.data.title)) // [ 'Edit 19', 'Edit 20' ]
// Test5 (no size)
// I expect this to return the default size 16
var Test5 = await client.query(q.Map(
q.Paginate(ref, {events: true}),
function(event) {
return q.Get(q.Select('resource', event), q.Select('ts', event))
}
))
console.log(Test5.data.length) // 21
}
runThese().catch((e) => console.log(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment