Documentation
¶
Overview ¶
Package appres provides utilities for creating and managing Appwrite resources programmatically. It simplifies the process of creating databases, collections, and attributes in your Appwrite backend.
Usage:
app.Utils()
db, err := app.CreateDatabase("my-database")
if err != nil {
log.Fatal(err)
}
col, err := app.CreateCollection(db.Id, "my-collection")
if err != nil {
log.Fatal(err)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
AppwriteDatabase *databases.Databases
)
AppwriteDatabase is the global database client instance used by all database operations. It is initialised by calling Utils() and should not be accessed directly.
Functions ¶
func CreateAttribute ¶
func CreateAttribute(dbID string, colID string, att AttributeType) error
CreateAttribute creates a new attribute in the specified collection or skips creation if it already exists. It first checks if an attribute with the given name already exists in the collection to avoid duplicates.
The function supports creating string and email attributes with full configuration options including size limits, default values, array types, and encryption settings.
Parameters:
- dbID: The ID of the database containing the collection
- colID: The ID of the collection where the attribute should be created
- att: AttributeType struct containing the attribute configuration
Returns:
- error: Any error that occurred during the operation, or nil if successful
Supported attribute types:
- "string": Text attributes with configurable size, defaults, arrays, and encryption
- "email": Email validation attributes with defaults and array support
References:
- Appwrite Documentation: https://appwrite.io/docs/references/cloud/server-go/databases
Example:
attr := app.AttributeType{
Type: "string",
Name: "title",
Size: 255,
Required: true,
Default: "",
Array: false,
Encrypt: false,
}
err := app.CreateAttribute(db.Id, col.Id, attr)
if err != nil {
log.Fatal("Failed to create attribute:", err)
}
func CreateCollection ¶
func CreateCollection(dbId string, name string) (*models.Collection, error)
CreateCollection creates a new collection in the specified database or returns the existing one if it already exists. It first checks if a collection with the given name already exists in the database to avoid duplicates.
The function automatically generates a unique ID for new collections and logs the creation process.
Parameters:
- dbId: The ID of the database where the collection should be created
- name: The name of the collection to create
Returns:
- *models.Collection: Pointer to the created or existing collection
- error: Any error that occurred during the operation
Example:
col, err := app.CreateCollection(db.Id, "users")
if err != nil {
log.Fatal("Failed to create collection:", err)
}
fmt.Printf("Collection created with ID: %s\n", col.Id)
func CreateDatabase ¶
CreateDatabase creates a new database with the specified name or returns the existing one if it already exists. It first checks if a database with the given name already exists to avoid duplicates.
The function automatically generates a unique ID for new databases and logs the creation process.
Parameters:
- name: The name of the database to create
Returns:
- *models.Database: Pointer to the created or existing database
- error: Any error that occurred during the operation
Example:
db, err := app.CreateDatabase("my-app-database")
if err != nil {
log.Fatal("Failed to create database:", err)
}
fmt.Printf("Database created with ID: %s\n", db.Id)
func Utils ¶
func Utils()
Utils initialises the Appwrite client with configuration from environment variables. It loads environment variables from the .env.local file and creates a new Appwrite client with the configured endpoint, project ID, and API key.
This function must be called before using any other functions in this package. It will terminate the program if the .env.local file cannot be loaded.
Environment variables required:
- NEXT_PUBLIC_APPWRITE_ENDPOINT: The Appwrite server endpoint URL
- NEXT_PUBLIC_APPWRITE_PROJECT: The Appwrite project ID
- APPWRITE_API_KEY_RESDEF: The API key with appropriate permissions
Example:
app.Utils() // Now you can use other functions such as CreateDatabase, CreateCollection, etc.
Types ¶
type AttributeType ¶
type AttributeType struct {
// Type specifies the attribute type. Supported values: "string", "email", "integer", "datetime", "boolean"
Type string
// Name is the key/identifier for the attribute in the collection
Name string
// Size defines the maximum length for string and email attributes
Size int
// Required determines whether this attribute must have a value
Required bool
// Default is the default value assigned to the attribute if no value is provided
Default interface{}
// Array indicates whether the attribute can store multiple values as an array
Array bool
// Encrypt determines whether the attribute value should be encrypted at rest
// Note: Only available for string attributes
Encrypt bool
// Min is the minimum value for integer attributes (optional)
// If not set (0), no minimum constraint will be applied
Min interface{}
// Max is the maximum value for integer attributes (optional)
// If not set (0), no maximum constraint will be applied
Max interface{}
RelatedCollectionID string
RelationshipType string
TwoWay bool
TwoWayKey string
OnDelete string
}
AttributeType defines the configuration for creating attributes in Appwrite collections. It contains all the necessary fields to specify the type, constraints, and behavior of an attribute when creating it in a collection.
Supported attribute types:
- "string": Text attributes with configurable size limits
- "email": Email validation attributes
- "integer": Integer attributes with configurable min/max constraints
- "datetime": Date and time attributes
- "boolean": Boolean (true/false) attributes
Example usage:
attr := AttributeType{
Type: "string",
Name: "username",
Size: 50,
Required: true,
Default: "",
Array: false,
Encrypt: false,
}
// Integer attribute example:
intAttr := AttributeType{
Type: "integer",
Name: "age",
Required: true,
Min: 0,
Max: 120,
Default: "18",
Array: false,
}
Directories
¶
| Path | Synopsis |
|---|---|
|
Package helper provides utility functions for loading and managing environment variables required for Appwrite client configuration.
|
Package helper provides utility functions for loading and managing environment variables required for Appwrite client configuration. |