Skip to content

Commit

Permalink
Improve consistency with other JS libraries (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Oct 23, 2023
1 parent 9dd671d commit eeec74c
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 190 deletions.
282 changes: 146 additions & 136 deletions __test__/index.spec.mjs
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 => {

Expand Down
28 changes: 14 additions & 14 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

export class Surreal {
constructor()
connect(endpoint: string, opts?: any | undefined | null): Promise<void>
use(value: any): Promise<void>
set(key: string, value: any): Promise<void>
connect(endpoint: string, opts?: Record<string, unknown>): Promise<void>
use(value: { namespace?: string; database?: string }): Promise<void>
set(key: string, value: unknown): Promise<void>
unset(key: string): Promise<void>
signup(credentials: any): Promise<any>
signin(credentials: any): Promise<any>
signup(credentials: { namespace: string; database: string; scope: string; [k: string]: unknown }): Promise<string>
signin(credentials: { username: string; password: string } | { namespace: string; username: string; password: string } | { namespace: string; database: string; username: string; password: string } | { namespace: string; database: string; scope: string; [k: string]: unknown }): Promise<string>
invalidate(): Promise<void>
authenticate(token: string): Promise<void>
query(sql: string, bindings?: any | undefined | null): Promise<any>
select(resource: string): Promise<any>
create(resource: string, data?: any | undefined | null): Promise<any>
update(resource: string, data?: any | undefined | null): Promise<any>
merge(resource: string, data: any): Promise<any>
patch(resource: string, data: any): Promise<any>
delete(resource: string): Promise<any>
version(): Promise<any>
authenticate(token: string): Promise<boolean>
query(sql: string, bindings?: Record<string, unknown>): Promise<unknown[]>
select(resource: string): Promise<{ id: string; [k: string]: unknown }[]>
create(resource: string, data?: Record<string, unknown>): Promise<{ id: string; [k: string]: unknown }[]>
update(resource: string, data?: Record<string, unknown>): Promise<{ id: string; [k: string]: unknown }[]>
merge(resource: string, data: Record<string, unknown>): Promise<{ id: string; [k: string]: unknown }[]>
patch(resource: string, data: unknown[]): Promise<unknown[]>
delete(resource: string): Promise<{ id: string; [k: string]: unknown }[]>
version(): Promise<string>
health(): Promise<void>
}
8 changes: 8 additions & 0 deletions src/array_response.rs
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))
}
}
Loading

0 comments on commit eeec74c

Please sign in to comment.