# Add Memory Table

## Content

A memory table stores data locally in the client.
Data is provided directly through the `data` option when calling [`addTable`](/spreadjs/api/classes/GC.Data.DataManager#addtable).
All operations occur in memory.
No server communication is involved.
Memory tables are suitable for:

* Static datasets
* Client-side applications
* Prototyping scenarios
* Offline use cases

## Creating a Memory Table

Use the `data` property of [`IDataSourceOption`](/spreadjs/api/modules/GC.Data#idatasourceoption):

```javascript
const dataManager = spread.dataManager();

const table = dataManager.addTable("employees", {
    data: [
        { id: 1, name: "Alice", age: 24 },
        { id: 2, name: "Bob", age: 26 },
        { id: 3, name: "Cindy", age: 25 }
    ]
});
```

The table is now defined, but not yet initialized.

## Required Initialization

After creating a table, call:

```javascript
table.fetch();
```

Although the data is already available locally, calling [`fetch()`](/spreadjs/api/classes/GC.Data.Table#fetch) ensures:

* The internal data state is initialized
* Schema processing (if defined) is applied
* The table is ready for View creation

Example:

```javascript
table.fetch().then(function () {
    const view = table.addView("defaultView");
});
```

All table types require `fetch()` before further operations.

## Providing Data Formats

The `data` option supports multiple formats:

* JSON array (default)
* CSV string
* XML string

If `data` is a JSON array, no additional configuration is required.

### JSON (Default)

```javascript
const table = dataManager.addTable("employees", {
    data: [
        { id: 1, name: "Alice", price: 24 },
        { id: 2, name: "Bob", price: 26 },
        { id: 3, name: "Cindy", age: 25 }
    ]
});
```

JSON is the default data type.
If `schema.type` is not specified, it is treated as `"json"`.

### CSV

When providing CSV data as a string, you must specify:

```javascript
schema: {
    type: "csv"
}
```

Example:

```javascript
const table = dataManager.addTable("employees", {
    data: `id,name,price
1,Alice,24
2,Bob,26
3,Cindy,25`,
    schema: {
        type: "csv"
    }
});
```

>type=note
> Without `schema.type = "csv"`, the string will be treated as a normal JSON value, and parsing will not occur.

### XML

When providing XML data, you must specify:

* `schema.type = "xml"`
* `schema.dataPath` (if the collection is nested)

Example:

```javascript
const table = dataManager.addTable("employees", {
    data: `
<root>
    <row>
        <id>1</id>
        <name>Alice</name>
        <price>24</price>
    </row>
    <row>
        <id>2</id>
        <name>Bob</name>
        <price>26</price>
    </row>
    <row>
        <id>3</id>
        <name>Cindy</name>
        <price>25</price>
    </row>
</root>
`,
    schema: {
        type: "xml",
        dataPath: "root.row"
    }
});
```

Explanation:

* `type: "xml"` enables XML parsing.
* `dataPath` specifies where the record collection resides.
* If `dataPath` is not specified, the entire parsed object is returned.

## Schema in Memory Tables

In memory tables, `schema` serves two primary purposes:

1. Data parsing (when using CSV or XML)
2. Column modeling and data behavior

If no schema is provided:

* JSON data is inferred automatically
* Columns are generated from object keys

Schema configuration is documented in the [Schema](/spreadjs/docs/features/data-manager/schema) section.
This section demonstrates typical usage in memory tables.

### Parsing Behavior

The `schema.type` property determines how the `data` string is parsed:

| schema.type | Expected data format |
| ----------- | -------------------- |
| json (default) | JSON array |
| csv | CSV string |
| xml | XML string |
| columnJson | Column-based JSON structure |

If a string is provided without setting `schema.type`, it is treated as `"json"` and will not be parsed as CSV or XML.

### Column Modeling

Schema can define column structure and data types:

```javascript
const table = dataManager.addTable("orders", {
    data: [
        { id: 1, orderDate: "2025-01-01" }
    ],
    schema: {
        columns: {
            orderDate: {
                dataType: "date"
            }
        }
    }
});
```

Column definitions allow:

* Data type conversion
* Formatting behavior
* Primary key definition
* Advanced column configuration

### Spreading Nested Fields

If a field contains a nested object, it can be expanded into multiple columns using `spread`.
Example:

```javascript
const table = dataManager.addTable("sales", {
    data: [
        {
            id: 1,
            address: {
                country: "USA",
                state: "California",
                city: "San Francisco",
                postalCode: "94105"
            }
        },
        {
            id: 2,
            address: {
                country: "Germany",
                state: "Berlin",
                city: "Berlin",
                postalCode: "10115"
            }
        }
    ],
    schema: {
        columns: {
            address: {
                spread: true
            }
        }
    }
});
```

After `fetch()`, the nested fields become separate columns.
This allows structured data to be flattened for View usage.

## Export and Import

Memory tables support export and import through:

* JSON
* SJS

Since data resides entirely in memory, full serialization is supported.