-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #283 from palmaresHQ:databases-migration-issues
Databases-migration-issues
- Loading branch information
Showing
23 changed files
with
259 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@palmares/databases': patch | ||
'@palmares/core': patch | ||
'@palmares/drizzle-engine': patch | ||
'@palmares/sequelize-engine': patch | ||
--- | ||
|
||
- Fix docs with better explanation on how to use palmares/databases when on standalone. | ||
- Fixed standalone to add palmares_migrations table when the engine runs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
export class SettingsNotFoundException extends Error { | ||
constructor() { | ||
super( | ||
`No settings file was found for the application.\nMake sure you either pass an option through:` + | ||
`\n-'Command.handleCommands'\n-PALMARES_SETTINGS_MODULE environment variable\n-Or inside` + | ||
` 'src' folder in the root directory` | ||
`No settings file was found for the application.\nMake sure you either pass an option through` + | ||
`'Command.handleCommands'` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ The [@palmares/databases](https://www.npmjs.com/package/@palmares/databases) pac | |
|
||
At its core it does nothing, at the same time it does everything! | ||
|
||
With 0 dependencies at its core (even no dependency on Node), you don't need to worry if it'll work on Expo. Without an adapter this will simply not do anything. But with the adapter this package offers you the ability to generate migrations, query your data and offer a really nice way to interact with your database. | ||
With 0 dependencies at its core (even no dependency on Node), you don't need to worry if it'll work on Expo, the Browser or even a Brain interface. Without an adapter this will simply not do anything. But with the adapter this package offers you the ability to generate migrations, query your data and offer a really nice way to interact with your database. | ||
|
||
Although we kinda see ourselves as an ORM, we are not **data frameworks** as drizzle like to call others like Django or Spring. You are not forced to build your project around our structure, although we think this is preferable most of the times, you are still free to use it the way you want, on your own existing projects without any hassle or problem. | ||
|
||
|
@@ -24,7 +24,16 @@ Although we kinda see ourselves as an ORM, we are not **data frameworks** as dri | |
|
||
**TIP:** This QuickStart uses [drizzle orm, reach out to their docs for reference](https://orm.drizzle.team/docs/overview) | ||
|
||
- **Step 1**. Create a `database.config.ts` with: | ||
- **Step 1**. Install a few more packages, and don't act like you cared about the number of dependencies on your projects | ||
|
||
```sh | ||
$ pnpm add @palmares/node-std @palmares/drizzle-engine | ||
$ npm i @palmares/node-std @palmares/drizzle-engine | ||
$ yarn i @palmares/node-std @palmares/drizzle-engine | ||
$ bun i @palmares/node-std @palmares/drizzle-engine | ||
``` | ||
|
||
- **Step 2**. Create a `database.config.ts` with: | ||
|
||
```ts | ||
import { | ||
|
@@ -103,11 +112,13 @@ export default setDatabaseConfig({ | |
}); | ||
``` | ||
|
||
- **Step 2**. Make your queries | ||
- **Step 3**. Make your queries | ||
|
||
- **Using your Palmares models:** | ||
|
||
```ts | ||
import './database.config'; // On this quickstart it's redundant, but make sure to initialize the DB before trying to query. | ||
|
||
import { Company, User } from './database.config'; | ||
|
||
await Company.default.set((qs) => | ||
|
@@ -118,13 +129,11 @@ export default setDatabaseConfig({ | |
firstName: 'Foo', | ||
lastName: 'bar', | ||
email: '[email protected]', | ||
isActive: true, | ||
}, | ||
{ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: '[email protected]', | ||
isActive: true, | ||
} | ||
) | ||
) | ||
|
@@ -138,15 +147,15 @@ export default setDatabaseConfig({ | |
|
||
- **Using your favorite ORM**: | ||
|
||
1. Create a file called `load.ts` and add the following: | ||
1. Create a file called `load.ts` and add the following (You are responsible for your own CLI): | ||
|
||
```ts | ||
import databasesConfig from './database.config'; | ||
databasesConfig.load(); | ||
``` | ||
|
||
2. Run (we are using to run typescript from the command line [tsx](https://tsx.is/)): | ||
2. Run (we are using [tsx](https://tsx.is/) to run typescript from the command line): | ||
|
||
```sh | ||
$ tsx load.ts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
packages/databases/__tests__/src/standalone/sequelize/database.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { setDatabaseConfig } from '@palmares/databases'; | ||
import { NodeStd } from '@palmares/node-std'; | ||
import { SequelizeEngine } from '@palmares/sequelize-engine'; | ||
import * as migrations from './migrations'; | ||
|
||
import { Company, User } from './models'; | ||
|
||
export default setDatabaseConfig({ | ||
databases: { | ||
default: { | ||
engine: SequelizeEngine.new({ | ||
dialect: 'sqlite', | ||
storage: './standalone.sequelize.db', | ||
}), | ||
}, | ||
}, | ||
locations: [ | ||
{ | ||
name: 'default', | ||
path: import.meta.dirname, | ||
getMigrations: () => migrations, | ||
getModels: () => [User, Company], | ||
}, | ||
], | ||
std: new NodeStd(), | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/databases/__tests__/src/standalone/sequelize/makemigrations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import db from './database.config'; | ||
|
||
db.makeMigrations({}); |
3 changes: 3 additions & 0 deletions
3
packages/databases/__tests__/src/standalone/sequelize/migrate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import db from './database.config'; | ||
|
||
db.migrate({}); |
61 changes: 61 additions & 0 deletions
61
...__tests__/src/standalone/sequelize/migrations/002_default_auto_migration_1731249515815.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* prettier-ignore-start */ | ||
|
||
/* eslint-disable */ | ||
|
||
// @ts-nocheck | ||
|
||
// noinspection JSUnusedGlobalSymbols | ||
|
||
/** | ||
* Automatically generated by palmares on 2024-11-10T14:38:35.815Z | ||
*/ | ||
|
||
import { models, actions } from '@palmares/databases'; | ||
|
||
|
||
export default { | ||
name: '002_default_auto_migration_1731249515815', | ||
database: 'default', | ||
dependsOn: 'create_palmares_migration_table', | ||
operations: [ | ||
new actions.CreateModel( | ||
"Company", | ||
{ | ||
id: models.fields.AutoField.new().primaryKey(true).allowNull(true).unique(true).dbIndex(true).databaseName("id").underscored(true).setCustomAttributes({}), | ||
name: models.fields.CharField.new({ maxLen: 255 }).primaryKey(false).allowNull(false).unique(false).dbIndex(false).databaseName("name").underscored(true).setCustomAttributes({}).allowBlank(false), | ||
slug: models.fields.CharField.new({ maxLen: 255 }).primaryKey(false).allowNull(false).unique(false).dbIndex(false).databaseName("slug").underscored(true).setCustomAttributes({}).allowBlank(false), | ||
isActive: models.fields.BooleanField.new().primaryKey(false).default(true).allowNull(false).unique(false).dbIndex(false).databaseName("is_active").underscored(true).setCustomAttributes({}) | ||
}, | ||
{ | ||
abstract: false, | ||
underscored: true, | ||
tableName: "company", | ||
managed: true, | ||
ordering: [], | ||
indexes: [], | ||
databases: ["default"], | ||
customOptions: {} | ||
} | ||
), | ||
new actions.CreateModel( | ||
"User", | ||
{ | ||
id: models.fields.AutoField.new().primaryKey(true).allowNull(true).unique(true).dbIndex(true).databaseName("id").underscored(true).setCustomAttributes({}), | ||
firstName: models.fields.CharField.new({ maxLen: 255 }).primaryKey(false).allowNull(false).unique(false).dbIndex(false).databaseName("first_name").underscored(true).setCustomAttributes({}).allowBlank(false), | ||
lastName: models.fields.CharField.new({ maxLen: 255 }).primaryKey(false).allowNull(false).unique(false).dbIndex(false).databaseName("last_name").underscored(true).setCustomAttributes({}).allowBlank(false), | ||
email: models.fields.TextField.new().primaryKey(false).allowNull(true).unique(false).dbIndex(false).databaseName("email").underscored(true).setCustomAttributes({}).allowBlank(false), | ||
companyId: models.fields.ForeignKeyField.new({relatedTo: "Company", toField: "id", onDelete: models.fields.ON_DELETE.CASCADE, relationName: "company", relatedName: "undefined"}).primaryKey(false).allowNull(false).unique(false).dbIndex(false).databaseName("company_id").underscored(true).setCustomAttributes({}) | ||
}, | ||
{ | ||
abstract: false, | ||
underscored: true, | ||
tableName: undefined, | ||
managed: true, | ||
ordering: [], | ||
indexes: [], | ||
databases: ["default"], | ||
customOptions: {} | ||
} | ||
) | ||
] | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/databases/__tests__/src/standalone/sequelize/migrations/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as M002_default_auto_migration_1731249515815 } from './002_default_auto_migration_1731249515815'; |
44 changes: 44 additions & 0 deletions
44
packages/databases/__tests__/src/standalone/sequelize/models.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
Model, | ||
define, | ||
auto, | ||
char, | ||
text, | ||
bool, | ||
ON_DELETE, | ||
foreignKey | ||
} from '@palmares/databases'; | ||
|
||
import type { ModelOptionsType } from '@palmares/databases' | ||
|
||
export class Company extends Model<Company>() { | ||
fields = { | ||
id: auto(), | ||
name: char({ maxLen: 255 }), | ||
slug: char({ maxLen: 255 }), | ||
isActive: bool().default(true) | ||
} | ||
|
||
options = { | ||
tableName: 'company' | ||
} satisfies ModelOptionsType<Company> // We use satisfies here so we can still infer and you don't lose intellisense. | ||
} | ||
|
||
export const User = define('User', { | ||
fields: { | ||
id: auto(), | ||
firstName: char({ maxLen: 255 }), | ||
lastName: char({ maxLen: 255 }), | ||
email: text().allowNull(), | ||
companyId: foreignKey({ | ||
relatedTo: () => Company, | ||
toField: 'id', | ||
relationName: 'company', | ||
relatedName: 'usersOfCompany', | ||
onDelete: ON_DELETE.CASCADE | ||
}) | ||
}, | ||
options: { | ||
tableName: 'user' | ||
} | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ The [@palmares/databases](https://www.npmjs.com/package/@palmares/databases) pac | |
|
||
At its core it does nothing, at the same time it does everything! | ||
|
||
With 0 dependencies at its core (even no dependency on Node), you don't need to worry if it'll work on Expo. Without an adapter this will simply not do anything. But with the adapter this package offers you the ability to generate migrations, query your data and offer a really nice way to interact with your database. | ||
With 0 dependencies at its core (even no dependency on Node), you don't need to worry if it'll work on Expo, the Browser or even a Brain interface. Without an adapter this will simply not do anything. But with the adapter this package offers you the ability to generate migrations, query your data and offer a really nice way to interact with your database. | ||
|
||
Although we kinda see ourselves as an ORM, we are not **data frameworks** as drizzle like to call others like Django or Spring. You are not forced to build your project around our structure, although we think this is preferable most of the times, you are still free to use it the way you want, on your own existing projects without any hassle or problem. | ||
|
||
|
@@ -24,7 +24,16 @@ Although we kinda see ourselves as an ORM, we are not **data frameworks** as dri | |
|
||
**TIP:** This QuickStart uses [drizzle orm, reach out to their docs for reference](https://orm.drizzle.team/docs/overview) | ||
|
||
- **Step 1**. Create a `database.config.ts` with: | ||
- **Step 1**. Install a few more packages, and don't act like you cared about the number of dependencies on your projects | ||
|
||
```sh | ||
$ pnpm add @palmares/node-std @palmares/drizzle-engine | ||
$ npm i @palmares/node-std @palmares/drizzle-engine | ||
$ yarn i @palmares/node-std @palmares/drizzle-engine | ||
$ bun i @palmares/node-std @palmares/drizzle-engine | ||
``` | ||
|
||
- **Step 2**. Create a `database.config.ts` with: | ||
|
||
```ts | ||
import { | ||
|
@@ -103,11 +112,13 @@ export default setDatabaseConfig({ | |
}); | ||
``` | ||
|
||
- **Step 2**. Make your queries | ||
- **Step 3**. Make your queries | ||
|
||
- **Using your Palmares models:** | ||
|
||
```ts | ||
import './database.config'; // On this quickstart it's redundant, but make sure to initialize the DB before trying to query. | ||
|
||
import { Company, User } from './database.config'; | ||
|
||
await Company.default.set((qs) => | ||
|
@@ -118,13 +129,11 @@ export default setDatabaseConfig({ | |
firstName: 'Foo', | ||
lastName: 'bar', | ||
email: '[email protected]', | ||
isActive: true, | ||
}, | ||
{ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: '[email protected]', | ||
isActive: true, | ||
} | ||
) | ||
) | ||
|
@@ -138,15 +147,15 @@ export default setDatabaseConfig({ | |
|
||
- **Using your favorite ORM**: | ||
|
||
1. Create a file called `load.ts` and add the following: | ||
1. Create a file called `load.ts` and add the following (You are responsible for your own CLI): | ||
|
||
```ts | ||
import databasesConfig from './database.config'; | ||
databasesConfig.load(); | ||
``` | ||
|
||
2. Run (we are using to run typescript from the command line [tsx](https://tsx.is/)): | ||
2. Run (we are using [tsx](https://tsx.is/) to run typescript from the command line): | ||
|
||
```sh | ||
$ tsx load.ts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.