Skip to content

X2UsageError: Unknown record type Contact. #2

@tablecell

Description

@tablecell

app.js

// let's use MySQL in this example
const mysql = require('mysql');

// load the framework modules
const records = require('x2node-records');
const dbos = require('x2node-dbos');
const rcMonitor = require('x2node-dbos-monitor-dbtable');
const ws = require('x2node-ws');
const resources = require('x2node-ws-resources');

// create the database connection pool
const pool = mysql.createPool({
    connectionLimit: 10,
    host: '127.0.0.1',
    port:  3306,
    database:'x2',
    user: 'root',
    password: 'root',
    timezone: '+00:00'
});

// build the record types library
const recordTypes = records.with(dbos).buildLibrary({
    recordTypes: {
        'Account': {
            table: 'accounts',
            properties: {
                'id': {
                    valueType: 'number',
                    role: 'id'
                },
                'version': {
                    valueType: 'number',
                    role: 'version'
                },
                'modifiedOn': {
                    valueType: 'datetime',
                    role: 'modificationTimestamp',
                    column: 'modified_on'
                },
                'firstName': {
                    valueType: 'string',
                    column: 'fname'
                },
                'lastName': {
                    valueType: 'string',
                    column: 'lname'
                },
                'orderRefs': {
                    valueType: 'ref(Order)[]',
                    reverseRefProperty: 'accountRef'
                }
            }
        },
        'Product': {
            table: 'products',
            properties: {
                'id': {
                    valueType: 'number',
                    role: 'id'
                },
                'version': {
                    valueType: 'number',
                    role: 'version'
                },
                'modifiedOn': {
                    valueType: 'datetime',
                    role: 'modificationTimestamp',
                    column: 'modified_on'
                },
                'name': {
                    valueType: 'string'
                },
                'price': {
                    valueType: 'number'
                }
            }
        },
        'Order': {
            table: 'orders',
            properties: {
                'id': {
                    valueType: 'number',
                    role: 'id'
                },
                'version': {
                    valueType: 'number',
                    role: 'version'
                },
                'modifiedOn': {
                    valueType: 'datetime',
                    role: 'modificationTimestamp',
                    column: 'modified_on'
                },
                'accountRef': {
                    valueType: 'ref(Account)',
                    column: 'account_id',
                    modifiable: false
                },
                'placedOn': {
                    valueType: 'datetime',
                    column: 'placed_on',
                    modifiable: false
                },
                'status': {
                    valueType: 'string'
                },
                'items': {
                    valueType: 'object[]',
                    table: 'order_items',
                    parentIdColumn: 'order_id',
                    properties: {
                        'id': {
                            valueType: 'number',
                            role: 'id'
                        },
                        'productRef': {
                            valueType: 'ref(Product)',
                            column: 'product_id',
                            modifiable: false
                        },
                        'quantity': {
                            valueType: 'number'
                        }
                    }
                }
            }
        }
    }
});

// create DBO factory
const dboFactory = dbos.createDBOFactory(recordTypes, 'mysql');

// create the data source for the database connections
const ds = dboFactory.adaptDataSource(pool);

// add record collections monitor to support "ETag" and "Last-Modified" headers
rcMonitor.assignTo(dboFactory, ds);

// create API endpoint handlers factory
const handlers = resources.createResourceHandlersFactory(ds, dboFactory);

// create, configure and run the application
ws.createApplication()
    .on('shutdown', () => {
        pool.end();
    })
    .addEndpoint(
        '/accounts',
        handlers.collectionResource('Contact')
    )
    .addEndpoint(
        '/accounts/([1-9][0-9]*)',
        handlers.individualResource('Contact')
    )
    .addEndpoint(
        '/products',
        handlers.collectionResource('Product')
    )
    .addEndpoint(
        '/products/([1-9][0-9]*)',
        handlers.individualResource('Product')
    )
    .addEndpoint(
        '/orders',
        handlers.collectionResource('Order')
    )
    .addEndpoint(
        '/orders/([1-9][0-9]*)',
        handlers.individualResource('Order')
    )
    .run(3000);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions