Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
A driver to query Fauna databases in browsers, Node.js, and other Javascript runtimes
WARNING This driver is in beta release and not recommended for production use. It operates with the Fauna database service via an API which is also in beta release, and is not recommended for production use. This driver is not compatible with v4 or earlier versions of Fauna. Please feel free to contact product@fauna.com to learn about our special Early Access program for FQL X.
See the Fauna Documentation for additional information how to configure and query your databases.
This driver can only be used with FQL X, and is not compatible with earlier versions of FQL. To query your databases with earlier API versions, see the faunadb package.
import { Client, fql, FaunaError } from "fauna";
// configure your client
const client = new Client({
secret: YOUR_FAUNA_SECRET,
});
try {
// build queries using the fql function
const collection_query = fql`Collection.create({ name: "Dogs" })`;
// execute the query
const collection_result = await client.query(collection_query);
// define some data in your app
const dog = { name: "Scout" };
// query using your app's local variables
const document_query = fql`
Dogs.create(${dog}) {
id,
ts,
name
}
`;
const document_result = await client.query(document_query);
} catch (error) {
if (error instanceof FaunaError) {
// handle errors
}
}
This Driver supports and is tested on:
The fauna-js driver is available on npm. You can install with your package manager of choice:
npm install fauna
or
yarn add fauna
The driver is additionally made available to browsers via CDN:
<script type="module">
import * as fauna from "https://cdn.jsdelivr.net/npm/fauna@latest/dist/browser/index.js";
</script>
fql
functionThe fql
function is your gateway to building safe, reuseable Fauna queries.
It allows you compose queries from sub-queries and variables native to your program. Variables passed in are treated as unexecutable values in Fauna's API - preventing security issues like injection attacks.
for example:
import { Client, fql } from "fauna";
const client = new Client();
const collectionName = "Pets";
// a reusable sub query to determine a collections existence
const collectionExists = fql`Collection.byName(${collectionName}) != null`;
client.query(fql`
if (${collectionExists}) {
"Collection exists!"
} else {
Collection.create({ name: ${collectionName} })
"Collection exists now!"
}`);
This has several advantages:
fql
to build a library of subqueries applicable to your domain - and combinable in whatever way you need${i'm interpoloated}
) parts of the query.<html>
<head></head>
<body>
<h1>Test</h1>
</body>
<script type="module">
import * as fauna from "https://cdn.jsdelivr.net/npm/fauna@latest/dist/browser/index.js";
/* ... */
</script>
</html>
import * as fauna from "fauna";
or using require
for CommonJS files
const fauna = require("fauna");
With TypeScript, you can apply a type parameter to your result.
import { Client, fql } from "fauna";
const client = new Client();
type User = {
name: string;
email: string;
};
const query = fql`{
name: "Alice",
email: "alice@site.example",
}`;
const result = await client.query<User>(query); // QuerySuccess<User>
const user_doc = result.data; // User
console.assert(user_doc.name === "Alice");
console.assert(user_doc.email === "alice@site.example");
Options are available to configure queries on each request.
import { fql, Client, type QueryRequestHeaders } from "fauna";
const client = new Client();
const options: QueryRequestHeaders = {
format: "tagged",
linearized: false,
query_timeout_ms: 60_000,
max_contention_retries: 5,
query_tags: { name: "readme query" },
traceparent: "00-750efa5fb6a131eb2cf4db39f28366cb-000000000000000b-00",
typecheck: true,
};
const result = await client.query(fql`"Hello, Fauna!"`, options);
The driver use's a default ClientConfiguration. We recommend most users stick with the defaults.
If your environment needs different configuration however, the default ClientConfiguration can be overriden.
Furthermore, on each request you can provide query specific configuration that will override the setting in your client for that request only.
import { Client, endpoints, type ClientConfiguration } from "fauna";
const config: ClientConfiguration = {
// configure client
secret: YOUR_FAUNA_SECRET,
endpoint: endpoints.default,
// set default query options
format: "tagged",
linearized: false,
query_timeout_ms: 60_000,
max_contention_retries: 5,
query_tags: { name: "readme query" },
traceparent: "00-750efa5fb6a131eb2cf4db39f28366cb-000000000000000b-00",
typecheck: true,
};
const client = new Client(config);
The driver will default to configuring your client with the values of the FAUNA_SECRET
and FAUNA_ENDPOINT
environment variable.
For example, if you set the following environment variables:
export FAUNA_SECRET=YOUR_FAUNA_SECRET
export FAUNA_ENDPOINT=https://db.fauna.com/
you can create a client without additional options
const client = new Client()
Query statistics are returned with successful query responses and ServiceError
s.
import {
fql,
Client,
ServiceError,
type QueryInfo,
type QueryStats,
type QuerySuccess,
} from "fauna";
const client = new Client();
try {
const result: QuerySuccess<string> = await client.query(fql`"Hello world"`);
const stats: QueryStats | undefined = result.stats;
} catch (error: any) {
if (error instanceof ServiceError) {
const info: QueryInfo = error.queryInfo;
const stats: QueryStats | undefined = info.stats;
}
}
console.log(stats);
/* example output
* ```
* {
* compute_ops: 1,
* read_ops: 0,
* write_ops: 0,
* query_time_ms: 15,
* storage_bytes_read: 0,
* storage_bytes_write: 0,
* contention_retries: 0
* }
* ```
*/
Any contributions are from the community are greatly appreciated!
If you have a suggestion that would make this better, please fork the repo and create a pull request. You may also simply open an issue. We provide templates, so please complete those to the best of your ability.
Don't forget to give the project a star! Thanks again!
gh repo clone fauna/fauna-js
if you use the GitHub CLIyarn install
yarn test
. This will start local fauna containers, verify they're up and run all tests.Linting runs automatically on each commit.
If you wish to run on-demand run yarn lint
.
Distributed under the MPL 2.0 License. See LICENSE for more information.
FAQs
A driver to query Fauna databases in browsers, Node.js, and other Javascript runtimes
The npm package fauna receives a total of 1,832 weekly downloads. As such, fauna popularity was classified as popular.
We found that fauna demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.