Add Appwrite database collection definitions
This commit is contained in:
parent
3870974605
commit
48a5f5c7b4
1 changed files with 91 additions and 0 deletions
91
appwrite-databases-schema.md
Normal file
91
appwrite-databases-schema.md
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
# Appwrite Database Collections
|
||||
|
||||
Definição textual das coleções necessárias no Appwrite Console e um exemplo simples de script JavaScript para criá-las via SDK.
|
||||
|
||||
## Collections
|
||||
|
||||
### cloud_accounts
|
||||
- **provider**: string (`'github'` | `'cloudflare'`), obrigatório.
|
||||
- **apiKey**: string (armazenada com criptografia no Appwrite), obrigatório.
|
||||
- **label**: string (nome amigável para exibir), obrigatório.
|
||||
|
||||
### projects
|
||||
- **name**: string (nome do projeto), obrigatório.
|
||||
- **repoUrl**: string (URL do repositório), obrigatório.
|
||||
- **deployStatus**: string (status do deploy), obrigatório.
|
||||
|
||||
### audit_logs
|
||||
- **action**: string (ação executada), obrigatório.
|
||||
- **timestamp**: datetime (instante do evento), obrigatório.
|
||||
- **userId**: string (ID do usuário responsável), obrigatório.
|
||||
|
||||
## Script de exemplo (Node.js)
|
||||
|
||||
O script abaixo usa o SDK do Appwrite para criar as coleções e seus atributos em um database existente. Ajuste as variáveis `APPWRITE_ENDPOINT`, `APPWRITE_PROJECT`, `APPWRITE_API_KEY` e `DATABASE_ID` antes de executar.
|
||||
|
||||
```bash
|
||||
npm install appwrite
|
||||
node create-collections.js
|
||||
```
|
||||
|
||||
```js
|
||||
// create-collections.js
|
||||
import { Client, Databases, ID } from 'appwrite';
|
||||
|
||||
const client = new Client()
|
||||
.setEndpoint(process.env.APPWRITE_ENDPOINT || 'https://cloud.appwrite.io/v1')
|
||||
.setProject(process.env.APPWRITE_PROJECT)
|
||||
.setKey(process.env.APPWRITE_API_KEY);
|
||||
|
||||
const DATABASE_ID = process.env.DATABASE_ID; // ID do banco existente
|
||||
const databases = new Databases(client);
|
||||
|
||||
async function createCollection({ id, name }) {
|
||||
await databases.createCollection(DATABASE_ID, id, name, [
|
||||
{
|
||||
type: 'document',
|
||||
roles: [
|
||||
{ role: 'all', permission: 'read' },
|
||||
{ role: 'users', permission: 'create' },
|
||||
{ role: 'users', permission: 'update' },
|
||||
{ role: 'users', permission: 'delete' },
|
||||
],
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
async function createStringAttribute(collectionId, key, size = 255, required = true, defaultValue = undefined) {
|
||||
await databases.createStringAttribute(DATABASE_ID, collectionId, key, size, required, defaultValue);
|
||||
}
|
||||
|
||||
async function createDatetimeAttribute(collectionId, key, required = true) {
|
||||
await databases.createDatetimeAttribute(DATABASE_ID, collectionId, key, required);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// cloud_accounts
|
||||
await createCollection({ id: 'cloud_accounts', name: 'Cloud Accounts' });
|
||||
await createStringAttribute('cloud_accounts', 'provider', 20);
|
||||
await createStringAttribute('cloud_accounts', 'apiKey', 512);
|
||||
await createStringAttribute('cloud_accounts', 'label', 100);
|
||||
|
||||
// projects
|
||||
await createCollection({ id: 'projects', name: 'Projects' });
|
||||
await createStringAttribute('projects', 'name', 200);
|
||||
await createStringAttribute('projects', 'repoUrl', 500);
|
||||
await createStringAttribute('projects', 'deployStatus', 100);
|
||||
|
||||
// audit_logs
|
||||
await createCollection({ id: 'audit_logs', name: 'Audit Logs' });
|
||||
await createStringAttribute('audit_logs', 'action', 200);
|
||||
await createDatetimeAttribute('audit_logs', 'timestamp');
|
||||
await createStringAttribute('audit_logs', 'userId', 128);
|
||||
|
||||
console.log('Collections e atributos criados.');
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error('Erro ao criar collections:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
```
|
||||
Loading…
Reference in a new issue