getStoreTablesSchema
The getStoreTablesSchema
method returns the TablesSchema
of the Store
as an object.
getStoreTablesSchema(): TablesSchema
returns | TablesSchema | A |
---|
If the Store
does not already have an explicit TablesSchema
associated with it, the data in the Store
will be scanned to attempt to infer a new TablesSchema
.
To be successful, this requires all the values of a given Cell
across a Table
object's Row
objects to have a consistent type. If a given Cell
Id
appears in every Row
, then a default
for that Cell
is specified in the TablesSchema
, based on the most common value found.
Examples
This example creates a Tools
object and gets the schema of a Store
that already has a TablesSchema
.
import {createStore} from 'tinybase';
import {createTools} from 'tinybase/tools';
const store = createStore().setTablesSchema({
pets: {
species: {type: 'string'},
color: {type: 'string'},
},
species: {
price: {type: 'number'},
},
});
const schema = createTools(store).getStoreTablesSchema();
console.log(schema.pets);
// -> {species: {type: 'string'}, color: {type: 'string'}}
This example creates a Tools
object and infers the schema of a Store
that doesn't already have a TablesSchema
.
import {createStore} from 'tinybase';
import {createTools} from 'tinybase/tools';
const store = createStore()
.setTable('pets', {
fido: {species: 'dog', color: 'brown'},
felix: {species: 'cat', color: 'black'},
cujo: {species: 'dog', color: 'black'},
})
.setTable('species', {
dog: {price: 5, barks: true},
cat: {price: 4, purrs: true},
});
const schema = createTools(store).getStoreTablesSchema();
console.log(schema.pets.species);
// -> {type: 'string', default: 'dog'}
console.log(schema.pets.color);
// -> {type: 'string', default: 'black'}
console.log(schema.species.price);
// -> {type: 'number', default: 5}
console.log(schema.species.barks);
// -> {type: 'boolean'}
console.log(schema.species.purrs);
// -> {type: 'boolean'}
Since
v3.0.0