node_modules ignore

This commit is contained in:
2025-05-08 23:43:47 +02:00
parent e19d52f172
commit 4574544c9f
65041 changed files with 10593536 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
/// <reference types="node" />
import type { Knex } from 'knex';
import Dialect from '../dialect';
import SqliteSchemaInspector from './schema-inspector';
import type { Database } from '../..';
export default class SqliteDialect extends Dialect {
schemaInspector: SqliteSchemaInspector;
constructor(db: Database);
configure(conn?: Knex.Sqlite3ConnectionConfig): void;
useReturning(): boolean;
initialize(nativeConnection: unknown): Promise<void>;
canAlterConstraints(): boolean;
getSqlType(type: string): string;
supportsOperator(operator: string): boolean;
startSchemaUpdate(): Promise<void>;
endSchemaUpdate(): Promise<void>;
transformErrors(error: NodeJS.ErrnoException): void;
canAddIncrements(): boolean;
}
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/dialects/sqlite/index.ts"],"names":[],"mappings":";AAEA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAGjC,OAAO,OAAO,MAAM,YAAY,CAAC;AACjC,OAAO,qBAAqB,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAItC,MAAM,CAAC,OAAO,OAAO,aAAc,SAAQ,OAAO;IAChD,eAAe,EAAE,qBAAqB,CAAC;gBAE3B,EAAE,EAAE,QAAQ;IAMxB,SAAS,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,uBAAuB;IAY7C,YAAY;IAIN,UAAU,CAAC,gBAAgB,EAAE,OAAO;IAI1C,mBAAmB;IAInB,UAAU,CAAC,IAAI,EAAE,MAAM;IAkBvB,gBAAgB,CAAC,QAAQ,EAAE,MAAM;IAI3B,iBAAiB;IAIjB,eAAe;IAIrB,eAAe,CAAC,KAAK,EAAE,MAAM,CAAC,cAAc;IAW5C,gBAAgB;CAGjB"}

View File

