Nextcloud Tables: Create a row in a table

Create a row in a table in Nextcloud Tables

Script nextcloud Verified

by marcel klehr12 · 7/23/2024

The script

Submitted by nextcloud Bun
Verified 86 days ago
1
import createClient, { type Middleware } from "openapi-fetch";
2

3
interface Data {
4
  [p: number]: any
5
}
6

7
export async function main(
8
  nextcloud: RT.Nextcloud,
9
  tableId: number,
10
  data: Data,
11
) {
12

13
  const client = createClient<paths>({ baseUrl: nextcloud.baseUrl });
14
  const authMiddleware: Middleware = {
15
    async onRequest({ request, options }) {
16
      // fetch token, if it doesn’t exist
17
      // add Authorization header to every request
18
      request.headers.set("Authorization", `Basic ${btoa((nextcloud.userId) + ':' + nextcloud.password)}`);
19
      return request;
20
    },
21
  };
22
  client.use(authMiddleware);
23

24
  const resp = await client.POST("/index.php/apps/tables/api/1/tables/{tableId}/rows", {
25
    params: {
26
      header: {
27
        "OCS-APIRequest": true,
28
      },
29
      query: {
30
        format: "json",
31
      },
32
      path: {
33
        tableId: tableId,
34
      },
35

36
    },
37
    body: {
38
      data: data
39
    },
40
  });
41

42
  return resp.data;
43

44
}
Other submissions
  • Submitted by marcel klehr12 Deno
    Created 568 days ago
    1
    import * as wmill from "npm:windmill-client@1";
    2
    import * as tb from "https://raw.githubusercontent.com/marcelklehr/nextcloud-client-deno/1629b11dc919b99d8dfadf2870a02128a24333fa/tables/index.ts";
    3
    
    
    4
    // fill the type, or use the +Resource type to get a type-safe reference to a resource
    5
    // type Postgresql = object
    6
    
    
    7
    interface Data {
    8
      [p: number]: any
    9
    }
    10
    
    
    11
    export async function main(
    12
      nextcloudResource: string,
    13
      userId: string|null = null,
    14
      tableId: number,
    15
      data: Data,
    16
      useAppApiAuth: boolean = false
    17
    ) {
    18
      const ncResource = await wmill.getResource(
    19
        nextcloudResource,
    20
      );
    21
      const config = new tb.Configuration({
    22
        username: userId || ncResource.username,
    23
        password: ncResource.password,
    24
        basePath: ncResource.baseUrl,
    25
        middleware: [{
    26
          async pre(context) {
    27
            if (!context.url.includes("?")) {
    28
              context.url += "?";
    29
            } else {
    30
              context.url += "&";
    31
            }
    32
            context.url += "format=json";
    33
            return context;
    34
          },
    35
        }],
    36
        ...(useAppApiAuth && ({
    37
          headers: {
    38
            "AA-VERSION": "2.3.0",
    39
            "EX-APP-ID": "flow",
    40
            "EX-APP-VERSION": "1.0.0",
    41
            "AUTHORIZATION-APP-API": btoa(`${userId || ncResource.username}:${ncResource.password}`),
    42
          },
    43
        })),
    44
      });
    45
      const api = new tb.Api1Api(config);
    46
    
    
    47
      return await api.api1CreateRowInTable({
    48
        tableId,
    49
        data: JSON.stringify(data)
    50
      });
    51
    }
    52
    
    
  • Submitted by nextcloud Bun
    Created 23 days ago
    This script is a newer edit of the script that had been approved, but it needs to be re-approved
    1
    import createClient, { type Middleware } from "openapi-fetch";
    2
    
    
    3
    interface Data {
    4
      [p: number]: any
    5
    }
    6
    
    
    7
    export async function main(
    8
      nextcloud: RT.Nextcloud,
    9
      tableId: number,
    10
      data: Data,
    11
    ) {
    12
    
    
    13
      const client = createClient<paths>({ baseUrl: nextcloud.baseUrl });
    14
      const authMiddleware: Middleware = {
    15
        async onRequest({ request, options }) {
    16
          // fetch token, if it doesn’t exist
    17
          // add Authorization header to every request
    18
          request.headers.set("Authorization", `Basic ${btoa((nextcloud.userId) + ':' + nextcloud.token)}`);
    19
          return request;
    20
        },
    21
      };
    22
      client.use(authMiddleware);
    23
    
    
    24
      const resp = await client.POST("/index.php/apps/tables/api/1/tables/{tableId}/rows", {
    25
        params: {
    26
          header: {
    27
            "OCS-APIRequest": true,
    28
          },
    29
          query: {
    30
            format: "json",
    31
          },
    32
          path: {
    33
            tableId: tableId,
    34
          },
    35
    
    
    36
        },
    37
        body: {
    38
          data: data
    39
        },
    40
      });
    41
    
    
    42
      return resp.data;
    43
    
    
    44
    }