# Instructions for Fluent Table API
Always reference the Table API to create Table (Dictionary) for more details.
1. Generated code should always start with proper import statements for referenced types.
2. Tables should always be assigned to an exported variable with the same name as the table name field to ensure proper typeahead support for Columns. 
3. The `name` field must be prefixed with scope name. If `name` is not prefixed with scope name there will be a design time error.
4. The `extends` field takes a table name as a string.
5. The `index` takes an array of column names to generate indexes in bootstrap.xml. To specify a composite index, add multiple column names to 'element' array.
6. Columns are specified as additional function calls within the Table API on the `schema` key. The `name` field is taken from the variable name or object key provided to the Table API.
7. For creating attributes refer to the provided attributes in the table spec.
8. Here are valid `functionDefinition` functions that Column objects can use: [
    'add',
    'coalesce',
    'concat',
    'datediff',
    'dayofweek',
    'distance_sphere',
    'divide',
    'greatest',
    'least',
    'length',
    'multiply',
    'position',
    'substring',
    'subtract'
]
9. `Now.attach('./images/logo.png')` attaches an image to a record. Do not use it as a column `default` (e.g. on `BasicImageColumn`): SDK 4.14.0 type-checks that but writes the literal `Symbol(NowAttachShape)` as the dictionary default and creates no sys_attachment; use it in record data fields such as `Record({ table: 'sp_portal', data: { icon: Now.attach('./images/icon.png') } })`. The path is relative to the `.now.ts` file that calls it and must start with `./` or `../` (a bare `images/logo.png` is rejected: "Now.attach() argument must be a relative path"); the image file must exist there, inside the project. Supported formats: jpg, png, bmp, gif, jpeg, ico, svg.
10. For tables outside application scope, configure `dependencies.global.tables` in `now.config.json` and run `now-sdk dependencies` to pull type definitions. This enables type-checked references to global tables like `cmdb_ci_server` via `#now:{scope}/{category}` imports.
11. **SDK v4.6.0 — Dictionary overrides**: To override a column inherited from a parent table, place an `OverrideColumn({ baseTable, … })` entry in the child table's `schema`. This produces the underlying `sys_dictionary_override` record automatically — there is no longer any need to author a separate `Record({ table: 'sys_dictionary_override', … })`. Override-able properties typically include `mandatory`, `default`, `readOnly`, `readOnlyOption`, `display`, and `max_length` (subject to the column type).
12. **SDK v4.7.0 — Table augments**: To add columns to a table owned by another scope (a platform table like `incident`, or a cross-scope table) without creating a new table, set `augments: '<target_table_name>'` instead of `name`. In augment mode only `augments`, `schema` and — SDK v4.13.0+ — `index` are allowed — the compiler rejects `name`, `extends`, `label`, `audit`, the access flags, etc. An augment `index` may reference the columns you add or pre-existing columns of the target table. As of SDK v4.13.0 an augment no longer overwrites the target table's platform-set attributes (such as access flags): only the table name and type are written. The build emits `sys_dictionary` records for the new columns but no `sys_db_object`. Every added column name MUST begin with the current app's ownership prefix to avoid collisions with platform fields: `<scope>_` in an `x_` scope (e.g. `x_acme_`), or `u_` in global — the build rejects anything else, so a Store app (always an `x_` scope) uses its scope prefix, never `u_`. Name the exported variable after the augmented table. Use `augments` only when the target table already exists on the platform; to create a brand-new table, use `name` as usual.
13. **SDK v4.10.1 — `actions` object form**: Always use the `TableActionAccess` object form, `actions: { read: true, create: true }`. The array form (`actions: ['read', 'create']`) is deprecated because it is a **complete enumeration**: every action it leaves out is written as `false`, so `actions: ['read']` silently writes `update_access=false`, `delete_access=false` and `create_access=false` as well. The object form is three-state instead — `true` and `false` are both written to the table's metadata, while an action you omit is not written at all, leaving the instance's install-time default in place. The same "only what you set is written" rule applies to `allowClientScripts`, `allowConfiguration` (SDK v4.13.3+), `allowNewFields`, `allowUiActions` and `allowWebServiceAccess`: the SDK derives no defaults for them, so set each one you care about explicitly. Note `maxLength` likewise has no SDK-applied default any more — omit it to inherit the instance's install-time default. Keep using the object form even though the SDK 4.13 table-api doc shows only `TableActionAccess`: the deprecated array form still compiles and still writes `false` for every omitted action.
14. **SDK v4.10.1 — state-driven requirements belong in a State Model.** When a field is required only to complete one specific move between states (rather than whenever the record sits in that state, however it got there), express it as a `'Mandatory Fields'` condition on that transition in a State Model instead of a `DataPolicy`. See the `state-model` spec.
15. **SDK v4.11.0** — `sizeClass?: number` declares the table's expected size category. It is a free-form number: the SDK derives no default and validates no range, so only set it when you have a specific classification to record from the platform's sizing guidance.
16. **SDK v4.13.3** — `allowConfiguration: boolean` controls design-time configuration of the table itself from other application scopes (written as `configuration_access`); omit it to inherit the parent table's value. `dbObjectId: true` pins the generated `sys_db_object` record to the table's own sys_id (written as `db_object_id`) — set it only when other metadata references the `sys_db_object` record directly. Neither is allowed with `augments`.
17. An `index` entry needs only `unique` and `element`; `name` is optional.
18. **SDK v4.14.0 — `basePolicy: 'public' | 'private'`** sets the default for every access flag you do not set explicitly: `'public'` writes true and `'private'` writes false for the four `actions` keys, `allowNewFields`, `allowUiActions` and `allowClientScripts`. Explicit values always win, so the usual pattern is `basePolicy: 'private'` plus the few flags you want opened. `allowWebServiceAccess` stays true under both policies (a platform legacy behaviour), so set `allowWebServiceAccess: false` explicitly when web-service access must be closed; `allowConfiguration` is not governed by the policy. Not allowed with `augments`.
19. **SDK v4.14.0 — table-level roles.** A named table (not an augment) accepts `createRoles`, `readRoles`, `writeRoles` and `deleteRoles`, each a `(string | Role)[]` written as comma-separated role names. Pass the app's `Role()` records, one role per array entry (a comma inside a role string is a build error). Columns accept the same four properties (see the `column` spec).
20. **Release-note corrections (SDK v4.14.0).** "`Table` supports `basePolicy`" omits that the policy never governs `allowWebServiceAccess` (always true) or `allowConfiguration`, and that it is rejected in augment mode and on a table written with the deprecated snake_case keys (`read_only`, `allow_web_service_access`, …), whose branch types `basePolicy` as `never` — migrate to the camelCase keys first. The table and column role arrays (see 19) are in no release note.
21. **Sample, demo or seed rows (demo-data-guide, SDK v4.14.2 docs).** Add each row with `Record()` and `$meta: { installMethod: 'demo' }`: it is written to the `unload.demo` folder, which `now-sdk install` loads by default (`--demoData` defaults to true). A sample row without it installs as application source, and later incremental installs can fail with `Found <file>.xml with invalid scope`. Do this only for tables that do not extend `sys_metadata`, such as the app's own data tables; properties, roles, choices and other `sys_metadata` records are application metadata, not demo data. The build does not enforce this.
