Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add simple Sequelize tests #1172

Merged
merged 15 commits into from
May 16, 2024
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
12 changes: 11 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ updates:
directory: "/src/test/nodejs/node-postgres"
schedule:
interval: "daily"
- package-ecosystem: "npm"
directory: "/src/test/nodejs/sequelize-tests"
schedule:
interval: "daily"
- package-ecosystem: "npm"
directory: "/src/test/nodejs/typeorm/data-test"
schedule:
Expand Down Expand Up @@ -74,7 +78,13 @@ updates:
directory: "/samples/java/spring-data-jpa"
schedule:
interval: "daily"


# Node.js samples
- package-ecosystem: "npm"
directory: "/samples/nodejs/sequelize"
schedule:
interval: "daily"

# Python Samples
# Python psycopg3 Sample
- package-ecosystem: "pip"
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/samples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ jobs:
run: |
npm install
npm start
- name: Run Sequelize Sample tests
working-directory: ./samples/nodejs/sequelize
run: |
npm install
npm start
ruby-samples:
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ samples/nodejs/**/.DS_Store
samples/cloud-run/nodejs/**/node_modules
samples/cloud-run/nodejs/**/package-lock.json
samples/cloud-run/nodejs/**/.DS_Store
samples/nodejs/sequelize/**/*.js

src/test/ruby/**/Gemfile.lock
samples/**/ruby/**/Gemfile.lock
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ PGAdapter can be used with the following frameworks and tools:
carefully for how to set up ActiveRecord to work with PGAdapter.
1. `Knex.js` query builder can be used with PGAdapter. See [Knex.js sample application](samples/nodejs/knex)
for a sample application.
1. `Sequelize.js` ORM can be used with PGAdapter. See [Sequelize.js sample application](samples/nodejs/sequelize)
for a sample application.

## FAQ
See [Frequently Asked Questions](docs/faq.md) for answers to frequently asked questions.
Expand Down
17 changes: 17 additions & 0 deletions samples/nodejs/sequelize/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<meta name='keywords' content='pgadapter, sequelize, sequelize.js, spanner, cloud spanner, node, node.js'>

# PGAdapter Spanner and Sequelize

PGAdapter has experimental support for [Sequelize](https://sequelize.org/) with the standard Node.js
`pg` driver. This sample application shows how to connect to PGAdapter with Sequelize, and how to
execute queries and transactions on Cloud Spanner.

The sample uses the Cloud Spanner emulator. You can run the sample on the emulator with this
command:

```shell
npm start
```

PGAdapter and the emulator are started in a Docker test container by the sample application.
Docker is therefore required to be installed on your system to run this sample.
264 changes: 264 additions & 0 deletions samples/nodejs/sequelize/models/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {
CreationOptional,
DataTypes,
HasManyAddAssociationMixin,
HasManyGetAssociationsMixin,
HasOneGetAssociationMixin,
HasOneSetAssociationMixin,
InferAttributes,
InferCreationAttributes,
Model,
Sequelize
} from 'sequelize';

export class Singer extends Model<InferAttributes<Singer>, InferCreationAttributes<Singer>> {
declare id: number;
declare firstName: string;
declare lastName: string;
declare fullName: string;
declare active: boolean;
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;

declare getAlbums: HasManyGetAssociationsMixin<Album>;
}

export class Album extends Model<InferAttributes<Album>, InferCreationAttributes<Album>> {
declare id: number;
declare title: string;
declare SingerId: number;
declare marketingBudget: number;
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;

declare getSinger: HasOneGetAssociationMixin<Singer>;
declare setSinger: HasOneSetAssociationMixin<Album, Singer>;
declare getTracks: HasManyGetAssociationsMixin<Track>;
declare addTrack: HasManyAddAssociationMixin<Track, Album>;
}

export class Track extends Model<InferAttributes<Track>, InferCreationAttributes<Track>> {
declare id: number;
declare trackNumber: number;
declare title: string;
declare sampleRate: number;
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;

declare getAlbum: HasOneGetAssociationMixin<Album>;
}

export class Venue extends Model<InferAttributes<Venue>, InferCreationAttributes<Venue>> {
declare id: number;
declare name: string;
// description is mapped to a JSONB column and can be used as an object.
declare description: string;
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;
}

export class Concert extends Model<InferAttributes<Concert>, InferCreationAttributes<Concert>> {
declare id: number;
declare VenueId: number;
declare SingerId: number;
declare name: string;
declare startTime: Date;
declare endTime: Date;
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;
}

export class TicketSale extends Model<InferAttributes<TicketSale>, InferCreationAttributes<TicketSale>> {
declare id: number;
declare ConcertId: number;
declare customerName: string;
declare price: number;
declare seats: string[];
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;
}

export function initModels(sequelize: Sequelize) {
Singer.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
firstName: {
type: DataTypes.STRING,
},
lastName: {
type: DataTypes.STRING,
},
// The fullName property is generated by Spanner
fullName: {
type: DataTypes.STRING,
},
active: {
type: DataTypes.BOOLEAN,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
}
}, {sequelize});

Album.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
title: {
type: DataTypes.STRING,
},
SingerId: {
type: DataTypes.INTEGER,
},
marketingBudget: {
type: DataTypes.DECIMAL,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
}
}, {sequelize});

// Note: Track is interleaved in Album.
Track.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
// Track.id is not an auto-increment column, because it has to have the same value
// as the Album that it belongs to.
autoIncrement: false,
},
trackNumber: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: false,
},
title: {
type: DataTypes.STRING,
},
sampleRate: {
type: DataTypes.DOUBLE,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
}
}, {sequelize});

Venue.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
},
description: {
type: DataTypes.STRING,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
}
}, {sequelize})

Concert.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
VenueId: {
type: DataTypes.BIGINT,
},
SingerId: {
type: DataTypes.BIGINT,
},
name: {
type: DataTypes.STRING,
},
startTime: {
type: DataTypes.DATE,
},
endTime: {
type: DataTypes.DATE,
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
}
}, {sequelize})

TicketSale.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
ConcertId: {
type: DataTypes.BIGINT,
},
customerName: {
type: DataTypes.STRING,
},
price: {
type: DataTypes.DECIMAL,
},
seats: {
type: DataTypes.ARRAY(DataTypes.STRING),
},
createdAt: {
type: DataTypes.DATE,
},
updatedAt: {
type: DataTypes.DATE,
}
}, {sequelize})

Singer.hasMany(Album);
Album.belongsTo(Singer);

// Tracks is interleaved in Albums.
// This means that they both share the first primary key column `id`.
// In addition, Tracks has a `trackNumber` primary key column.
// The reference ('foreign key') column from Track to Album is therefore the `id` column.
Album.hasMany(Track, {foreignKey: "id"});
Track.belongsTo(Album, {foreignKey: "id"});

Venue.hasMany(Concert);
Concert.belongsTo(Venue);

Concert.hasMany(TicketSale);
TicketSale.belongsTo(Concert);
}
22 changes: 22 additions & 0 deletions samples/nodejs/sequelize/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "pgadapter-sequelize-sample",
"version": "0.0.1",
"description": "PGAdapter Sequelize Sample",
"type": "commonjs",
"devDependencies": {
"@types/node": "^20.1.4",
"sequelize-cli": "^6.6.2",
"testcontainers": "^10.7.1",
"ts-node": "10.9.1",
"typescript": "5.2.2"
},
"dependencies": {
"pg": "^8.11.3",
"sequelize": "^6.36.0",
"umzug": "^3.6.1",
"yargs": "^17.5.1"
},
"scripts": {
"start": "ts-node src/index.ts"
}
}
Loading
Loading