Schema Map

Schema is an object that notion uses to define the structure of a database. It looks something like this

const schema = {
"LXec": {
"name": "Number",
"type": "number"
},
// schema_key/schema_id
"title": {
// schema_info
"name": "Title",
"type": "title"
}
}

Parts of a schema#

  1. schema_id: The schema id is the key used to store the schema, the schema ids in the above schema object are LXec and title.
  2. name: The name stores the name of the column/property/schema_unit
  3. type: The type key contains the type of data stored.

Schema Map#

To verify and obtain the property referenced in the formula, all the exposed methods expect an optional second argument that is a map of the schema hence named schema map.

It has the following structure

  1. schema_id: string
  2. name: string
  3. type: TSchemaUnitType
Different schema_map structure

Schema Unit types like multi_select, select, formula, and relation, rollup are a bit more complex than the above mentioned structure

Example#

Lets see an example of a simple schema_map:

const schema_map = new Map([
[ 'Checkbox', { schema_id: 'checkbox', type: 'checkbox', name: 'Checkbox' } ],
[ 'Number', { schema_id: 'number', type: 'number', name: 'Number' } ]
]);

Notice the syntax used to create a map. The Map constructor takes an array of entries, where the first item is the key and the second item is the value stored in that particular key.

Name and Key

Remember to keep the name and the key of each entries the same. For example this wont work

const schema_map = new Map([
// the key is Checkbox but the name is 'checkbox name'
[ 'Checkbox', { schema_id: 'checkbox', type: 'checkbox', name: 'checkbox name' } ],
]);

Creation#

There are two ways you can create a schema_map

  1. You can create a schema_map manually, using the Map constructor
  2. You can use the @nishans/utils package to create a schema_map dynamically from the remote schema.
const { generateSchemaMap } = require("@nishans/utils");
async function main(){
const schema_map = await generateSchemaMap(TOKEN, cvp_id);
console.log(schema_map);
}
main()
Last updated on