Getting Started
Connecting
Create a SinterDB client, connect to a server, and select a database and collection.
Create a client
Import SinterClient from the official sinterdb package.
import { SinterClient } from "sinterdb";
const client = new SinterClient("sinterdb://localhost/my-app");
await client.connect();Connection strings
SinterDB connection strings use the sinterdb:// scheme.
sinterdb://localhost/my-app
sinterdb://127.0.0.1:4721/my-appThe portion after the final slash is the database name. If the connection string includes a database, you can access it with client.db().
Access a collection
Collections can be typed with your own TypeScript interface.
interface User {
name: string;
active: boolean;
}
const users = client.db().collection<User>("users");The document type is used by filters, updates, sorts, and other driver APIs to provide compile-time type checking. See SinterCollection for everything a collection can do.
Always close the client
Close the connection when your application is finished using it. A try/finally block is a simple way to guarantee cleanup.
import { SinterClient } from "sinterdb";
const client = new SinterClient("sinterdb://localhost/my-app");
await client.connect();
try {
const users = client.db().collection("users");
// Use the database...
} finally {
await client.close();
}Client options
You can configure connection, request, and idle socket timeouts when creating the client.
const client = new SinterClient("sinterdb://localhost/my-app", {
connectTimeoutMS: 10_000,
requestTimeoutMS: 10_000,
socketTimeoutMS: 0,
});| Option | Description |
|---|---|
connectTimeoutMS | Maximum time allowed for connecting and completing the handshake. |
requestTimeoutMS | Maximum time to wait for an individual server response. |
socketTimeoutMS | Idle socket timeout. A value of 0 disables it. |
All options are listed in SinterClientOptions.
Complete example
import { SinterClient } from "sinterdb";
interface User {
name: string;
active: boolean;
}
const client = new SinterClient("sinterdb://localhost/my-app");
await client.connect();
try {
const users = client.db().collection<User>("users");
await users.insertOne({
name: "Kage",
active: true,
});
const user = await users.findOne({
name: "Kage",
});
console.log(user);
} finally {
await client.close();
}