@@ -0,0 +1,82 @@
'use strict';
var path = require('path');
var fse = require('fs-extra');
var notNull = require('../../errors/not-null.js');
var dialect = require('../dialect.js');
var schemaInspector = require('./schema-inspector.js');
const UNSUPPORTED_OPERATORS = [
'$jsonSupersetOf'
];
class SqliteDialect extends dialect {
configure(conn) {
const connection = conn || this.db.config.connection.connection;
if (typeof connection !== 'string') {
connection.filename = path.resolve(connection.filename);
}
const dbDir = path.dirname(connection.filename);
fse.ensureDirSync(dbDir);
}
useReturning() {
return true;
}
async initialize(nativeConnection) {
await this.db.connection.raw('pragma foreign_keys = on').connection(nativeConnection);
}
canAlterConstraints() {
return false;
}
getSqlType(type) {
switch(type){
case 'enum':
{
return 'text';
}
case 'double':
case 'decimal':
{
return 'float';
}
case 'timestamp':
{
return 'datetime';
}
default:
{
return type;
}
}
}
supportsOperator(operator) {
return !UNSUPPORTED_OPERATORS.includes(operator);
}
async startSchemaUpdate() {
await this.db.connection.raw(`pragma foreign_keys = off`);
}
async endSchemaUpdate() {
await this.db.connection.raw(`pragma foreign_keys = on`);
}
transformErrors(error) {
switch(error.errno){
case 19:
{
throw new notNull(); // TODO: extract column name
}
default:
{
super.transformErrors(error);
}
}
}
canAddIncrements() {
return false;
}
constructor(db){
super(db, 'sqlite');
this.schemaInspector = new schemaInspector(db);
}
}
module.exports = SqliteDialect;
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sources":["../../../src/dialects/sqlite/index.ts"],"sourcesContent":["import path from 'path';\nimport fse from 'fs-extra';\nimport type { Knex } from 'knex';\n\nimport * as errors from '../../errors';\nimport Dialect from '../dialect';\nimport SqliteSchemaInspector from './schema-inspector';\nimport type { Database } from '../..';\n\nconst UNSUPPORTED_OPERATORS = ['$jsonSupersetOf'];\n\nexport default class SqliteDialect extends Dialect {\n schemaInspector: SqliteSchemaInspector;\n\n constructor(db: Database) {\n super(db, 'sqlite');\n\n this.schemaInspector = new SqliteSchemaInspector(db);\n }\n\n configure(conn?: Knex.Sqlite3ConnectionConfig) {\n const connection =\n conn || (this.db.config.connection.connection as Knex.Sqlite3ConnectionConfig);\n if (typeof connection !== 'string') {\n connection.filename = path.resolve(connection.filename);\n }\n\n const dbDir = path.dirname(connection.filename);\n\n fse.ensureDirSync(dbDir);\n }\n\n useReturning() {\n return true;\n }\n\n async initialize(nativeConnection: unknown) {\n await this.db.connection.raw('pragma foreign_keys = on').connection(nativeConnection);\n }\n\n canAlterConstraints() {\n return false;\n }\n\n getSqlType(type: string) {\n switch (type) {\n case 'enum': {\n return 'text';\n }\n case 'double':\n case 'decimal': {\n return 'float';\n }\n case 'timestamp': {\n return 'datetime';\n }\n default: {\n return type;\n }\n }\n }\n\n supportsOperator(operator: string) {\n return !UNSUPPORTED_OPERATORS.includes(operator);\n }\n\n async startSchemaUpdate() {\n await this.db.connection.raw(`pragma foreign_keys = off`);\n }\n\n async endSchemaUpdate() {\n await this.db.connection.raw(`pragma foreign_keys = on`);\n }\n\n transformErrors(error: NodeJS.ErrnoException) {\n switch (error.errno) {\n case 19: {\n throw new errors.NotNullError(); // TODO: extract column name\n }\n default: {\n super.transformErrors(error);\n }\n }\n }\n\n canAddIncrements() {\n return false;\n }\n}\n"],"names":["UNSUPPORTED_OPERATORS","SqliteDialect","Dialect","configure","conn","connection","db","config","filename","path","resolve","dbDir","dirname","fse","ensureDirSync","useReturning","initialize","nativeConnection","raw","canAlterConstraints","getSqlType","type","supportsOperator","operator","includes","startSchemaUpdate","endSchemaUpdate","transformErrors","error","errno","errors","canAddIncrements","constructor","schemaInspector","SqliteSchemaInspector"],"mappings":";;;;;;;;AASA,MAAMA,qBAAwB,GAAA;AAAC,IAAA;AAAkB,CAAA;AAElC,MAAMC,aAAsBC,SAAAA,OAAAA,CAAAA;AASzCC,IAAAA,SAAAA,CAAUC,IAAmC,EAAE;QAC7C,MAAMC,UAAAA,GACJD,IAAS,IAAA,IAAI,CAACE,EAAE,CAACC,MAAM,CAACF,UAAU,CAACA,UAAU;QAC/C,IAAI,OAAOA,eAAe,QAAU,EAAA;AAClCA,YAAAA,UAAAA,CAAWG,QAAQ,GAAGC,IAAAA,CAAKC,OAAO,CAACL,WAAWG,QAAQ,CAAA;AACxD;AAEA,QAAA,MAAMG,KAAQF,GAAAA,IAAAA,CAAKG,OAAO,CAACP,WAAWG,QAAQ,CAAA;AAE9CK,QAAAA,GAAAA,CAAIC,aAAa,CAACH,KAAAA,CAAAA;AACpB;IAEAI,YAAe,GAAA;QACb,OAAO,IAAA;AACT;IAEA,MAAMC,UAAAA,CAAWC,gBAAyB,EAAE;QAC1C,MAAM,IAAI,CAACX,EAAE,CAACD,UAAU,CAACa,GAAG,CAAC,0BAA4Bb,CAAAA,CAAAA,UAAU,CAACY,gBAAAA,CAAAA;AACtE;IAEAE,mBAAsB,GAAA;QACpB,OAAO,KAAA;AACT;AAEAC,IAAAA,UAAAA,CAAWC,IAAY,EAAE;QACvB,OAAQA,IAAAA;YACN,KAAK,MAAA;AAAQ,gBAAA;oBACX,OAAO,MAAA;AACT;YACA,KAAK,QAAA;YACL,KAAK,SAAA;AAAW,gBAAA;oBACd,OAAO,OAAA;AACT;YACA,KAAK,WAAA;AAAa,gBAAA;oBAChB,OAAO,UAAA;AACT;AACA,YAAA;AAAS,gBAAA;oBACP,OAAOA,IAAAA;AACT;AACF;AACF;AAEAC,IAAAA,gBAAAA,CAAiBC,QAAgB,EAAE;QACjC,OAAO,CAACvB,qBAAsBwB,CAAAA,QAAQ,CAACD,QAAAA,CAAAA;AACzC;AAEA,IAAA,MAAME,iBAAoB,GAAA;QACxB,MAAM,IAAI,CAACnB,EAAE,CAACD,UAAU,CAACa,GAAG,CAAC,CAAC,yBAAyB,CAAC,CAAA;AAC1D;AAEA,IAAA,MAAMQ,eAAkB,GAAA;QACtB,MAAM,IAAI,CAACpB,EAAE,CAACD,UAAU,CAACa,GAAG,CAAC,CAAC,wBAAwB,CAAC,CAAA;AACzD;AAEAS,IAAAA,eAAAA,CAAgBC,KAA4B,EAAE;AAC5C,QAAA,OAAQA,MAAMC,KAAK;YACjB,KAAK,EAAA;AAAI,gBAAA;AACP,oBAAA,MAAM,IAAIC,OAAmB,EAAA,CAAA;AAC/B;AACA,YAAA;AAAS,gBAAA;AACP,oBAAA,KAAK,CAACH,eAAgBC,CAAAA,KAAAA,CAAAA;AACxB;AACF;AACF;IAEAG,gBAAmB,GAAA;QACjB,OAAO,KAAA;AACT;AAzEAC,IAAAA,WAAAA,CAAY1B,EAAY,CAAE;AACxB,QAAA,KAAK,CAACA,EAAI,EAAA,QAAA,CAAA;AAEV,QAAA,IAAI,CAAC2B,eAAe,GAAG,IAAIC,eAAsB5B,CAAAA,EAAAA,CAAAA;AACnD;AAsEF;;;;"}

View File

@@ -0,0 +1,80 @@
import path from 'path';
import fse from 'fs-extra';
import NotNullError from '../../errors/not-null.mjs';
import Dialect from '../dialect.mjs';
import SqliteSchemaInspector from './schema-inspector.mjs';
const UNSUPPORTED_OPERATORS = [
'$jsonSupersetOf'
];
class SqliteDialect extends Dialect {
configure(conn) {
const connection = conn || this.db.config.connection.connection;
if (typeof connection !== 'string') {
connection.filename = path.resolve(connection.filename);
}
const dbDir = path.dirname(connection.filename);
fse.ensureDirSync(dbDir);
}
useReturning() {
return true;
}
async initialize(nativeConnection) {
await this.db.connection.raw('pragma foreign_keys = on').connection(nativeConnection);
}
canAlterConstraints() {
return false;
}
getSqlType(type) {
switch(type){
case 'enum':
{
return 'text';
}
case 'double':
case 'decimal':
{
return 'float';
}
case 'timestamp':
{
return 'datetime';
}
default:
{
return type;
}
}
}
supportsOperator(operator) {
return !UNSUPPORTED_OPERATORS.includes(operator);
}
async startSchemaUpdate() {
await this.db.connection.raw(`pragma foreign_keys = off`);
}
async endSchemaUpdate() {
await this.db.connection.raw(`pragma foreign_keys = on`);
}
transformErrors(error) {
switch(error.errno){
case 19:
{
throw new NotNullError(); // TODO: extract column name
}
default:
{
super.transformErrors(error);
}
}
}
canAddIncrements() {
return false;
}
constructor(db){
super(db, 'sqlite');
this.schemaInspector = new SqliteSchemaInspector(db);
}
}
export { SqliteDialect as default };
//# sourceMappingURL=index.mjs.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.mjs","sources":["../../../src/dialects/sqlite/index.ts"],"sourcesContent":["import path from 'path';\nimport fse from 'fs-extra';\nimport type { Knex } from 'knex';\n\nimport * as errors from '../../errors';\nimport Dialect from '../dialect';\nimport SqliteSchemaInspector from './schema-inspector';\nimport type { Database } from '../..';\n\nconst UNSUPPORTED_OPERATORS = ['$jsonSupersetOf'];\n\nexport default class SqliteDialect extends Dialect {\n schemaInspector: SqliteSchemaInspector;\n\n constructor(db: Database) {\n super(db, 'sqlite');\n\n this.schemaInspector = new SqliteSchemaInspector(db);\n }\n\n configure(conn?: Knex.Sqlite3ConnectionConfig) {\n const connection =\n conn || (this.db.config.connection.connection as Knex.Sqlite3ConnectionConfig);\n if (typeof connection !== 'string') {\n connection.filename = path.resolve(connection.filename);\n }\n\n const dbDir = path.dirname(connection.filename);\n\n fse.ensureDirSync(dbDir);\n }\n\n useReturning() {\n return true;\n }\n\n async initialize(nativeConnection: unknown) {\n await this.db.connection.raw('pragma foreign_keys = on').connection(nativeConnection);\n }\n\n canAlterConstraints() {\n return false;\n }\n\n getSqlType(type: string) {\n switch (type) {\n case 'enum': {\n return 'text';\n }\n case 'double':\n case 'decimal': {\n return 'float';\n }\n case 'timestamp': {\n return 'datetime';\n }\n default: {\n return type;\n }\n }\n }\n\n supportsOperator(operator: string) {\n return !UNSUPPORTED_OPERATORS.includes(operator);\n }\n\n async startSchemaUpdate() {\n await this.db.connection.raw(`pragma foreign_keys = off`);\n }\n\n async endSchemaUpdate() {\n await this.db.connection.raw(`pragma foreign_keys = on`);\n }\n\n transformErrors(error: NodeJS.ErrnoException) {\n switch (error.errno) {\n case 19: {\n throw new errors.NotNullError(); // TODO: extract column name\n }\n default: {\n super.transformErrors(error);\n }\n }\n }\n\n canAddIncrements() {\n return false;\n }\n}\n"],"names":["UNSUPPORTED_OPERATORS","SqliteDialect","Dialect","configure","conn","connection","db","config","filename","path","resolve","dbDir","dirname","fse","ensureDirSync","useReturning","initialize","nativeConnection","raw","canAlterConstraints","getSqlType","type","supportsOperator","operator","includes","startSchemaUpdate","endSchemaUpdate","transformErrors","error","errno","errors","canAddIncrements","constructor","schemaInspector","SqliteSchemaInspector"],"mappings":";;;;;;AASA,MAAMA,qBAAwB,GAAA;AAAC,IAAA;AAAkB,CAAA;AAElC,MAAMC,aAAsBC,SAAAA,OAAAA,CAAAA;AASzCC,IAAAA,SAAAA,CAAUC,IAAmC,EAAE;QAC7C,MAAMC,UAAAA,GACJD,IAAS,IAAA,IAAI,CAACE,EAAE,CAACC,MAAM,CAACF,UAAU,CAACA,UAAU;QAC/C,IAAI,OAAOA,eAAe,QAAU,EAAA;AAClCA,YAAAA,UAAAA,CAAWG,QAAQ,GAAGC,IAAAA,CAAKC,OAAO,CAACL,WAAWG,QAAQ,CAAA;AACxD;AAEA,QAAA,MAAMG,KAAQF,GAAAA,IAAAA,CAAKG,OAAO,CAACP,WAAWG,QAAQ,CAAA;AAE9CK,QAAAA,GAAAA,CAAIC,aAAa,CAACH,KAAAA,CAAAA;AACpB;IAEAI,YAAe,GAAA;QACb,OAAO,IAAA;AACT;IAEA,MAAMC,UAAAA,CAAWC,gBAAyB,EAAE;QAC1C,MAAM,IAAI,CAACX,EAAE,CAACD,UAAU,CAACa,GAAG,CAAC,0BAA4Bb,CAAAA,CAAAA,UAAU,CAACY,gBAAAA,CAAAA;AACtE;IAEAE,mBAAsB,GAAA;QACpB,OAAO,KAAA;AACT;AAEAC,IAAAA,UAAAA,CAAWC,IAAY,EAAE;QACvB,OAAQA,IAAAA;YACN,KAAK,MAAA;AAAQ,gBAAA;oBACX,OAAO,MAAA;AACT;YACA,KAAK,QAAA;YACL,KAAK,SAAA;AAAW,gBAAA;oBACd,OAAO,OAAA;AACT;YACA,KAAK,WAAA;AAAa,gBAAA;oBAChB,OAAO,UAAA;AACT;AACA,YAAA;AAAS,gBAAA;oBACP,OAAOA,IAAAA;AACT;AACF;AACF;AAEAC,IAAAA,gBAAAA,CAAiBC,QAAgB,EAAE;QACjC,OAAO,CAACvB,qBAAsBwB,CAAAA,QAAQ,CAACD,QAAAA,CAAAA;AACzC;AAEA,IAAA,MAAME,iBAAoB,GAAA;QACxB,MAAM,IAAI,CAACnB,EAAE,CAACD,UAAU,CAACa,GAAG,CAAC,CAAC,yBAAyB,CAAC,CAAA;AAC1D;AAEA,IAAA,MAAMQ,eAAkB,GAAA;QACtB,MAAM,IAAI,CAACpB,EAAE,CAACD,UAAU,CAACa,GAAG,CAAC,CAAC,wBAAwB,CAAC,CAAA;AACzD;AAEAS,IAAAA,eAAAA,CAAgBC,KAA4B,EAAE;AAC5C,QAAA,OAAQA,MAAMC,KAAK;YACjB,KAAK,EAAA;AAAI,gBAAA;AACP,oBAAA,MAAM,IAAIC,YAAmB,EAAA,CAAA;AAC/B;AACA,YAAA;AAAS,gBAAA;AACP,oBAAA,KAAK,CAACH,eAAgBC,CAAAA,KAAAA,CAAAA;AACxB;AACF;AACF;IAEAG,gBAAmB,GAAA;QACjB,OAAO,KAAA;AACT;AAzEAC,IAAAA,WAAAA,CAAY1B,EAAY,CAAE;AACxB,QAAA,KAAK,CAACA,EAAI,EAAA,QAAA,CAAA;AAEV,QAAA,IAAI,CAAC2B,eAAe,GAAG,IAAIC,qBAAsB5B,CAAAA,EAAAA,CAAAA;AACnD;AAsEF;;;;"}

View File

@@ -0,0 +1,13 @@
import type { Database } from '../..';
import type { Schema, Column, Index, ForeignKey } from '../../schema/types';
import type { SchemaInspector } from '../dialect';
export default class SqliteSchemaInspector implements SchemaInspector {
db: Database;
constructor(db: Database);
getSchema(): Promise<Schema>;
getTables(): Promise<string[]>;
getColumns(tableName: string): Promise<Column[]>;
getIndexes(tableName: string): Promise<Index[]>;
getForeignKeys(tableName: string): Promise<ForeignKey[]>;
}
//# sourceMappingURL=schema-inspector.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"schema-inspector.d.ts","sourceRoot":"","sources":["../../../src/dialects/sqlite/schema-inspector.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAqGlD,MAAM,CAAC,OAAO,OAAO,qBAAsB,YAAW,eAAe;IACnE,EAAE,EAAE,QAAQ,CAAC;gBAED,EAAE,EAAE,QAAQ;IAIlB,SAAS;IAoBT,SAAS,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAM9B,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAkBhD,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,CAAC;IAyB/C,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC;CA0B/D"}

View File

@@ -0,0 +1,211 @@
'use strict';
const SQL_QUERIES = {
TABLE_LIST: `select name from sqlite_master where type = 'table' and name NOT LIKE 'sqlite%'`,
TABLE_INFO: `pragma table_info(??)`,
INDEX_LIST: 'pragma index_list(??)',
INDEX_INFO: 'pragma index_info(??)',
FOREIGN_KEY_LIST: 'pragma foreign_key_list(??)'
};
const toStrapiType = (column)=>{
const { type } = column;
const rootType = type.toLowerCase().match(/[^(), ]+/)?.[0];
switch(rootType){
case 'integer':
{
if (column.pk) {
return {
type: 'increments',
args: [
{
primary: true,
primaryKey: true
}
]
};
}
return {
type: 'integer'
};
}
case 'float':
{
return {
type: 'float',
args: [
10,
2
]
};
}
case 'bigint':
{
return {
type: 'bigInteger'
};
}
case 'varchar':
{
const length = type.slice(8, type.length - 1);
return {
type: 'string',
args: [
Number(length)
]
};
}
case 'text':
{
return {
type: 'text',
args: [
'longtext'
]
};
}
case 'json':
{
return {
type: 'jsonb'
};
}
case 'boolean':
{
return {
type: 'boolean'
};
}
case 'datetime':
{
return {
type: 'datetime',
args: [
{
useTz: false,
precision: 6
}
]
};
}
case 'date':
{
return {
type: 'date'
};
}
case 'time':
{
return {
type: 'time',
args: [
{
precision: 3
}
]
};
}
default:
{
return {
type: 'specificType',
args: [
column.data_type
]
};
}
}
};
class SqliteSchemaInspector {
async getSchema() {
const schema = {
tables: []
};
const tables = await this.getTables();
for (const tableName of tables){
const columns = await this.getColumns(tableName);
const indexes = await this.getIndexes(tableName);
const foreignKeys = await this.getForeignKeys(tableName);
schema.tables.push({
name: tableName,
columns,
indexes,
foreignKeys
});
}
return schema;
}
async getTables() {
const rows = await this.db.connection.raw(SQL_QUERIES.TABLE_LIST);
return rows.map((row)=>row.name);
}
async getColumns(tableName) {
const rows = await this.db.connection.raw(SQL_QUERIES.TABLE_INFO, [
tableName
]);
return rows.map((row)=>{
const { type, args = [], ...rest } = toStrapiType(row);
return {
type,
args,
name: row.name,
defaultTo: row.dflt_value,
notNullable: row.notnull !== null ? Boolean(row.notnull) : null,
unsigned: false,
...rest
};
});
}
async getIndexes(tableName) {
const indexes = await this.db.connection.raw(SQL_QUERIES.INDEX_LIST, [
tableName
]);
const ret = [];
for (const index of indexes.filter((index)=>!index.name.startsWith('sqlite_'))){
const res = await this.db.connection.raw(SQL_QUERIES.INDEX_INFO, [
index.name
]);
const indexInfo = {
columns: res.map((row)=>row.name),
name: index.name
};
if (index.unique) {
indexInfo.type = 'unique';
}
ret.push(indexInfo);
}
return ret;
}
async getForeignKeys(tableName) {
const fks = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_LIST, [
tableName
]);
const ret = {};
for (const fk of fks){
if (!ret[fk.id]) {
ret[fk.id] = {
// TODO: name, // find name
name: '',
columns: [
fk.from
],
referencedColumns: [
fk.to
],
referencedTable: fk.table,
onUpdate: fk.on_update.toUpperCase(),
onDelete: fk.on_delete.toUpperCase()
};
} else {
ret[fk.id].columns.push(fk.from);
ret[fk.id].referencedColumns.push(fk.to);
}
}
return Object.values(ret);
}
constructor(db){
this.db = db;
}
}
module.exports = SqliteSchemaInspector;
//# sourceMappingURL=schema-inspector.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,209 @@
const SQL_QUERIES = {
TABLE_LIST: `select name from sqlite_master where type = 'table' and name NOT LIKE 'sqlite%'`,
TABLE_INFO: `pragma table_info(??)`,
INDEX_LIST: 'pragma index_list(??)',
INDEX_INFO: 'pragma index_info(??)',
FOREIGN_KEY_LIST: 'pragma foreign_key_list(??)'
};
const toStrapiType = (column)=>{
const { type } = column;
const rootType = type.toLowerCase().match(/[^(), ]+/)?.[0];
switch(rootType){
case 'integer':
{
if (column.pk) {
return {
type: 'increments',
args: [
{
primary: true,
primaryKey: true
}
]
};
}
return {
type: 'integer'
};
}
case 'float':
{
return {
type: 'float',
args: [
10,
2
]
};
}
case 'bigint':
{
return {
type: 'bigInteger'
};
}
case 'varchar':
{
const length = type.slice(8, type.length - 1);
return {
type: 'string',
args: [
Number(length)
]
};
}
case 'text':
{
return {
type: 'text',
args: [
'longtext'
]
};
}
case 'json':
{
return {
type: 'jsonb'
};
}
case 'boolean':
{
return {
type: 'boolean'
};
}
case 'datetime':
{
return {
type: 'datetime',
args: [
{
useTz: false,
precision: 6
}
]
};
}
case 'date':
{
return {
type: 'date'
};
}
case 'time':
{
return {
type: 'time',
args: [
{
precision: 3
}
]
};
}
default:
{
return {
type: 'specificType',
args: [
column.data_type
]
};
}
}
};
class SqliteSchemaInspector {
async getSchema() {
const schema = {
tables: []
};
const tables = await this.getTables();
for (const tableName of tables){
const columns = await this.getColumns(tableName);
const indexes = await this.getIndexes(tableName);
const foreignKeys = await this.getForeignKeys(tableName);
schema.tables.push({
name: tableName,
columns,
indexes,
foreignKeys
});
}
return schema;
}
async getTables() {
const rows = await this.db.connection.raw(SQL_QUERIES.TABLE_LIST);
return rows.map((row)=>row.name);
}
async getColumns(tableName) {
const rows = await this.db.connection.raw(SQL_QUERIES.TABLE_INFO, [
tableName
]);
return rows.map((row)=>{
const { type, args = [], ...rest } = toStrapiType(row);
return {
type,
args,
name: row.name,
defaultTo: row.dflt_value,
notNullable: row.notnull !== null ? Boolean(row.notnull) : null,
unsigned: false,
...rest
};
});
}
async getIndexes(tableName) {
const indexes = await this.db.connection.raw(SQL_QUERIES.INDEX_LIST, [
tableName
]);
const ret = [];
for (const index of indexes.filter((index)=>!index.name.startsWith('sqlite_'))){
const res = await this.db.connection.raw(SQL_QUERIES.INDEX_INFO, [
index.name
]);
const indexInfo = {
columns: res.map((row)=>row.name),
name: index.name
};
if (index.unique) {
indexInfo.type = 'unique';
}
ret.push(indexInfo);
}
return ret;
}
async getForeignKeys(tableName) {
const fks = await this.db.connection.raw(SQL_QUERIES.FOREIGN_KEY_LIST, [
tableName
]);
const ret = {};
for (const fk of fks){
if (!ret[fk.id]) {
ret[fk.id] = {
// TODO: name, // find name
name: '',
columns: [
fk.from
],
referencedColumns: [
fk.to
],
referencedTable: fk.table,
onUpdate: fk.on_update.toUpperCase(),
onDelete: fk.on_delete.toUpperCase()
};
} else {
ret[fk.id].columns.push(fk.from);
ret[fk.id].referencedColumns.push(fk.to);
}
}
return Object.values(ret);
}
constructor(db){
this.db = db;
}
}
export { SqliteSchemaInspector as default };
//# sourceMappingURL=schema-inspector.mjs.map

File diff suppressed because one or more lines are too long