2023-08-12 14:28:29 +00:00
#! /usr/bin/env node
2023-08-12 13:36:18 +00:00
// @ts-check
const fs = require ( "fs" ) ;
const path = require ( "path" ) ;
const { execSync } = require ( "child_process" ) ;
require ( "dotenv" ) . config ( {
path : path . resolve ( process . cwd ( ) , ".env" ) ,
} ) ;
const datasquirel = require ( "../index" ) ;
const createDbFromSchema = require ( "./engine/createDbFromSchema" ) ;
2023-08-25 08:28:22 +00:00
const colors = require ( "../console-colors" ) ;
2023-08-12 13:36:18 +00:00
if ( ! fs . existsSync ( path . resolve ( process . cwd ( ) , ".env" ) ) ) {
console . log ( ".env file not found" ) ;
process . exit ( ) ;
}
const { DSQL _HOST , DSQL _USER , DSQL _PASS , DSQL _DB _NAME , DSQL _KEY , DSQL _REF _DB _NAME , DSQL _FULL _SYNC , DSQL _ENCRYPTION _KEY , DSQL _ENCRYPTION _SALT } = process . env ;
if ( ! DSQL _HOST ? . match ( /./ ) ) {
console . log ( "DSQL_HOST is required in your `.env` file" ) ;
process . exit ( ) ;
}
if ( ! DSQL _USER ? . match ( /./ ) ) {
console . log ( "DSQL_USER is required in your `.env` file" ) ;
process . exit ( ) ;
}
if ( ! DSQL _PASS ? . match ( /./ ) ) {
console . log ( "DSQL_PASS is required in your `.env` file" ) ;
process . exit ( ) ;
}
const dbSchemaLocalFilePath = path . resolve ( process . cwd ( ) , "dsql.schema.json" ) ;
async function run ( ) {
let schemaData ;
if ( DSQL _KEY && DSQL _REF _DB _NAME ? . match ( /./ ) ) {
const dbSchemaDataResponse = await datasquirel . getSchema ( {
key : DSQL _KEY ,
database : DSQL _REF _DB _NAME || undefined ,
} ) ;
if ( ! dbSchemaDataResponse . payload || Array . isArray ( dbSchemaDataResponse . payload ) ) {
console . log ( "DSQL_KEY+DSQL_REF_DB_NAME => Error in fetching DB schema" ) ;
console . log ( dbSchemaDataResponse ) ;
process . exit ( ) ;
}
let fetchedDbSchemaObject = dbSchemaDataResponse . payload ;
if ( DSQL _DB _NAME ) fetchedDbSchemaObject . dbFullName = DSQL _DB _NAME ;
schemaData = [ fetchedDbSchemaObject ] ;
} else if ( DSQL _KEY ) {
const dbSchemaDataResponse = await datasquirel . getSchema ( {
key : DSQL _KEY ,
database : DSQL _REF _DB _NAME || undefined ,
} ) ;
if ( ! dbSchemaDataResponse . payload || ! Array . isArray ( dbSchemaDataResponse . payload ) ) {
console . log ( "DSQL_KEY => Error in fetching DB schema" ) ;
console . log ( dbSchemaDataResponse ) ;
process . exit ( ) ;
}
let fetchedDbSchemaObject = dbSchemaDataResponse . payload ;
// fetchedDbSchemaObject.forEach((db, index) => {
// db.dbFullName = db.dbFullName?.replace(/^datasquirel_user_\d+_/, "");
// });
schemaData = fetchedDbSchemaObject ;
} else if ( fs . existsSync ( dbSchemaLocalFilePath ) ) {
2023-08-25 07:44:17 +00:00
schemaData = [ JSON . parse ( fs . readFileSync ( dbSchemaLocalFilePath , "utf8" ) ) ] ;
2023-08-12 13:36:18 +00:00
} else {
console . log ( "No source for DB Schema. Please provide a local `dsql.schema.json` file, or provide `DSQL_KEY` and `DSQL_REF_DB_NAME` environment variables." ) ;
process . exit ( ) ;
}
if ( ! schemaData ) {
console . log ( "No schema found" ) ;
process . exit ( ) ;
}
if ( DSQL _FULL _SYNC ? . match ( /true/i ) ) {
fs . writeFileSync ( dbSchemaLocalFilePath , JSON . stringify ( schemaData [ 0 ] , null , 4 ) , "utf8" ) ;
}
2023-08-25 08:28:22 +00:00
console . log ( ` - ${ colors . FgBlue } Info: ${ colors . Reset } Now generating and mapping databases ... ` ) ;
2023-08-12 13:36:18 +00:00
// deepcode ignore reDOS: <please specify a reason of ignoring this>
await createDbFromSchema ( schemaData ) ;
2023-08-25 08:28:22 +00:00
console . log ( ` - ${ colors . FgGreen } Success: ${ colors . Reset } Databases created Successfully! ` ) ;
2023-08-12 13:36:18 +00:00
}
// let timeout;
let interval ;
2023-08-25 08:28:22 +00:00
if ( fs . existsSync ( dbSchemaLocalFilePath ) && ! DSQL _KEY ? . match ( /....../ ) ) {
fs . watchFile ( dbSchemaLocalFilePath , { interval : 1000 } , ( curr , prev ) => {
console . log ( ` - ${ colors . FgBlue } Info: ${ colors . Reset } Syncing Databases Locally ... ` ) ;
run ( ) ;
} ) ;
} else if ( DSQL _KEY ? . match ( /....../ ) ) {
interval = setInterval ( ( ) => {
console . log ( ` - ${ colors . FgMagenta } Info: ${ colors . Reset } Syncing Databases from the cloud ... ` ) ;
run ( ) ;
} , 20000 ) ;
}
2023-08-12 13:36:18 +00:00
run ( ) ;