Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/generators/configurations/generateEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type GenerateEnvProps = Pick<

const databaseURLS = {
cockroachdb: 'postgresql://root@localhost:26257/database',
gel: 'gel://user:password@localhost:5432/database',
gel: 'gel://admin@localhost:5656/main?tls_security=insecure',
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is using insecure a problem further down the line, like i think that means it needs to run on https correct? if its one of those things where its different for dev and prod we will need to put some sort of toggle

mariadb: 'mariadb://user:userpassword@localhost:3306/database',
mongodb: 'mongodb://user:password@localhost:27017/database',
mssql: 'Server=localhost,1433;Database=master;User Id=sa;Password=SApassword1;Encrypt=true;TrustServerCertificate=true',
Expand Down
12 changes: 10 additions & 2 deletions src/generators/configurations/generatePackageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const dbScripts = {
clientCmd: 'cockroach sql --insecure --database=database',
waitCmd: initTemplates.cockroachdb.wait
},
gel: {
clientCmd:
'gel -H localhost -P 5656 -u admin --tls-security insecure -b main',
waitCmd: initTemplates.gel.wait
},
mariadb: {
clientCmd:
'MYSQL_PWD=userpassword mariadb -h127.0.0.1 -u user database',
Expand Down Expand Up @@ -210,8 +215,7 @@ export const createPackageJson = ({
databaseEngine !== undefined &&
databaseEngine !== 'none' &&
databaseEngine !== 'sqlite' &&
databaseEngine !== 'mongodb' &&
databaseEngine !== 'gel' // TODO: Add support for gel
databaseEngine !== 'mongodb'
) {
const config = dbScripts[databaseEngine];
const dockerPrefix = `docker compose -p ${databaseEngine} -f db/docker-compose.db.yml`;
Expand Down Expand Up @@ -250,6 +254,10 @@ export const createPackageJson = ({
);
}

if (isLocal && databaseEngine === 'gel') {
dependencies['gel'] = resolveVersion('gel', '2.1.1');
}

if (isLocal && databaseEngine === 'sqlite') {
scripts['db:sqlite'] = 'sqlite3 db/database.sqlite';
scripts['db:init'] = 'sqlite3 db/database.sqlite < db/init.sql';
Expand Down
42 changes: 29 additions & 13 deletions src/generators/db/dockerInitTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,33 @@ BEGIN
);
END;`;

const gelUsers = `CREATE TABLE IF NOT EXISTS users (
auth_sub VARCHAR(255) PRIMARY KEY,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
metadata JSON DEFAULT '{}'::json
);`;

const gelCountHistory = `CREATE TABLE IF NOT EXISTS count_history (
uid INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
count INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);`;
const gelUsers = `create type users {
create required property auth_sub: str {
create constraint exclusive;
};

create required property created_at: datetime {
set default := datetime_current();
};

create required property metadata: json {
set default := to_json('{}');
};
Comment thread
bnziv marked this conversation as resolved.
};`;

const gelCountHistory = `create scalar type CountHistoryUid extending sequence;
create type count_history {
create required property uid: CountHistoryUid {
create constraint exclusive;
set default := sequence_next(introspect CountHistoryUid);
};

create required property count: int16;

create required property created_at: datetime {
set default := datetime_current();
};
};`;
Comment thread
coderabbitai[bot] marked this conversation as resolved.

export const userTables = {
cockroachdb: cockroachdbUsers,
Expand Down Expand Up @@ -115,8 +131,8 @@ export const initTemplates = {
wait: 'until (cockroach sql --insecure -e "select 1" >/dev/null 2>&1) ; do sleep 1; done'
},
gel: {
cli: 'psql -U user -d database -c',
wait: 'until pg_isready -U user -h localhost --quiet; do sleep 1; done'
cli: 'gel query -H localhost -P 5656 -u admin --tls-security insecure -b main ',
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as above about tls insecure

wait: 'until gel query -H localhost -P 5656 -u admin --tls-security insecure "select 1"; do sleep 1; done'
},
mariadb: {
cli: 'MYSQL_PWD=userpassword mariadb -h127.0.0.1 -u user database -e',
Expand Down
5 changes: 5 additions & 0 deletions src/generators/db/generateDatabaseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export const generateDatabaseTypes = ({
isDrizzleDialect(databaseEngine)
) {
switch (databaseEngine) {
case 'gel':
dbImport = `import { GelJsDatabase } from 'drizzle-orm/gel';`;
dbTypeLine =
'export type DatabaseType = GelJsDatabase<SchemaType>;';
break;
case 'mariadb':
case 'mysql':
dbImport = `import { Mysql2Database } from 'drizzle-orm/mysql2';`;
Expand Down
10 changes: 4 additions & 6 deletions src/generators/db/generateDockerContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ const templates: Record<
},
gel: {
env: {
GEL_DB: 'database',
GEL_PASSWORD: 'password',
GEL_USER: 'user'
GEL_SERVER_SECURITY: 'insecure_dev_mode'
},
image: 'gel:latest',
port: '4000:4000',
volumePath: '/var/lib/gel'
image: 'geldata/gel:latest',
port: '5656:5656',
volumePath: '/var/lib/gel/data'
},
mariadb: {
env: {
Expand Down
4 changes: 2 additions & 2 deletions src/generators/db/generateDrizzleSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { AuthProvider, AvailableDrizzleDialect } from '../../types';

const DIALECTS = {
gel: {
builders: ['text', 'gelTable', 'timestamp', 'integer'],
json: 'text()',
builders: ['text', 'gelTable', 'timestamp', 'integer', 'json'],
json: 'json()',
pkg: 'gel-core',
string: 'text()',
table: 'gelTable',
Expand Down
47 changes: 32 additions & 15 deletions src/generators/db/handlerTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,27 @@ const mongodbQueryOperations: QueryOperations = {
return user ?? null`
};

const gelSqlQueryOperations: QueryOperations = {
insertHistory: `await db.query('INSERT INTO count_history (count) VALUES (?)', [count])
const [rows] = await db.query('SELECT * FROM count_history ORDER BY uid DESC LIMIT 1')
return rows[0]`,
insertUser: `await db.query('INSERT INTO users (auth_sub, metadata) VALUES (?, ?)', [authSub, JSON.stringify(userIdentity)])
const [rows] = await db.query('SELECT * FROM users WHERE auth_sub = ? LIMIT 1', [authSub])
const newUser = rows[0]
if (!newUser) throw new Error('Failed to create user')
const gelClientQueryOperations: QueryOperations = {
insertHistory: `const newHistory = await db.queryRequiredSingle(
'select (insert count_history { count := <int16>$count }) { uid, count, created_at }',
{ count }
)
return newHistory`,
insertUser: `const newUser = await db.queryRequiredSingle(
'select (insert users { auth_sub := <str>$authSub, metadata := <json>$metadata }) { auth_sub, created_at, metadata }',
{ authSub, metadata: userIdentity }
)
return newUser`,
selectHistory: `const [rows] = await db.query('SELECT * FROM count_history WHERE uid = ? LIMIT 1', [uid])
return rows[0] ?? null`,
selectUser: `const [rows] = await db.query('SELECT * FROM users WHERE auth_sub = ? LIMIT 1', [authSub])
return rows[0] ?? null`
selectHistory: `const history = await db.querySingle(
'select count_history { uid, count, created_at } filter .uid = <int64>$uid',
{ uid }
)
return history ?? null`,
selectUser: `const user = await db.querySingle(
'select users { auth_sub, created_at, metadata } filter .auth_sub = <str>$authSub',
{ authSub }
)
return user ?? null`
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const singlestoreSqlQueryOperations: QueryOperations = {
Expand Down Expand Up @@ -319,10 +327,19 @@ const driverConfigurations = {
importLines: `import { SQL } from 'bun'`,
queries: postgresSqlQueryOperations
},
'gel:drizzle:local': {
dbType: 'GelJsDatabase<SchemaType>',
importLines: `
import { eq } from 'drizzle-orm'
import { GelJsDatabase } from 'drizzle-orm/gel'
import { schema, type SchemaType } from '../../../db/schema'
`,
queries: drizzleQueryOperations
},
'gel:sql:local': {
dbType: 'GelClient',
importLines: `import { GelClient } from 'gel'`,
queries: gelSqlQueryOperations
dbType: 'Client',
importLines: `import { Client } from 'gel'`,
queries: gelClientQueryOperations
},
'mariadb:drizzle:local': {
dbType: 'MySql2Database<SchemaType>',
Expand Down
2 changes: 1 addition & 1 deletion src/generators/project/generateDBBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const connectionMap: Record<string, Record<string, DBExpr>> = {
none: { expr: 'new SQL(getEnv("DATABASE_URL"))' }
},
gel: {
none: { expr: 'gelClient({ url: getEnv("DATABASE_URL") })' }
none: { expr: 'createClient(getEnv("DATABASE_URL"))' }
},
mariadb: {
none: { expr: 'new SQL(getEnv("DATABASE_URL"))' }
Expand Down
10 changes: 8 additions & 2 deletions src/generators/project/generateImportsBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export const generateImportsBlock = ({

const ormDatabaseImports = {
drizzle: {
gel: [],
gel: [
`import { createClient } from 'gel'`,
`import { drizzle } from 'drizzle-orm/gel'`
],
mariadb: [
`import { drizzle } from 'drizzle-orm/mysql2'`,
`import { createPool } from 'mysql2/promise'`
Expand Down Expand Up @@ -151,7 +154,10 @@ export const generateImportsBlock = ({
`import { SQL } from 'bun'`,
`import { getEnv } from '@absolutejs/absolute'`
],
gel: [],
gel: [
`import { createClient } from 'gel'`,
`import { getEnv } from '@absolutejs/absolute'`
],
mariadb: [
`import { SQL } from 'bun'`,
`import { getEnv } from '@absolutejs/absolute'`
Expand Down