Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
surrealised
Advanced tools
A basic SurrealDB Server-Side Client Library for NodeJS. It acts more akin to tranditional database connections, and is designed to be used in a similar way to other SQL libraries. If you have any problems, or suggestions, please open an issue.
yarn add surrealised@latest
const Surrealised = require('surrealised');
let surrealClient = new Surrealised();
let results = await surrealClient.query('SELECT * FROM users');
Configuration is set either via Environment Variables, or via the class initialisation.
SURREAL_DB_HOST=http://localhost:8000/rpc
SURREAL_DB_USER=your_user
SURREAL_DB_PASSWORD=your_password
SURREAL_DB_NAMESPACE=your_namespace
SURREAL_DB_DATABASE=your_database
SURREAL_DB_DEBUG=false #show debug output in the console logs
const Surrealised = require('surrealised');
let surrealClient = new Surrealised({
debug: true,
connection: {
host: 'http://localhost:8000/rpc',
user: 'your_user',
password: 'your_password',
namespace: 'your_namespace',
database: 'your_database'
}
});
// The rest of your code
I have neatened up the methods to make them more intuitive and easier to use (akin to other SQL libraries or ORMs out there)
Return the first row of the last query given.
let result:User = await surrealClient.queryOne<User>('SELECT * FROM users WHERE email = $email', {
email: 'user@company.com'
});
Return all the results from the last query given.
let results:User[] = await surrealClient.queryMany<User>('SELECT * FROM users WHERE email contains $domain', {
domain: 'company.com'
});
Fetch a record via it's ID field
let user:User = await surrealClient.fetch<User>('user:bob');
Fetch all records from a table
let users:User[] = await surrealClient.fetchMany<User>('user');
Create a record
let user:User = await surrealClient.create<User>('user', {
name: 'Bob',
email: 'bob@company.com',
age: 30
});
Update a record, merges if it exists, create a new record if it doesn't
let user:User = await surrealClient.update<User>('user:bob', {
age: 31
});
Delete a record
await surrealClient.delete('user:bob');
// #RIP Bob :(
Execute a native surrealdb.js query, will return an array of results for each query in the query string.
let results = await surrealClient.execute('SELECT * FROM users');
If you want to instantiate the class once and use it throughout your application, keeping the same connection, you can construct a "master class" to handle it. This is not recommended due to SurrealDBs use of Websockets to maintain a connection, and the fact that NodeJS is single threaded, but it is possible if you have a slow(ish) influx of instructions.
// surrealClient.ts
const Surrealised = require('surrealised');
let surrealClient = new Surrealised();
module.exports = surrealClient;
// index.ts (or whatever)
const surrealClient = require('./surrealClient');
let users = surrealClient.queryMany<User>('SELECT * FROM users');
The SurrealQueryBuilder
class provides a fluent interface for constructing and executing queries against a SurrealDB database. This guide will walk you through the instantiation of the query builder and the use of its major functions.
To create a new instance of the SurrealQueryBuilder
, you need to provide the name of the table you'll be querying:
const query = new SurrealQueryBuilder("table_name");
Selects fields to return from the query. If no fields are specified, *
is used to select all fields.
Example:
query.select("id", "name", "age");
Starts a condition. Must be present before any AND
or OR
statements. Adds a condition to the WHERE
clause of the query.
Example:
query.where("age > 18");
Adds an AND
condition to the query. It's essentially an alias to the where
method for chaining conditions.
Example:
query.where("age > 18").and("active = true");
Starts a new condition group with an OR
operator. Useful for grouping conditions together.
Example:
query.where("age < 18").or("guardian_approved = true");
Ends a condition group started with or
. Necessary to close the grouping of conditions.
Example:
query.where("age < 18").or("guardian_approved = true").endGroup();
Specifies record joins to fetch details of related records.
Example:
query.fetch("profile", "contacts");
Offsets the results by a specified number, for pagination.
Example:
query.offset(10);
Limits the number of results returned by the query.
Example:
query.limit(5);
Groups the results by one or more fields.
Example:
query.groupBy("department");
Groups by all fields
Example:
query.groupAll();
Orders the results by one or more fields, with optional direction (ASC
or DESC
).
Example:
query.orderBy({ field: "name", direction: "ASC" });
Splits the query results by specified fields.
Example:
query.split("category");
Adds indexes to the query to optimize its execution.
Example:
query.index("index_on_name");
Constructs and returns the query string based on the specified parameters.
Example:
const queryString = query.build();
Executes the query and returns a single row or none. It can take a parameter object for any variables within the query.
Example:
query.select("id", "name").where("id = $id").queryOne<{ id: string, name: string }>({ id: "someId" });
Executes the query and returns many rows. Similar to queryOne
, but for retrieving multiple records.
Example:
query.select("id", "name").where("active = true").queryMany<{ id: string, name: string }>();
You can access the variables
dictionary directly to modify, remove or add variables to the query.
query.variables = {
user: "user:123456",
date: new Date(),
count: 10
}
I have also included some helper functions to make this easier:
Add a variable to be passed to the query. If the variable already exists, it will be overwritten.
query.addVariable("user", "user:123456")
//... other operations
query.where('created_by = $user')
Remove a variable from the query.
query.removeVariable("user")
Clear all variables from the query.
query.clearVariables()
FAQs
Another SurrealDB Library for NodeJS
The npm package surrealised receives a total of 5 weekly downloads. As such, surrealised popularity was classified as not popular.
We found that surrealised demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.