@adonisjs/lucid
Version:
SQL ORM built on top of Active Record pattern
120 lines (119 loc) • 3.62 kB
JavaScript
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { INTERNAL_TYPES } from "./mappings.js";
/**
* Default schema rules for mapping internal types to TypeScript types
*/
export const DEFAULT_SCHEMA_RULES = {
types: {
[]: {
tsType: 'number',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'bigint | number',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'string',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'boolean',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'string',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'DateTime',
imports: [{ source: 'luxon', namedImports: ['DateTime'] }],
decorator: '@column.date()',
},
[]: {
tsType: 'string',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'Buffer',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'any',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'any',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'DateTime',
imports: [{ source: 'luxon', namedImports: ['DateTime'] }],
decorator: '@column.dateTime()',
},
[]: {
tsType: 'string',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'string',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'string',
imports: [],
decorator: '@column()',
},
[]: {
tsType: 'any',
imports: [],
decorator: '@column()',
},
},
columns: {
id: (dataType) => {
const inferredDataType = typeof DEFAULT_SCHEMA_RULES.types[dataType] === 'function'
? DEFAULT_SCHEMA_RULES.types[dataType](dataType)
: DEFAULT_SCHEMA_RULES.types[dataType];
return {
tsType: inferredDataType?.tsType ?? dataType,
imports: inferredDataType?.imports ?? [],
decorator: '@column({ isPrimary: true })',
};
},
password: {
tsType: 'string',
imports: [],
decorator: '@column({ serializeAs: null })',
},
created_at: {
tsType: 'DateTime',
imports: [{ source: 'luxon', namedImports: ['DateTime'] }],
decorator: '@column.dateTime({ autoCreate: true })',
},
updated_at: {
tsType: 'DateTime',
imports: [{ source: 'luxon', namedImports: ['DateTime'] }],
decorator: '@column.dateTime({ autoCreate: true, autoUpdate: true })',
},
},
tables: {},
};