Insert Document
The zkDatabase library provides the insert
method to add new documents to a specified collection within a database. This method supports both basic and advanced usage, allowing you to specify detailed permissions for different users or groups.
Definition
The insert
function is called on a collection object, with the first parameter being the document to insert and an optional second parameter for permissions.
await zkdb.db(databaseName: string)
.collection(collectionName: string)
.insert(document: Document, permission?: Permission): Promise<TDocumentCreateResponse>;
Parameters
document
: The object containing the data to be inserted into the collection. It should match the schema of the collection.permission
(Optional): A permission policy object that defines how the document insertion is exposed in terms of security or access control.
Returns
The function returns a Promise
that resolves to an TDocumentCreateResponse
, which can provide details regarding the outcome of the insertion, including the success status or identifiers for the new document.
Example
The example shows how to insert a document into the collection both with and without specifying a permission policy.
import { CircuitString, UInt64 } from 'o1js';
import { Schema, ZkDatabase, Permission } from './index';
const zkdb = await ZkDatabase.connect({
userName: "chiro-user",
privateKey: "EKFTciRxyxshZjimay9sktsn7v5PvmC5zPq7q4JnitHUytxUVnFP",
environment: "node",
// This URL is for test environment
url: "https://api.zkdatabase.org/graphql",
});
await zkdb.auth.signIn();
class Shirt extends Schema.create({
name: CircuitString,
price: UInt64,
}) {}
type TShirt = typeof Shirt;
const collection = await zkdb
.db('zkdb_test')
.collection<TShirt>('test_collection');
await collection.insert({
name: 'Test Shirt',
price: 10n,
});
await collection.insert(
{
name: 'Orochi',
price: 10n ** 9n,
},
Permission.policyStrict()
);
await zkdb.auth.signOut();