-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve consistency with other JS libraries (#19)
- Loading branch information
Showing
5 changed files
with
214 additions
and
190 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,149 @@ | ||
import test from 'ava' | ||
|
||
import { Surreal } from '../index.js' | ||
|
||
test('Connect in-memory SurrealDB instance', async t => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
t.pass(); | ||
}) | ||
|
||
test('set ns/db', async t => { | ||
{ | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ 'ns': 'test' }) | ||
} | ||
|
||
{ | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ 'db': 'test' }) | ||
} | ||
|
||
{ | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ 'ns': 'test', 'db': 'test' }) | ||
} | ||
|
||
t.pass(); | ||
}) | ||
|
||
test('test query method', async t => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ 'ns': 'test', 'db': 'test' }) | ||
|
||
{ | ||
const res = await db.query('SELECT * FROM person'); | ||
t.deepEqual(res, []); | ||
} | ||
|
||
|
||
{ | ||
const res = await db.query('CREATE |foo:100|'); | ||
t.is(res.length, 100); | ||
} | ||
|
||
{ | ||
const data = { 'first_name': 'Tobie', 'projects': ['SurrealDB'] }; | ||
const res = await db.query('CREATE person:tobie content $data', { 'data': data }); | ||
data.id = 'person:tobie'; | ||
t.deepEqual(res, [data]); | ||
} | ||
|
||
}) | ||
|
||
test('set and and unset', async t => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ 'ns': 'test', 'db': 'test' }); | ||
|
||
const data = { 'first': 'Tobie', 'last': 'Morgan Hitchcock' }; | ||
|
||
await db.set('name', data) | ||
{ | ||
const res = await db.query("RETURN $name"); | ||
t.deepEqual(res, [data]); | ||
} | ||
|
||
await db.unset('name'); | ||
|
||
{ | ||
const res = await db.query("RETURN $name"); | ||
t.deepEqual(res, []); | ||
} | ||
|
||
}) | ||
|
||
test.failing('auth', async t => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
// await db.connect("ws://127.0.0.1:8000"); | ||
await db.use({ 'ns': 'test', 'db': 'test' }); | ||
|
||
await db.signin({ username: 'root', password: 'root' }) | ||
|
||
const scope = await db.query(`DEFINE SCOPE user_scope SESSION 5s | ||
SIGNUP (CREATE user SET email = $email, pass = crypto::argon2::generate($pass)) | ||
SIGNIN (SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass)) | ||
`); | ||
console.log(scope); | ||
|
||
|
||
const token = await db.signup({ | ||
namespace: 'namespace', | ||
database: 'database', | ||
scope: 'user_scope', | ||
email: '[email protected]', | ||
password: 'password123' | ||
}); | ||
console.log(token); | ||
|
||
// // const token2 = await db_ws.signin({ | ||
// // namespace: 'namespace', | ||
// // database: 'database', | ||
// // scope: 'user_scope', | ||
// // email: '[email protected]', | ||
// // password: 'password123' | ||
// // }); | ||
// // console.log(token2); | ||
|
||
t.pass() | ||
}) | ||
|
||
// test('test select method', async t => { | ||
// const db = new Surreal(); | ||
// await db.connect("memory"); | ||
// await db.use({ 'ns': 'test', 'db': 'test' }); | ||
|
||
// const jason = { 'id': 'person:jason' }; | ||
// const john = { 'id': 'person:john' }; | ||
// const jaime = { 'id': 'person:jaime' }; | ||
// const people = [jason, john, jaime]; | ||
|
||
// await db.create(jason.id); | ||
// await db.create(john.id); | ||
// await db.create(jaime.id); | ||
|
||
// { | ||
// const res = await db.select('person'); | ||
// console.log(res); | ||
// t.is(new Set(res), new Set(people)); | ||
// } | ||
|
||
// }) | ||
import test from "ava"; | ||
|
||
import { Surreal } from "../index.js"; | ||
|
||
test("Connect in-memory SurrealDB instance", async (t) => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
t.pass(); | ||
}); | ||
|
||
test("set ns/db", async (t) => { | ||
{ | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ namespace: "test" }); | ||
} | ||
|
||
{ | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ database: "test" }); | ||
} | ||
|
||
{ | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ namespace: "test", database: "test" }); | ||
} | ||
|
||
t.pass(); | ||
}); | ||
|
||
test("test query method", async (t) => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ namespace: "test", database: "test" }); | ||
|
||
{ | ||
const [res] = await db.query("SELECT * FROM person"); | ||
t.deepEqual(res, []); | ||
} | ||
|
||
{ | ||
const [res] = await db.query("CREATE |foo:100|"); | ||
t.is(res.length, 100); | ||
} | ||
|
||
{ | ||
const data = { first_name: "Tobie", projects: ["SurrealDB"] }; | ||
const [res] = await db.query("CREATE person:tobie content $data", { | ||
data: data, | ||
}); | ||
data.id = "person:tobie"; | ||
t.deepEqual(res, [data]); | ||
} | ||
}); | ||
|
||
test("set and and unset", async (t) => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ namespace: "test", database: "test" }); | ||
|
||
const data = { first: "Tobie", last: "Morgan Hitchcock" }; | ||
|
||
await db.set("name", data); | ||
{ | ||
const [res] = await db.query("RETURN $name"); | ||
t.deepEqual(res, [data]); | ||
} | ||
|
||
await db.unset("name"); | ||
|
||
{ | ||
const [res] = await db.query("RETURN $name"); | ||
t.deepEqual(res, []); | ||
} | ||
}); | ||
|
||
test("auth", async (t) => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ | ||
namespace: "test", | ||
database: "test" | ||
}); | ||
|
||
const scope = /* surql */ `DEFINE SCOPE user SESSION 5s SIGNUP (CREATE type::thing('user', $username) SET email = $email, pass = crypto::argon2::generate($pass)) SIGNIN (SELECT * FROM user WHERE email = $email AND crypto::argon2::compare(pass, $pass))`; | ||
const [_, [{ scopes }]] = await db.query(/* surql */ ` | ||
${scope}; | ||
INFO FOR DB; | ||
`); | ||
t.is(scopes.user, scope); | ||
|
||
{ | ||
const token = await db.signup({ | ||
namespace: "test", | ||
database: "test", | ||
scope: "user", | ||
|
||
username: 'john', | ||
email: "[email protected]", | ||
pass: "password123", | ||
}); | ||
t.is(typeof token, 'string'); | ||
|
||
const [[user_id]] = await db.query(/* surql */ `$auth`); | ||
t.is(user_id, 'user:john'); | ||
} | ||
|
||
{ | ||
const token = await db.signin({ | ||
namespace: "test", | ||
database: "test", | ||
scope: "user", | ||
|
||
email: "[email protected]", | ||
pass: "password123", | ||
}); | ||
t.is(typeof token, 'string'); | ||
|
||
const [[user_id]] = await db.query(/* surql */ `$auth`); | ||
t.is(user_id, 'user:john'); | ||
} | ||
}); | ||
|
||
test("test select method", async (t) => { | ||
const db = new Surreal(); | ||
await db.connect("memory"); | ||
await db.use({ namespace: "test", database: "test" }); | ||
|
||
const jason = { id: "person:jason" }; | ||
const john = { id: "person:john" }; | ||
const jaime = { id: "person:jaime" }; | ||
const people = [jaime, jason, john]; | ||
|
||
await db.create(jason.id); | ||
await db.create(john.id); | ||
await db.create(jaime.id); | ||
|
||
{ | ||
const res = await db.select("person"); | ||
t.deepEqual(new Set(res), new Set(people)); | ||
const person = await db.select("person:jason"); | ||
t.deepEqual(person, [jason]); | ||
} | ||
}); | ||
|
||
// test('examples', async t => { | ||
|
||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use surrealdb::sql::{Array, Value}; | ||
|
||
pub fn array_response(response: Value) -> Value { | ||
match response { | ||
Value::Array(_) => response, | ||
_ => Value::Array(Array::from(response)) | ||
} | ||
} |
Oops, something went wrong.