-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update readme * Fix method name * Add line breaks * Improve formatting
- Loading branch information
1 parent
afbd1cf
commit 621eb28
Showing
6 changed files
with
185 additions
and
45 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,6 +1,6 @@ | ||
# .github/release.yml | ||
|
||
name: Release | ||
name: release | ||
on: | ||
push: | ||
branches: | ||
|
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,4 +1,4 @@ | ||
name: Tests | ||
name: tests | ||
|
||
on: | ||
push: | ||
|
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,58 +1,176 @@ | ||
# The official Node.js client library for EdgeDB | ||
<div align="center"> | ||
<h1>The official Node.js client library for EdgeDB</h1> | ||
|
||
<a href="https://github.com/edgedb/edgedb-js/actions" rel="nofollow"> | ||
<img src="https://github.com/edgedb/edgedb-js/actions/workflows/tests.yml/badge.svg?event=push&branch=master" alt="Build status"> | ||
</a> | ||
<a href="https://www.npmjs.com/package/edgedb" rel="nofollow"> | ||
<img src="https://img.shields.io/npm/v/edgedb" alt="NPM version"> | ||
</a> | ||
<a href="https://github.com/edgedb/edgedb" rel="nofollow"> | ||
<img src="https://img.shields.io/github/stars/edgedb/edgedb" alt="Stars"> | ||
</a> | ||
<a href="https://github.com/edgedb/edgedb/blob/master/LICENSE"> | ||
<img src="https://img.shields.io/badge/license-Apache%202.0-blue" /> | ||
</a> | ||
<br /> | ||
<br /> | ||
<a href="https://www.edgedb.com/docs/guides/quickstart">Quickstart</a> | ||
<span> • </span> | ||
<a href="https://www.edgedb.com">Website</a> | ||
<span> • </span> | ||
<a href="https://www.edgedb.com/docs/clients/01_js/index">Docs</a> | ||
<span> • </span> | ||
<a href="https://discord.gg/umUueND6ag">Discord</a> | ||
<span> • </span> | ||
<a href="https://twitter.com/edgedatabase">Twitter</a> | ||
<br /> | ||
|
||
</div> | ||
|
||
<br /> | ||
<br /> | ||
|
||
This is the official [EdgeDB](https://github.com/edgedb/edgedb) client library | ||
for JavaScript and TypeScript. | ||
|
||
[![Build Status](https://github.com/edgedb/edgedb-js/workflows/Tests/badge.svg?event=push&branch=master)](https://github.com/edgedb/edgedb-js/actions) [![NPM](https://img.shields.io/npm/v/edgedb)](https://www.npmjs.com/package/edgedb) [![Join GitHub discussions](https://img.shields.io/badge/join-github%20discussions-green)](https://github.com/edgedb/edgedb/discussions) | ||
If you're just getting started with EdgeDB, we recommend going through the | ||
[EdgeDB Quickstart](https://www.edgedb.com/docs/quickstart) first. This walks | ||
you through the process of installing EdgeDB, creating a simple schema, and | ||
writing some simple queries. | ||
|
||
**edgedb** is the official [EdgeDB](https://github.com/edgedb/edgedb) driver | ||
for JavaScript and TypeScript. | ||
### Requirements | ||
|
||
The library requires Node.js 12+. | ||
- Node.js 12+ | ||
- _TypeScript only_ | ||
- TypeScript 4.4+ | ||
- `yarn add @types/node` | ||
|
||
## Installation | ||
### Installation | ||
|
||
```bash | ||
npm install edgedb | ||
# or | ||
yarn add edgedb | ||
npm install edgedb # npm users | ||
yarn add edgedb # yarn users | ||
``` | ||
|
||
## Quickstart | ||
## Basic usage | ||
|
||
First, go through the | ||
[EdgeDB Quickstart](https://www.edgedb.com/docs/quickstart) to install EdgeDB | ||
and set up your first EdgeDB project. | ||
> The examples below demonstrate only the most fundamental use cases for this | ||
> library. **[Go to the complete documentation site. >](https://www.edgedb.com/docs/clients/01_js/index)** | ||
Now in your project directory, install the "edgedb" library: | ||
### Create a client | ||
|
||
```bash | ||
npm init | ||
A _client_ is an instance of the `Client` class, which maintains a pool of | ||
connections to your database and provides methods for executing queries. | ||
|
||
_For TypeScript (and Node.js+ESM)_ | ||
|
||
npm install edgedb | ||
```ts | ||
import * as edgedb from "edgedb"; | ||
|
||
const client = edgedb.createClient(); | ||
``` | ||
|
||
And here's a simple script to connect to your EdgeDB instance and | ||
run a simple query: | ||
_For Node.js (CommonJS)_ | ||
|
||
```js | ||
const edgedb = require("edgedb"); | ||
|
||
async function main() { | ||
const client = edgedb.createClient(); | ||
const client = edgedb.createClient(); | ||
``` | ||
|
||
### Configuring the connection | ||
|
||
The call to `edgedb.createClient()` doesn't require arguments, as the library | ||
can determine how to connect to your database using the following mechanisms. | ||
|
||
1. _For local development_: initialize a project with the `edgedb project init` | ||
command. As long as the file is within a project directory, `createClient` | ||
will be able to auto-discover the connection information of the project's | ||
associated instance. For more information on projects, follow the | ||
[Using projects](https://www.edgedb.com/docs/guides/projects) guide. | ||
|
||
2. _In production_: configure the connection using **environment variables**. | ||
(This can also be used during local development if you prefer.) The easiest | ||
way is to set the `EDGEDB_DSN` variable; a DSN (also known as a "connection | ||
string") is a string of the form | ||
`edgedb://USERNAME:PASSWORD@HOSTNAME:PORT/DATABASE`. | ||
|
||
For advanced cases, see the | ||
[DSN specification](https://www.edgedb.com/docs/reference/dsn) and | ||
[Reference > Connection Parameters](https://www.edgedb.com/docs/reference/connection). | ||
|
||
### Run a query | ||
|
||
> The remainder of the documentation assumes you are using ES module (`import`) | ||
> syntax. | ||
```ts | ||
import * as edgedb from "edgedb"; | ||
|
||
const client = edgedb.createClient(); | ||
await client.query("select 2 + 2"); // => [4] | ||
``` | ||
|
||
Note that the result is an _array_. The `.query()` method always returns an | ||
array, regardless of the result cardinality of your query. If your query | ||
returns _zero or one elements_, use the `.querySingle()` instead. | ||
|
||
```ts | ||
// empty set, zero elements | ||
await client.querySingle("select <str>{}"); // => null | ||
|
||
// one element | ||
await client.querySingle("select 2 + 2"); // => 4 | ||
|
||
// one element | ||
await client.querySingle( | ||
`select Movie { title } | ||
filter .id = <uuid>'2eb3bc76-a014-45dc-af66-2e6e8cc23e7e';` | ||
); // => { title: "Dune" } | ||
``` | ||
|
||
## Query builder | ||
|
||
Instead of writing queries as strings, you can use this package to generate a | ||
_query builder_. The query builder lets you write queries in a code-first way | ||
and automatically infers the return type of your queries. | ||
|
||
To generate the query builder, install the `edgedb`, initialize a project (if | ||
you haven't already), then run the following command: | ||
|
||
```sh | ||
$ npx edgeql-js | ||
``` | ||
|
||
This will generate an EdgeQL query builder into the `"./dbschema/edgeql-js` | ||
directory, as defined relative to your project root. | ||
|
||
For details on using the query builder, refer to the [complete documentation](https://www.edgedb.com/docs/clients/01_js/generation). Below is a simple | ||
`select` query as an example. | ||
|
||
```ts | ||
import {createClient} from "edgedb"; | ||
import e from "./dbschema/edgeql-js"; | ||
|
||
console.log( | ||
await client.querySingle( | ||
`SELECT re_replace('World', 'EdgeDB', 'Hello World!')` | ||
) | ||
); | ||
} | ||
const client = createClient(); | ||
const query = e.select(e.Movie, movie => ({ | ||
id: true, | ||
title: true, | ||
actors: { name: true }, | ||
num_actors: e.count(movie.actors) | ||
filter: e.op(movie.title, '=', 'Dune') | ||
})); | ||
|
||
main(); | ||
const result = await query.run(client); | ||
result.actors[0].name; // => Timothee Chalamet | ||
``` | ||
|
||
## Development | ||
## Contribute | ||
|
||
A local installation of EdgeDB is required to run tests. Download | ||
Contributing to this library requires a local installation of EdgeDB. Install | ||
EdgeDB from [here](https://www.edgedb.com/download) or | ||
[build it manually](https://www.edgedb.com/docs/reference/dev). | ||
[build it from source](https://www.edgedb.com/docs/reference/dev). | ||
|
||
```bash | ||
$ git clone [email protected]:edgedb/edgedb-js.git | ||
|
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
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