Docly Developers
Knowledge base Best practice Recommended

Model a record as a document named by its key

In shortA record - a transfer, a case, an order with attachments - is one schema-bound document named by its key. Its fields hold all metadata and state, and everything that repeats is a table field in the same document: the files as embedded files in a file column, log entries, sent codes. Code finds the record by one path. No folder per record, no index files, no helper documents.
Applies to DataSchemasAPI
  1. What you'll see

    You are building a module that keeps records with files of their own: a file transfer, a support case with attachments, an order with documents. Two designs look reasonable and both add structure Docly does not need. One comes from databases: readable folder names, an index that maps ids to folders, state in a separate JSON file and the bytes in helper documents. The other gives every record a folder with a folder schema, and puts each file in a document of its own inside that folder.

    Both spread one record over many files. That is more to keep in sync, more to clean up, and a record nobody can open as a whole.

  2. What's actually happening

    A schema-bound document is a record. Its fields are the record's fields, and the schema gives it an editor form where the whole record — settings, files, log — can be read and corrected in one place.

    A table field holds what repeats. One row per file, one row per log entry, one row per sent code. A file column (file field) in a table stores the id of an embedded file in the same document, so the document owns its bytes: the editor shows them as its attachments, and an embedded file that no row refers to is removed. Verified 2026-11-28: a 17 MB file stored as two embedded files in two table rows was read back byte for byte identical (SHA-256).

    The document's name is the record's key, so finding a record is one path: docly.getFile("#/Transfers/" + key, false). An index repeats what the file tree already gives you, can disagree with it, and every write to it is a read-modify-write two requests can race on — the reason Keep private data under the # folder already rules out JSON files whose entries point at other files by id.

    A folder per record is only worth it when the record really is several documents that live on their own, for example pages that are published, or files that several people write at the same time.

  3. What to do

    Give each record one document, named by its key, in one folder for that kind of record:

    #/Transfers/<key>.docly    schema "Transfer"
        fields      Customer, Title, Status, ValidTo, ...   metadata and state
        Files       table: Id, Name, Size, Status, ...      one row per file
        Parts       table: FileId, Nr, Sha256, File (file)  the bytes, one embedded file per row
        Log         table: Time, Event, By                  history

    Create, read, change and attach:

    // create the record
    docly.saveFile("#/Transfers/" + key, { Customer: name, Status: "open", Files: [], Parts: [], Log: [] }, { schema: "#/Schemas/Transfer" });
    
    // read it - one path, no lookup
    var t = docly.getFile("#/Transfers/" + key, false);
    
    // change only the fields that changed
    docly.patchFile("#/Transfers/" + key, { Status: "closed" });
    
    // attach bytes to the document that owns them: embedded file + a table row that refers to it
    var id = fileId + "-" + n;
    docly.saveEmbeddedFile("#/Transfers/" + key, id, base64);
    docly.patchFile("#/Transfers/" + key, { Parts: t.Parts.concat([{ FileId: fileId, Nr: n, File: id }]) });
    • Name the document by the key the code looks up. No index, and no scanning to find one.
    • Write with patchFile and only the fields that changed. patchFile replaces each top-level field it is given, so a table is written as the whole array: read it, change it, write it back.
    • Write one document one request at a time. Two requests that update the same table at the same moment can lose a row. For chunked uploads: one file and one chunk at a time. Requests are capped at 30 MB, so large files are split into chunks, one embedded file each.
    • Use the field types the editor can show. Dropdowns for fixed values, number fields for numbers. Write timestamps as plain text in a local format — see Date and time values written from code show as empty in the editor.
    • Cap tables that grow, such as a log, so the document stays small: keep the newest few hundred rows.

    Don't:

    #/TransferIndex/<id>                    { "Folder": "2026-11-28 Customer AS - New portal" }  <- index
    #/Transfers/<key>/  (folder schema)       metadata on the folder                <- a folder that only holds one record
    #/Transfers/<key>/<fileId>.docly        one document per file                 <- the record split up
    #/Transfers/<key>/state                  schemaless, no .json                  <- state outside the schema

Last reviewed 28 Nov 2026